WebCore: https://bugs.webkit.org/show_bug.cgi?id=20329, shadows and reflections incor...
[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 "ChromeClient.h"
33 #include "DocLoader.h"
34 #include "EventHandler.h"
35 #include "FloatRect.h"
36 #include "FocusController.h"
37 #include "Frame.h"
38 #include "FrameLoader.h"
39 #include "FrameLoaderClient.h"
40 #include "FrameTree.h"
41 #include "GraphicsContext.h"
42 #include "HTMLDocument.h"
43 #include "HTMLFrameElement.h"
44 #include "HTMLFrameSetElement.h"
45 #include "HTMLNames.h"
46 #include "OverflowEvent.h"
47 #include "Page.h"
48 #include "RenderPart.h"
49 #include "RenderPartObject.h"
50 #include "RenderScrollbar.h"
51 #include "RenderTheme.h"
52 #include "RenderView.h"
53 #include "Settings.h"
54 #include <wtf/CurrentTime.h>
55
56 #if USE(ACCELERATED_COMPOSITING)
57 #include "RenderLayerCompositor.h"
58 #endif
59
60 namespace WebCore {
61
62 using namespace HTMLNames;
63
64 double FrameView::sCurrentPaintTimeStamp = 0.0;
65
66 #if ENABLE(REPAINT_THROTTLING)
67 // Normal delay
68 static const double deferredRepaintDelay = 0.025;
69 // Negative value would mean that first few repaints happen without a delay
70 static const double initialDeferredRepaintDelayDuringLoading = 0;
71 // The delay grows on each repaint to this maximum value
72 static const double maxDeferredRepaintDelayDuringLoading = 2.5;
73 // On each repaint the delay increses by this amount
74 static const double deferredRepaintDelayIncrementDuringLoading = 0.5;
75 #else
76 // FIXME: Repaint throttling could be good to have on all platform.
77 // The balance between CPU use and repaint frequency will need some tuning for desktop.
78 // More hooks may be needed to reset the delay on things like GIF and CSS animations.
79 static const double deferredRepaintDelay = 0;
80 static const double initialDeferredRepaintDelayDuringLoading = 0;
81 static const double maxDeferredRepaintDelayDuringLoading = 0;
82 static const double deferredRepaintDelayIncrementDuringLoading = 0;
83 #endif
84
85 // The maximum number of updateWidgets iterations that should be done before returning.
86 static const unsigned maxUpdateWidgetsIterations = 2;
87
88 struct ScheduledEvent {
89     RefPtr<Event> m_event;
90     RefPtr<Node> m_eventTarget;
91 };
92
93 FrameView::FrameView(Frame* frame)
94     : m_frame(frame)
95     , m_vmode(ScrollbarAuto)
96     , m_hmode(ScrollbarAuto)
97     , m_slowRepaintObjectCount(0)
98     , m_layoutTimer(this, &FrameView::layoutTimerFired)
99     , m_layoutRoot(0)
100     , m_postLayoutTasksTimer(this, &FrameView::postLayoutTimerFired)
101     , m_needToInitScrollbars(true)
102     , m_isTransparent(false)
103     , m_baseBackgroundColor(Color::white)
104     , m_mediaType("screen")
105     , m_enqueueEvents(0)
106     , m_overflowStatusDirty(true)
107     , m_viewportRenderer(0)
108     , m_wasScrolledByUser(false)
109     , m_inProgrammaticScroll(false)
110     , m_deferredRepaintTimer(this, &FrameView::deferredRepaintTimerFired)
111     , m_shouldUpdateWhileOffscreen(true)
112     , m_deferSetNeedsLayouts(0)
113     , m_setNeedsLayoutWasDeferred(false)
114 {
115     init();
116 }
117
118 PassRefPtr<FrameView> FrameView::create(Frame* frame)
119 {
120     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
121     view->show();
122     return view.release();
123 }
124
125 PassRefPtr<FrameView> FrameView::create(Frame* frame, const IntSize& initialSize)
126 {
127     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
128     view->Widget::setFrameRect(IntRect(view->pos(), initialSize));
129     view->show();
130     return view.release();
131 }
132
133 FrameView::~FrameView()
134 {
135     if (m_postLayoutTasksTimer.isActive()) {
136         m_postLayoutTasksTimer.stop();
137         m_scheduledEvents.clear();
138         m_enqueueEvents = 0;
139     }
140
141     resetScrollbars();
142     setHasHorizontalScrollbar(false); // Remove native scrollbars now before we lose the connection to the HostWindow.
143     setHasVerticalScrollbar(false);
144     
145     ASSERT(m_scheduledEvents.isEmpty());
146     ASSERT(!m_enqueueEvents);
147
148     if (m_frame) {
149         ASSERT(m_frame->view() != this || !m_frame->contentRenderer());
150         RenderPart* renderer = m_frame->ownerRenderer();
151         if (renderer && renderer->widget() == this)
152             renderer->setWidget(0);
153     }
154 }
155
156 void FrameView::reset()
157 {
158     m_useSlowRepaints = false;
159     m_isOverlapped = false;
160     m_contentIsOpaque = false;
161     m_borderX = 30;
162     m_borderY = 30;
163     m_layoutTimer.stop();
164     m_layoutRoot = 0;
165     m_delayedLayout = false;
166     m_doFullRepaint = true;
167     m_layoutSchedulingEnabled = true;
168     m_midLayout = false;
169     m_layoutCount = 0;
170     m_nestedLayoutCount = 0;
171     m_postLayoutTasksTimer.stop();
172     m_firstLayout = true;
173     m_firstLayoutCallbackPending = false;
174     m_wasScrolledByUser = false;
175     m_lastLayoutSize = IntSize();
176     m_lastZoomFactor = 1.0f;
177     m_deferringRepaints = 0;
178     m_repaintCount = 0;
179     m_repaintRects.clear();
180     m_deferredRepaintDelay = initialDeferredRepaintDelayDuringLoading;
181     m_deferredRepaintTimer.stop();
182     m_lastPaintTime = 0;
183     m_paintRestriction = PaintRestrictionNone;
184     m_isPainting = false;
185     m_isVisuallyNonEmpty = false;
186     m_firstVisuallyNonEmptyLayoutCallbackPending = true;
187     m_maintainScrollPositionAnchor = 0;
188 }
189
190 bool FrameView::isFrameView() const 
191
192     return true; 
193 }
194
195 void FrameView::clearFrame()
196 {
197     m_frame = 0;
198 }
199
200 void FrameView::resetScrollbars()
201 {
202     // Reset the document's scrollbars back to our defaults before we yield the floor.
203     m_firstLayout = true;
204     setScrollbarsSuppressed(true);
205     setScrollbarModes(m_hmode, m_vmode);
206     setScrollbarsSuppressed(false);
207 }
208
209 void FrameView::init()
210 {
211     reset();
212
213     m_margins = IntSize(-1, -1); // undefined
214     m_size = IntSize();
215
216     // Propagate the marginwidth/height and scrolling modes to the view.
217     Element* ownerElement = m_frame && m_frame->document() ? m_frame->document()->ownerElement() : 0;
218     if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) {
219         HTMLFrameElement* frameElt = static_cast<HTMLFrameElement*>(ownerElement);
220         if (frameElt->scrollingMode() == ScrollbarAlwaysOff)
221             setCanHaveScrollbars(false);
222         int marginWidth = frameElt->getMarginWidth();
223         int marginHeight = frameElt->getMarginHeight();
224         if (marginWidth != -1)
225             setMarginWidth(marginWidth);
226         if (marginHeight != -1)
227             setMarginHeight(marginHeight);
228     }
229 }
230
231 void FrameView::detachCustomScrollbars()
232 {
233     if (!m_frame)
234         return;
235
236     Document* document = m_frame->document();
237     if (!document)
238         return;
239
240     Element* body = document->body();
241     if (!body)
242         return;
243
244     RenderBox* renderBox = body->renderBox();
245
246     Scrollbar* horizontalBar = horizontalScrollbar();
247     if (horizontalBar && horizontalBar->isCustomScrollbar() && toRenderScrollbar(horizontalBar)->owningRenderer() == renderBox)
248         setHasHorizontalScrollbar(false);
249
250     Scrollbar* verticalBar = verticalScrollbar();
251     if (verticalBar && verticalBar->isCustomScrollbar() && toRenderScrollbar(verticalBar)->owningRenderer() == renderBox)
252         setHasVerticalScrollbar(false);
253 }
254
255 void FrameView::clear()
256 {
257     setCanBlitOnScroll(true);
258     
259     reset();
260
261     if (m_frame) {
262         if (RenderPart* renderer = m_frame->ownerRenderer())
263             renderer->viewCleared();
264     }
265
266     setScrollbarsSuppressed(true);
267 }
268
269 bool FrameView::didFirstLayout() const
270 {
271     return !m_firstLayout;
272 }
273
274 void FrameView::initScrollbars()
275 {
276     if (!m_needToInitScrollbars)
277         return;
278     m_needToInitScrollbars = false;
279     updateDefaultScrollbarState();
280 }
281
282 void FrameView::updateDefaultScrollbarState()
283 {
284     m_hmode = horizontalScrollbarMode();
285     m_vmode = verticalScrollbarMode();
286     setScrollbarModes(m_hmode, m_vmode);
287 }
288
289 void FrameView::invalidateRect(const IntRect& rect)
290 {
291     if (!parent()) {
292         if (hostWindow())
293             hostWindow()->repaint(rect, true);
294         return;
295     }
296
297     if (!m_frame)
298         return;
299
300     RenderPart* renderer = m_frame->ownerRenderer();
301     if (!renderer)
302         return;
303
304     IntRect repaintRect = rect;
305     repaintRect.move(renderer->borderLeft() + renderer->paddingLeft(),
306                      renderer->borderTop() + renderer->paddingTop());
307     renderer->repaintRectangle(repaintRect);
308 }
309
310 void FrameView::setMarginWidth(int w)
311 {
312     // make it update the rendering area when set
313     m_margins.setWidth(w);
314 }
315
316 void FrameView::setMarginHeight(int h)
317 {
318     // make it update the rendering area when set
319     m_margins.setHeight(h);
320 }
321
322 void FrameView::setCanHaveScrollbars(bool canScroll)
323 {
324     ScrollView::setCanHaveScrollbars(canScroll);
325     scrollbarModes(m_hmode, m_vmode);
326 }
327
328 PassRefPtr<Scrollbar> FrameView::createScrollbar(ScrollbarOrientation orientation)
329 {
330     // FIXME: We need to update the scrollbar dynamically as documents change (or as doc elements and bodies get discovered that have custom styles).
331     Document* doc = m_frame->document();
332
333     // Try the <body> element first as a scrollbar source.
334     Element* body = doc ? doc->body() : 0;
335     if (body && body->renderer() && body->renderer()->style()->hasPseudoStyle(SCROLLBAR))
336         return RenderScrollbar::createCustomScrollbar(this, orientation, body->renderBox());
337     
338     // If the <body> didn't have a custom style, then the root element might.
339     Element* docElement = doc ? doc->documentElement() : 0;
340     if (docElement && docElement->renderer() && docElement->renderer()->style()->hasPseudoStyle(SCROLLBAR))
341         return RenderScrollbar::createCustomScrollbar(this, orientation, docElement->renderBox());
342         
343     // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
344     RenderPart* frameRenderer = m_frame->ownerRenderer();
345     if (frameRenderer && frameRenderer->style()->hasPseudoStyle(SCROLLBAR))
346         return RenderScrollbar::createCustomScrollbar(this, orientation, frameRenderer);
347     
348     // Nobody set a custom style, so we just use a native scrollbar.
349     return ScrollView::createScrollbar(orientation);
350 }
351
352 void FrameView::setContentsSize(const IntSize& size)
353 {
354     m_deferSetNeedsLayouts++;
355
356     ScrollView::setContentsSize(size);
357
358     Page* page = frame() ? frame()->page() : 0;
359     if (!page)
360         return;
361
362     page->chrome()->contentsSizeChanged(frame(), size); //notify only
363     
364     m_deferSetNeedsLayouts--;
365     
366     if (!m_deferSetNeedsLayouts)
367         m_setNeedsLayoutWasDeferred = false; // FIXME: Find a way to make the deferred layout actually happen.
368 }
369
370 void FrameView::adjustViewSize()
371 {
372     ASSERT(m_frame->view() == this);
373     RenderView* root = m_frame->contentRenderer();
374     if (!root)
375         return;
376     setContentsSize(IntSize(root->rightLayoutOverflow(), root->bottomLayoutOverflow()));
377 }
378
379 void FrameView::applyOverflowToViewport(RenderObject* o, ScrollbarMode& hMode, ScrollbarMode& vMode)
380 {
381     // Handle the overflow:hidden/scroll case for the body/html elements.  WinIE treats
382     // overflow:hidden and overflow:scroll on <body> as applying to the document's
383     // scrollbars.  The CSS2.1 draft states that HTML UAs should use the <html> or <body> element and XML/XHTML UAs should
384     // use the root element.
385     switch (o->style()->overflowX()) {
386         case OHIDDEN:
387             hMode = ScrollbarAlwaysOff;
388             break;
389         case OSCROLL:
390             hMode = ScrollbarAlwaysOn;
391             break;
392         case OAUTO:
393             hMode = ScrollbarAuto;
394             break;
395         default:
396             // Don't set it at all.
397             ;
398     }
399     
400      switch (o->style()->overflowY()) {
401         case OHIDDEN:
402             vMode = ScrollbarAlwaysOff;
403             break;
404         case OSCROLL:
405             vMode = ScrollbarAlwaysOn;
406             break;
407         case OAUTO:
408             vMode = ScrollbarAuto;
409             break;
410         default:
411             // Don't set it at all.
412             ;
413     }
414
415     m_viewportRenderer = o;
416 }
417
418 #if USE(ACCELERATED_COMPOSITING)
419 void FrameView::updateCompositingLayers()
420 {
421     RenderView* view = m_frame->contentRenderer();
422     if (!view)
423         return;
424
425     // This call will make sure the cached hasAcceleratedCompositing is updated from the pref
426     view->compositor()->cacheAcceleratedCompositingEnabledFlag();
427     
428     if (!view->usesCompositing())
429         return;
430
431     view->compositor()->updateCompositingLayers();
432 }
433
434 void FrameView::setNeedsOneShotDrawingSynchronization()
435 {
436     Page* page = frame() ? frame()->page() : 0;
437     if (page)
438         page->chrome()->client()->setNeedsOneShotDrawingSynchronization();
439 }
440 #endif // USE(ACCELERATED_COMPOSITING)
441
442 bool FrameView::syncCompositingStateRecursive()
443 {
444 #if USE(ACCELERATED_COMPOSITING)
445     ASSERT(m_frame->view() == this);
446     RenderView* contentRenderer = m_frame->contentRenderer();
447     if (!contentRenderer)
448         return true;    // We don't want to keep trying to update layers if we have no renderer.
449
450     if (m_layoutTimer.isActive()) {
451         // Don't sync layers if there's a layout pending.
452         return false;
453     }
454     
455     if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
456         rootLayer->syncCompositingState();
457
458     bool allSubframesSynced = true;
459     const HashSet<RefPtr<Widget> >* viewChildren = children();
460     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
461     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
462         Widget* widget = (*current).get();
463         if (widget->isFrameView()) {
464             bool synced = static_cast<FrameView*>(widget)->syncCompositingStateRecursive();
465             allSubframesSynced &= synced;
466         }
467     }
468     return allSubframesSynced;
469 #endif // USE(ACCELERATED_COMPOSITING)
470     return true;
471 }
472
473 void FrameView::didMoveOnscreen()
474 {
475     RenderView* view = m_frame->contentRenderer();
476     if (view)
477         view->didMoveOnscreen();
478 }
479
480 void FrameView::willMoveOffscreen()
481 {
482     RenderView* view = m_frame->contentRenderer();
483     if (view)
484         view->willMoveOffscreen();
485 }
486
487 RenderObject* FrameView::layoutRoot(bool onlyDuringLayout) const
488 {
489     return onlyDuringLayout && layoutPending() ? 0 : m_layoutRoot;
490 }
491
492 void FrameView::layout(bool allowSubtree)
493 {
494     if (m_midLayout)
495         return;
496
497     m_layoutTimer.stop();
498     m_delayedLayout = false;
499     m_setNeedsLayoutWasDeferred = false;
500
501     // Protect the view from being deleted during layout (in recalcStyle)
502     RefPtr<FrameView> protector(this);
503
504     if (!m_frame) {
505         // FIXME: Do we need to set m_size.width here?
506         // FIXME: Should we set m_size.height here too?
507         m_size.setWidth(layoutWidth());
508         return;
509     }
510     
511     // we shouldn't enter layout() while painting
512     ASSERT(!isPainting());
513     if (isPainting())
514         return;
515
516     if (!allowSubtree && m_layoutRoot) {
517         m_layoutRoot->markContainingBlocksForLayout(false);
518         m_layoutRoot = 0;
519     }
520
521     ASSERT(m_frame->view() == this);
522     // This early return should be removed when rdar://5598072 is resolved. In the meantime, there is a
523     // gigantic CrashTracer because of this issue, and the early return will hopefully cause graceful 
524     // failure instead.  
525     if (m_frame->view() != this)
526         return;
527
528     Document* document = m_frame->document();
529
530     m_layoutSchedulingEnabled = false;
531
532     if (!m_nestedLayoutCount && m_postLayoutTasksTimer.isActive()) {
533         // This is a new top-level layout. If there are any remaining tasks from the previous
534         // layout, finish them now.
535         m_postLayoutTasksTimer.stop();
536         performPostLayoutTasks();
537     }
538
539     // Viewport-dependent media queries may cause us to need completely different style information.
540     // Check that here.
541     if (document->styleSelector()->affectedByViewportChange())
542         document->updateStyleSelector();
543
544     // Always ensure our style info is up-to-date.  This can happen in situations where
545     // the layout beats any sort of style recalc update that needs to occur.
546     if (m_frame->needsReapplyStyles())
547         m_frame->reapplyStyles();
548     else if (document->childNeedsStyleRecalc())
549         document->recalcStyle();
550     
551     bool subtree = m_layoutRoot;
552
553     // If there is only one ref to this view left, then its going to be destroyed as soon as we exit, 
554     // so there's no point to continuing to layout
555     if (protector->hasOneRef())
556         return;
557
558     RenderObject* root = subtree ? m_layoutRoot : document->renderer();
559     if (!root) {
560         // FIXME: Do we need to set m_size here?
561         m_layoutSchedulingEnabled = true;
562         return;
563     }
564
565     m_nestedLayoutCount++;
566
567     ScrollbarMode hMode = m_hmode;
568     ScrollbarMode vMode = m_vmode;
569
570     if (!subtree) {
571         RenderObject* rootRenderer = document->documentElement() ? document->documentElement()->renderer() : 0;
572         Node* body = document->body();
573         if (body && body->renderer()) {
574             if (body->hasTagName(framesetTag)) {
575                 body->renderer()->setChildNeedsLayout(true);
576                 vMode = ScrollbarAlwaysOff;
577                 hMode = ScrollbarAlwaysOff;
578             } else if (body->hasTagName(bodyTag)) {
579                 if (!m_firstLayout && m_size.height() != layoutHeight()
580                         && toRenderBox(body->renderer())->stretchesToViewHeight())
581                     body->renderer()->setChildNeedsLayout(true);
582                 // It's sufficient to just check the X overflow,
583                 // since it's illegal to have visible in only one direction.
584                 RenderObject* o = rootRenderer->style()->overflowX() == OVISIBLE && document->documentElement()->hasTagName(htmlTag) ? body->renderer() : rootRenderer;
585                 applyOverflowToViewport(o, hMode, vMode);
586             }
587         } else if (rootRenderer)
588             applyOverflowToViewport(rootRenderer, hMode, vMode);
589 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
590         if (m_firstLayout && !document->ownerElement())
591             printf("Elapsed time before first layout: %d\n", document->elapsedTime());
592 #endif
593     }
594
595     m_doFullRepaint = !subtree && (m_firstLayout || toRenderView(root)->printing());
596
597     if (!subtree) {
598         // Now set our scrollbar state for the layout.
599         ScrollbarMode currentHMode = horizontalScrollbarMode();
600         ScrollbarMode currentVMode = verticalScrollbarMode();
601
602         if (m_firstLayout || (hMode != currentHMode || vMode != currentVMode)) {
603             setScrollbarsSuppressed(true);
604             if (m_firstLayout) {
605                 m_firstLayout = false;
606                 m_firstLayoutCallbackPending = true;
607                 m_lastLayoutSize = IntSize(width(), height());
608                 m_lastZoomFactor = root->style()->zoom();
609
610                 // Set the initial vMode to AlwaysOn if we're auto.
611                 if (vMode == ScrollbarAuto)
612                     setVerticalScrollbarMode(ScrollbarAlwaysOn); // This causes a vertical scrollbar to appear.
613                 // Set the initial hMode to AlwaysOff if we're auto.
614                 if (hMode == ScrollbarAuto)
615                     setHorizontalScrollbarMode(ScrollbarAlwaysOff); // This causes a horizontal scrollbar to disappear.
616             }
617             setScrollbarModes(hMode, vMode);
618             setScrollbarsSuppressed(false, true);
619         }
620
621         IntSize oldSize = m_size;
622
623         m_size = IntSize(layoutWidth(), layoutHeight());
624
625         if (oldSize != m_size)
626             m_doFullRepaint = true;
627     }
628
629     RenderLayer* layer = root->enclosingLayer();
630
631     pauseScheduledEvents();
632
633     if (subtree)
634         root->view()->pushLayoutState(root);
635         
636     m_midLayout = true;
637     beginDeferredRepaints();
638     root->layout();
639     endDeferredRepaints();
640     m_midLayout = false;
641
642     if (subtree)
643         root->view()->popLayoutState();
644     m_layoutRoot = 0;
645
646     m_frame->invalidateSelection();
647    
648     m_layoutSchedulingEnabled = true;
649
650     if (!subtree && !toRenderView(root)->printing())
651         adjustViewSize();
652
653     // Now update the positions of all layers.
654     beginDeferredRepaints();
655     layer->updateLayerPositions((m_doFullRepaint ? RenderLayer::DoFullRepaint : 0)
656                                 | RenderLayer::CheckForRepaint
657                                 | RenderLayer::UpdateCompositingLayers);
658     endDeferredRepaints();
659
660 #if USE(ACCELERATED_COMPOSITING)
661     updateCompositingLayers();
662 #endif
663     
664     m_layoutCount++;
665
666 #if PLATFORM(MAC)
667     if (AXObjectCache::accessibilityEnabled())
668         root->document()->axObjectCache()->postNotification(root, "AXLayoutComplete", true);
669 #endif
670 #if ENABLE(DASHBOARD_SUPPORT)
671     updateDashboardRegions();
672 #endif
673
674     ASSERT(!root->needsLayout());
675
676     setCanBlitOnScroll(!useSlowRepaints());
677
678     if (document->hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
679         updateOverflowStatus(layoutWidth() < contentsWidth(),
680                              layoutHeight() < contentsHeight());
681
682     if (!m_postLayoutTasksTimer.isActive()) {
683         // Calls resumeScheduledEvents()
684         performPostLayoutTasks();
685
686         if (!m_postLayoutTasksTimer.isActive() && needsLayout()) {
687             // Post-layout widget updates or an event handler made us need layout again.
688             // Lay out again, but this time defer widget updates and event dispatch until after
689             // we return.
690             m_postLayoutTasksTimer.startOneShot(0);
691             pauseScheduledEvents();
692             layout();
693         }
694     } else {
695         resumeScheduledEvents();
696         ASSERT(m_enqueueEvents);
697     }
698
699     m_nestedLayoutCount--;
700 }
701
702 void FrameView::addWidgetToUpdate(RenderPartObject* object)
703 {
704     if (!m_widgetUpdateSet)
705         m_widgetUpdateSet.set(new HashSet<RenderPartObject*>);
706
707     m_widgetUpdateSet->add(object);
708 }
709
710 void FrameView::removeWidgetToUpdate(RenderPartObject* object)
711 {
712     if (!m_widgetUpdateSet)
713         return;
714
715     m_widgetUpdateSet->remove(object);
716 }
717
718 void FrameView::setMediaType(const String& mediaType)
719 {
720     m_mediaType = mediaType;
721 }
722
723 String FrameView::mediaType() const
724 {
725     // See if we have an override type.
726     String overrideType = m_frame->loader()->client()->overrideMediaType();
727     if (!overrideType.isNull())
728         return overrideType;
729     return m_mediaType;
730 }
731
732 bool FrameView::useSlowRepaints() const
733 {
734     return m_useSlowRepaints || m_slowRepaintObjectCount > 0 || m_isOverlapped || !m_contentIsOpaque;
735 }
736
737 void FrameView::setUseSlowRepaints()
738 {
739     m_useSlowRepaints = true;
740     setCanBlitOnScroll(false);
741 }
742
743 void FrameView::addSlowRepaintObject()
744 {
745     if (!m_slowRepaintObjectCount)
746         setCanBlitOnScroll(false);
747     m_slowRepaintObjectCount++;
748 }
749
750 void FrameView::removeSlowRepaintObject()
751 {
752     ASSERT(m_slowRepaintObjectCount > 0);
753     m_slowRepaintObjectCount--;
754     if (!m_slowRepaintObjectCount)
755         setCanBlitOnScroll(!useSlowRepaints());
756 }
757
758 void FrameView::setIsOverlapped(bool isOverlapped)
759 {
760     if (isOverlapped == m_isOverlapped)
761         return;
762
763     m_isOverlapped = isOverlapped;
764     setCanBlitOnScroll(!useSlowRepaints());
765 }
766
767 void FrameView::setContentIsOpaque(bool contentIsOpaque)
768 {
769     if (contentIsOpaque == m_contentIsOpaque)
770         return;
771
772     m_contentIsOpaque = contentIsOpaque;
773     setCanBlitOnScroll(!useSlowRepaints());
774 }
775
776 void FrameView::restoreScrollbar()
777 {
778     setScrollbarsSuppressed(false);
779 }
780
781 void FrameView::maintainScrollPositionAtAnchor(Node* anchorNode)
782 {
783     m_maintainScrollPositionAnchor = anchorNode;
784     if (!m_maintainScrollPositionAnchor)
785         return;
786
787     // We need to update the layout before scrolling, otherwise we could
788     // really mess things up if an anchor scroll comes at a bad moment.
789     m_frame->document()->updateStyleIfNeeded();
790     // Only do a layout if changes have occurred that make it necessary.
791     if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout())
792         layout();
793     else
794         scrollToAnchor();
795 }
796
797 void FrameView::scrollRectIntoViewRecursively(const IntRect& r)
798 {
799     bool wasInProgrammaticScroll = m_inProgrammaticScroll;
800     m_inProgrammaticScroll = true;
801     m_maintainScrollPositionAnchor = 0;
802     ScrollView::scrollRectIntoViewRecursively(r);
803     m_inProgrammaticScroll = wasInProgrammaticScroll;
804 }
805
806 void FrameView::setScrollPosition(const IntPoint& scrollPoint)
807 {
808     bool wasInProgrammaticScroll = m_inProgrammaticScroll;
809     m_inProgrammaticScroll = true;
810     m_maintainScrollPositionAnchor = 0;
811     ScrollView::setScrollPosition(scrollPoint);
812     m_inProgrammaticScroll = wasInProgrammaticScroll;
813 }
814
815 HostWindow* FrameView::hostWindow() const
816 {
817     Page* page = frame() ? frame()->page() : 0;
818     if (!page)
819         return 0;
820     return page->chrome();
821 }
822
823 const unsigned cRepaintRectUnionThreshold = 25;
824
825 void FrameView::repaintContentRectangle(const IntRect& r, bool immediate)
826 {
827     ASSERT(!m_frame->document()->ownerElement());
828
829     double delay = adjustedDeferredRepaintDelay();
830     if ((m_deferringRepaints || m_deferredRepaintTimer.isActive() || delay) && !immediate) {
831         IntRect visibleContent = visibleContentRect();
832         visibleContent.intersect(r);
833         if (visibleContent.isEmpty())
834             return;
835         if (m_repaintCount == cRepaintRectUnionThreshold) {
836             IntRect unionedRect;
837             for (unsigned i = 0; i < cRepaintRectUnionThreshold; ++i)
838                 unionedRect.unite(m_repaintRects[i]);
839             m_repaintRects.clear();
840             m_repaintRects.append(unionedRect);
841         }
842         if (m_repaintCount < cRepaintRectUnionThreshold)
843             m_repaintRects.append(r);
844         else
845             m_repaintRects[0].unite(r);
846         m_repaintCount++;
847     
848         if (!m_deferringRepaints && !m_deferredRepaintTimer.isActive())
849              m_deferredRepaintTimer.startOneShot(delay);
850         return;
851     }
852     
853     if (!immediate && isOffscreen() && !shouldUpdateWhileOffscreen())
854         return;
855
856     ScrollView::repaintContentRectangle(r, immediate);
857 }
858
859 void FrameView::visibleContentsResized()
860 {
861     // We check to make sure the view is attached to a frame() as this method can
862     // be triggered before the view is attached by Frame::createView(...) setting
863     // various values such as setScrollBarModes(...) for example.  An ASSERT is
864     // triggered when a view is layout before being attached to a frame().
865     if (!frame()->view())
866         return;
867
868     if (needsLayout())
869         layout();
870 }
871
872 void FrameView::beginDeferredRepaints()
873 {
874     Page* page = m_frame->page();
875     if (page->mainFrame() != m_frame)
876         return page->mainFrame()->view()->beginDeferredRepaints();
877
878     m_deferringRepaints++;
879 }
880
881
882 void FrameView::endDeferredRepaints()
883 {
884     Page* page = m_frame->page();
885     if (page->mainFrame() != m_frame)
886         return page->mainFrame()->view()->endDeferredRepaints();
887
888     ASSERT(m_deferringRepaints > 0);
889
890     if (--m_deferringRepaints)
891         return;
892     
893     if (m_deferredRepaintTimer.isActive())
894         return;
895
896     if (double delay = adjustedDeferredRepaintDelay()) {
897         m_deferredRepaintTimer.startOneShot(delay);
898         return;
899     }
900     
901     doDeferredRepaints();
902 }
903
904 void FrameView::checkStopDelayingDeferredRepaints()
905 {
906     if (!m_deferredRepaintTimer.isActive())
907         return;
908
909     Document* document = m_frame->document();
910     if (document && (document->parsing() || document->docLoader()->requestCount()))
911         return;
912     
913     m_deferredRepaintTimer.stop();
914
915     doDeferredRepaints();
916 }
917     
918 void FrameView::doDeferredRepaints()
919 {
920     ASSERT(!m_deferringRepaints);
921     if (isOffscreen() && !shouldUpdateWhileOffscreen()) {
922         m_repaintRects.clear();
923         m_repaintCount = 0;
924         return;
925     }
926     unsigned size = m_repaintRects.size();
927     for (unsigned i = 0; i < size; i++)
928         ScrollView::repaintContentRectangle(m_repaintRects[i], false);
929     m_repaintRects.clear();
930     m_repaintCount = 0;
931     
932     updateDeferredRepaintDelay();
933 }
934
935 void FrameView::updateDeferredRepaintDelay()
936 {
937     Document* document = m_frame->document();
938     if (!document || (!document->parsing() && !document->docLoader()->requestCount())) {
939         m_deferredRepaintDelay = deferredRepaintDelay;
940         return;
941     }
942     if (m_deferredRepaintDelay < maxDeferredRepaintDelayDuringLoading) {
943         m_deferredRepaintDelay += deferredRepaintDelayIncrementDuringLoading;
944         if (m_deferredRepaintDelay > maxDeferredRepaintDelayDuringLoading)
945             m_deferredRepaintDelay = maxDeferredRepaintDelayDuringLoading;
946     }
947 }
948
949 void FrameView::resetDeferredRepaintDelay()
950 {
951     m_deferredRepaintDelay = 0;
952     if (m_deferredRepaintTimer.isActive()) {
953         m_deferredRepaintTimer.stop();
954         if (!m_deferringRepaints)
955             doDeferredRepaints();
956     }
957 }
958
959 double FrameView::adjustedDeferredRepaintDelay() const
960 {
961     if (!m_deferredRepaintDelay)
962         return 0;
963     double timeSinceLastPaint = currentTime() - m_lastPaintTime;
964     return max(0., m_deferredRepaintDelay - timeSinceLastPaint);
965 }
966     
967 void FrameView::deferredRepaintTimerFired(Timer<FrameView>*)
968 {
969     doDeferredRepaints();
970 }    
971
972 void FrameView::layoutTimerFired(Timer<FrameView>*)
973 {
974 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
975     if (!m_frame->document()->ownerElement())
976         printf("Layout timer fired at %d\n", m_frame->document()->elapsedTime());
977 #endif
978     layout();
979 }
980
981 void FrameView::scheduleRelayout()
982 {
983     ASSERT(!m_frame->document()->inPageCache());
984     ASSERT(m_frame->view() == this);
985
986     if (m_layoutRoot) {
987         m_layoutRoot->markContainingBlocksForLayout(false);
988         m_layoutRoot = 0;
989     }
990     if (!m_layoutSchedulingEnabled)
991         return;
992     if (!needsLayout())
993         return;
994     if (!m_frame->document()->shouldScheduleLayout())
995         return;
996
997     int delay = m_frame->document()->minimumLayoutDelay();
998     if (m_layoutTimer.isActive() && m_delayedLayout && !delay)
999         unscheduleRelayout();
1000     if (m_layoutTimer.isActive())
1001         return;
1002
1003     m_delayedLayout = delay != 0;
1004
1005 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1006     if (!m_frame->document()->ownerElement())
1007         printf("Scheduling layout for %d\n", delay);
1008 #endif
1009
1010     m_layoutTimer.startOneShot(delay * 0.001);
1011 }
1012
1013 static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
1014 {
1015     for (RenderObject* r = descendant; r; r = r->container()) {
1016         if (r == ancestor)
1017             return true;
1018     }
1019     return false;
1020 }
1021
1022 void FrameView::scheduleRelayoutOfSubtree(RenderObject* relayoutRoot)
1023 {
1024     ASSERT(m_frame->view() == this);
1025
1026     if (!m_layoutSchedulingEnabled || (m_frame->contentRenderer()
1027             && m_frame->contentRenderer()->needsLayout())) {
1028         if (relayoutRoot)
1029             relayoutRoot->markContainingBlocksForLayout(false);
1030         return;
1031     }
1032
1033     if (layoutPending()) {
1034         if (m_layoutRoot != relayoutRoot) {
1035             if (isObjectAncestorContainerOf(m_layoutRoot, relayoutRoot)) {
1036                 // Keep the current root
1037                 relayoutRoot->markContainingBlocksForLayout(false, m_layoutRoot);
1038             } else if (m_layoutRoot && isObjectAncestorContainerOf(relayoutRoot, m_layoutRoot)) {
1039                 // Re-root at relayoutRoot
1040                 m_layoutRoot->markContainingBlocksForLayout(false, relayoutRoot);
1041                 m_layoutRoot = relayoutRoot;
1042             } else {
1043                 // Just do a full relayout
1044                 if (m_layoutRoot)
1045                     m_layoutRoot->markContainingBlocksForLayout(false);
1046                 m_layoutRoot = 0;
1047                 relayoutRoot->markContainingBlocksForLayout(false);
1048             }
1049         }
1050     } else {
1051         int delay = m_frame->document()->minimumLayoutDelay();
1052         m_layoutRoot = relayoutRoot;
1053         m_delayedLayout = delay != 0;
1054         m_layoutTimer.startOneShot(delay * 0.001);
1055     }
1056 }
1057
1058 bool FrameView::layoutPending() const
1059 {
1060     return m_layoutTimer.isActive();
1061 }
1062
1063 bool FrameView::needsLayout() const
1064 {
1065     // This can return true in cases where the document does not have a body yet.
1066     // Document::shouldScheduleLayout takes care of preventing us from scheduling
1067     // layout in that case.
1068     if (!m_frame)
1069         return false;
1070     RenderView* root = m_frame->contentRenderer();
1071     Document* document = m_frame->document();
1072     return layoutPending()
1073         || (root && root->needsLayout())
1074         || m_layoutRoot
1075         || (document && document->childNeedsStyleRecalc()) // can occur when using WebKit ObjC interface
1076         || m_frame->needsReapplyStyles()
1077         || (m_deferSetNeedsLayouts && m_setNeedsLayoutWasDeferred);
1078 }
1079
1080 void FrameView::setNeedsLayout()
1081 {
1082     if (m_deferSetNeedsLayouts) {
1083         m_setNeedsLayoutWasDeferred = true;
1084         return;
1085     }
1086     RenderView* root = m_frame->contentRenderer();
1087     if (root)
1088         root->setNeedsLayout(true);
1089 }
1090
1091 void FrameView::unscheduleRelayout()
1092 {
1093     if (!m_layoutTimer.isActive())
1094         return;
1095
1096 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1097     if (!m_frame->document()->ownerElement())
1098         printf("Layout timer unscheduled at %d\n", m_frame->document()->elapsedTime());
1099 #endif
1100     
1101     m_layoutTimer.stop();
1102     m_delayedLayout = false;
1103 }
1104
1105 bool FrameView::isTransparent() const
1106 {
1107     return m_isTransparent;
1108 }
1109
1110 void FrameView::setTransparent(bool isTransparent)
1111 {
1112     m_isTransparent = isTransparent;
1113 }
1114
1115 Color FrameView::baseBackgroundColor() const
1116 {
1117     return m_baseBackgroundColor;
1118 }
1119
1120 void FrameView::setBaseBackgroundColor(Color bc)
1121 {
1122     if (!bc.isValid())
1123         bc = Color::white;
1124     m_baseBackgroundColor = bc;
1125 }
1126
1127 void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
1128 {
1129     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1130         FrameView* view = frame->view();
1131         if (!view)
1132             continue;
1133
1134         view->setTransparent(transparent);
1135         view->setBaseBackgroundColor(backgroundColor);
1136     }
1137 }
1138
1139 bool FrameView::shouldUpdateWhileOffscreen() const
1140 {
1141     return m_shouldUpdateWhileOffscreen;
1142 }
1143
1144 void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
1145 {
1146     m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
1147 }
1148
1149 void FrameView::scheduleEvent(PassRefPtr<Event> event, PassRefPtr<Node> eventTarget)
1150 {
1151     if (!m_enqueueEvents) {
1152         ExceptionCode ec = 0;
1153         eventTarget->dispatchEvent(event, ec);
1154         return;
1155     }
1156
1157     ScheduledEvent* scheduledEvent = new ScheduledEvent;
1158     scheduledEvent->m_event = event;
1159     scheduledEvent->m_eventTarget = eventTarget;
1160     m_scheduledEvents.append(scheduledEvent);
1161 }
1162
1163 void FrameView::pauseScheduledEvents()
1164 {
1165     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1166     m_enqueueEvents++;
1167 }
1168
1169 void FrameView::resumeScheduledEvents()
1170 {
1171     m_enqueueEvents--;
1172     if (!m_enqueueEvents)
1173         dispatchScheduledEvents();
1174     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1175 }
1176
1177 void FrameView::scrollToAnchor()
1178 {
1179     RefPtr<Node> anchorNode = m_maintainScrollPositionAnchor;
1180     if (!anchorNode)
1181         return;
1182
1183     if (!anchorNode->renderer())
1184         return;
1185
1186     IntRect rect;
1187     if (anchorNode != m_frame->document())
1188         rect = anchorNode->getRect();
1189
1190     // Scroll nested layers and frames to reveal the anchor.
1191     // Align to the top and to the closest side (this matches other browsers).
1192     anchorNode->renderer()->enclosingLayer()->scrollRectToVisible(rect, true, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
1193
1194     // scrollRectToVisible can call into scrollRectIntoViewRecursively(), which resets m_maintainScrollPositionAnchor.
1195     m_maintainScrollPositionAnchor = anchorNode;
1196 }
1197
1198 bool FrameView::updateWidgets()
1199 {
1200     if (m_nestedLayoutCount > 1 || !m_widgetUpdateSet || m_widgetUpdateSet->isEmpty())
1201         return true;
1202     
1203     Vector<RenderPartObject*> objectVector;
1204     copyToVector(*m_widgetUpdateSet, objectVector);
1205     size_t size = objectVector.size();
1206     for (size_t i = 0; i < size; ++i) {
1207         RenderPartObject* object = objectVector[i];
1208         object->updateWidget(false);
1209         
1210         // updateWidget() can destroy the RenderPartObject, so we need to make sure it's
1211         // alive by checking if it's still in m_widgetUpdateSet.
1212         if (m_widgetUpdateSet->contains(object)) {
1213             object->updateWidgetPosition();
1214             m_widgetUpdateSet->remove(object);
1215         }
1216     }
1217     
1218     return m_widgetUpdateSet->isEmpty();
1219 }
1220     
1221 void FrameView::performPostLayoutTasks()
1222 {
1223     if (m_firstLayoutCallbackPending) {
1224         m_firstLayoutCallbackPending = false;
1225         m_frame->loader()->didFirstLayout();
1226     }
1227
1228     if (m_isVisuallyNonEmpty && m_firstVisuallyNonEmptyLayoutCallbackPending) {
1229         m_firstVisuallyNonEmptyLayoutCallbackPending = false;
1230         m_frame->loader()->didFirstVisuallyNonEmptyLayout();
1231     }
1232
1233     RenderView* root = m_frame->contentRenderer();
1234
1235     root->updateWidgetPositions();
1236     
1237     for (unsigned i = 0; i < maxUpdateWidgetsIterations; i++) {
1238         if (updateWidgets())
1239             break;
1240     }
1241
1242     scrollToAnchor();
1243
1244     resumeScheduledEvents();
1245
1246     if (!root->printing()) {
1247         IntSize currentSize = IntSize(width(), height());
1248         float currentZoomFactor = root->style()->zoom();
1249         bool resized = !m_firstLayout && (currentSize != m_lastLayoutSize || currentZoomFactor != m_lastZoomFactor);
1250         m_lastLayoutSize = currentSize;
1251         m_lastZoomFactor = currentZoomFactor;
1252         if (resized)
1253             m_frame->eventHandler()->sendResizeEvent();
1254     }
1255 }
1256
1257 void FrameView::postLayoutTimerFired(Timer<FrameView>*)
1258 {
1259     performPostLayoutTasks();
1260 }
1261
1262 void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
1263 {
1264     if (!m_viewportRenderer)
1265         return;
1266     
1267     if (m_overflowStatusDirty) {
1268         m_horizontalOverflow = horizontalOverflow;
1269         m_verticalOverflow = verticalOverflow;
1270         m_overflowStatusDirty = false;
1271         return;
1272     }
1273     
1274     bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
1275     bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
1276     
1277     if (horizontalOverflowChanged || verticalOverflowChanged) {
1278         m_horizontalOverflow = horizontalOverflow;
1279         m_verticalOverflow = verticalOverflow;
1280         
1281         scheduleEvent(OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
1282             verticalOverflowChanged, verticalOverflow),
1283             m_viewportRenderer->node());
1284     }
1285     
1286 }
1287
1288 void FrameView::dispatchScheduledEvents()
1289 {
1290     if (m_scheduledEvents.isEmpty())
1291         return;
1292
1293     Vector<ScheduledEvent*> scheduledEventsCopy = m_scheduledEvents;
1294     m_scheduledEvents.clear();
1295     
1296     Vector<ScheduledEvent*>::iterator end = scheduledEventsCopy.end();
1297     for (Vector<ScheduledEvent*>::iterator it = scheduledEventsCopy.begin(); it != end; ++it) {
1298         ScheduledEvent* scheduledEvent = *it;
1299         
1300         ExceptionCode ec = 0;
1301         
1302         // Only dispatch events to nodes that are in the document
1303         if (scheduledEvent->m_eventTarget->inDocument())
1304             scheduledEvent->m_eventTarget->dispatchEvent(scheduledEvent->m_event, ec);
1305         
1306         delete scheduledEvent;
1307     }
1308 }
1309
1310 IntRect FrameView::windowClipRect(bool clipToContents) const
1311 {
1312     ASSERT(m_frame->view() == this);
1313
1314     // Set our clip rect to be our contents.
1315     IntRect clipRect = contentsToWindow(visibleContentRect(!clipToContents));
1316     if (!m_frame || !m_frame->document()->ownerElement())
1317         return clipRect;
1318
1319     // Take our owner element and get the clip rect from the enclosing layer.
1320     Element* elt = m_frame->document()->ownerElement();
1321     RenderLayer* layer = elt->renderer()->enclosingLayer();
1322     // FIXME: layer should never be null, but sometimes seems to be anyway.
1323     if (!layer)
1324         return clipRect;
1325     FrameView* parentView = elt->document()->view();
1326     clipRect.intersect(parentView->windowClipRectForLayer(layer, true));
1327     return clipRect;
1328 }
1329
1330 IntRect FrameView::windowClipRectForLayer(const RenderLayer* layer, bool clipToLayerContents) const
1331 {
1332     // If we have no layer, just return our window clip rect.
1333     if (!layer)
1334         return windowClipRect();
1335
1336     // Apply the clip from the layer.
1337     IntRect clipRect;
1338     if (clipToLayerContents)
1339         clipRect = layer->childrenClipRect();
1340     else
1341         clipRect = layer->selfClipRect();
1342     clipRect = contentsToWindow(clipRect); 
1343     return intersection(clipRect, windowClipRect());
1344 }
1345
1346 bool FrameView::isActive() const
1347 {
1348     Page* page = frame()->page();
1349     return page && page->focusController()->isActive();
1350 }
1351
1352 void FrameView::valueChanged(Scrollbar* bar)
1353 {
1354     // Figure out if we really moved.
1355     IntSize offset = scrollOffset();
1356     ScrollView::valueChanged(bar);
1357     if (offset != scrollOffset())
1358         frame()->eventHandler()->sendScrollEvent();
1359 }
1360
1361 void FrameView::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
1362 {
1363     // Add in our offset within the FrameView.
1364     IntRect dirtyRect = rect;
1365     dirtyRect.move(scrollbar->x(), scrollbar->y());
1366     invalidateRect(dirtyRect);
1367 }
1368
1369 void FrameView::getTickmarks(Vector<IntRect>& tickmarks) const
1370 {
1371     tickmarks = frame()->document()->renderedRectsForMarkers(DocumentMarker::TextMatch);
1372 }
1373
1374 IntRect FrameView::windowResizerRect() const
1375 {
1376     Page* page = frame() ? frame()->page() : 0;
1377     if (!page)
1378         return IntRect();
1379     return page->chrome()->windowResizerRect();
1380 }
1381
1382 #if ENABLE(DASHBOARD_SUPPORT)
1383 void FrameView::updateDashboardRegions()
1384 {
1385     Document* document = m_frame->document();
1386     if (!document->hasDashboardRegions())
1387         return;
1388     Vector<DashboardRegionValue> newRegions;
1389     document->renderBox()->collectDashboardRegions(newRegions);
1390     if (newRegions == document->dashboardRegions())
1391         return;
1392     document->setDashboardRegions(newRegions);
1393     Page* page = m_frame->page();
1394     if (!page)
1395         return;
1396     page->chrome()->client()->dashboardRegionsChanged();
1397 }
1398 #endif
1399
1400 void FrameView::updateControlTints()
1401 {
1402     // This is called when control tints are changed from aqua/graphite to clear and vice versa.
1403     // We do a "fake" paint, and when the theme gets a paint call, it can then do an invalidate.
1404     // This is only done if the theme supports control tinting. It's up to the theme and platform
1405     // to define when controls get the tint and to call this function when that changes.
1406     
1407     // Optimize the common case where we bring a window to the front while it's still empty.
1408     if (!m_frame || m_frame->loader()->url().isEmpty())
1409         return;
1410
1411     if (m_frame->contentRenderer() && m_frame->contentRenderer()->theme()->supportsControlTints()) {
1412         if (needsLayout())
1413             layout();
1414         PlatformGraphicsContext* const noContext = 0;
1415         GraphicsContext context(noContext);
1416         context.setUpdatingControlTints(true);
1417         if (platformWidget())
1418             paintContents(&context, visibleContentRect());
1419         else
1420             paint(&context, frameRect());
1421     }
1422 }
1423
1424 bool FrameView::wasScrolledByUser() const
1425 {
1426     return m_wasScrolledByUser;
1427 }
1428
1429 void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
1430 {
1431     if (m_inProgrammaticScroll)
1432         return;
1433     m_maintainScrollPositionAnchor = 0;
1434     m_wasScrolledByUser = wasScrolledByUser;
1435 }
1436
1437 void FrameView::paintContents(GraphicsContext* p, const IntRect& rect)
1438 {
1439     if (!frame())
1440         return;
1441     
1442     Document* document = frame()->document();
1443
1444 #ifndef NDEBUG
1445     bool fillWithRed;
1446     if (document->printing())
1447         fillWithRed = false; // Printing, don't fill with red (can't remember why).
1448     else if (document->ownerElement())
1449         fillWithRed = false; // Subframe, don't fill with red.
1450     else if (isTransparent())
1451         fillWithRed = false; // Transparent, don't fill with red.
1452     else if (m_paintRestriction == PaintRestrictionSelectionOnly || m_paintRestriction == PaintRestrictionSelectionOnlyBlackText)
1453         fillWithRed = false; // Selections are transparent, don't fill with red.
1454     else if (m_nodeToDraw)
1455         fillWithRed = false; // Element images are transparent, don't fill with red.
1456     else
1457         fillWithRed = true;
1458     
1459     if (fillWithRed)
1460         p->fillRect(rect, Color(0xFF, 0, 0));
1461 #endif
1462
1463     bool isTopLevelPainter = !sCurrentPaintTimeStamp;
1464     if (isTopLevelPainter)
1465         sCurrentPaintTimeStamp = currentTime();
1466     
1467     RenderView* contentRenderer = frame()->contentRenderer();
1468     if (!contentRenderer) {
1469         LOG_ERROR("called Frame::paint with nil renderer");
1470         return;
1471     }
1472
1473     ASSERT(!needsLayout());
1474     if (needsLayout())
1475         return;
1476
1477 #if USE(ACCELERATED_COMPOSITING)
1478     if (!p->paintingDisabled()) {
1479         if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
1480             rootLayer->syncCompositingState();
1481     }
1482 #endif
1483
1484     ASSERT(!m_isPainting);
1485         
1486     m_isPainting = true;
1487         
1488     // m_nodeToDraw is used to draw only one element (and its descendants)
1489     RenderObject* eltRenderer = m_nodeToDraw ? m_nodeToDraw->renderer() : 0;
1490     if (m_paintRestriction == PaintRestrictionNone)
1491         document->invalidateRenderedRectsForMarkersInRect(rect);
1492     contentRenderer->layer()->paint(p, rect, m_paintRestriction, eltRenderer);
1493     
1494     m_isPainting = false;
1495     m_lastPaintTime = currentTime();
1496
1497 #if ENABLE(DASHBOARD_SUPPORT)
1498     // Regions may have changed as a result of the visibility/z-index of element changing.
1499     if (document->dashboardRegionsDirty())
1500         updateDashboardRegions();
1501 #endif
1502
1503     if (isTopLevelPainter)
1504         sCurrentPaintTimeStamp = 0;
1505 }
1506
1507 void FrameView::setPaintRestriction(PaintRestriction pr)
1508 {
1509     m_paintRestriction = pr;
1510 }
1511     
1512 bool FrameView::isPainting() const
1513 {
1514     return m_isPainting;
1515 }
1516
1517 void FrameView::setNodeToDraw(Node* node)
1518 {
1519     m_nodeToDraw = node;
1520 }
1521
1522 void FrameView::layoutIfNeededRecursive()
1523 {
1524     // We have to crawl our entire tree looking for any FrameViews that need
1525     // layout and make sure they are up to date.
1526     // Mac actually tests for intersection with the dirty region and tries not to
1527     // update layout for frames that are outside the dirty region.  Not only does this seem
1528     // pointless (since those frames will have set a zero timer to layout anyway), but
1529     // it is also incorrect, since if two frames overlap, the first could be excluded from the dirty
1530     // region but then become included later by the second frame adding rects to the dirty region
1531     // when it lays out.
1532
1533     if (needsLayout())
1534         layout();
1535
1536     const HashSet<RefPtr<Widget> >* viewChildren = children();
1537     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
1538     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
1539         Widget* widget = (*current).get();
1540         if (widget->isFrameView())
1541             static_cast<FrameView*>(widget)->layoutIfNeededRecursive();
1542     }
1543
1544     // layoutIfNeededRecursive is called when we need to make sure layout is up-to-date before
1545     // painting, so we need to flush out any deferred repaints too.
1546     if (m_deferredRepaintTimer.isActive()) {
1547         m_deferredRepaintTimer.stop();
1548         doDeferredRepaints();
1549     }
1550 }
1551
1552 void FrameView::forceLayout(bool allowSubtree)
1553 {
1554     layout(allowSubtree);
1555     // We cannot unschedule a pending relayout, since the force can be called with
1556     // a tiny rectangle from a drawRect update.  By unscheduling we in effect
1557     // "validate" and stop the necessary full repaint from occurring.  Basically any basic
1558     // append/remove DHTML is broken by this call.  For now, I have removed the optimization
1559     // until we have a better invalidation stategy. -dwh
1560     //unscheduleRelayout();
1561 }
1562
1563 void FrameView::forceLayoutWithPageWidthRange(float minPageWidth, float maxPageWidth, bool _adjustViewSize)
1564 {
1565     // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
1566     // the state of things before and after the layout
1567     RenderView *root = toRenderView(m_frame->document()->renderer());
1568     if (root) {
1569         // This magic is basically copied from khtmlview::print
1570         int pageW = (int)ceilf(minPageWidth);
1571         root->setWidth(pageW);
1572         root->setNeedsLayoutAndPrefWidthsRecalc();
1573         forceLayout();
1574
1575         // If we don't fit in the minimum page width, we'll lay out again. If we don't fit in the
1576         // maximum page width, we will lay out to the maximum page width and clip extra content.
1577         // FIXME: We are assuming a shrink-to-fit printing implementation.  A cropping
1578         // implementation should not do this!
1579         int rightmostPos = root->rightmostPosition();
1580         if (rightmostPos > minPageWidth) {
1581             pageW = std::min(rightmostPos, (int)ceilf(maxPageWidth));
1582             root->setWidth(pageW);
1583             root->setNeedsLayoutAndPrefWidthsRecalc();
1584             forceLayout();
1585         }
1586     }
1587
1588     if (_adjustViewSize)
1589         adjustViewSize();
1590 }
1591
1592 void FrameView::adjustPageHeight(float *newBottom, float oldTop, float oldBottom, float /*bottomLimit*/)
1593 {
1594     RenderView* root = m_frame->contentRenderer();
1595     if (root) {
1596         // Use a context with painting disabled.
1597         GraphicsContext context((PlatformGraphicsContext*)0);
1598         root->setTruncatedAt((int)floorf(oldBottom));
1599         IntRect dirtyRect(0, (int)floorf(oldTop), root->rightLayoutOverflow(), (int)ceilf(oldBottom - oldTop));
1600         root->layer()->paint(&context, dirtyRect);
1601         *newBottom = root->bestTruncatedAt();
1602         if (*newBottom == 0)
1603             *newBottom = oldBottom;
1604     } else
1605         *newBottom = oldBottom;
1606 }
1607
1608 IntRect FrameView::convertFromRenderer(const RenderObject* renderer, const IntRect& rendererRect) const
1609 {
1610     IntRect rect = renderer->localToAbsoluteQuad(FloatRect(rendererRect)).enclosingBoundingBox();
1611
1612     // Convert from page ("absolute") to FrameView coordinates.
1613     rect.move(-scrollX(), -scrollY());
1614
1615     return rect;
1616 }
1617
1618 IntRect FrameView::convertToRenderer(const RenderObject* renderer, const IntRect& viewRect) const
1619 {
1620     IntRect rect = viewRect;
1621     
1622     // Convert from FrameView coords into page ("absolute") coordinates.
1623     rect.move(scrollX(), scrollY());
1624
1625     // FIXME: we don't have a way to map an absolute rect down to a local quad, so just
1626     // move the rect for now.
1627     rect.setLocation(roundedIntPoint(renderer->absoluteToLocal(rect.location(), false, true /* use transforms */)));
1628     return rect;
1629 }
1630
1631 IntPoint FrameView::convertFromRenderer(const RenderObject* renderer, const IntPoint& rendererPoint) const
1632 {
1633     IntPoint point = roundedIntPoint(renderer->localToAbsolute(rendererPoint, false, true /* use transforms */));
1634
1635     // Convert from page ("absolute") to FrameView coordinates.
1636     point.move(-scrollX(), -scrollY());
1637     return point;
1638 }
1639
1640 IntPoint FrameView::convertToRenderer(const RenderObject* renderer, const IntPoint& viewPoint) const
1641 {
1642     IntPoint point = viewPoint;
1643     
1644     // Convert from FrameView coords into page ("absolute") coordinates.
1645     point += IntSize(scrollX(), scrollY());
1646
1647     return roundedIntPoint(renderer->absoluteToLocal(point, false, true /* use transforms */));
1648 }
1649
1650 IntRect FrameView::convertToContainingView(const IntRect& localRect) const
1651 {
1652     if (const ScrollView* parentScrollView = parent()) {
1653         if (parentScrollView->isFrameView()) {
1654             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
1655             // Get our renderer in the parent view
1656             RenderPart* renderer = m_frame->ownerRenderer();
1657             if (!renderer)
1658                 return localRect;
1659                 
1660             IntRect rect(localRect);
1661             // Add borders and padding??
1662             rect.move(renderer->borderLeft() + renderer->paddingLeft(),
1663                       renderer->borderTop() + renderer->paddingTop());
1664             return parentView->convertFromRenderer(renderer, rect);
1665         }
1666         
1667         return Widget::convertToContainingView(localRect);
1668     }
1669     
1670     return localRect;
1671 }
1672
1673 IntRect FrameView::convertFromContainingView(const IntRect& parentRect) const
1674 {
1675     if (const ScrollView* parentScrollView = parent()) {
1676         if (parentScrollView->isFrameView()) {
1677             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
1678
1679             // Get our renderer in the parent view
1680             RenderPart* renderer = m_frame->ownerRenderer();
1681             if (!renderer)
1682                 return parentRect;
1683
1684             IntRect rect = parentView->convertToRenderer(renderer, parentRect);
1685             // Subtract borders and padding
1686             rect.move(-renderer->borderLeft() - renderer->paddingLeft(),
1687                       -renderer->borderTop() - renderer->paddingTop());
1688             return rect;
1689         }
1690         
1691         return Widget::convertFromContainingView(parentRect);
1692     }
1693     
1694     return parentRect;
1695 }
1696
1697 IntPoint FrameView::convertToContainingView(const IntPoint& localPoint) const
1698 {
1699     if (const ScrollView* parentScrollView = parent()) {
1700         if (parentScrollView->isFrameView()) {
1701             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
1702
1703             // Get our renderer in the parent view
1704             RenderPart* renderer = m_frame->ownerRenderer();
1705             if (!renderer)
1706                 return localPoint;
1707                 
1708             IntPoint point(localPoint);
1709
1710             // Add borders and padding
1711             point.move(renderer->borderLeft() + renderer->paddingLeft(),
1712                        renderer->borderTop() + renderer->paddingTop());
1713             return parentView->convertFromRenderer(renderer, point);
1714         }
1715         
1716         return Widget::convertToContainingView(localPoint);
1717     }
1718     
1719     return localPoint;
1720 }
1721
1722 IntPoint FrameView::convertFromContainingView(const IntPoint& parentPoint) const
1723 {
1724     if (const ScrollView* parentScrollView = parent()) {
1725         if (parentScrollView->isFrameView()) {
1726             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
1727
1728             // Get our renderer in the parent view
1729             RenderPart* renderer = m_frame->ownerRenderer();
1730             if (!renderer)
1731                 return parentPoint;
1732
1733             IntPoint point = parentView->convertToRenderer(renderer, parentPoint);
1734             // Subtract borders and padding
1735             point.move(-renderer->borderLeft() - renderer->paddingLeft(),
1736                        -renderer->borderTop() - renderer->paddingTop());
1737             return point;
1738         }
1739         
1740         return Widget::convertFromContainingView(parentPoint);
1741     }
1742     
1743     return parentPoint;
1744 }
1745
1746 } // namespace WebCore