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 updateAcceleratedCompositingSettings();
118 void recalcOverflowAfterStyleChange();
119 void updateCompositingLayersAfterStyleChange();
121 bool hasCompositedContent() const;
122 bool isEnclosedInCompositingLayer() const;
124 void resetScrollbars();
125 void prepareForDetach();
126 void detachCustomScrollbars();
127 virtual void recalculateScrollbarOverlayStyle();
131 bool isTransparent() const;
132 void setTransparent(bool isTransparent);
134 // True if the FrameView is not transparent, and the base background color is opaque.
135 bool hasOpaqueBackground() const;
137 Color baseBackgroundColor() const;
138 void setBaseBackgroundColor(const Color&);
139 void updateBackgroundRecursively(const Color&, bool);
141 void adjustViewSize();
143 virtual IntRect windowClipRect(IncludeScrollbarsInRect = ExcludeScrollbars) const OVERRIDE;
144 IntRect windowClipRectForFrameOwner(const HTMLFrameOwnerElement*) const;
146 virtual IntRect windowResizerRect() const OVERRIDE;
148 virtual float visibleContentScaleFactor() const OVERRIDE { return m_visibleContentScaleFactor; }
149 void setVisibleContentScaleFactor(float);
151 virtual float inputEventsScaleFactor() const OVERRIDE;
152 virtual IntSize inputEventsOffsetForEmulation() const OVERRIDE;
153 void setInputEventsTransformForEmulation(const IntSize&, float);
155 virtual void setScrollPosition(const IntPoint&) OVERRIDE;
156 virtual bool shouldRubberBandInDirection(ScrollDirection) const OVERRIDE;
157 virtual bool isRubberBandInProgress() const OVERRIDE;
158 void setScrollPositionNonProgrammatically(const IntPoint&);
160 // This is different than visibleContentRect() in that it ignores negative (or overly positive)
161 // offsets from rubber-banding, and it takes zooming into account.
162 LayoutRect viewportConstrainedVisibleContentRect() const;
163 void viewportConstrainedVisibleContentSizeChanged(bool widthChanged, bool heightChanged);
165 AtomicString mediaType() const;
166 void setMediaType(const AtomicString&);
167 void adjustMediaTypeForPrinting(bool printing);
169 void setCannotBlitToWindow();
170 void setIsOverlapped(bool);
171 bool isOverlapped() const { return m_isOverlapped; }
172 void setContentIsOpaque(bool);
174 void addSlowRepaintObject();
175 void removeSlowRepaintObject();
176 bool hasSlowRepaintObjects() const { return m_slowRepaintObjectCount; }
178 // Includes fixed- and sticky-position objects.
179 typedef HashSet<RenderObject*> ViewportConstrainedObjectSet;
180 void addViewportConstrainedObject(RenderObject*);
181 void removeViewportConstrainedObject(RenderObject*);
182 const ViewportConstrainedObjectSet* viewportConstrainedObjects() const { return m_viewportConstrainedObjects.get(); }
183 bool hasViewportConstrainedObjects() const { return m_viewportConstrainedObjects && m_viewportConstrainedObjects->size() > 0; }
185 void handleLoadCompleted();
187 void updateAnnotatedRegions();
188 void updateControlTints();
190 void restoreScrollbar();
192 void postLayoutTimerFired(Timer<FrameView>*);
194 bool wasScrolledByUser() const;
195 void setWasScrolledByUser(bool);
197 bool safeToPropagateScrollToParent() const { return m_safeToPropagateScrollToParent; }
198 void setSafeToPropagateScrollToParent(bool isSafe) { m_safeToPropagateScrollToParent = isSafe; }
200 void addWidget(RenderWidget*);
201 void removeWidget(RenderWidget*);
202 void updateWidgetPositions();
204 void addWidgetToUpdate(RenderEmbeddedObject&);
206 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) OVERRIDE;
207 void setPaintBehavior(PaintBehavior);
208 PaintBehavior paintBehavior() const;
209 bool isPainting() const;
210 bool hasEverPainted() const { return m_lastPaintTime; }
211 void setNodeToDraw(Node*);
213 virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect) OVERRIDE;
214 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect) OVERRIDE;
215 virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&) OVERRIDE;
217 Color documentBackgroundColor() const;
219 static double currentFrameTimeStamp() { return s_currentFrameTimeStamp; }
221 void updateLayoutAndStyleForPainting();
222 void updateLayoutAndStyleIfNeededRecursive();
224 void incrementVisuallyNonEmptyCharacterCount(unsigned);
225 void incrementVisuallyNonEmptyPixelCount(const IntSize&);
226 void setIsVisuallyNonEmpty() { m_isVisuallyNonEmpty = true; }
227 void enableAutoSizeMode(bool enable, const IntSize& minSize, const IntSize& maxSize);
229 void forceLayout(bool allowSubtree = false);
230 void forceLayoutForPagination(const FloatSize& pageSize, const FloatSize& originalPageSize, float maximumShrinkFactor);
232 bool scrollToFragment(const KURL&);
233 bool scrollToAnchor(const String&);
234 void maintainScrollPositionAtAnchor(Node*);
235 void scrollElementToRect(Element*, const IntRect&);
236 void scrollContentsIfNeededRecursive();
238 // Methods to convert points and rects between the coordinate space of the renderer, and this view.
239 IntRect convertFromRenderer(const RenderObject&, const IntRect&) const;
240 IntRect convertToRenderer(const RenderObject&, const IntRect&) const;
241 IntPoint convertFromRenderer(const RenderObject&, const IntPoint&) const;
242 IntPoint convertToRenderer(const RenderObject&, const IntPoint&) const;
244 bool isFrameViewScrollCorner(RenderScrollbarPart* scrollCorner) const { return m_scrollCorner == scrollCorner; }
248 enum ScrollbarModesCalculationStrategy { RulesFromWebContentOnly, AnyRule };
249 void calculateScrollbarModesForLayoutAndSetViewportRenderer(ScrollbarMode& hMode, ScrollbarMode& vMode, ScrollbarModesCalculationStrategy = AnyRule);
251 virtual IntPoint lastKnownMousePosition() const OVERRIDE;
252 bool shouldSetCursor() const;
254 void setCursor(const Cursor&);
256 virtual bool scrollbarsCanBeActive() const OVERRIDE;
258 // FIXME: Remove this method once plugin loading is decoupled from layout.
259 void flushAnyPendingPostLayoutTasks();
261 virtual bool shouldSuspendScrollAnimations() const OVERRIDE;
262 virtual void scrollbarStyleChanged() OVERRIDE;
264 RenderBox* embeddedContentBox() const;
266 void setTracksRepaints(bool);
267 bool isTrackingRepaints() const { return m_isTrackingRepaints; }
268 void resetTrackedRepaints();
269 String trackedRepaintRectsAsText() const;
271 typedef HashSet<ScrollableArea*> ScrollableAreaSet;
272 // Returns whether the scrollable area has just been newly added.
273 bool addScrollableArea(ScrollableArea*);
274 // Returns whether the scrollable area has just been removed.
275 bool removeScrollableArea(ScrollableArea*);
276 const ScrollableAreaSet* scrollableAreas() const { return m_scrollableAreas.get(); }
278 // With CSS style "resize:" enabled, a little resizer handle will appear at the bottom
279 // right of the object. We keep track of these resizer areas for checking if touches
280 // (implemented using Scroll gesture) are targeting the resizer.
281 typedef HashSet<RenderBox*> ResizerAreaSet;
282 void addResizerArea(RenderBox&);
283 void removeResizerArea(RenderBox&);
284 const ResizerAreaSet* resizerAreas() const { return m_resizerAreas.get(); }
286 virtual void removeChild(Widget*) OVERRIDE;
288 // This function exists for ports that need to handle wheel events manually.
289 // On Mac WebKit1 the underlying NSScrollView just does the scrolling, but on most other platforms
290 // we need this function in order to do the scroll ourselves.
291 bool wheelEvent(const PlatformWheelEvent&);
293 bool inProgrammaticScroll() const { return m_inProgrammaticScroll; }
294 void setInProgrammaticScroll(bool programmaticScroll) { m_inProgrammaticScroll = programmaticScroll; }
296 void setHasSoftwareFilters(bool hasSoftwareFilters) { m_hasSoftwareFilters = hasSoftwareFilters; }
297 bool hasSoftwareFilters() const { return m_hasSoftwareFilters; }
299 virtual bool isActive() const OVERRIDE;
301 // DEPRECATED: Use viewportConstrainedVisibleContentRect() instead.
302 IntSize scrollOffsetForFixedPosition() const;
304 virtual bool shouldPlaceVerticalScrollbarOnLeft() const OVERRIDE;
306 // Override scrollbar notifications to update the AXObject cache.
307 virtual void didAddScrollbar(Scrollbar*, ScrollbarOrientation) OVERRIDE;
308 virtual void willRemoveScrollbar(Scrollbar*, ScrollbarOrientation) OVERRIDE;
310 virtual bool shouldAttemptToScrollUsingFastPath() const OVERRIDE;
311 // FIXME: This should probably be renamed as the 'inSubtreeLayout' parameter
312 // passed around the FrameView layout methods can be true while this returns
314 bool isSubtreeLayout() const { return !!m_layoutSubtreeRoot; }
316 // Sets the tickmarks for the FrameView, overriding the default behavior
317 // which is to display the tickmarks corresponding to find results.
318 // If |m_tickmarks| is empty, the default behavior is restored.
319 void setTickmarks(const Vector<IntRect>& tickmarks) { m_tickmarks = tickmarks; }
321 // ScrollableArea interface
322 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE;
323 virtual void getTickmarks(Vector<IntRect>&) const OVERRIDE;
324 virtual void scrollTo(const IntSize&) OVERRIDE;
325 virtual IntRect scrollableAreaBoundingBox() const OVERRIDE;
326 virtual bool scrollAnimatorEnabled() const OVERRIDE;
327 virtual bool usesCompositedScrolling() const OVERRIDE;
328 virtual GraphicsLayer* layerForScrolling() const OVERRIDE;
329 virtual GraphicsLayer* layerForHorizontalScrollbar() const OVERRIDE;
330 virtual GraphicsLayer* layerForVerticalScrollbar() const OVERRIDE;
331 virtual GraphicsLayer* layerForScrollCorner() const OVERRIDE;
334 virtual void scrollContentsIfNeeded();
335 virtual bool scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect) OVERRIDE;
336 virtual void scrollContentsSlowPath(const IntRect& updateRect) OVERRIDE;
338 virtual bool isVerticalDocument() const OVERRIDE;
339 virtual bool isFlippedDocument() const OVERRIDE;
342 explicit FrameView(LocalFrame*);
347 virtual void frameRectsChanged() OVERRIDE;
348 virtual bool isFrameView() const OVERRIDE { return true; }
350 friend class RenderWidget;
351 bool useSlowRepaints(bool considerOverlap = true) const;
352 bool useSlowRepaintsIfNotOverlapped() const;
354 bool contentsInCompositedLayer() const;
356 void applyOverflowToViewportAndSetRenderer(RenderObject*, ScrollbarMode& hMode, ScrollbarMode& vMode);
357 void updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow);
359 void updateCounters();
360 void autoSizeIfEnabled();
361 void forceLayoutParentViewIfNeeded();
362 void performPreLayoutTasks();
363 void performLayout(RenderObject* rootForThisLayout, bool inSubtreeLayout);
364 void scheduleOrPerformPostLayoutTasks();
365 void performPostLayoutTasks();
367 void repaintTree(RenderObject* root);
369 void gatherDebugLayoutRects(RenderObject* layoutRoot);
371 DocumentLifecycle& lifecycle() const;
373 virtual void repaintContentRectangle(const IntRect&) OVERRIDE;
374 virtual void contentsResized() OVERRIDE;
375 virtual void scrollbarExistenceDidChange() OVERRIDE;
377 // Override ScrollView methods to do point conversion via renderers, in order to
378 // take transforms into account.
379 virtual IntRect convertToContainingView(const IntRect&) const OVERRIDE;
380 virtual IntRect convertFromContainingView(const IntRect&) const OVERRIDE;
381 virtual IntPoint convertToContainingView(const IntPoint&) const OVERRIDE;
382 virtual IntPoint convertFromContainingView(const IntPoint&) const OVERRIDE;
384 void sendResizeEventIfNeeded();
386 void updateScrollableAreaSet();
388 virtual void notifyPageThatContentAreaWillPaint() const OVERRIDE;
390 void scheduleUpdateWidgetsIfNecessary();
391 void updateWidgetsTimerFired(Timer<FrameView>*);
392 bool updateWidgets();
394 void scrollToAnchor();
395 void scrollPositionChanged();
396 void didScrollTimerFired(Timer<FrameView>*);
398 void updateLayersAndCompositingAfterScrollIfNeeded();
399 void updateFixedElementRepaintRectsAfterScroll();
401 bool hasCustomScrollbars() const;
402 bool shouldUseCustomScrollbars(Element*& customScrollbarElement, LocalFrame*& customScrollbarFrame);
404 virtual void updateScrollCorner() OVERRIDE;
406 FrameView* parentFrameView() const;
408 AXObjectCache* axObjectCache() const;
409 void removeFromAXObjectCache();
411 void setLayoutSizeInternal(const IntSize&);
413 bool repaintAllowed() const
415 if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
418 return !isInPerformLayout() || canRepaintDuringPerformLayout();
421 static double s_currentFrameTimeStamp; // used for detecting decoded resource thrash in the cache
422 static bool s_inPaintContents;
426 typedef HashSet<RefPtr<RenderEmbeddedObject> > EmbeddedObjectSet;
427 EmbeddedObjectSet m_widgetUpdateSet;
429 // FIXME: These are just "children" of the FrameView and should be RefPtr<Widget> instead.
430 HashSet<RefPtr<RenderWidget> > m_widgets;
432 RefPtr<LocalFrame> m_frame;
434 bool m_doFullRepaint;
436 bool m_canHaveScrollbars;
437 bool m_cannotBlitToWindow;
439 bool m_contentIsOpaque;
440 unsigned m_slowRepaintObjectCount;
442 bool m_hasPendingLayout;
443 RenderObject* m_layoutSubtreeRoot;
445 bool m_layoutSchedulingEnabled;
446 bool m_inPerformLayout;
447 bool m_canRepaintDuringPerformLayout;
448 bool m_inSynchronousPostLayout;
450 unsigned m_nestedLayoutCount;
451 Timer<FrameView> m_postLayoutTasksTimer;
452 Timer<FrameView> m_updateWidgetsTimer;
453 bool m_firstLayoutCallbackPending;
456 bool m_isTransparent;
457 Color m_baseBackgroundColor;
458 IntSize m_lastViewportSize;
459 float m_lastZoomFactor;
461 AtomicString m_mediaType;
462 AtomicString m_mediaTypeWhenNotPrinting;
464 bool m_overflowStatusDirty;
465 bool m_horizontalOverflow;
466 bool m_verticalOverflow;
467 RenderObject* m_viewportRenderer;
469 bool m_wasScrolledByUser;
470 bool m_inProgrammaticScroll;
471 bool m_safeToPropagateScrollToParent;
473 double m_lastPaintTime;
475 bool m_isTrackingRepaints; // Used for testing.
476 Vector<IntRect> m_trackedRepaintRects;
478 RefPtr<Node> m_nodeToDraw;
479 PaintBehavior m_paintBehavior;
482 unsigned m_visuallyNonEmptyCharacterCount;
483 unsigned m_visuallyNonEmptyPixelCount;
484 bool m_isVisuallyNonEmpty;
485 bool m_firstVisuallyNonEmptyLayoutCallbackPending;
487 RefPtr<Node> m_maintainScrollPositionAnchor;
489 // Renderer to hold our custom scroll corner.
490 RenderScrollbarPart* m_scrollCorner;
492 // If true, automatically resize the frame view around its content.
493 bool m_shouldAutoSize;
495 // True if autosize has been run since m_shouldAutoSize was set.
496 bool m_didRunAutosize;
497 // The lower bound on the size when autosizing.
498 IntSize m_minAutoSize;
499 // The upper bound on the size when autosizing.
500 IntSize m_maxAutoSize;
502 OwnPtr<ScrollableAreaSet> m_scrollableAreas;
503 OwnPtr<ResizerAreaSet> m_resizerAreas;
504 OwnPtr<ViewportConstrainedObjectSet> m_viewportConstrainedObjects;
506 bool m_hasSoftwareFilters;
508 float m_visibleContentScaleFactor;
509 IntSize m_inputEventsOffsetForEmulation;
510 float m_inputEventsScaleFactorForEmulation;
512 IntSize m_layoutSize;
513 bool m_layoutSizeFixedToFrameSize;
515 Timer<FrameView> m_didScrollTimer;
517 Vector<IntRect> m_tickmarks;
520 inline void FrameView::incrementVisuallyNonEmptyCharacterCount(unsigned count)
522 if (m_isVisuallyNonEmpty)
524 m_visuallyNonEmptyCharacterCount += count;
525 // Use a threshold value to prevent very small amounts of visible content from triggering didFirstVisuallyNonEmptyLayout.
526 // The first few hundred characters rarely contain the interesting content of the page.
527 static const unsigned visualCharacterThreshold = 200;
528 if (m_visuallyNonEmptyCharacterCount > visualCharacterThreshold)
529 setIsVisuallyNonEmpty();
532 inline void FrameView::incrementVisuallyNonEmptyPixelCount(const IntSize& size)
534 if (m_isVisuallyNonEmpty)
536 m_visuallyNonEmptyPixelCount += size.width() * size.height();
537 // Use a threshold value to prevent very small amounts of visible content from triggering didFirstVisuallyNonEmptyLayout
538 static const unsigned visualPixelThreshold = 32 * 32;
539 if (m_visuallyNonEmptyPixelCount > visualPixelThreshold)
540 setIsVisuallyNonEmpty();
543 DEFINE_TYPE_CASTS(FrameView, Widget, widget, widget->isFrameView(), widget.isFrameView());
545 class AllowRepaintScope {
547 explicit AllowRepaintScope(FrameView* view)
549 , m_originalValue(view ? view->canRepaintDuringPerformLayout() : false)
554 m_view->setCanRepaintDuringPerformLayout(true);
562 m_view->setCanRepaintDuringPerformLayout(m_originalValue);
566 bool m_originalValue;
569 } // namespace WebCore
571 #endif // FrameView_h