1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2006, 2007, 2009 Apple Inc. All rights reserved.
5 * Copyright (C) 2010 Google Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef RenderBoxModelObject_h
25#define RenderBoxModelObject_h
26
27#include "RenderObject.h"
28#include "ShadowData.h"
29
30namespace WebCore {
31
32// Modes for some of the line-related functions.
33enum LinePositionMode { PositionOnContainingLine, PositionOfInteriorLineBoxes };
34enum LineDirectionMode { HorizontalLine, VerticalLine };
35typedef unsigned BorderEdgeFlags;
36
37// This class is the base for all objects that adhere to the CSS box model as described
38// at http://www.w3.org/TR/CSS21/box.html
39
40class RenderBoxModelObject : public RenderObject {
41public:
42    RenderBoxModelObject(Node*);
43    virtual ~RenderBoxModelObject();
44
45    virtual void destroy();
46
47    int relativePositionOffsetX() const;
48    int relativePositionOffsetY() const;
49    IntSize relativePositionOffset() const { return IntSize(relativePositionOffsetX(), relativePositionOffsetY()); }
50    IntSize relativePositionLogicalOffset() const { return style()->isHorizontalWritingMode() ? relativePositionOffset() : relativePositionOffset().transposedSize(); }
51
52    // IE extensions. Used to calculate offsetWidth/Height.  Overridden by inlines (RenderFlow)
53    // to return the remaining width on a given line (and the height of a single line).
54    virtual int offsetLeft() const;
55    virtual int offsetTop() const;
56    virtual int offsetWidth() const = 0;
57    virtual int offsetHeight() const = 0;
58
59    virtual void styleWillChange(StyleDifference, const RenderStyle* newStyle);
60    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
61    virtual void updateBoxModelInfoFromStyle();
62
63    bool hasSelfPaintingLayer() const;
64    RenderLayer* layer() const { return m_layer; }
65#if PLATFORM(ANDROID)
66    virtual bool requiresLayer() const { return isRoot() || isPositioned() || isRelPositioned() || isTransparent() || hasOverflowClip() || hasTransform() || hasMask() || hasReflection() || style()->specifiesColumns() || style()->hasFixedBackgroundImage(); }
67#else
68    virtual bool requiresLayer() const { return isRoot() || isPositioned() || isRelPositioned() || isTransparent() || hasOverflowClip() || hasTransform() || hasMask() || hasReflection() || style()->specifiesColumns(); }
69#endif
70
71    // This will work on inlines to return the bounding box of all of the lines' border boxes.
72    virtual IntRect borderBoundingBox() const = 0;
73
74    // Virtual since table cells override
75    virtual int paddingTop(bool includeIntrinsicPadding = true) const;
76    virtual int paddingBottom(bool includeIntrinsicPadding = true) const;
77    virtual int paddingLeft(bool includeIntrinsicPadding = true) const;
78    virtual int paddingRight(bool includeIntrinsicPadding = true) const;
79    virtual int paddingBefore(bool includeIntrinsicPadding = true) const;
80    virtual int paddingAfter(bool includeIntrinsicPadding = true) const;
81    virtual int paddingStart(bool includeIntrinsicPadding = true) const;
82    virtual int paddingEnd(bool includeIntrinsicPadding = true) const;
83
84    virtual int borderTop() const { return style()->borderTopWidth(); }
85    virtual int borderBottom() const { return style()->borderBottomWidth(); }
86    virtual int borderLeft() const { return style()->borderLeftWidth(); }
87    virtual int borderRight() const { return style()->borderRightWidth(); }
88    virtual int borderBefore() const { return style()->borderBeforeWidth(); }
89    virtual int borderAfter() const { return style()->borderAfterWidth(); }
90    virtual int borderStart() const { return style()->borderStartWidth(); }
91    virtual int borderEnd() const { return style()->borderEndWidth(); }
92
93    int borderAndPaddingHeight() const { return borderTop() + borderBottom() + paddingTop() + paddingBottom(); }
94    int borderAndPaddingWidth() const { return borderLeft() + borderRight() + paddingLeft() + paddingRight(); }
95    int borderAndPaddingLogicalHeight() const { return borderBefore() + borderAfter() + paddingBefore() + paddingAfter(); }
96    int borderAndPaddingLogicalWidth() const { return borderStart() + borderEnd() + paddingStart() + paddingEnd(); }
97    int borderAndPaddingLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop(); }
98
99    int borderAndPaddingStart() const { return borderStart() + paddingStart(); }
100    int borderLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() : borderTop(); }
101    int borderLogicalRight() const { return style()->isHorizontalWritingMode() ? borderRight() : borderBottom(); }
102
103    virtual int marginTop() const = 0;
104    virtual int marginBottom() const = 0;
105    virtual int marginLeft() const = 0;
106    virtual int marginRight() const = 0;
107    virtual int marginBefore() const = 0;
108    virtual int marginAfter() const = 0;
109    virtual int marginStart() const = 0;
110    virtual int marginEnd() const = 0;
111
112    bool hasInlineDirectionBordersPaddingOrMargin() const { return hasInlineDirectionBordersOrPadding() || marginStart()|| marginEnd(); }
113    bool hasInlineDirectionBordersOrPadding() const { return borderStart() || borderEnd() || paddingStart()|| paddingEnd(); }
114
115    virtual int containingBlockLogicalWidthForContent() const;
116
117    virtual void childBecameNonInline(RenderObject* /*child*/) { }
118
119    void paintBorder(GraphicsContext*, int tx, int ty, int w, int h, const RenderStyle*, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
120    bool paintNinePieceImage(GraphicsContext*, int tx, int ty, int w, int h, const RenderStyle*, const NinePieceImage&, CompositeOperator = CompositeSourceOver);
121    void paintBoxShadow(GraphicsContext*, int tx, int ty, int w, int h, const RenderStyle*, ShadowStyle, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
122    void paintFillLayerExtended(const PaintInfo&, const Color&, const FillLayer*, int tx, int ty, int width, int height, InlineFlowBox* = 0, int inlineBoxWidth = 0, int inlineBoxHeight = 0, CompositeOperator = CompositeSourceOver, RenderObject* backgroundObject = 0);
123
124    // Overridden by subclasses to determine line height and baseline position.
125    virtual int lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
126    virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
127
128    // Called by RenderObject::destroy() (and RenderWidget::destroy()) and is the only way layers should ever be destroyed
129    void destroyLayer();
130
131    void highQualityRepaintTimerFired(Timer<RenderBoxModelObject>*);
132
133    virtual void setSelectionState(SelectionState s);
134protected:
135    void calculateBackgroundImageGeometry(const FillLayer*, int tx, int ty, int w, int h, IntRect& destRect, IntPoint& phase, IntSize& tileSize);
136
137    bool shouldPaintAtLowQuality(GraphicsContext*, Image*, const void*, const IntSize&);
138
139    RenderBoxModelObject* continuation() const;
140    void setContinuation(RenderBoxModelObject*);
141
142private:
143    virtual bool isBoxModelObject() const { return true; }
144
145    IntSize calculateFillTileSize(const FillLayer*, IntSize scaledSize) const;
146
147    RoundedIntRect getBackgroundRoundedRect(const IntRect&, InlineFlowBox*, int inlineBoxWidth, int inlineBoxHeight,
148        bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
149
150    void clipBorderSidePolygon(GraphicsContext*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
151                               BoxSide, bool firstEdgeMatches, bool secondEdgeMatches);
152    void paintOneBorderSide(GraphicsContext*, const RenderStyle*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
153                                const IntRect& sideRect, BoxSide, BoxSide adjacentSide1, BoxSide adjacentSide2, const class BorderEdge[],
154                                const Path*, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias, const Color* overrideColor = 0);
155    void paintTranslucentBorderSides(GraphicsContext*, const RenderStyle*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
156                          const class BorderEdge[], bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false);
157    void paintBorderSides(GraphicsContext*, const RenderStyle*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
158                          const class BorderEdge[], BorderEdgeFlags, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false, const Color* overrideColor = 0);
159    void drawBoxSideFromPath(GraphicsContext*, const IntRect&, const Path&, const class BorderEdge[],
160                            float thickness, float drawThickness, BoxSide, const RenderStyle*,
161                            Color, EBorderStyle, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
162
163    friend class RenderView;
164
165    RenderLayer* m_layer;
166
167    // Used to store state between styleWillChange and styleDidChange
168    static bool s_wasFloating;
169    static bool s_hadLayer;
170    static bool s_layerWasSelfPainting;
171};
172
173inline RenderBoxModelObject* toRenderBoxModelObject(RenderObject* object)
174{
175    ASSERT(!object || object->isBoxModelObject());
176    return static_cast<RenderBoxModelObject*>(object);
177}
178
179inline const RenderBoxModelObject* toRenderBoxModelObject(const RenderObject* object)
180{
181    ASSERT(!object || object->isBoxModelObject());
182    return static_cast<const RenderBoxModelObject*>(object);
183}
184
185// This will catch anyone doing an unnecessary cast.
186void toRenderBoxModelObject(const RenderBoxModelObject*);
187
188} // namespace WebCore
189
190#endif // RenderBoxModelObject_h
191