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 "RenderObject.h"
27 #include "ScrollTypes.h"
28
29 namespace WebCore {
30
31 enum WidthType { Width, MinWidth, MaxWidth };
32
33 class RenderBox : public RenderObject {
34 public:
35 RenderBox(Node*);
36 virtual ~RenderBox();
37
renderName()38 virtual const char* renderName() const { return "RenderBox"; }
39
x()40 int x() const { return m_frameRect.x(); }
y()41 int y() const { return m_frameRect.y(); }
width()42 int width() const { ASSERT(!isRenderInline()); return m_frameRect.width(); }
height()43 int height() const { ASSERT(!isRenderInline()); return m_frameRect.height(); }
44
setX(int x)45 void setX(int x) { m_frameRect.setX(x); }
setY(int y)46 void setY(int y) { m_frameRect.setY(y); }
setWidth(int width)47 void setWidth(int width) { m_frameRect.setWidth(width); }
setHeight(int height)48 void setHeight(int height) { m_frameRect.setHeight(height); }
49
location()50 IntPoint location() const { return m_frameRect.location(); }
size()51 IntSize size() const { ASSERT(!isRenderInline()); return m_frameRect.size(); }
52
setLocation(const IntPoint & location)53 void setLocation(const IntPoint& location) { m_frameRect.setLocation(location); }
setLocation(int x,int y)54 void setLocation(int x, int y) { setLocation(IntPoint(x, y)); }
55
setSize(const IntSize & size)56 void setSize(const IntSize& size) { m_frameRect.setSize(size); }
move(int dx,int dy)57 void move(int dx, int dy) { m_frameRect.move(dx, dy); }
58
frameRect()59 IntRect frameRect() const { ASSERT(!isRenderInline()); return m_frameRect; }
setFrameRect(const IntRect & rect)60 void setFrameRect(const IntRect& rect) { m_frameRect = rect; }
61
borderBoxRect()62 IntRect borderBoxRect() const { return IntRect(0, 0, width(), height()); }
borderBoundingBox()63 virtual IntRect borderBoundingBox() const { return borderBoxRect(); } // This will work on inlines to return the bounding box of all of the lines' border boxes.
64
65 // The content area of the box (excludes padding and border).
contentBoxRect()66 IntRect contentBoxRect() const { return IntRect(borderLeft() + paddingLeft(), borderTop() + paddingTop(), contentWidth(), contentHeight()); }
67 // The content box in absolute coords. Ignores transforms.
68 IntRect absoluteContentBox() const;
69 // The content box converted to absolute coords (taking transforms into account).
70 FloatQuad absoluteContentQuad() const;
71
72 // Bounds of the outline box in absolute coords. Respects transforms
73 virtual IntRect outlineBoundsForRepaint(RenderBox* /*repaintContainer*/) const;
74 virtual void addFocusRingRects(GraphicsContext*, int tx, int ty);
75
76 // Use this with caution! No type checking is done!
77 RenderBox* previousSiblingBox() const;
78 RenderBox* nextSiblingBox() const;
79 RenderBox* parentBox() const;
80
81 // The height of a block when you include normal flow overflow spillage out of the bottom
82 // of the block (e.g., a <div style="height:25px"> that has a 100px tall image inside
83 // it would have an overflow height of borderTop() + paddingTop() + 100px.
84 virtual int overflowHeight(bool /*includeInterior*/ = true) const { return height(); }
85 virtual int overflowWidth(bool /*includeInterior*/ = true) const { return width(); }
setOverflowHeight(int)86 virtual void setOverflowHeight(int) { }
setOverflowWidth(int)87 virtual void setOverflowWidth(int) { }
88 virtual int overflowLeft(bool /*includeInterior*/ = true) const { return 0; }
89 virtual int overflowTop(bool /*includeInterior*/ = true) const { return 0; }
90 virtual IntRect overflowRect(bool /*includeInterior*/ = true) const { return borderBoxRect(); }
91
contentWidth()92 int contentWidth() const { return clientWidth() - paddingLeft() - paddingRight(); }
contentHeight()93 int contentHeight() const { return clientHeight() - paddingTop() - paddingBottom(); }
94
95 // IE extensions. Used to calculate offsetWidth/Height. Overridden by inlines (RenderFlow)
96 // to return the remaining width on a given line (and the height of a single line).
offsetWidth()97 virtual int offsetWidth() const { return width(); }
offsetHeight()98 virtual int offsetHeight() const { return height(); }
99 virtual int offsetLeft() const;
100 virtual int offsetTop() const;
101 virtual RenderBox* offsetParent() const;
102
103 // More IE extensions. clientWidth and clientHeight represent the interior of an object
104 // excluding border and scrollbar. clientLeft/Top are just the borderLeftWidth and borderTopWidth.
clientLeft()105 int clientLeft() const { return borderLeft(); }
clientTop()106 int clientTop() const { return borderTop(); }
107 int clientWidth() const;
108 int clientHeight() const;
109
110 // scrollWidth/scrollHeight will be the same as clientWidth/clientHeight unless the
111 // object has overflow:hidden/scroll/auto specified and also has overflow.
112 // scrollLeft/Top return the current scroll position. These methods are virtual so that objects like
113 // textareas can scroll shadow content (but pretend that they are the objects that are
114 // scrolling).
115 virtual int scrollLeft() const;
116 virtual int scrollTop() const;
117 virtual int scrollWidth() const;
118 virtual int scrollHeight() const;
119 virtual void setScrollLeft(int);
120 virtual void setScrollTop(int);
121
hasHorizontalBordersPaddingOrMargin()122 bool hasHorizontalBordersPaddingOrMargin() const { return hasHorizontalBordersOrPadding() || marginLeft() != 0 || marginRight() != 0; }
hasHorizontalBordersOrPadding()123 bool hasHorizontalBordersOrPadding() const { return borderLeft() != 0 || borderRight() != 0 || paddingLeft() != 0 || paddingRight() != 0; }
124
marginTop()125 int marginTop() const { return m_marginTop; }
marginBottom()126 int marginBottom() const { return m_marginBottom; }
marginLeft()127 int marginLeft() const { return m_marginLeft; }
marginRight()128 int marginRight() const { return m_marginRight; }
129
130 // Virtual since table cells override
131 virtual int paddingTop(bool includeIntrinsicPadding = true) const;
132 virtual int paddingBottom(bool includeIntrinsicPadding = true) const;
133 virtual int paddingLeft(bool includeIntrinsicPadding = true) const;
134 virtual int paddingRight(bool includeIntrinsicPadding = true) const;
135
borderTop()136 virtual int borderTop() const { return style()->borderTopWidth(); }
borderBottom()137 virtual int borderBottom() const { return style()->borderBottomWidth(); }
borderLeft()138 virtual int borderLeft() const { return style()->borderLeftWidth(); }
borderRight()139 virtual int borderRight() const { return style()->borderRightWidth(); }
140
141 // The following seven functions are used to implement collapsing margins.
142 // All objects know their maximal positive and negative margins. The
143 // formula for computing a collapsed margin is |maxPosMargin| - |maxNegmargin|.
144 // For a non-collapsing box, such as a leaf element, this formula will simply return
145 // the margin of the element. Blocks override the maxTopMargin and maxBottomMargin
146 // methods.
isSelfCollapsingBlock()147 virtual bool isSelfCollapsingBlock() const { return false; }
collapsedMarginTop()148 int collapsedMarginTop() const { return maxTopMargin(true) - maxTopMargin(false); }
collapsedMarginBottom()149 int collapsedMarginBottom() const { return maxBottomMargin(true) - maxBottomMargin(false); }
isTopMarginQuirk()150 virtual bool isTopMarginQuirk() const { return false; }
isBottomMarginQuirk()151 virtual bool isBottomMarginQuirk() const { return false; }
maxTopMargin(bool positive)152 virtual int maxTopMargin(bool positive) const { return positive ? std::max(0, marginTop()) : -std::min(0, marginTop()); }
maxBottomMargin(bool positive)153 virtual int maxBottomMargin(bool positive) const { return positive ? std::max(0, marginBottom()) : -std::min(0, marginBottom()); }
154
155 virtual void absoluteRects(Vector<IntRect>&, int tx, int ty, bool topLevel = true);
156 virtual void absoluteQuads(Vector<FloatQuad>&, bool topLevel = true);
157
158 IntRect reflectionBox() const;
159 int reflectionOffset() const;
160 // Given a rect in the object's coordinate space, returns the corresponding rect in the reflection.
161 IntRect reflectedRect(const IntRect&) const;
162
163 virtual void paint(PaintInfo&, int tx, int ty);
164 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
165
166 virtual void destroy();
167
168 virtual int minPrefWidth() const;
169 virtual int maxPrefWidth() const;
170
171 virtual int overrideSize() const;
172 virtual int overrideWidth() const;
173 virtual int overrideHeight() const;
174 virtual void setOverrideSize(int);
175
176 virtual FloatPoint localToAbsolute(FloatPoint localPoint = FloatPoint(), bool fixed = false, bool useTransforms = false) const;
177 virtual FloatPoint absoluteToLocal(FloatPoint containerPoint, bool fixed = false, bool useTransforms = false) const;
178
179 virtual IntSize offsetFromContainer(RenderObject*) const;
180
181 int calcBorderBoxWidth(int width) const;
182 int calcBorderBoxHeight(int height) const;
183 int calcContentBoxWidth(int width) const;
184 int calcContentBoxHeight(int height) const;
185
borderFitAdjust(int &,int &)186 virtual void borderFitAdjust(int& /*x*/, int& /*w*/) const { } // Shrink the box in which the border paints if border-fit is set.
187
188 // This method is now public so that centered objects like tables that are
189 // shifted right by left-aligned floats can recompute their left and
190 // right margins (so that they can remain centered after being
191 // shifted. -dwh
192 void calcHorizontalMargins(const Length& marginLeft, const Length& marginRight, int containerWidth);
193
194 virtual void position(InlineBox*);
195
196 virtual void dirtyLineBoxes(bool fullLayout, bool isRootLineBox = false);
197
198 // For inline replaced elements, this function returns the inline box that owns us. Enables
199 // the replaced RenderObject to quickly determine what line it is contained on and to easily
200 // iterate over structures on the line.
inlineBoxWrapper()201 virtual InlineBox* inlineBoxWrapper() const { return m_inlineBoxWrapper; }
setInlineBoxWrapper(InlineBox * boxWrapper)202 virtual void setInlineBoxWrapper(InlineBox* boxWrapper) { m_inlineBoxWrapper = boxWrapper; }
203 virtual void deleteLineBoxWrapper();
204
205 virtual int lowestPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
206 virtual int rightmostPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
207 virtual int leftmostPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
208
209 virtual IntRect clippedOverflowRectForRepaint(RenderBox* repaintContainer);
210 virtual void computeRectForRepaint(IntRect&, RenderBox* repaintContainer, bool fixed = false);
211 IntSize offsetForPositionedInContainer(RenderObject*) const;
212 virtual FloatQuad localToContainerQuad(const FloatQuad&, RenderBox* repaintContainer, bool fixed = false) const;
213
214 virtual void repaintDuringLayoutIfMoved(const IntRect&);
215
216 virtual int containingBlockWidth() const;
217
218 virtual void calcWidth();
219 virtual void calcHeight();
220
stretchesToViewHeight()221 bool stretchesToViewHeight() const
222 {
223 return style()->htmlHacks() && style()->height().isAuto() && !isFloatingOrPositioned() && (isRoot() || isBody());
224 }
225
intrinsicSize()226 virtual IntSize intrinsicSize() const { return IntSize(); }
227
228 // Whether or not the element shrinks to its intrinsic width (rather than filling the width
229 // of a containing block). HTML4 buttons, <select>s, <input>s, legends, and floating/compact elements do this.
230 bool sizesToIntrinsicWidth(WidthType) const;
stretchesToMinIntrinsicWidth()231 virtual bool stretchesToMinIntrinsicWidth() const { return false; }
232
233 int calcWidthUsing(WidthType, int containerWidth);
234 int calcHeightUsing(const Length& height);
235 int calcReplacedWidthUsing(Length width) const;
236 int calcReplacedHeightUsing(Length height) const;
237
238 virtual int calcReplacedWidth(bool includeMaxWidth = true) const;
239 virtual int calcReplacedHeight() const;
240
241 int calcPercentageHeight(const Length& height);
242
243 // Block flows subclass availableWidth to handle multi column layout (shrinking the width available to children when laying out.)
availableWidth()244 virtual int availableWidth() const { return contentWidth(); }
245 virtual int availableHeight() const;
246 int availableHeightUsing(const Length&) const;
247
248 void calcVerticalMargins();
249
250 int relativePositionOffsetX() const;
251 int relativePositionOffsetY() const;
relativePositionOffset()252 IntSize relativePositionOffset() const { return IntSize(relativePositionOffsetX(), relativePositionOffsetY()); }
253
layer()254 RenderLayer* layer() const { return m_layer; }
requiresLayer()255 virtual bool requiresLayer() const { return isRoot() || isPositioned() || isRelPositioned() || isTransparent() || hasOverflowClip() || hasTransform() || hasMask() || hasReflection(); }
256
257 virtual int verticalScrollbarWidth() const;
258 int horizontalScrollbarHeight() const;
259 virtual bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1.0f);
260 virtual bool canBeProgramaticallyScrolled(bool) const;
261 virtual void autoscroll();
stopAutoscroll()262 virtual void stopAutoscroll() { }
263 virtual void panScroll(const IntPoint&);
hasAutoVerticalScrollbar()264 bool hasAutoVerticalScrollbar() const { return hasOverflowClip() && (style()->overflowY() == OAUTO || style()->overflowY() == OOVERLAY); }
hasAutoHorizontalScrollbar()265 bool hasAutoHorizontalScrollbar() const { return hasOverflowClip() && (style()->overflowX() == OAUTO || style()->overflowX() == OOVERLAY); }
scrollsOverflow()266 bool scrollsOverflow() const { return scrollsOverflowX() || scrollsOverflowY(); }
scrollsOverflowX()267 bool scrollsOverflowX() const { return hasOverflowClip() && (style()->overflowX() == OSCROLL || hasAutoHorizontalScrollbar()); }
scrollsOverflowY()268 bool scrollsOverflowY() const { return hasOverflowClip() && (style()->overflowY() == OSCROLL || hasAutoVerticalScrollbar()); }
269
270 virtual IntRect localCaretRect(InlineBox*, int caretOffset, int* extraWidthToEndOfLine = 0);
271
272 virtual void paintFillLayerExtended(const PaintInfo&, const Color&, const FillLayer*, int clipY, int clipHeight,
273 int tx, int ty, int width, int height, InlineFlowBox* = 0, CompositeOperator = CompositeSourceOver);
274 IntSize calculateBackgroundSize(const FillLayer*, int scaledWidth, int scaledHeight) const;
275
276 virtual int staticX() const;
277 virtual int staticY() const;
278 virtual void setStaticX(int staticX);
279 virtual void setStaticY(int staticY);
280
281 virtual IntRect getOverflowClipRect(int tx, int ty);
282 virtual IntRect getClipRect(int tx, int ty);
283
284 virtual void paintBoxDecorations(PaintInfo&, int tx, int ty);
285 virtual void paintMask(PaintInfo& paintInfo, int tx, int ty);
286 virtual void imageChanged(WrappedImagePtr, const IntRect* = 0);
287
288 // Called when a positioned object moves but doesn't change size. A simplified layout is done
289 // that just updates the object's position.
tryLayoutDoingPositionedMovementOnly()290 virtual void tryLayoutDoingPositionedMovementOnly()
291 {
292 int oldWidth = width();
293 calcWidth();
294 // If we shrink to fit our width may have changed, so we still need full layout.
295 if (oldWidth != width())
296 return;
297 calcHeight();
298 setNeedsLayout(false);
299 }
300
301 IntRect maskClipRect();
302
303 #if ENABLE(SVG)
304 virtual TransformationMatrix localTransform() const;
305 #endif
306
307 protected:
308 virtual void styleWillChange(RenderStyle::Diff, const RenderStyle* newStyle);
309 virtual void styleDidChange(RenderStyle::Diff, const RenderStyle* oldStyle);
310
311 void paintFillLayer(const PaintInfo&, const Color&, const FillLayer*, int clipY, int clipHeight, int tx, int ty, int width, int height, CompositeOperator = CompositeSourceOver);
312 void paintFillLayers(const PaintInfo&, const Color&, const FillLayer*, int clipY, int clipHeight, int tx, int ty, int width, int height, CompositeOperator = CompositeSourceOver);
313
314 void paintMaskImages(const PaintInfo&, int clipY, int clipHeight, int tx, int ty, int width, int height);
315
316 #if PLATFORM(MAC)
317 void paintCustomHighlight(int tx, int ty, const AtomicString& type, bool behindText);
318 #endif
319
320 void calcAbsoluteHorizontal();
321
shouldCalculateSizeAsReplaced()322 virtual bool shouldCalculateSizeAsReplaced() const { return isReplaced() && !isInlineBlockOrInlineTable(); }
323
324 private:
includeVerticalScrollbarSize()325 bool includeVerticalScrollbarSize() const { return hasOverflowClip() && (style()->overflowY() == OSCROLL || style()->overflowY() == OAUTO); }
includeHorizontalScrollbarSize()326 bool includeHorizontalScrollbarSize() const { return hasOverflowClip() && (style()->overflowX() == OSCROLL || style()->overflowX() == OAUTO); }
327
328 void paintRootBoxDecorations(PaintInfo&, int tx, int ty);
329 // Returns true if we did a full repaint
330 bool repaintLayerRectsForImage(WrappedImagePtr image, const FillLayer* layers, bool drawingBackground);
331
332 void calculateBackgroundImageGeometry(const FillLayer*, int tx, int ty, int w, int h, IntRect& destRect, IntPoint& phase, IntSize& tileSize);
333
334 int containingBlockWidthForPositioned(const RenderObject* containingBlock) const;
335 int containingBlockHeightForPositioned(const RenderObject* containingBlock) const;
336
337 void calcAbsoluteVertical();
338 void calcAbsoluteHorizontalValues(Length width, const RenderBox* cb, TextDirection containerDirection,
339 int containerWidth, int bordersPlusPadding,
340 Length left, Length right, Length marginLeft, Length marginRight,
341 int& widthValue, int& marginLeftValue, int& marginRightValue, int& xPos);
342 void calcAbsoluteVerticalValues(Length height, const RenderBox* cb,
343 int containerHeight, int bordersPlusPadding,
344 Length top, Length bottom, Length marginTop, Length marginBottom,
345 int& heightValue, int& marginTopValue, int& marginBottomValue, int& yPos);
346
347 void calcAbsoluteVerticalReplaced();
348 void calcAbsoluteHorizontalReplaced();
349
350 // This function calculates the minimum and maximum preferred widths for an object.
351 // These values are used in shrink-to-fit layout systems.
352 // These include tables, positioned objects, floats and flexible boxes.
353 virtual void calcPrefWidths() = 0;
354
355 private:
356 // The width/height of the contents + borders + padding. The x/y location is relative to our container (which is not always our parent).
357 IntRect m_frameRect;
358
359 protected:
360
361 #ifdef ANDROID_LAYOUT
362 int m_visibleWidth;
363 #endif
364
365 int m_marginLeft;
366 int m_marginRight;
367 int m_marginTop;
368 int m_marginBottom;
369
370 // The preferred width of the element if it were to break its lines at every possible opportunity.
371 int m_minPrefWidth;
372
373 // The preferred width of the element if it never breaks any lines at all.
374 int m_maxPrefWidth;
375
376 // A pointer to our layer if we have one.
377 RenderLayer* m_layer;
378
379 // For inline replaced elements, the inline box that owns us.
380 InlineBox* m_inlineBoxWrapper;
381
382 private:
383 // Used to store state between styleWillChange and styleDidChange
384 static bool s_wasFloating;
385 static bool s_hadOverflowClip;
386 };
387
toRenderBox(RenderObject * o)388 inline RenderBox* toRenderBox(RenderObject* o)
389 {
390 ASSERT(!o || o->isBox());
391 return static_cast<RenderBox*>(o);
392 }
393
toRenderBox(const RenderObject * o)394 inline const RenderBox* toRenderBox(const RenderObject* o)
395 {
396 ASSERT(!o || o->isBox());
397 return static_cast<const RenderBox*>(o);
398 }
399
previousSiblingBox()400 inline RenderBox* RenderBox::previousSiblingBox() const
401 {
402 return toRenderBox(previousSibling());
403 }
404
nextSiblingBox()405 inline RenderBox* RenderBox::nextSiblingBox() const
406 {
407 return toRenderBox(nextSibling());
408 }
409
parentBox()410 inline RenderBox* RenderBox::parentBox() const
411 {
412 return toRenderBox(parent());
413 }
414
415 } // namespace WebCore
416
417 #endif // RenderBox_h
418