2 Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 (C) 1998 Waldo Bastian (bastian@kde.org)
4 (C) 1998, 1999 Torben Weis (weis@kde.org)
5 (C) 1999 Lars Knoll (knoll@kde.org)
6 (C) 1999 Antti Koivisto (koivisto@kde.org)
7 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
28 #include "RuntimeEnabledFeatures.h"
29 #include "core/rendering/PaintPhase.h"
30 #include "platform/geometry/LayoutRect.h"
31 #include "platform/graphics/Color.h"
32 #include "platform/scroll/ScrollView.h"
33 #include "wtf/Forward.h"
34 #include "wtf/OwnPtr.h"
35 #include "wtf/text/WTFString.h"
40 class DocumentLifecycle;
44 class HTMLFrameOwnerElement;
50 class RenderEmbeddedObject;
52 class RenderScrollbarPart;
57 typedef unsigned long long DOMTimeStamp;
59 class FrameView FINAL : public ScrollView {
61 friend class RenderView;
62 friend class Internals;
64 static PassRefPtr<FrameView> create(LocalFrame*);
65 static PassRefPtr<FrameView> create(LocalFrame*, const IntSize& initialSize);
69 virtual HostWindow* hostWindow() const OVERRIDE;
71 virtual void invalidateRect(const IntRect&) OVERRIDE;
72 virtual void setFrameRect(const IntRect&) OVERRIDE;
74 virtual bool scheduleAnimation() OVERRIDE;
76 LocalFrame& frame() const { return *m_frame; }
79 RenderView* renderView() const;
81 virtual void setCanHaveScrollbars(bool) OVERRIDE;
83 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation) OVERRIDE;
85 virtual void setContentsSize(const IntSize&) OVERRIDE;
86 IntPoint clampOffsetAtScale(const IntPoint& offset, float scale) const;
88 void layout(bool allowSubtree = true);
89 bool didFirstLayout() const;
90 void scheduleRelayout();
91 void scheduleRelayoutOfSubtree(RenderObject*);
92 bool layoutPending() const;
93 bool isInPerformLayout() const;
95 void setCanRepaintDuringPerformLayout(bool b) { m_canRepaintDuringPerformLayout = b; }
96 bool canRepaintDuringPerformLayout() const { return m_canRepaintDuringPerformLayout; }
98 RenderObject* layoutRoot(bool onlyDuringLayout = false) const;
99 void clearLayoutSubtreeRoot() { m_layoutSubtreeRoot = 0; }
100 int layoutCount() const { return m_layoutCount; }
102 bool needsLayout() const;
103 void setNeedsLayout();
105 // Methods for getting/setting the size Blink should use to layout the contents.
106 IntSize layoutSize(IncludeScrollbarsInRect = ExcludeScrollbars) const;
107 void setLayoutSize(const IntSize&);
109 // If this is set to false, the layout size will need to be explicitly set by the owner.
110 // E.g. WebViewImpl sets its mainFrame's layout size manually
111 void setLayoutSizeFixedToFrameSize(bool isFixed) { m_layoutSizeFixedToFrameSize = isFixed; }
112 bool layoutSizeFixedToFrameSize() { return m_layoutSizeFixedToFrameSize; }
114 bool needsFullRepaint() const { return m_doFullRepaint; }
116 void updateCompositingLayersAfterStyleChange();
117 void updateCompositingLayersAfterLayout();
119 bool hasCompositedContent() const;
120 bool isEnclosedInCompositingLayer() const;
122 void resetScrollbars();
123 void prepareForDetach();
124 void detachCustomScrollbars();
125 virtual void recalculateScrollbarOverlayStyle();
129 bool isTransparent() const;
130 void setTransparent(bool isTransparent);
132 // True if the FrameView is not transparent, and the base background color is opaque.
133 bool hasOpaqueBackground() const;
135 Color baseBackgroundColor() const;
136 void setBaseBackgroundColor(const Color&);
137 void updateBackgroundRecursively(const Color&, bool);
139 void adjustViewSize();
141 virtual IntRect windowClipRect(IncludeScrollbarsInRect = ExcludeScrollbars) const OVERRIDE;
142 IntRect windowClipRectForFrameOwner(const HTMLFrameOwnerElement*) const;
144 virtual IntRect windowResizerRect() const OVERRIDE;
146 virtual float visibleContentScaleFactor() const OVERRIDE { return m_visibleContentScaleFactor; }
147 void setVisibleContentScaleFactor(float);
149 virtual float inputEventsScaleFactor() const OVERRIDE;
150 virtual IntSize inputEventsOffsetForEmulation() const OVERRIDE;
151 void setInputEventsTransformForEmulation(const IntSize&, float);
153 virtual void setScrollPosition(const IntPoint&) OVERRIDE;
154 virtual bool shouldRubberBandInDirection(ScrollDirection) const OVERRIDE;
155 virtual bool isRubberBandInProgress() const OVERRIDE;
156 void setScrollPositionNonProgrammatically(const IntPoint&);
158 // This is different than visibleContentRect() in that it ignores negative (or overly positive)
159 // offsets from rubber-banding, and it takes zooming into account.
160 LayoutRect viewportConstrainedVisibleContentRect() const;
161 void viewportConstrainedVisibleContentSizeChanged(bool widthChanged, bool heightChanged);
163 AtomicString mediaType() const;
164 void setMediaType(const AtomicString&);
165 void adjustMediaTypeForPrinting(bool printing);
167 void setCannotBlitToWindow();
168 void setIsOverlapped(bool);
169 bool isOverlapped() const { return m_isOverlapped; }
170 void setContentIsOpaque(bool);
172 void addSlowRepaintObject();
173 void removeSlowRepaintObject();
174 bool hasSlowRepaintObjects() const { return m_slowRepaintObjectCount; }
176 // Includes fixed- and sticky-position objects.
177 typedef HashSet<RenderObject*> ViewportConstrainedObjectSet;
178 void addViewportConstrainedObject(RenderObject*);
179 void removeViewportConstrainedObject(RenderObject*);
180 const ViewportConstrainedObjectSet* viewportConstrainedObjects() const { return m_viewportConstrainedObjects.get(); }
181 bool hasViewportConstrainedObjects() const { return m_viewportConstrainedObjects && m_viewportConstrainedObjects->size() > 0; }
183 void handleLoadCompleted();
185 void updateAnnotatedRegions();
186 void updateControlTints();
188 void restoreScrollbar();
190 void postLayoutTimerFired(Timer<FrameView>*);
192 bool wasScrolledByUser() const;
193 void setWasScrolledByUser(bool);
195 bool safeToPropagateScrollToParent() const { return m_safeToPropagateScrollToParent; }
196 void setSafeToPropagateScrollToParent(bool isSafe) { m_safeToPropagateScrollToParent = isSafe; }
198 void addWidget(RenderWidget*);
199 void removeWidget(RenderWidget*);
200 void updateWidgetPositions();
202 void addWidgetToUpdate(RenderEmbeddedObject&);
204 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) OVERRIDE;
205 void setPaintBehavior(PaintBehavior);
206 PaintBehavior paintBehavior() const;
207 bool isPainting() const;
208 bool hasEverPainted() const { return m_lastPaintTime; }
209 void setNodeToDraw(Node*);
211 virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect) OVERRIDE;
212 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect) OVERRIDE;
213 virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&) OVERRIDE;
215 Color documentBackgroundColor() const;
217 static double currentFrameTimeStamp() { return s_currentFrameTimeStamp; }
219 void updateLayoutAndStyleForPainting();
220 void updateLayoutAndStyleIfNeededRecursive();
222 void incrementVisuallyNonEmptyCharacterCount(unsigned);
223 void incrementVisuallyNonEmptyPixelCount(const IntSize&);
224 void setIsVisuallyNonEmpty() { m_isVisuallyNonEmpty = true; }
225 void enableAutoSizeMode(bool enable, const IntSize& minSize, const IntSize& maxSize);
227 void forceLayout(bool allowSubtree = false);
228 void forceLayoutForPagination(const FloatSize& pageSize, const FloatSize& originalPageSize, float maximumShrinkFactor);
230 bool scrollToFragment(const KURL&);
231 bool scrollToAnchor(const String&);
232 void maintainScrollPositionAtAnchor(Node*);
233 void scrollElementToRect(Element*, const IntRect&);
234 void scrollContentsIfNeededRecursive();
236 // Methods to convert points and rects between the coordinate space of the renderer, and this view.
237 IntRect convertFromRenderer(const RenderObject*, const IntRect&) const;
238 IntRect convertToRenderer(const RenderObject*, const IntRect&) const;
239 IntPoint convertFromRenderer(const RenderObject*, const IntPoint&) const;
240 IntPoint convertToRenderer(const RenderObject*, const IntPoint&) const;
242 bool isFrameViewScrollCorner(RenderScrollbarPart* scrollCorner) const { return m_scrollCorner == scrollCorner; }
246 enum ScrollbarModesCalculationStrategy { RulesFromWebContentOnly, AnyRule };
247 void calculateScrollbarModesForLayoutAndSetViewportRenderer(ScrollbarMode& hMode, ScrollbarMode& vMode, ScrollbarModesCalculationStrategy = AnyRule);
249 virtual IntPoint lastKnownMousePosition() const OVERRIDE;
250 bool shouldSetCursor() const;
252 void setCursor(const Cursor&);
254 virtual bool scrollbarsCanBeActive() const OVERRIDE;
256 // FIXME: Remove this method once plugin loading is decoupled from layout.
257 void flushAnyPendingPostLayoutTasks();
259 virtual bool shouldSuspendScrollAnimations() const OVERRIDE;
260 virtual void scrollbarStyleChanged(int newStyle, bool forceUpdate) OVERRIDE;
262 RenderBox* embeddedContentBox() const;
264 void setTracksRepaints(bool);
265 bool isTrackingRepaints() const { return m_isTrackingRepaints; }
266 void resetTrackedRepaints();
267 String trackedRepaintRectsAsText() const;
269 typedef HashSet<ScrollableArea*> ScrollableAreaSet;
270 // Returns whether the scrollable area has just been newly added.
271 bool addScrollableArea(ScrollableArea*);
272 // Returns whether the scrollable area has just been removed.
273 bool removeScrollableArea(ScrollableArea*);
274 bool containsScrollableArea(const ScrollableArea*) const;
275 const ScrollableAreaSet* scrollableAreas() const { return m_scrollableAreas.get(); }
277 // With CSS style "resize:" enabled, a little resizer handle will appear at the bottom
278 // right of the object. We keep track of these resizer areas for checking if touches
279 // (implemented using Scroll gesture) are targeting the resizer.
280 typedef HashSet<RenderBox*> ResizerAreaSet;
281 void addResizerArea(RenderBox*);
282 void removeResizerArea(RenderBox*);
283 const ResizerAreaSet* resizerAreas() const { return m_resizerAreas.get(); }
285 virtual void removeChild(Widget*) OVERRIDE;
287 // This function exists for ports that need to handle wheel events manually.
288 // On Mac WebKit1 the underlying NSScrollView just does the scrolling, but on most other platforms
289 // we need this function in order to do the scroll ourselves.
290 bool wheelEvent(const PlatformWheelEvent&);
292 bool inProgrammaticScroll() const { return m_inProgrammaticScroll; }
293 void setInProgrammaticScroll(bool programmaticScroll) { m_inProgrammaticScroll = programmaticScroll; }
295 void setHasSoftwareFilters(bool hasSoftwareFilters) { m_hasSoftwareFilters = hasSoftwareFilters; }
296 bool hasSoftwareFilters() const { return m_hasSoftwareFilters; }
298 virtual bool isActive() const OVERRIDE;
300 // DEPRECATED: Use viewportConstrainedVisibleContentRect() instead.
301 IntSize scrollOffsetForFixedPosition() const;
303 virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE;
305 // Override scrollbar notifications to update the AXObject cache.
306 virtual void didAddScrollbar(Scrollbar*, ScrollbarOrientation) OVERRIDE;
307 virtual void willRemoveScrollbar(Scrollbar*, ScrollbarOrientation) OVERRIDE;
309 virtual bool shouldAttemptToScrollUsingFastPath() const OVERRIDE;
310 // FIXME: This should probably be renamed as the 'inSubtreeLayout' parameter
311 // passed around the FrameView layout methods can be true while this returns
313 bool isSubtreeLayout() const { return !!m_layoutSubtreeRoot; }
315 // Sets the tickmarks for the FrameView, overriding the default behavior
316 // which is to display the tickmarks corresponding to find results.
317 // If |m_tickmarks| is empty, the default behavior is restored.
318 void setTickmarks(const Vector<IntRect>& tickmarks) { m_tickmarks = tickmarks; }
320 // ScrollableArea interface
321 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE;
322 virtual void getTickmarks(Vector<IntRect>&) const OVERRIDE;
323 virtual void scrollTo(const IntSize&) OVERRIDE;
324 virtual IntRect scrollableAreaBoundingBox() const OVERRIDE;
325 virtual bool scrollAnimatorEnabled() const OVERRIDE;
326 virtual bool usesCompositedScrolling() const OVERRIDE;
327 virtual GraphicsLayer* layerForScrolling() const OVERRIDE;
328 virtual GraphicsLayer* layerForHorizontalScrollbar() const OVERRIDE;
329 virtual GraphicsLayer* layerForVerticalScrollbar() const OVERRIDE;
330 virtual GraphicsLayer* layerForScrollCorner() const OVERRIDE;
333 virtual void scrollContentsIfNeeded();
334 virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect) OVERRIDE;
335 virtual void scrollContentsSlowPath(const IntRect& updateRect) OVERRIDE;
337 virtual bool isVerticalDocument() const OVERRIDE;
338 virtual bool isFlippedDocument() const OVERRIDE;
341 explicit FrameView(LocalFrame*);
346 virtual void frameRectsChanged() OVERRIDE;
347 virtual bool isFrameView() const OVERRIDE { return true; }
349 friend class RenderWidget;
350 bool useSlowRepaints(bool considerOverlap = true) const;
351 bool useSlowRepaintsIfNotOverlapped() const;
353 bool contentsInCompositedLayer() const;
355 void applyOverflowToViewportAndSetRenderer(RenderObject*, ScrollbarMode& hMode, ScrollbarMode& vMode);
356 void updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow);
358 void updateCounters();
359 void autoSizeIfEnabled();
360 void forceLayoutParentViewIfNeeded();
361 void performPreLayoutTasks();
362 void performLayout(RenderObject* rootForThisLayout, bool inSubtreeLayout);
363 void scheduleOrPerformPostLayoutTasks();
364 void performPostLayoutTasks();
366 void repaintTree(RenderObject* root);
368 void gatherDebugLayoutRects(RenderObject* layoutRoot);
370 DocumentLifecycle& lifecycle() const;
372 virtual void repaintContentRectangle(const IntRect&) OVERRIDE;
373 virtual void contentsResized() OVERRIDE;
374 virtual void scrollbarExistenceDidChange() OVERRIDE;
376 // Override ScrollView methods to do point conversion via renderers, in order to
377 // take transforms into account.
378 virtual IntRect convertToContainingView(const IntRect&) const OVERRIDE;
379 virtual IntRect convertFromContainingView(const IntRect&) const OVERRIDE;
380 virtual IntPoint convertToContainingView(const IntPoint&) const OVERRIDE;
381 virtual IntPoint convertFromContainingView(const IntPoint&) const OVERRIDE;
383 void sendResizeEventIfNeeded();
385 void updateScrollableAreaSet();
387 virtual void notifyPageThatContentAreaWillPaint() const OVERRIDE;
389 void scheduleUpdateWidgetsIfNecessary();
390 void updateWidgetsTimerFired(Timer<FrameView>*);
391 bool updateWidgets();
393 void scrollToAnchor();
394 void scrollPositionChanged();
395 void didScrollTimerFired(Timer<FrameView>*);
397 void updateLayersAndCompositingAfterScrollIfNeeded();
398 void updateFixedElementRepaintRectsAfterScroll();
400 bool hasCustomScrollbars() const;
401 bool shouldUseCustomScrollbars(Element*& customScrollbarElement, LocalFrame*& customScrollbarFrame);
403 virtual void updateScrollCorner() OVERRIDE;
405 FrameView* parentFrameView() const;
407 AXObjectCache* axObjectCache() const;
408 void removeFromAXObjectCache();
410 bool isMainFrame() const;
412 void setLayoutSizeInternal(const IntSize&);
414 bool repaintAllowed() const
416 if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
419 return !isInPerformLayout() || canRepaintDuringPerformLayout();
422 static double s_currentFrameTimeStamp; // used for detecting decoded resource thrash in the cache
423 static bool s_inPaintContents;
427 typedef HashSet<RefPtr<RenderEmbeddedObject> > EmbeddedObjectSet;
428 EmbeddedObjectSet m_widgetUpdateSet;
430 // FIXME: These are just "children" of the FrameView and should be RefPtr<Widget> instead.
431 HashSet<RefPtr<RenderWidget> > m_widgets;
433 RefPtr<LocalFrame> m_frame;
435 bool m_doFullRepaint;
437 bool m_canHaveScrollbars;
438 bool m_cannotBlitToWindow;
440 bool m_contentIsOpaque;
441 unsigned m_slowRepaintObjectCount;
443 bool m_hasPendingLayout;
444 RenderObject* m_layoutSubtreeRoot;
446 bool m_layoutSchedulingEnabled;
447 bool m_inPerformLayout;
448 bool m_canRepaintDuringPerformLayout;
449 bool m_doingPreLayoutStyleUpdate;
450 bool m_inSynchronousPostLayout;
452 unsigned m_nestedLayoutCount;
453 Timer<FrameView> m_postLayoutTasksTimer;
454 Timer<FrameView> m_updateWidgetsTimer;
455 bool m_firstLayoutCallbackPending;
458 bool m_isTransparent;
459 Color m_baseBackgroundColor;
460 IntSize m_lastViewportSize;
461 float m_lastZoomFactor;
463 AtomicString m_mediaType;
464 AtomicString m_mediaTypeWhenNotPrinting;
466 bool m_overflowStatusDirty;
467 bool m_horizontalOverflow;
468 bool m_verticalOverflow;
469 RenderObject* m_viewportRenderer;
471 bool m_wasScrolledByUser;
472 bool m_inProgrammaticScroll;
473 bool m_safeToPropagateScrollToParent;
475 double m_lastPaintTime;
477 bool m_isTrackingRepaints; // Used for testing.
478 Vector<IntRect> m_trackedRepaintRects;
480 RefPtr<Node> m_nodeToDraw;
481 PaintBehavior m_paintBehavior;
484 unsigned m_visuallyNonEmptyCharacterCount;
485 unsigned m_visuallyNonEmptyPixelCount;
486 bool m_isVisuallyNonEmpty;
487 bool m_firstVisuallyNonEmptyLayoutCallbackPending;
489 RefPtr<Node> m_maintainScrollPositionAnchor;
491 // Renderer to hold our custom scroll corner.
492 RenderScrollbarPart* m_scrollCorner;
494 // If true, automatically resize the frame view around its content.
495 bool m_shouldAutoSize;
497 // True if autosize has been run since m_shouldAutoSize was set.
498 bool m_didRunAutosize;
499 // The lower bound on the size when autosizing.
500 IntSize m_minAutoSize;
501 // The upper bound on the size when autosizing.
502 IntSize m_maxAutoSize;
504 OwnPtr<ScrollableAreaSet> m_scrollableAreas;
505 OwnPtr<ResizerAreaSet> m_resizerAreas;
506 OwnPtr<ViewportConstrainedObjectSet> m_viewportConstrainedObjects;
508 bool m_hasSoftwareFilters;
510 float m_visibleContentScaleFactor;
511 IntSize m_inputEventsOffsetForEmulation;
512 float m_inputEventsScaleFactorForEmulation;
514 IntSize m_layoutSize;
515 bool m_layoutSizeFixedToFrameSize;
517 Timer<FrameView> m_didScrollTimer;
519 Vector<IntRect> m_tickmarks;
522 inline void FrameView::incrementVisuallyNonEmptyCharacterCount(unsigned count)
524 if (m_isVisuallyNonEmpty)
526 m_visuallyNonEmptyCharacterCount += count;
527 // Use a threshold value to prevent very small amounts of visible content from triggering didFirstVisuallyNonEmptyLayout.
528 // The first few hundred characters rarely contain the interesting content of the page.
529 static const unsigned visualCharacterThreshold = 200;
530 if (m_visuallyNonEmptyCharacterCount > visualCharacterThreshold)
531 setIsVisuallyNonEmpty();
534 inline void FrameView::incrementVisuallyNonEmptyPixelCount(const IntSize& size)
536 if (m_isVisuallyNonEmpty)
538 m_visuallyNonEmptyPixelCount += size.width() * size.height();
539 // Use a threshold value to prevent very small amounts of visible content from triggering didFirstVisuallyNonEmptyLayout
540 static const unsigned visualPixelThreshold = 32 * 32;
541 if (m_visuallyNonEmptyPixelCount > visualPixelThreshold)
542 setIsVisuallyNonEmpty();
545 DEFINE_TYPE_CASTS(FrameView, Widget, widget, widget->isFrameView(), widget.isFrameView());
547 class AllowRepaintScope {
549 explicit AllowRepaintScope(FrameView* view)
551 , m_originalValue(view ? view->canRepaintDuringPerformLayout() : false)
556 m_view->setCanRepaintDuringPerformLayout(true);
564 m_view->setCanRepaintDuringPerformLayout(m_originalValue);
568 bool m_originalValue;
571 } // namespace WebCore
573 #endif // FrameView_h