1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef RenderBox_h
24#define RenderBox_h
25
26#include "core/animation/ActiveAnimations.h"
27#include "core/rendering/RenderBoxModelObject.h"
28#include "core/rendering/RenderOverflow.h"
29#include "core/rendering/shapes/ShapeOutsideInfo.h"
30#include "platform/scroll/ScrollTypes.h"
31
32namespace WebCore {
33
34struct PaintInfo;
35
36enum SizeType { MainOrPreferredSize, MinSize, MaxSize };
37enum AvailableLogicalHeightType { ExcludeMarginBorderPadding, IncludeMarginBorderPadding };
38enum OverlayScrollbarSizeRelevancy { IgnoreOverlayScrollbarSize, IncludeOverlayScrollbarSize };
39enum MarginDirection { BlockDirection, InlineDirection };
40
41enum ShouldComputePreferred { ComputeActual, ComputePreferred };
42
43enum ContentsClipBehavior { ForceContentsClip, SkipContentsClipIfPossible };
44
45enum ScrollOffsetClamping {
46    ScrollOffsetUnclamped,
47    ScrollOffsetClamped
48};
49
50struct RenderBoxRareData {
51    WTF_MAKE_NONCOPYABLE(RenderBoxRareData); WTF_MAKE_FAST_ALLOCATED;
52public:
53    RenderBoxRareData()
54        : m_inlineBoxWrapper(0)
55        , m_overrideLogicalContentHeight(-1)
56        , m_overrideLogicalContentWidth(-1)
57    {
58    }
59
60    // For inline replaced elements, the inline box that owns us.
61    InlineBox* m_inlineBoxWrapper;
62
63    LayoutUnit m_overrideLogicalContentHeight;
64    LayoutUnit m_overrideLogicalContentWidth;
65};
66
67
68class RenderBox : public RenderBoxModelObject {
69public:
70    explicit RenderBox(ContainerNode*);
71
72    // hasAutoZIndex only returns true if the element is positioned or a flex-item since
73    // position:static elements that are not flex-items get their z-index coerced to auto.
74    virtual LayerType layerTypeRequired() const OVERRIDE
75    {
76        if (isPositioned() || createsGroup() || hasClipPath() || hasTransform() || hasHiddenBackface() || hasReflection() || style()->specifiesColumns() || !style()->hasAutoZIndex() || style()->shouldCompositeForCurrentAnimations())
77            return NormalLayer;
78        if (hasOverflowClip())
79            return OverflowClipLayer;
80
81        return NoLayer;
82    }
83
84    virtual bool backgroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect) const OVERRIDE;
85
86    // Use this with caution! No type checking is done!
87    RenderBox* firstChildBox() const;
88    RenderBox* lastChildBox() const;
89
90    LayoutUnit x() const { return m_frameRect.x(); }
91    LayoutUnit y() const { return m_frameRect.y(); }
92    LayoutUnit width() const { return m_frameRect.width(); }
93    LayoutUnit height() const { return m_frameRect.height(); }
94
95    int pixelSnappedWidth() const { return m_frameRect.pixelSnappedWidth(); }
96    int pixelSnappedHeight() const { return m_frameRect.pixelSnappedHeight(); }
97
98    // These represent your location relative to your container as a physical offset.
99    // In layout related methods you almost always want the logical location (e.g. x() and y()).
100    LayoutUnit top() const { return topLeftLocation().y(); }
101    LayoutUnit left() const { return topLeftLocation().x(); }
102
103    void setX(LayoutUnit x) { m_frameRect.setX(x); }
104    void setY(LayoutUnit y) { m_frameRect.setY(y); }
105    void setWidth(LayoutUnit width) { m_frameRect.setWidth(width); }
106    void setHeight(LayoutUnit height) { m_frameRect.setHeight(height); }
107
108    LayoutUnit logicalLeft() const { return style()->isHorizontalWritingMode() ? x() : y(); }
109    LayoutUnit logicalRight() const { return logicalLeft() + logicalWidth(); }
110    LayoutUnit logicalTop() const { return style()->isHorizontalWritingMode() ? y() : x(); }
111    LayoutUnit logicalBottom() const { return logicalTop() + logicalHeight(); }
112    LayoutUnit logicalWidth() const { return style()->isHorizontalWritingMode() ? width() : height(); }
113    LayoutUnit logicalHeight() const { return style()->isHorizontalWritingMode() ? height() : width(); }
114
115    LayoutUnit constrainLogicalWidthByMinMax(LayoutUnit, LayoutUnit, RenderBlock*) const;
116    LayoutUnit constrainLogicalHeightByMinMax(LayoutUnit logicalHeight, LayoutUnit intrinsicContentHeight) const;
117    LayoutUnit constrainContentBoxLogicalHeightByMinMax(LayoutUnit logicalHeight, LayoutUnit intrinsicContentHeight) const;
118
119    int pixelSnappedLogicalHeight() const { return style()->isHorizontalWritingMode() ? pixelSnappedHeight() : pixelSnappedWidth(); }
120    int pixelSnappedLogicalWidth() const { return style()->isHorizontalWritingMode() ? pixelSnappedWidth() : pixelSnappedHeight(); }
121
122    void setLogicalLeft(LayoutUnit left)
123    {
124        if (style()->isHorizontalWritingMode())
125            setX(left);
126        else
127            setY(left);
128    }
129    void setLogicalTop(LayoutUnit top)
130    {
131        if (style()->isHorizontalWritingMode())
132            setY(top);
133        else
134            setX(top);
135    }
136    void setLogicalLocation(const LayoutPoint& location)
137    {
138        if (style()->isHorizontalWritingMode())
139            setLocation(location);
140        else
141            setLocation(location.transposedPoint());
142    }
143    void setLogicalWidth(LayoutUnit size)
144    {
145        if (style()->isHorizontalWritingMode())
146            setWidth(size);
147        else
148            setHeight(size);
149    }
150    void setLogicalHeight(LayoutUnit size)
151    {
152        if (style()->isHorizontalWritingMode())
153            setHeight(size);
154        else
155            setWidth(size);
156    }
157    void setLogicalSize(const LayoutSize& size)
158    {
159        if (style()->isHorizontalWritingMode())
160            setSize(size);
161        else
162            setSize(size.transposedSize());
163    }
164
165    LayoutPoint location() const { return m_frameRect.location(); }
166    LayoutSize locationOffset() const { return LayoutSize(x(), y()); }
167    LayoutSize size() const { return m_frameRect.size(); }
168    IntSize pixelSnappedSize() const { return m_frameRect.pixelSnappedSize(); }
169
170    void setLocation(const LayoutPoint& location) { m_frameRect.setLocation(location); }
171
172    void setSize(const LayoutSize& size) { m_frameRect.setSize(size); }
173    void move(LayoutUnit dx, LayoutUnit dy) { m_frameRect.move(dx, dy); }
174
175    LayoutRect frameRect() const { return m_frameRect; }
176    IntRect pixelSnappedFrameRect() const { return pixelSnappedIntRect(m_frameRect); }
177    void setFrameRect(const LayoutRect& rect) { m_frameRect = rect; }
178
179    LayoutRect borderBoxRect() const { return LayoutRect(LayoutPoint(), size()); }
180    LayoutRect paddingBoxRect() const { return LayoutRect(borderLeft(), borderTop(), contentWidth() + paddingLeft() + paddingRight(), contentHeight() + paddingTop() + paddingBottom()); }
181    IntRect pixelSnappedBorderBoxRect() const { return IntRect(IntPoint(), m_frameRect.pixelSnappedSize()); }
182    virtual IntRect borderBoundingBox() const OVERRIDE FINAL { return pixelSnappedBorderBoxRect(); }
183
184    // The content area of the box (excludes padding - and intrinsic padding for table cells, etc... - and border).
185    LayoutRect contentBoxRect() const { return LayoutRect(borderLeft() + paddingLeft(), borderTop() + paddingTop(), contentWidth(), contentHeight()); }
186    // The content box in absolute coords. Ignores transforms.
187    IntRect absoluteContentBox() const;
188    // The content box converted to absolute coords (taking transforms into account).
189    FloatQuad absoluteContentQuad() const;
190
191    // This returns the content area of the box (excluding padding and border). The only difference with contentBoxRect is that computedCSSContentBoxRect
192    // does include the intrinsic padding in the content box as this is what some callers expect (like getComputedStyle).
193    LayoutRect computedCSSContentBoxRect() const { return LayoutRect(borderLeft() + computedCSSPaddingLeft(), borderTop() + computedCSSPaddingTop(), clientWidth() - computedCSSPaddingLeft() - computedCSSPaddingRight(), clientHeight() - computedCSSPaddingTop() - computedCSSPaddingBottom()); }
194
195    virtual void addFocusRingRects(Vector<IntRect>&, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer = 0) OVERRIDE;
196
197    // Use this with caution! No type checking is done!
198    RenderBox* previousSiblingBox() const;
199    RenderBox* nextSiblingBox() const;
200    RenderBox* parentBox() const;
201
202    bool canResize() const;
203
204    // Visual and layout overflow are in the coordinate space of the box.  This means that they aren't purely physical directions.
205    // For horizontal-tb and vertical-lr they will match physical directions, but for horizontal-bt and vertical-rl, the top/bottom and left/right
206    // respectively are flipped when compared to their physical counterparts.  For example minX is on the left in vertical-lr,
207    // but it is on the right in vertical-rl.
208    LayoutRect noOverflowRect() const;
209    LayoutRect layoutOverflowRect() const { return m_overflow ? m_overflow->layoutOverflowRect() : noOverflowRect(); }
210    IntRect pixelSnappedLayoutOverflowRect() const { return pixelSnappedIntRect(layoutOverflowRect()); }
211    LayoutSize maxLayoutOverflow() const { return LayoutSize(layoutOverflowRect().maxX(), layoutOverflowRect().maxY()); }
212    LayoutUnit logicalLeftLayoutOverflow() const { return style()->isHorizontalWritingMode() ? layoutOverflowRect().x() : layoutOverflowRect().y(); }
213    LayoutUnit logicalRightLayoutOverflow() const { return style()->isHorizontalWritingMode() ? layoutOverflowRect().maxX() : layoutOverflowRect().maxY(); }
214
215    virtual LayoutRect visualOverflowRect() const { return m_overflow ? m_overflow->visualOverflowRect() : borderBoxRect(); }
216    LayoutUnit logicalLeftVisualOverflow() const { return style()->isHorizontalWritingMode() ? visualOverflowRect().x() : visualOverflowRect().y(); }
217    LayoutUnit logicalRightVisualOverflow() const { return style()->isHorizontalWritingMode() ? visualOverflowRect().maxX() : visualOverflowRect().maxY(); }
218
219    LayoutRect overflowRectForPaintRejection() const;
220
221    LayoutRect contentsVisualOverflowRect() const { return m_overflow ? m_overflow->contentsVisualOverflowRect() : LayoutRect(); }
222
223    void addLayoutOverflow(const LayoutRect&);
224    void addVisualOverflow(const LayoutRect&);
225
226    // Clipped by the contents clip, if one exists.
227    void addContentsVisualOverflow(const LayoutRect&);
228
229    void addVisualEffectOverflow();
230    void addOverflowFromChild(RenderBox* child) { addOverflowFromChild(child, child->locationOffset()); }
231    void addOverflowFromChild(RenderBox* child, const LayoutSize& delta);
232    void clearLayoutOverflow();
233    void clearAllOverflows() { m_overflow.clear(); }
234
235    void updateLayerTransformAfterLayout();
236
237    LayoutUnit contentWidth() const { return clientWidth() - paddingLeft() - paddingRight(); }
238    LayoutUnit contentHeight() const { return clientHeight() - paddingTop() - paddingBottom(); }
239    LayoutUnit contentLogicalWidth() const { return style()->isHorizontalWritingMode() ? contentWidth() : contentHeight(); }
240    LayoutUnit contentLogicalHeight() const { return style()->isHorizontalWritingMode() ? contentHeight() : contentWidth(); }
241
242    // IE extensions. Used to calculate offsetWidth/Height.  Overridden by inlines (RenderFlow)
243    // to return the remaining width on a given line (and the height of a single line).
244    virtual LayoutUnit offsetWidth() const OVERRIDE { return width(); }
245    virtual LayoutUnit offsetHeight() const OVERRIDE { return height(); }
246
247    virtual int pixelSnappedOffsetWidth() const OVERRIDE FINAL;
248    virtual int pixelSnappedOffsetHeight() const OVERRIDE FINAL;
249
250    // More IE extensions.  clientWidth and clientHeight represent the interior of an object
251    // excluding border and scrollbar.  clientLeft/Top are just the borderLeftWidth and borderTopWidth.
252    LayoutUnit clientLeft() const { return borderLeft() + (style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft() ? verticalScrollbarWidth() : 0); }
253    LayoutUnit clientTop() const { return borderTop(); }
254    LayoutUnit clientWidth() const;
255    LayoutUnit clientHeight() const;
256    LayoutUnit clientLogicalWidth() const { return style()->isHorizontalWritingMode() ? clientWidth() : clientHeight(); }
257    LayoutUnit clientLogicalHeight() const { return style()->isHorizontalWritingMode() ? clientHeight() : clientWidth(); }
258    LayoutUnit clientLogicalBottom() const { return borderBefore() + clientLogicalHeight(); }
259    LayoutRect clientBoxRect() const { return LayoutRect(clientLeft(), clientTop(), clientWidth(), clientHeight()); }
260
261    int pixelSnappedClientWidth() const;
262    int pixelSnappedClientHeight() const;
263
264    // scrollWidth/scrollHeight will be the same as clientWidth/clientHeight unless the
265    // object has overflow:hidden/scroll/auto specified and also has overflow.
266    // scrollLeft/Top return the current scroll position.  These methods are virtual so that objects like
267    // textareas can scroll shadow content (but pretend that they are the objects that are
268    // scrolling).
269    virtual LayoutUnit scrollLeft() const;
270    virtual LayoutUnit scrollTop() const;
271    virtual LayoutUnit scrollWidth() const;
272    virtual LayoutUnit scrollHeight() const;
273    int pixelSnappedScrollWidth() const;
274    int pixelSnappedScrollHeight() const;
275    virtual void setScrollLeft(LayoutUnit);
276    virtual void setScrollTop(LayoutUnit);
277
278    void scrollToOffset(const IntSize&);
279    void scrollByRecursively(const IntSize& delta, ScrollOffsetClamping = ScrollOffsetUnclamped);
280    void scrollRectToVisible(const LayoutRect&, const ScrollAlignment& alignX, const ScrollAlignment& alignY);
281
282    virtual LayoutUnit marginTop() const OVERRIDE { return m_marginBox.top(); }
283    virtual LayoutUnit marginBottom() const OVERRIDE { return m_marginBox.bottom(); }
284    virtual LayoutUnit marginLeft() const OVERRIDE { return m_marginBox.left(); }
285    virtual LayoutUnit marginRight() const OVERRIDE { return m_marginBox.right(); }
286    void setMarginTop(LayoutUnit margin) { m_marginBox.setTop(margin); }
287    void setMarginBottom(LayoutUnit margin) { m_marginBox.setBottom(margin); }
288    void setMarginLeft(LayoutUnit margin) { m_marginBox.setLeft(margin); }
289    void setMarginRight(LayoutUnit margin) { m_marginBox.setRight(margin); }
290
291    LayoutUnit marginLogicalLeft() const { return m_marginBox.logicalLeft(style()->writingMode()); }
292    LayoutUnit marginLogicalRight() const { return m_marginBox.logicalRight(style()->writingMode()); }
293
294    virtual LayoutUnit marginBefore(const RenderStyle* overrideStyle = 0) const OVERRIDE FINAL { return m_marginBox.before((overrideStyle ? overrideStyle : style())->writingMode()); }
295    virtual LayoutUnit marginAfter(const RenderStyle* overrideStyle = 0) const OVERRIDE FINAL { return m_marginBox.after((overrideStyle ? overrideStyle : style())->writingMode()); }
296    virtual LayoutUnit marginStart(const RenderStyle* overrideStyle = 0) const OVERRIDE FINAL
297    {
298        const RenderStyle* styleToUse = overrideStyle ? overrideStyle : style();
299        return m_marginBox.start(styleToUse->writingMode(), styleToUse->direction());
300    }
301    virtual LayoutUnit marginEnd(const RenderStyle* overrideStyle = 0) const OVERRIDE FINAL
302    {
303        const RenderStyle* styleToUse = overrideStyle ? overrideStyle : style();
304        return m_marginBox.end(styleToUse->writingMode(), styleToUse->direction());
305    }
306    void setMarginBefore(LayoutUnit value, const RenderStyle* overrideStyle = 0) { m_marginBox.setBefore((overrideStyle ? overrideStyle : style())->writingMode(), value); }
307    void setMarginAfter(LayoutUnit value, const RenderStyle* overrideStyle = 0) { m_marginBox.setAfter((overrideStyle ? overrideStyle : style())->writingMode(), value); }
308    void setMarginStart(LayoutUnit value, const RenderStyle* overrideStyle = 0)
309    {
310        const RenderStyle* styleToUse = overrideStyle ? overrideStyle : style();
311        m_marginBox.setStart(styleToUse->writingMode(), styleToUse->direction(), value);
312    }
313    void setMarginEnd(LayoutUnit value, const RenderStyle* overrideStyle = 0)
314    {
315        const RenderStyle* styleToUse = overrideStyle ? overrideStyle : style();
316        m_marginBox.setEnd(styleToUse->writingMode(), styleToUse->direction(), value);
317    }
318
319    // The following five functions are used to implement collapsing margins.
320    // All objects know their maximal positive and negative margins.  The
321    // formula for computing a collapsed margin is |maxPosMargin| - |maxNegmargin|.
322    // For a non-collapsing box, such as a leaf element, this formula will simply return
323    // the margin of the element.  Blocks override the maxMarginBefore and maxMarginAfter
324    // methods.
325    enum MarginSign { PositiveMargin, NegativeMargin };
326    virtual bool isSelfCollapsingBlock() const { return false; }
327    virtual LayoutUnit collapsedMarginBefore() const { return marginBefore(); }
328    virtual LayoutUnit collapsedMarginAfter() const { return marginAfter(); }
329
330    virtual void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const OVERRIDE;
331    virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const OVERRIDE;
332
333    int reflectionOffset() const;
334    // Given a rect in the object's coordinate space, returns the corresponding rect in the reflection.
335    LayoutRect reflectedRect(const LayoutRect&) const;
336
337    virtual void layout() OVERRIDE;
338    virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE;
339    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE;
340
341    virtual LayoutUnit minPreferredLogicalWidth() const OVERRIDE;
342    virtual LayoutUnit maxPreferredLogicalWidth() const OVERRIDE;
343
344    // FIXME: We should rename these back to overrideLogicalHeight/Width and have them store
345    // the border-box height/width like the regular height/width accessors on RenderBox.
346    // Right now, these are different than contentHeight/contentWidth because they still
347    // include the scrollbar height/width.
348    LayoutUnit overrideLogicalContentWidth() const;
349    LayoutUnit overrideLogicalContentHeight() const;
350    bool hasOverrideHeight() const;
351    bool hasOverrideWidth() const;
352    void setOverrideLogicalContentHeight(LayoutUnit);
353    void setOverrideLogicalContentWidth(LayoutUnit);
354    void clearOverrideSize();
355    void clearOverrideLogicalContentHeight();
356    void clearOverrideLogicalContentWidth();
357
358    LayoutUnit overrideContainingBlockContentLogicalWidth() const;
359    LayoutUnit overrideContainingBlockContentLogicalHeight() const;
360    bool hasOverrideContainingBlockLogicalWidth() const;
361    bool hasOverrideContainingBlockLogicalHeight() const;
362    void setOverrideContainingBlockContentLogicalWidth(LayoutUnit);
363    void setOverrideContainingBlockContentLogicalHeight(LayoutUnit);
364    void clearContainingBlockOverrideSize();
365    void clearOverrideContainingBlockContentLogicalHeight();
366
367    virtual LayoutSize offsetFromContainer(const RenderObject*, const LayoutPoint&, bool* offsetDependsOnPoint = 0) const OVERRIDE;
368
369    LayoutUnit adjustBorderBoxLogicalWidthForBoxSizing(LayoutUnit width) const;
370    LayoutUnit adjustBorderBoxLogicalHeightForBoxSizing(LayoutUnit height) const;
371    LayoutUnit adjustContentBoxLogicalWidthForBoxSizing(LayoutUnit width) const;
372    LayoutUnit adjustContentBoxLogicalHeightForBoxSizing(LayoutUnit height) const;
373
374    struct ComputedMarginValues {
375        ComputedMarginValues() { }
376
377        LayoutUnit m_before;
378        LayoutUnit m_after;
379        LayoutUnit m_start;
380        LayoutUnit m_end;
381    };
382    struct LogicalExtentComputedValues {
383        LogicalExtentComputedValues() { }
384
385        LayoutUnit m_extent;
386        LayoutUnit m_position;
387        ComputedMarginValues m_margins;
388    };
389    // Resolve auto margins in the chosen direction of the containing block so that objects can be pushed to the start, middle or end
390    // of the containing block.
391    void computeMarginsForDirection(MarginDirection forDirection, const RenderBlock* containingBlock, LayoutUnit containerWidth, LayoutUnit childWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd, Length marginStartLength, Length marginStartEnd) const;
392
393    // Used to resolve margins in the containing block's block-flow direction.
394    void computeAndSetBlockDirectionMargins(const RenderBlock* containingBlock);
395
396    virtual LayoutUnit offsetFromLogicalTopOfFirstPage() const;
397
398    void positionLineBox(InlineBox*);
399
400    virtual InlineBox* createInlineBox();
401    void dirtyLineBoxes(bool fullLayout);
402
403    // For inline replaced elements, this function returns the inline box that owns us.  Enables
404    // the replaced RenderObject to quickly determine what line it is contained on and to easily
405    // iterate over structures on the line.
406    InlineBox* inlineBoxWrapper() const { return m_rareData ? m_rareData->m_inlineBoxWrapper : 0; }
407    void setInlineBoxWrapper(InlineBox*);
408    void deleteLineBoxWrapper();
409
410    virtual LayoutRect clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer) const OVERRIDE;
411    virtual void mapRectToPaintInvalidationBacking(const RenderLayerModelObject* paintInvalidationContainer, LayoutRect&, bool fixed = false) const OVERRIDE;
412    void repaintDuringLayoutIfMoved(const LayoutRect&);
413    virtual void repaintOverhangingFloats(bool paintAllDescendants);
414
415    virtual LayoutUnit containingBlockLogicalWidthForContent() const OVERRIDE;
416    LayoutUnit containingBlockLogicalHeightForContent(AvailableLogicalHeightType) const;
417
418    LayoutUnit containingBlockAvailableLineWidth() const;
419    LayoutUnit perpendicularContainingBlockLogicalHeight() const;
420
421    virtual void updateLogicalWidth();
422    virtual void updateLogicalHeight();
423    virtual void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const;
424
425    void computeLogicalWidth(LogicalExtentComputedValues&) const;
426
427    bool stretchesToViewport() const
428    {
429        return document().inQuirksMode() && style()->logicalHeight().isAuto() && !isFloatingOrOutOfFlowPositioned() && (isDocumentElement() || isBody()) && !isInline();
430    }
431
432    virtual LayoutSize intrinsicSize() const { return LayoutSize(); }
433    LayoutUnit intrinsicLogicalWidth() const { return style()->isHorizontalWritingMode() ? intrinsicSize().width() : intrinsicSize().height(); }
434    LayoutUnit intrinsicLogicalHeight() const { return style()->isHorizontalWritingMode() ? intrinsicSize().height() : intrinsicSize().width(); }
435    virtual LayoutUnit intrinsicContentLogicalHeight() const { return m_intrinsicContentLogicalHeight; }
436
437    // Whether or not the element shrinks to its intrinsic width (rather than filling the width
438    // of a containing block).  HTML4 buttons, <select>s, <input>s, legends, and floating/compact elements do this.
439    bool sizesLogicalWidthToFitContent(const Length& logicalWidth) const;
440
441    LayoutUnit shrinkLogicalWidthToAvoidFloats(LayoutUnit childMarginStart, LayoutUnit childMarginEnd, const RenderBlockFlow* cb) const;
442
443    LayoutUnit computeLogicalWidthUsing(SizeType, const Length& logicalWidth, LayoutUnit availableLogicalWidth, const RenderBlock* containingBlock) const;
444    LayoutUnit computeLogicalHeightUsing(const Length& height, LayoutUnit intrinsicContentHeight) const;
445    LayoutUnit computeContentLogicalHeight(const Length& height, LayoutUnit intrinsicContentHeight) const;
446    LayoutUnit computeContentAndScrollbarLogicalHeightUsing(const Length& height, LayoutUnit intrinsicContentHeight) const;
447    LayoutUnit computeReplacedLogicalWidthUsing(const Length& width) const;
448    LayoutUnit computeReplacedLogicalWidthRespectingMinMaxWidth(LayoutUnit logicalWidth, ShouldComputePreferred  = ComputeActual) const;
449    LayoutUnit computeReplacedLogicalHeightUsing(const Length& height) const;
450    LayoutUnit computeReplacedLogicalHeightRespectingMinMaxHeight(LayoutUnit logicalHeight) const;
451
452    virtual LayoutUnit computeReplacedLogicalWidth(ShouldComputePreferred  = ComputeActual) const;
453    virtual LayoutUnit computeReplacedLogicalHeight() const;
454
455    static bool percentageLogicalHeightIsResolvableFromBlock(const RenderBlock* containingBlock, bool outOfFlowPositioned);
456    LayoutUnit computePercentageLogicalHeight(const Length& height) const;
457
458    // Block flows subclass availableWidth/Height to handle multi column layout (shrinking the width/height available to children when laying out.)
459    virtual LayoutUnit availableLogicalWidth() const { return contentLogicalWidth(); }
460    virtual LayoutUnit availableLogicalHeight(AvailableLogicalHeightType) const;
461    LayoutUnit availableLogicalHeightUsing(const Length&, AvailableLogicalHeightType) const;
462
463    // There are a few cases where we need to refer specifically to the available physical width and available physical height.
464    // Relative positioning is one of those cases, since left/top offsets are physical.
465    LayoutUnit availableWidth() const { return style()->isHorizontalWritingMode() ? availableLogicalWidth() : availableLogicalHeight(IncludeMarginBorderPadding); }
466    LayoutUnit availableHeight() const { return style()->isHorizontalWritingMode() ? availableLogicalHeight(IncludeMarginBorderPadding) : availableLogicalWidth(); }
467
468    virtual int verticalScrollbarWidth() const;
469    int horizontalScrollbarHeight() const;
470    int instrinsicScrollbarLogicalWidth() const;
471    int scrollbarLogicalHeight() const { return style()->isHorizontalWritingMode() ? horizontalScrollbarHeight() : verticalScrollbarWidth(); }
472    virtual bool scroll(ScrollDirection, ScrollGranularity, float delta = 1);
473    bool canBeScrolledAndHasScrollableArea() const;
474    virtual bool canBeProgramaticallyScrolled() const;
475    virtual void autoscroll(const IntPoint&);
476    bool autoscrollInProgress() const;
477    bool canAutoscroll() const;
478    IntSize calculateAutoscrollDirection(const IntPoint& windowPoint) const;
479    static RenderBox* findAutoscrollable(RenderObject*);
480    virtual void stopAutoscroll() { }
481    virtual void panScroll(const IntPoint&);
482
483    bool hasAutoVerticalScrollbar() const { return hasOverflowClip() && (style()->overflowY() == OAUTO || style()->overflowY() == OOVERLAY); }
484    bool hasAutoHorizontalScrollbar() const { return hasOverflowClip() && (style()->overflowX() == OAUTO || style()->overflowX() == OOVERLAY); }
485    bool scrollsOverflow() const { return scrollsOverflowX() || scrollsOverflowY(); }
486
487    bool hasScrollableOverflowX() const { return scrollsOverflowX() && pixelSnappedScrollWidth() != pixelSnappedClientWidth(); }
488    bool hasScrollableOverflowY() const { return scrollsOverflowY() && pixelSnappedScrollHeight() != pixelSnappedClientHeight(); }
489    virtual bool scrollsOverflowX() const { return hasOverflowClip() && (style()->overflowX() == OSCROLL || hasAutoHorizontalScrollbar()); }
490    virtual bool scrollsOverflowY() const { return hasOverflowClip() && (style()->overflowY() == OSCROLL || hasAutoVerticalScrollbar()); }
491    bool usesCompositedScrolling() const;
492
493    // Elements such as the <input> field override this to specify that they are scrollable
494    // outside the context of the CSS overflow style
495    virtual bool isIntristicallyScrollable(ScrollbarOrientation orientation) const { return false; }
496
497    bool hasUnsplittableScrollingOverflow() const;
498    bool isUnsplittableForPagination() const;
499
500    virtual LayoutRect localCaretRect(InlineBox*, int caretOffset, LayoutUnit* extraWidthToEndOfLine = 0) OVERRIDE;
501
502    virtual LayoutRect overflowClipRect(const LayoutPoint& location, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize);
503    LayoutRect clipRect(const LayoutPoint& location);
504    virtual bool hasControlClip() const { return false; }
505    virtual LayoutRect controlClipRect(const LayoutPoint&) const { return LayoutRect(); }
506    bool pushContentsClip(PaintInfo&, const LayoutPoint& accumulatedOffset, ContentsClipBehavior);
507    void popContentsClip(PaintInfo&, PaintPhase originalPhase, const LayoutPoint& accumulatedOffset);
508
509    virtual void paintObject(PaintInfo&, const LayoutPoint&) { ASSERT_NOT_REACHED(); }
510    virtual void paintBoxDecorations(PaintInfo&, const LayoutPoint&);
511    virtual void paintMask(PaintInfo&, const LayoutPoint&);
512    virtual void paintClippingMask(PaintInfo&, const LayoutPoint&);
513    virtual void imageChanged(WrappedImagePtr, const IntRect* = 0) OVERRIDE;
514
515    // Called when a positioned object moves but doesn't necessarily change size.  A simplified layout is attempted
516    // that just updates the object's position. If the size does change, the object remains dirty.
517    bool tryLayoutDoingPositionedMovementOnly()
518    {
519        LayoutUnit oldWidth = width();
520        updateLogicalWidth();
521        // If we shrink to fit our width may have changed, so we still need full layout.
522        if (oldWidth != width())
523            return false;
524        updateLogicalHeight();
525        return true;
526    }
527
528    virtual PositionWithAffinity positionForPoint(const LayoutPoint&) OVERRIDE;
529
530    void removeFloatingOrPositionedChildFromBlockLists();
531
532    RenderLayer* enclosingFloatPaintingLayer() const;
533
534    virtual int firstLineBoxBaseline() const { return -1; }
535    virtual int inlineBlockBaseline(LineDirectionMode) const { return -1; } // Returns -1 if we should skip this box when computing the baseline of an inline-block.
536
537    bool shrinkToAvoidFloats() const;
538    virtual bool avoidsFloats() const;
539
540    virtual void markForPaginationRelayoutIfNeeded(SubtreeLayoutScope&);
541
542    bool isWritingModeRoot() const { return !parent() || parent()->style()->writingMode() != style()->writingMode(); }
543
544    bool isDeprecatedFlexItem() const { return !isInline() && !isFloatingOrOutOfFlowPositioned() && parent() && parent()->isDeprecatedFlexibleBox(); }
545    bool isFlexItemIncludingDeprecated() const { return !isInline() && !isFloatingOrOutOfFlowPositioned() && parent() && parent()->isFlexibleBoxIncludingDeprecated(); }
546
547    virtual LayoutUnit lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const OVERRIDE;
548    virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const OVERRIDE;
549
550    virtual LayoutUnit offsetLeft() const OVERRIDE;
551    virtual LayoutUnit offsetTop() const OVERRIDE;
552
553    LayoutPoint flipForWritingModeForChild(const RenderBox* child, const LayoutPoint&) const;
554    LayoutUnit flipForWritingMode(LayoutUnit position) const; // The offset is in the block direction (y for horizontal writing modes, x for vertical writing modes).
555    LayoutPoint flipForWritingMode(const LayoutPoint&) const;
556    LayoutPoint flipForWritingModeIncludingColumns(const LayoutPoint&) const;
557    LayoutSize flipForWritingMode(const LayoutSize&) const;
558    void flipForWritingMode(LayoutRect&) const;
559    FloatPoint flipForWritingMode(const FloatPoint&) const;
560    void flipForWritingMode(FloatRect&) const;
561    // These represent your location relative to your container as a physical offset.
562    // In layout related methods you almost always want the logical location (e.g. x() and y()).
563    LayoutPoint topLeftLocation() const;
564    LayoutSize topLeftLocationOffset() const;
565
566    LayoutRect logicalVisualOverflowRectForPropagation(RenderStyle*) const;
567    LayoutRect visualOverflowRectForPropagation(RenderStyle*) const;
568    LayoutRect logicalLayoutOverflowRectForPropagation(RenderStyle*) const;
569    LayoutRect layoutOverflowRectForPropagation(RenderStyle*) const;
570
571    bool hasRenderOverflow() const { return m_overflow; }
572    bool hasVisualOverflow() const { return m_overflow && !borderBoxRect().contains(m_overflow->visualOverflowRect()); }
573
574    virtual bool needsPreferredWidthsRecalculation() const;
575    virtual void computeIntrinsicRatioInformation(FloatSize& /* intrinsicSize */, double& /* intrinsicRatio */) const { }
576
577    IntSize scrolledContentOffset() const;
578    LayoutSize cachedSizeForOverflowClip() const;
579    void applyCachedClipAndScrollOffsetForRepaint(LayoutRect& paintRect) const;
580
581    virtual bool hasRelativeLogicalHeight() const;
582
583    bool hasHorizontalLayoutOverflow() const
584    {
585        if (!m_overflow)
586            return false;
587
588        LayoutRect layoutOverflowRect = m_overflow->layoutOverflowRect();
589        LayoutRect noOverflowRect = this->noOverflowRect();
590        return layoutOverflowRect.x() < noOverflowRect.x() || layoutOverflowRect.maxX() > noOverflowRect.maxX();
591    }
592
593    bool hasVerticalLayoutOverflow() const
594    {
595        if (!m_overflow)
596            return false;
597
598        LayoutRect layoutOverflowRect = m_overflow->layoutOverflowRect();
599        LayoutRect noOverflowRect = this->noOverflowRect();
600        return layoutOverflowRect.y() < noOverflowRect.y() || layoutOverflowRect.maxY() > noOverflowRect.maxY();
601    }
602
603    virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject*) const
604    {
605        ASSERT_NOT_REACHED();
606        return 0;
607    }
608
609    bool hasSameDirectionAs(const RenderBox* object) const { return style()->direction() == object->style()->direction(); }
610
611    ShapeOutsideInfo* shapeOutsideInfo() const
612    {
613        return ShapeOutsideInfo::isEnabledFor(*this) ? ShapeOutsideInfo::info(*this) : 0;
614    }
615
616    void markShapeOutsideDependentsForLayout()
617    {
618        if (isFloating())
619            removeFloatingOrPositionedChildFromBlockLists();
620    }
621
622    virtual void invalidateTreeAfterLayout(const RenderLayerModelObject&) OVERRIDE;
623
624protected:
625    virtual void willBeDestroyed() OVERRIDE;
626
627    virtual void styleWillChange(StyleDifference, const RenderStyle& newStyle) OVERRIDE;
628    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
629    virtual void updateFromStyle() OVERRIDE;
630
631    // Returns false if it could not cheaply compute the extent (e.g. fixed background), in which case the returned rect may be incorrect.
632    bool getBackgroundPaintedExtent(LayoutRect&) const;
633    virtual bool foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const;
634    virtual bool computeBackgroundIsKnownToBeObscured() OVERRIDE;
635
636    void paintBackgroundWithBorderAndBoxShadow(PaintInfo&, const LayoutRect&, BackgroundBleedAvoidance);
637    void paintBackground(const PaintInfo&, const LayoutRect&, BackgroundBleedAvoidance = BackgroundBleedNone);
638
639    void paintFillLayer(const PaintInfo&, const Color&, const FillLayer*, const LayoutRect&, BackgroundBleedAvoidance, CompositeOperator, RenderObject* backgroundObject);
640    void paintFillLayers(const PaintInfo&, const Color&, const FillLayer*, const LayoutRect&, BackgroundBleedAvoidance = BackgroundBleedNone, CompositeOperator = CompositeSourceOver, RenderObject* backgroundObject = 0);
641
642    void paintMaskImages(const PaintInfo&, const LayoutRect&);
643    void paintBoxDecorationsWithRect(PaintInfo&, const LayoutPoint&, const LayoutRect&);
644
645    BackgroundBleedAvoidance determineBackgroundBleedAvoidance(GraphicsContext*) const;
646    bool backgroundHasOpaqueTopLayer() const;
647
648    void computePositionedLogicalWidth(LogicalExtentComputedValues&) const;
649
650    LayoutUnit computeIntrinsicLogicalWidthUsing(const Length& logicalWidthLength, LayoutUnit availableLogicalWidth, LayoutUnit borderAndPadding) const;
651    LayoutUnit computeIntrinsicLogicalContentHeightUsing(const Length& logicalHeightLength, LayoutUnit intrinsicContentHeight, LayoutUnit borderAndPadding) const;
652
653    virtual bool shouldComputeSizeAsReplaced() const { return isReplaced() && !isInlineBlockOrInlineTable(); }
654
655    virtual void mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState&, MapCoordinatesFlags = ApplyContainerFlip, bool* wasFixed = 0) const OVERRIDE;
656    virtual void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const OVERRIDE;
657
658    void paintRootBoxFillLayers(const PaintInfo&);
659
660    RenderObject* splitAnonymousBoxesAroundChild(RenderObject* beforeChild);
661
662    virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentCompositedLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const OVERRIDE;
663    virtual void computeSelfHitTestRects(Vector<LayoutRect>&, const LayoutPoint& layerOffset) const OVERRIDE;
664
665    void updateIntrinsicContentLogicalHeight(LayoutUnit intrinsicContentLogicalHeight) const { m_intrinsicContentLogicalHeight = intrinsicContentLogicalHeight; }
666
667private:
668    void updateShapeOutsideInfoAfterStyleChange(const RenderStyle&, const RenderStyle* oldStyle);
669    void updateGridPositionAfterStyleChange(const RenderStyle*);
670
671    bool autoWidthShouldFitContent() const;
672    void shrinkToFitWidth(const LayoutUnit availableSpace, const LayoutUnit logicalLeftValue, const LayoutUnit bordersPlusPadding, LogicalExtentComputedValues&) const;
673
674    // Returns true if we did a full repaint
675    bool repaintLayerRectsForImage(WrappedImagePtr image, const FillLayer* layers, bool drawingBackground);
676
677    bool skipContainingBlockForPercentHeightCalculation(const RenderBox* containingBlock) const;
678
679    LayoutUnit containingBlockLogicalWidthForPositioned(const RenderBoxModelObject* containingBlock, bool checkForPerpendicularWritingMode = true) const;
680    LayoutUnit containingBlockLogicalHeightForPositioned(const RenderBoxModelObject* containingBlock, bool checkForPerpendicularWritingMode = true) const;
681
682    void computePositionedLogicalHeight(LogicalExtentComputedValues&) const;
683    void computePositionedLogicalWidthUsing(Length logicalWidth, const RenderBoxModelObject* containerBlock, TextDirection containerDirection,
684                                            LayoutUnit containerLogicalWidth, LayoutUnit bordersPlusPadding,
685                                            const Length& logicalLeft, const Length& logicalRight, const Length& marginLogicalLeft,
686                                            const Length& marginLogicalRight, LogicalExtentComputedValues&) const;
687    void computePositionedLogicalHeightUsing(Length logicalHeightLength, const RenderBoxModelObject* containerBlock,
688                                             LayoutUnit containerLogicalHeight, LayoutUnit bordersPlusPadding, LayoutUnit logicalHeight,
689                                             const Length& logicalTop, const Length& logicalBottom, const Length& marginLogicalTop,
690                                             const Length& marginLogicalBottom, LogicalExtentComputedValues&) const;
691
692    void computePositionedLogicalHeightReplaced(LogicalExtentComputedValues&) const;
693    void computePositionedLogicalWidthReplaced(LogicalExtentComputedValues&) const;
694
695    LayoutUnit fillAvailableMeasure(LayoutUnit availableLogicalWidth) const;
696    LayoutUnit fillAvailableMeasure(LayoutUnit availableLogicalWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd) const;
697
698    virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const;
699
700    // This function calculates the minimum and maximum preferred widths for an object.
701    // These values are used in shrink-to-fit layout systems.
702    // These include tables, positioned objects, floats and flexible boxes.
703    virtual void computePreferredLogicalWidths() { clearPreferredLogicalWidthsDirty(); }
704
705    virtual LayoutRect frameRectForStickyPositioning() const OVERRIDE FINAL { return frameRect(); }
706
707    RenderBoxRareData& ensureRareData()
708    {
709        if (!m_rareData)
710            m_rareData = adoptPtr(new RenderBoxRareData());
711        return *m_rareData.get();
712    }
713
714private:
715    // The width/height of the contents + borders + padding.  The x/y location is relative to our container (which is not always our parent).
716    LayoutRect m_frameRect;
717
718    // Our intrinsic height, used for min-height: min-content etc. Maintained by
719    // updateLogicalHeight. This is logicalHeight() before it is clamped to
720    // min/max.
721    mutable LayoutUnit m_intrinsicContentLogicalHeight;
722
723protected:
724    LayoutBoxExtent m_marginBox;
725
726    // The preferred logical width of the element if it were to break its lines at every possible opportunity.
727    LayoutUnit m_minPreferredLogicalWidth;
728
729    // The preferred logical width of the element if it never breaks any lines at all.
730    LayoutUnit m_maxPreferredLogicalWidth;
731
732    // Our overflow information.
733    OwnPtr<RenderOverflow> m_overflow;
734
735private:
736    OwnPtr<RenderBoxRareData> m_rareData;
737};
738
739DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderBox, isBox());
740
741inline RenderBox* RenderBox::previousSiblingBox() const
742{
743    return toRenderBox(previousSibling());
744}
745
746inline RenderBox* RenderBox::nextSiblingBox() const
747{
748    return toRenderBox(nextSibling());
749}
750
751inline RenderBox* RenderBox::parentBox() const
752{
753    return toRenderBox(parent());
754}
755
756inline RenderBox* RenderBox::firstChildBox() const
757{
758    return toRenderBox(slowFirstChild());
759}
760
761inline RenderBox* RenderBox::lastChildBox() const
762{
763    return toRenderBox(slowLastChild());
764}
765
766inline void RenderBox::setInlineBoxWrapper(InlineBox* boxWrapper)
767{
768    if (boxWrapper) {
769        ASSERT(!inlineBoxWrapper());
770        // m_inlineBoxWrapper should already be 0. Deleting it is a safeguard against security issues.
771        // Otherwise, there will two line box wrappers keeping the reference to this renderer, and
772        // only one will be notified when the renderer is getting destroyed. The second line box wrapper
773        // will keep a stale reference.
774        if (UNLIKELY(inlineBoxWrapper() != 0))
775            deleteLineBoxWrapper();
776    }
777
778    ensureRareData().m_inlineBoxWrapper = boxWrapper;
779}
780
781} // namespace WebCore
782
783#endif // RenderBox_h
784