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
30 namespace WebCore {
31
32 // Modes for some of the line-related functions.
33 enum LinePositionMode { PositionOnContainingLine, PositionOfInteriorLineBoxes };
34 enum LineDirectionMode { HorizontalLine, VerticalLine };
35 typedef unsigned BorderEdgeFlags;
36
37 enum BackgroundBleedAvoidance {
38 BackgroundBleedNone,
39 BackgroundBleedShrinkBackground,
40 BackgroundBleedUseTransparencyLayer
41 };
42
43 // This class is the base for all objects that adhere to the CSS box model as described
44 // at http://www.w3.org/TR/CSS21/box.html
45
46 class RenderBoxModelObject : public RenderObject {
47 public:
48 RenderBoxModelObject(Node*);
49 virtual ~RenderBoxModelObject();
50
51 virtual void destroy();
52
53 int relativePositionOffsetX() const;
54 int relativePositionOffsetY() const;
relativePositionOffset()55 IntSize relativePositionOffset() const { return IntSize(relativePositionOffsetX(), relativePositionOffsetY()); }
relativePositionLogicalOffset()56 IntSize relativePositionLogicalOffset() const { return style()->isHorizontalWritingMode() ? relativePositionOffset() : relativePositionOffset().transposedSize(); }
57
58 // IE extensions. Used to calculate offsetWidth/Height. Overridden by inlines (RenderFlow)
59 // to return the remaining width on a given line (and the height of a single line).
60 virtual int offsetLeft() const;
61 virtual int offsetTop() const;
62 virtual int offsetWidth() const = 0;
63 virtual int offsetHeight() const = 0;
64
65 virtual void styleWillChange(StyleDifference, const RenderStyle* newStyle);
66 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
67 virtual void updateBoxModelInfoFromStyle();
68
69 bool hasSelfPaintingLayer() const;
layer()70 RenderLayer* layer() const { return m_layer; }
requiresLayer()71 virtual bool requiresLayer() const { return isRoot() || isPositioned() || isRelPositioned() || isTransparent() || hasOverflowClip() || hasTransform() || hasMask() || hasReflection() || style()->specifiesColumns(); }
72
73 // This will work on inlines to return the bounding box of all of the lines' border boxes.
74 virtual IntRect borderBoundingBox() const = 0;
75
76 // Virtual since table cells override
77 virtual int paddingTop(bool includeIntrinsicPadding = true) const;
78 virtual int paddingBottom(bool includeIntrinsicPadding = true) const;
79 virtual int paddingLeft(bool includeIntrinsicPadding = true) const;
80 virtual int paddingRight(bool includeIntrinsicPadding = true) const;
81 virtual int paddingBefore(bool includeIntrinsicPadding = true) const;
82 virtual int paddingAfter(bool includeIntrinsicPadding = true) const;
83 virtual int paddingStart(bool includeIntrinsicPadding = true) const;
84 virtual int paddingEnd(bool includeIntrinsicPadding = true) const;
85
borderTop()86 virtual int borderTop() const { return style()->borderTopWidth(); }
borderBottom()87 virtual int borderBottom() const { return style()->borderBottomWidth(); }
borderLeft()88 virtual int borderLeft() const { return style()->borderLeftWidth(); }
borderRight()89 virtual int borderRight() const { return style()->borderRightWidth(); }
borderBefore()90 virtual int borderBefore() const { return style()->borderBeforeWidth(); }
borderAfter()91 virtual int borderAfter() const { return style()->borderAfterWidth(); }
borderStart()92 virtual int borderStart() const { return style()->borderStartWidth(); }
borderEnd()93 virtual int borderEnd() const { return style()->borderEndWidth(); }
94
borderAndPaddingHeight()95 int borderAndPaddingHeight() const { return borderTop() + borderBottom() + paddingTop() + paddingBottom(); }
borderAndPaddingWidth()96 int borderAndPaddingWidth() const { return borderLeft() + borderRight() + paddingLeft() + paddingRight(); }
borderAndPaddingLogicalHeight()97 int borderAndPaddingLogicalHeight() const { return borderBefore() + borderAfter() + paddingBefore() + paddingAfter(); }
borderAndPaddingLogicalWidth()98 int borderAndPaddingLogicalWidth() const { return borderStart() + borderEnd() + paddingStart() + paddingEnd(); }
borderAndPaddingLogicalLeft()99 int borderAndPaddingLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop(); }
100
borderAndPaddingStart()101 int borderAndPaddingStart() const { return borderStart() + paddingStart(); }
borderLogicalLeft()102 int borderLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() : borderTop(); }
borderLogicalRight()103 int borderLogicalRight() const { return style()->isHorizontalWritingMode() ? borderRight() : borderBottom(); }
104
105 virtual int marginTop() const = 0;
106 virtual int marginBottom() const = 0;
107 virtual int marginLeft() const = 0;
108 virtual int marginRight() const = 0;
109 virtual int marginBefore() const = 0;
110 virtual int marginAfter() const = 0;
111 virtual int marginStart() const = 0;
112 virtual int marginEnd() const = 0;
113
hasInlineDirectionBordersPaddingOrMargin()114 bool hasInlineDirectionBordersPaddingOrMargin() const { return hasInlineDirectionBordersOrPadding() || marginStart()|| marginEnd(); }
hasInlineDirectionBordersOrPadding()115 bool hasInlineDirectionBordersOrPadding() const { return borderStart() || borderEnd() || paddingStart()|| paddingEnd(); }
116
117 virtual int containingBlockLogicalWidthForContent() const;
118
childBecameNonInline(RenderObject *)119 virtual void childBecameNonInline(RenderObject* /*child*/) { }
120
121 void paintBorder(GraphicsContext*, int tx, int ty, int w, int h, const RenderStyle*, BackgroundBleedAvoidance = BackgroundBleedNone, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
122 bool paintNinePieceImage(GraphicsContext*, int tx, int ty, int w, int h, const RenderStyle*, const NinePieceImage&, CompositeOperator = CompositeSourceOver);
123 void paintBoxShadow(GraphicsContext*, int tx, int ty, int w, int h, const RenderStyle*, ShadowStyle, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
124 void paintFillLayerExtended(const PaintInfo&, const Color&, const FillLayer*, int tx, int ty, int width, int height, BackgroundBleedAvoidance, InlineFlowBox* = 0, int inlineBoxWidth = 0, int inlineBoxHeight = 0, CompositeOperator = CompositeSourceOver, RenderObject* backgroundObject = 0);
125
126 // Overridden by subclasses to determine line height and baseline position.
127 virtual int lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
128 virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
129
130 // Called by RenderObject::destroy() (and RenderWidget::destroy()) and is the only way layers should ever be destroyed
131 void destroyLayer();
132
133 void highQualityRepaintTimerFired(Timer<RenderBoxModelObject>*);
134
135 virtual void setSelectionState(SelectionState s);
136 protected:
137 void calculateBackgroundImageGeometry(const FillLayer*, int tx, int ty, int w, int h, IntRect& destRect, IntPoint& phase, IntSize& tileSize);
138 void getBorderEdgeInfo(class BorderEdge[], bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true) const;
139 bool borderObscuresBackgroundEdge(const FloatSize& contextScale) const;
140
141 bool shouldPaintAtLowQuality(GraphicsContext*, Image*, const void*, const IntSize&);
142
143 RenderBoxModelObject* continuation() const;
144 void setContinuation(RenderBoxModelObject*);
145
146 private:
isBoxModelObject()147 virtual bool isBoxModelObject() const { return true; }
148
149 IntSize calculateFillTileSize(const FillLayer*, IntSize scaledSize) const;
150
151 RoundedIntRect getBackgroundRoundedRect(const IntRect&, InlineFlowBox*, int inlineBoxWidth, int inlineBoxHeight,
152 bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
153
154 void clipBorderSidePolygon(GraphicsContext*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
155 BoxSide, bool firstEdgeMatches, bool secondEdgeMatches);
156 void paintOneBorderSide(GraphicsContext*, const RenderStyle*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
157 const IntRect& sideRect, BoxSide, BoxSide adjacentSide1, BoxSide adjacentSide2, const class BorderEdge[],
158 const Path*, BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias, const Color* overrideColor = 0);
159 void paintTranslucentBorderSides(GraphicsContext*, const RenderStyle*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
160 const class BorderEdge[], BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false);
161 void paintBorderSides(GraphicsContext*, const RenderStyle*, const RoundedIntRect& outerBorder, const RoundedIntRect& innerBorder,
162 const class BorderEdge[], BorderEdgeFlags, BackgroundBleedAvoidance,
163 bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false, const Color* overrideColor = 0);
164 void drawBoxSideFromPath(GraphicsContext*, const IntRect&, const Path&, const class BorderEdge[],
165 float thickness, float drawThickness, BoxSide, const RenderStyle*,
166 Color, EBorderStyle, BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
167
168 friend class RenderView;
169
170 RenderLayer* m_layer;
171
172 // Used to store state between styleWillChange and styleDidChange
173 static bool s_wasFloating;
174 static bool s_hadLayer;
175 static bool s_layerWasSelfPainting;
176 };
177
toRenderBoxModelObject(RenderObject * object)178 inline RenderBoxModelObject* toRenderBoxModelObject(RenderObject* object)
179 {
180 ASSERT(!object || object->isBoxModelObject());
181 return static_cast<RenderBoxModelObject*>(object);
182 }
183
toRenderBoxModelObject(const RenderObject * object)184 inline const RenderBoxModelObject* toRenderBoxModelObject(const RenderObject* object)
185 {
186 ASSERT(!object || object->isBoxModelObject());
187 return static_cast<const RenderBoxModelObject*>(object);
188 }
189
190 // This will catch anyone doing an unnecessary cast.
191 void toRenderBoxModelObject(const RenderBoxModelObject*);
192
193 } // namespace WebCore
194
195 #endif // RenderBoxModelObject_h
196