initial import
[vuplus_webkit] / Source / WebCore / rendering / RenderView.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2006 Apple Computer, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #ifndef RenderView_h
23 #define RenderView_h
24
25 #include "FrameView.h"
26 #include "LayoutState.h"
27 #include "RenderBlock.h"
28 #include <wtf/ListHashSet.h>
29 #include <wtf/OwnPtr.h>
30
31 namespace WebCore {
32
33 class RenderFlowThread;
34 class RenderWidget;
35
36 #if USE(ACCELERATED_COMPOSITING)
37 class RenderLayerCompositor;
38 #endif
39
40 typedef ListHashSet<RenderFlowThread*> RenderFlowThreadList;
41
42 class RenderView : public RenderBlock {
43 public:
44     RenderView(Node*, FrameView*);
45     virtual ~RenderView();
46
47     virtual const char* renderName() const { return "RenderView"; }
48
49     virtual bool isRenderView() const { return true; }
50
51     virtual bool requiresLayer() const { return true; }
52
53     virtual bool isChildAllowed(RenderObject*, RenderStyle*) const;
54
55     virtual void layout();
56     virtual void computeLogicalWidth();
57     virtual void computeLogicalHeight();
58     virtual void computePreferredLogicalWidths();
59
60     // The same as the FrameView's layoutHeight/layoutWidth but with null check guards.
61     int viewHeight() const;
62     int viewWidth() const;
63     int viewLogicalWidth() const { return style()->isHorizontalWritingMode() ? viewWidth() : viewHeight(); }
64     int viewLogicalHeight() const { return style()->isHorizontalWritingMode() ? viewHeight() : viewWidth(); }
65
66     float zoomFactor() const;
67
68     FrameView* frameView() const { return m_frameView; }
69
70     virtual void computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect&, bool fixed = false) const;
71     virtual void repaintViewRectangle(const IntRect&, bool immediate = false);
72     // Repaint the view, and all composited layers that intersect the given absolute rectangle.
73     // FIXME: ideally we'd never have to do this, if all repaints are container-relative.
74     virtual void repaintRectangleInViewAndCompositedLayers(const IntRect&, bool immediate = false);
75
76     virtual void paint(PaintInfo&, const LayoutPoint&);
77     virtual void paintBoxDecorations(PaintInfo&, const IntPoint&);
78
79     enum SelectionRepaintMode { RepaintNewXOROld, RepaintNewMinusOld };
80     void setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode = RepaintNewXOROld);
81     void clearSelection();
82     RenderObject* selectionStart() const { return m_selectionStart; }
83     RenderObject* selectionEnd() const { return m_selectionEnd; }
84     IntRect selectionBounds(bool clipToVisibleContent = true) const;
85     void selectionStartEnd(int& startPos, int& endPos) const;
86
87     bool printing() const;
88
89     virtual void absoluteRects(Vector<LayoutRect>&, const LayoutPoint& accumulatedOffset);
90     virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed);
91
92 #if USE(ACCELERATED_COMPOSITING)
93     void setMaximalOutlineSize(int o);
94 #else
95     void setMaximalOutlineSize(int o) { m_maximalOutlineSize = o; }
96 #endif
97     int maximalOutlineSize() const { return m_maximalOutlineSize; }
98
99     virtual IntRect viewRect() const;
100
101     void updateWidgetPositions();
102     void addWidget(RenderWidget*);
103     void removeWidget(RenderWidget*);
104     
105     void notifyWidgets(WidgetNotification);
106
107     // layoutDelta is used transiently during layout to store how far an object has moved from its
108     // last layout location, in order to repaint correctly.
109     // If we're doing a full repaint m_layoutState will be 0, but in that case layoutDelta doesn't matter.
110     LayoutSize layoutDelta() const
111     {
112         return m_layoutState ? m_layoutState->m_layoutDelta : LayoutSize();
113     }
114     void addLayoutDelta(const LayoutSize& delta) 
115     {
116         if (m_layoutState)
117             m_layoutState->m_layoutDelta += delta;
118     }
119
120     bool doingFullRepaint() const { return m_frameView->needsFullRepaint(); }
121
122     // Subtree push/pop
123     void pushLayoutState(RenderObject*);
124     void popLayoutState(RenderObject*) { return popLayoutState(); } // Just doing this to keep popLayoutState() private and to make the subtree calls symmetrical.
125
126     bool shouldDisableLayoutStateForSubtree(RenderObject*) const;
127
128     // Returns true if layoutState should be used for its cached offset and clip.
129     bool layoutStateEnabled() const { return m_layoutStateDisableCount == 0 && m_layoutState; }
130     LayoutState* layoutState() const { return m_layoutState; }
131
132     virtual void updateHitTestResult(HitTestResult&, const LayoutPoint&);
133
134     unsigned pageLogicalHeight() const { return m_pageLogicalHeight; }
135     void setPageLogicalHeight(unsigned height)
136     {
137         if (m_pageLogicalHeight != height) {
138             m_pageLogicalHeight = height;
139             m_pageLogicalHeightChanged = true;
140         }
141     }
142
143     // FIXME: These functions are deprecated. No code should be added that uses these.
144     int bestTruncatedAt() const { return m_legacyPrinting.m_bestTruncatedAt; }
145     void setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak = false);
146     int truncatedAt() const { return m_legacyPrinting.m_truncatedAt; }
147     void setTruncatedAt(int y)
148     { 
149         m_legacyPrinting.m_truncatedAt = y;
150         m_legacyPrinting.m_bestTruncatedAt = 0;
151         m_legacyPrinting.m_truncatorWidth = 0;
152         m_legacyPrinting.m_forcedPageBreak = false;
153     }
154     const IntRect& printRect() const { return m_legacyPrinting.m_printRect; }
155     void setPrintRect(const IntRect& r) { m_legacyPrinting.m_printRect = r; }
156     // End deprecated functions.
157
158     // Notifications that this view became visible in a window, or will be
159     // removed from the window.
160     void didMoveOnscreen();
161     void willMoveOffscreen();
162
163 #if USE(ACCELERATED_COMPOSITING)
164     RenderLayerCompositor* compositor();
165     bool usesCompositing() const;
166 #endif
167
168     IntRect unscaledDocumentRect() const;
169
170     IntRect documentRect() const;
171
172     RenderFlowThread* renderFlowThreadWithName(const AtomicString& flowThread);
173     bool hasRenderFlowThreads() const { return m_renderFlowThreadList && !m_renderFlowThreadList->isEmpty(); }
174     void layoutRenderFlowThreads();
175     bool isRenderFlowThreadOrderDirty() const { return m_isRenderFlowThreadOrderDirty; }
176     void setIsRenderFlowThreadOrderDirty(bool dirty)
177     {
178         m_isRenderFlowThreadOrderDirty = dirty;
179         if (dirty)
180             setNeedsLayout(true);
181     }
182     const RenderFlowThreadList* renderFlowThreadList() const { return m_renderFlowThreadList.get(); }
183
184     bool hasRenderFlowThread() const { return m_currentRenderFlowThread; }
185     RenderFlowThread* currentRenderFlowThread() const { return m_currentRenderFlowThread; }
186     void setCurrentRenderFlowThread(RenderFlowThread* flowThread) { m_currentRenderFlowThread = flowThread; }
187
188     void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
189     
190 protected:
191     virtual void mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool useTransforms, bool fixed, TransformState&, bool* wasFixed = 0) const;
192     virtual void mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState&) const;
193
194 private:
195     bool shouldRepaint(const IntRect& r) const;
196     
197     virtual RenderBlock* containingBlock() const;
198
199     // These functions may only be accessed by LayoutStateMaintainer.
200     bool pushLayoutState(RenderBox* renderer, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
201     {
202         // We push LayoutState even if layoutState is disabled because it stores layoutDelta too.
203         if (!doingFullRepaint() || renderer->hasColumns() || m_layoutState->isPaginated() || hasRenderFlowThread()) {
204             m_layoutState = new (renderArena()) LayoutState(m_layoutState, renderer, offset, pageHeight, pageHeightChanged, colInfo);
205             return true;
206         }
207         return false;
208     }
209
210     void popLayoutState()
211     {
212         LayoutState* state = m_layoutState;
213         m_layoutState = state->m_next;
214         state->destroy(renderArena());
215     }
216
217     // Suspends the LayoutState optimization. Used under transforms that cannot be represented by
218     // LayoutState (common in SVG) and when manipulating the render tree during layout in ways
219     // that can trigger repaint of a non-child (e.g. when a list item moves its list marker around).
220     // Note that even when disabled, LayoutState is still used to store layoutDelta.
221     // These functions may only be accessed by LayoutStateMaintainer or LayoutStateDisabler.
222     void disableLayoutState() { m_layoutStateDisableCount++; }
223     void enableLayoutState() { ASSERT(m_layoutStateDisableCount > 0); m_layoutStateDisableCount--; }
224
225     size_t getRetainedWidgets(Vector<RenderWidget*>&);
226     void releaseWidgets(Vector<RenderWidget*>&);
227     
228     friend class LayoutStateMaintainer;
229     friend class LayoutStateDisabler;
230         
231 protected:
232     FrameView* m_frameView;
233
234     RenderObject* m_selectionStart;
235     RenderObject* m_selectionEnd;
236     int m_selectionStartPos;
237     int m_selectionEndPos;
238
239     // FIXME: Only used by embedded WebViews inside AppKit NSViews.  Find a way to remove.
240     struct LegacyPrinting {
241         LegacyPrinting()
242             : m_bestTruncatedAt(0)
243             , m_truncatedAt(0)
244             , m_truncatorWidth(0)
245             , m_forcedPageBreak(false)
246         { }
247
248         int m_bestTruncatedAt;
249         int m_truncatedAt;
250         int m_truncatorWidth;
251         IntRect m_printRect;
252         bool m_forcedPageBreak;
253     };
254     LegacyPrinting m_legacyPrinting;
255     // End deprecated members.
256
257     int m_maximalOutlineSize; // Used to apply a fudge factor to dirty-rect checks on blocks/tables.
258
259     typedef HashSet<RenderWidget*> RenderWidgetSet;
260     RenderWidgetSet m_widgets;
261     
262 private:
263     unsigned m_pageLogicalHeight;
264     bool m_pageLogicalHeightChanged;
265     bool m_isRenderFlowThreadOrderDirty;
266     LayoutState* m_layoutState;
267     unsigned m_layoutStateDisableCount;
268 #if USE(ACCELERATED_COMPOSITING)
269     OwnPtr<RenderLayerCompositor> m_compositor;
270 #endif
271     OwnPtr<RenderFlowThreadList> m_renderFlowThreadList;
272     RenderFlowThread* m_currentRenderFlowThread;
273 };
274
275 inline RenderView* toRenderView(RenderObject* object)
276 {
277     ASSERT(!object || object->isRenderView());
278     return static_cast<RenderView*>(object);
279 }
280
281 inline const RenderView* toRenderView(const RenderObject* object)
282 {
283     ASSERT(!object || object->isRenderView());
284     return static_cast<const RenderView*>(object);
285 }
286
287 // This will catch anyone doing an unnecessary cast.
288 void toRenderView(const RenderView*);
289
290
291 // Stack-based class to assist with LayoutState push/pop
292 class LayoutStateMaintainer {
293     WTF_MAKE_NONCOPYABLE(LayoutStateMaintainer);
294 public:
295     // ctor to push now
296     LayoutStateMaintainer(RenderView* view, RenderBox* root, LayoutSize offset, bool disableState = false, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
297         : m_view(view)
298         , m_disabled(disableState)
299         , m_didStart(false)
300         , m_didEnd(false)
301         , m_didCreateLayoutState(false)
302     {
303         push(root, offset, pageHeight, pageHeightChanged, colInfo);
304     }
305     
306     // ctor to maybe push later
307     LayoutStateMaintainer(RenderView* view)
308         : m_view(view)
309         , m_disabled(false)
310         , m_didStart(false)
311         , m_didEnd(false)
312         , m_didCreateLayoutState(false)
313     {
314     }
315     
316     ~LayoutStateMaintainer()
317     {
318         ASSERT(m_didStart == m_didEnd);   // if this fires, it means that someone did a push(), but forgot to pop().
319     }
320
321     void push(RenderBox* root, LayoutSize offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
322     {
323         ASSERT(!m_didStart);
324         // We push state even if disabled, because we still need to store layoutDelta
325         m_didCreateLayoutState = m_view->pushLayoutState(root, offset, pageHeight, pageHeightChanged, colInfo);
326         if (m_disabled && m_didCreateLayoutState)
327             m_view->disableLayoutState();
328         m_didStart = true;
329     }
330
331     void pop()
332     {
333         if (m_didStart) {
334             ASSERT(!m_didEnd);
335             if (m_didCreateLayoutState) {
336                 m_view->popLayoutState();
337                 if (m_disabled)
338                     m_view->enableLayoutState();
339             }
340             
341             m_didEnd = true;
342         }
343     }
344
345     bool didPush() const { return m_didStart; }
346
347 private:
348     RenderView* m_view;
349     bool m_disabled : 1;        // true if the offset and clip part of layoutState is disabled
350     bool m_didStart : 1;        // true if we did a push or disable
351     bool m_didEnd : 1;          // true if we popped or re-enabled
352     bool m_didCreateLayoutState : 1; // true if we actually made a layout state.
353 };
354
355 class LayoutStateDisabler {
356     WTF_MAKE_NONCOPYABLE(LayoutStateDisabler);
357 public:
358     LayoutStateDisabler(RenderView* view)
359         : m_view(view)
360     {
361         if (m_view)
362             m_view->disableLayoutState();
363     }
364
365     ~LayoutStateDisabler()
366     {
367         if (m_view)
368             m_view->enableLayoutState();
369     }
370 private:
371     RenderView* m_view;
372 };
373
374 } // namespace WebCore
375
376 #endif // RenderView_h