2 * This file is part of the render object implementation for KHTML.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2007 David Smith (catfish.man@gmail.com)
7 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
28 #include "DeprecatedPtrList.h"
30 #include "RenderFlow.h"
31 #include "RootInlineBox.h"
32 #include <wtf/ListHashSet.h>
41 template <class Iterator, class Run> class BidiResolver;
42 typedef BidiResolver<BidiIterator, BidiRun> BidiState;
44 enum CaretType { CursorCaret, DragCaret };
46 class RenderBlock : public RenderFlow {
49 virtual ~RenderBlock();
51 virtual const char* renderName() const;
53 // These two functions are overridden for inline-block.
54 virtual short lineHeight(bool firstLine, bool isRootLineBox = false) const;
55 virtual short baselinePosition(bool firstLine, bool isRootLineBox = false) const;
57 virtual bool isRenderBlock() const { return true; }
58 virtual bool isBlockFlow() const { return (!isInline() || isReplaced()) && !isTable(); }
59 virtual bool isInlineFlow() const { return isInline() && !isReplaced(); }
60 virtual bool isInlineBlockOrInlineTable() const { return isInline() && isReplaced(); }
62 virtual bool childrenInline() const { return m_childrenInline; }
63 virtual void setChildrenInline(bool b) { m_childrenInline = b; }
64 void makeChildrenNonInline(RenderObject* insertionPoint = 0);
65 void deleteLineBoxTree();
67 // The height (and width) of a block when you include overflow spillage out of the bottom
68 // of the block (e.g., a <div style="height:25px"> that has a 100px tall image inside
69 // it would have an overflow height of borderTop() + paddingTop() + 100px.
70 virtual int overflowHeight(bool includeInterior = true) const;
71 virtual int overflowWidth(bool includeInterior = true) const;
72 virtual int overflowLeft(bool includeInterior = true) const;
73 virtual int overflowTop(bool includeInterior = true) const;
74 virtual IntRect overflowRect(bool includeInterior = true) const;
75 virtual void setOverflowHeight(int h) { m_overflowHeight = h; }
76 virtual void setOverflowWidth(int w) { m_overflowWidth = w; }
78 void addVisualOverflow(const IntRect&);
80 virtual bool isSelfCollapsingBlock() const;
81 virtual bool isTopMarginQuirk() const { return m_topMarginQuirk; }
82 virtual bool isBottomMarginQuirk() const { return m_bottomMarginQuirk; }
84 virtual int maxTopMargin(bool positive) const { return positive ? maxTopPosMargin() : maxTopNegMargin(); }
85 virtual int maxBottomMargin(bool positive) const { return positive ? maxBottomPosMargin() : maxBottomNegMargin(); }
87 int maxTopPosMargin() const { return m_maxMargin ? m_maxMargin->m_topPos : MaxMargin::topPosDefault(this); }
88 int maxTopNegMargin() const { return m_maxMargin ? m_maxMargin->m_topNeg : MaxMargin::topNegDefault(this); }
89 int maxBottomPosMargin() const { return m_maxMargin ? m_maxMargin->m_bottomPos : MaxMargin::bottomPosDefault(this); }
90 int maxBottomNegMargin() const { return m_maxMargin ? m_maxMargin->m_bottomNeg : MaxMargin::bottomNegDefault(this); }
91 void setMaxTopMargins(int pos, int neg);
92 void setMaxBottomMargins(int pos, int neg);
94 void initMaxMarginValues()
97 m_maxMargin->m_topPos = MaxMargin::topPosDefault(this);
98 m_maxMargin->m_topNeg = MaxMargin::topNegDefault(this);
99 m_maxMargin->m_bottomPos = MaxMargin::bottomPosDefault(this);
100 m_maxMargin->m_bottomNeg = MaxMargin::bottomNegDefault(this);
104 virtual void addChildToFlow(RenderObject* newChild, RenderObject* beforeChild);
105 virtual void removeChild(RenderObject*);
107 virtual void repaintOverhangingFloats(bool paintAllDescendants);
109 virtual void setStyle(RenderStyle*);
111 virtual void layout();
112 virtual void layoutBlock(bool relayoutChildren);
113 void layoutBlockChildren(bool relayoutChildren, int& maxFloatBottom);
114 void layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom);
116 void layoutPositionedObjects(bool relayoutChildren);
117 void insertPositionedObject(RenderObject*);
118 void removePositionedObject(RenderObject*);
119 virtual void removePositionedObjects(RenderBlock*);
121 virtual void positionListMarker() { }
123 virtual void borderFitAdjust(int& x, int& w) const; // Shrink the box in which the border paints if border-fit is set.
125 // Called to lay out the legend for a fieldset.
126 virtual RenderObject* layoutLegend(bool relayoutChildren) { return 0; };
128 // the implementation of the following functions is in bidi.cpp
129 void bidiReorderLine(const BidiIterator& start, const BidiIterator& end, BidiState&);
130 RootInlineBox* determineStartPosition(bool fullLayout, BidiIterator& start, BidiState&);
131 RootInlineBox* determineEndPosition(RootInlineBox* startBox, BidiIterator& cleanLineStart,
132 BidiStatus& cleanLineBidiStatus,
134 bool matchedEndLine(const BidiIterator& start, const BidiStatus& status,
135 const BidiIterator& endLineStart, const BidiStatus& endLineStatus,
136 RootInlineBox*& endLine, int& endYPos, int& repaintBottom, int& repaintTop);
137 bool generatesLineBoxesForInlineChild(RenderObject*);
138 int skipWhitespace(BidiIterator&, BidiState&);
139 void fitBelowFloats(int widthToFit, int& availableWidth);
140 BidiIterator findNextLineBreak(BidiIterator& start, BidiState& info);
141 RootInlineBox* constructLine(const BidiIterator& start, const BidiIterator& end);
142 InlineFlowBox* createLineBoxes(RenderObject*);
143 void computeHorizontalPositionsForLine(RootInlineBox*, bool reachedEnd);
144 void computeVerticalPositionsForLine(RootInlineBox*);
145 void checkLinesForOverflow();
146 void deleteEllipsisLineBoxes();
147 void checkLinesForTextOverflow();
148 // end bidi.cpp functions
150 virtual void paint(PaintInfo&, int tx, int ty);
151 virtual void paintObject(PaintInfo&, int tx, int ty);
152 void paintFloats(PaintInfo&, int tx, int ty, bool paintSelection = false);
153 void paintContents(PaintInfo&, int tx, int ty);
154 void paintColumns(PaintInfo&, int tx, int ty, bool paintFloats = false);
155 void paintChildren(PaintInfo&, int tx, int ty);
156 void paintEllipsisBoxes(PaintInfo&, int tx, int ty);
157 void paintSelection(PaintInfo&, int tx, int ty);
158 void paintCaret(PaintInfo&, CaretType);
160 void insertFloatingObject(RenderObject*);
161 void removeFloatingObject(RenderObject*);
163 // called from lineWidth, to position the floats added in the last line.
164 void positionNewFloats();
166 int getClearDelta(RenderObject* child);
167 virtual void markAllDescendantsWithFloatsForLayout(RenderObject* floatToRemove = 0);
168 void markPositionedObjectsForLayout();
170 // FIXME: containsFloats() should not return true if the floating objects list
171 // is empty. However, layoutInlineChildren() relies on the current behavior.
172 // http://bugs.webkit.org/show_bug.cgi?id=7395#c3
173 virtual bool containsFloats() { return m_floatingObjects; }
174 virtual bool containsFloat(RenderObject*);
176 virtual bool avoidsFloats() const;
178 virtual bool hasOverhangingFloats() { return !hasColumns() && floatBottom() > m_height; }
179 void addIntrudingFloats(RenderBlock* prev, int xoffset, int yoffset);
180 int addOverhangingFloats(RenderBlock* child, int xoffset, int yoffset, bool makeChildPaintOtherFloats);
182 int nextFloatBottomBelow(int) const;
183 int floatBottom() const;
184 inline int leftBottom();
185 inline int rightBottom();
186 IntRect floatRect() const;
188 virtual int lineWidth(int y) const;
189 virtual int lowestPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
190 virtual int rightmostPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
191 virtual int leftmostPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
193 int rightOffset() const;
194 int rightRelOffset(int y, int fixedOffset, bool applyTextIndent = true, int* heightRemaining = 0) const;
195 int rightOffset(int y) const { return rightRelOffset(y, rightOffset(), true); }
197 int leftOffset() const;
198 int leftRelOffset(int y, int fixedOffset, bool applyTextIndent = true, int* heightRemaining = 0) const;
199 int leftOffset(int y) const { return leftRelOffset(y, leftOffset(), true); }
201 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
202 virtual bool hitTestColumns(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
203 virtual bool hitTestContents(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
205 virtual bool isPointInOverflowControl(HitTestResult&, int x, int y, int tx, int ty);
207 virtual VisiblePosition positionForCoordinates(int x, int y);
209 // Block flows subclass availableWidth to handle multi column layout (shrinking the width available to children when laying out.)
210 virtual int availableWidth() const;
212 virtual void calcPrefWidths();
213 void calcInlinePrefWidths();
214 void calcBlockPrefWidths();
216 virtual int getBaselineOfFirstLineBox() const;
217 virtual int getBaselineOfLastLineBox() const;
219 RootInlineBox* firstRootBox() const { return static_cast<RootInlineBox*>(firstLineBox()); }
220 RootInlineBox* lastRootBox() const { return static_cast<RootInlineBox*>(lastLineBox()); }
222 // Obtains the nearest enclosing block (including this block) that contributes a first-line style to our inline
224 virtual RenderBlock* firstLineBlock() const;
225 virtual void updateFirstLetter();
227 bool inRootBlockContext() const;
229 void setHasMarkupTruncation(bool b = true) { m_hasMarkupTruncation = b; }
230 bool hasMarkupTruncation() const { return m_hasMarkupTruncation; }
232 virtual bool hasSelectedChildren() const { return m_selectionState != SelectionNone; }
233 virtual SelectionState selectionState() const { return static_cast<SelectionState>(m_selectionState); }
234 virtual void setSelectionState(SelectionState s);
236 struct BlockSelectionInfo {
237 RenderBlock* m_block;
239 SelectionState m_state;
243 , m_state(SelectionNone)
247 BlockSelectionInfo(RenderBlock* b)
249 , m_rects(b->needsLayout() ? GapRects() : b->selectionGapRects())
250 , m_state(b->selectionState())
254 RenderBlock* block() const { return m_block; }
255 GapRects rects() const { return m_rects; }
256 SelectionState state() const { return m_state; }
259 virtual IntRect selectionRect(bool) { return selectionGapRects(); }
260 GapRects selectionGapRects();
261 virtual bool shouldPaintSelectionGaps() const;
262 bool isSelectionRoot() const;
263 GapRects fillSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
264 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* = 0);
265 GapRects fillInlineSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
266 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo*);
267 GapRects fillBlockSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
268 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo*);
269 IntRect fillVerticalSelectionGap(int lastTop, int lastLeft, int lastRight, int bottomY, RenderBlock* rootBlock,
270 int blockX, int blockY, const PaintInfo*);
271 IntRect fillLeftSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock,
272 int blockX, int blockY, int tx, int ty, const PaintInfo*);
273 IntRect fillRightSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock,
274 int blockX, int blockY, int tx, int ty, const PaintInfo*);
275 IntRect fillHorizontalSelectionGap(RenderObject* selObj, int xPos, int yPos, int width, int height, const PaintInfo*);
277 void getHorizontalSelectionGapInfo(SelectionState, bool& leftGap, bool& rightGap);
278 int leftSelectionOffset(RenderBlock* rootBlock, int y);
279 int rightSelectionOffset(RenderBlock* rootBlock, int y);
281 // Helper methods for computing line counts and heights for line counts.
282 RootInlineBox* lineAtIndex(int);
284 int heightForLineCount(int);
285 void clearTruncation();
287 int desiredColumnWidth() const;
288 unsigned desiredColumnCount() const;
289 Vector<IntRect>* columnRects() const;
290 void setDesiredColumnCountAndWidth(int count, int width);
292 void adjustRectForColumns(IntRect&) const;
294 void addContinuationWithOutline(RenderFlow*);
295 void paintContinuationOutlines(PaintInfo&, int tx, int ty);
298 void adjustPointToColumnContents(IntPoint&) const;
299 void adjustForBorderFit(int x, int& left, int& right) const; // Helper function for borderFitAdjust
303 virtual bool hasLineIfEmpty() const;
304 bool layoutOnlyPositionedObjects();
307 Position positionForBox(InlineBox*, bool start = true) const;
308 Position positionForRenderer(RenderObject*, bool start = true) const;
310 int columnGap() const;
311 void calcColumnWidth();
312 int layoutColumns(int endOfContent = -1);
315 struct FloatingObject {
321 FloatingObject(Type type)
332 Type type() { return static_cast<Type>(m_type); }
339 unsigned m_type : 1; // Type (left or right aligned)
343 // The following helper functions and structs are used by layoutBlockChildren.
345 // A compact child that needs to be collapsed into the margin of the following block.
346 RenderObject* m_compact;
348 // The block with the open margin that the compact child is going to place itself within.
349 RenderObject* m_block;
352 RenderObject* compact() const { return m_compact; }
353 RenderObject* block() const { return m_block; }
354 bool matches(RenderObject* child) const { return m_compact && m_block == child; }
356 void clear() { set(0, 0); }
357 void set(RenderObject* c, RenderObject* b) { m_compact = c; m_block = b; }
359 CompactInfo() { clear(); }
363 // Collapsing flags for whether we can collapse our margins with our children's margins.
364 bool m_canCollapseWithChildren : 1;
365 bool m_canCollapseTopWithChildren : 1;
366 bool m_canCollapseBottomWithChildren : 1;
368 // Whether or not we are a quirky container, i.e., do we collapse away top and bottom
369 // margins in our container. Table cells and the body are the common examples. We
370 // also have a custom style property for Safari RSS to deal with TypePad blog articles.
371 bool m_quirkContainer : 1;
373 // This flag tracks whether we are still looking at child margins that can all collapse together at the beginning of a block.
374 // They may or may not collapse with the top margin of the block (|m_canCollapseTopWithChildren| tells us that), but they will
375 // always be collapsing with one another. This variable can remain set to true through multiple iterations
376 // as long as we keep encountering self-collapsing blocks.
377 bool m_atTopOfBlock : 1;
379 // This flag is set when we know we're examining bottom margins and we know we're at the bottom of the block.
380 bool m_atBottomOfBlock : 1;
382 // If our last normal flow child was a self-collapsing block that cleared a float,
383 // we track it in this variable.
384 bool m_selfCollapsingBlockClearedFloat : 1;
386 // These variables are used to detect quirky margins that we need to collapse away (in table cells
387 // and in the body element).
389 bool m_bottomQuirk : 1;
390 bool m_determinedTopQuirk : 1;
392 // These flags track the previous maximal positive and negative margins.
397 MarginInfo(RenderBlock* b, int top, int bottom);
399 void setAtTopOfBlock(bool b) { m_atTopOfBlock = b; }
400 void setAtBottomOfBlock(bool b) { m_atBottomOfBlock = b; }
401 void clearMargin() { m_posMargin = m_negMargin = 0; }
402 void setSelfCollapsingBlockClearedFloat(bool b) { m_selfCollapsingBlockClearedFloat = b; }
403 void setTopQuirk(bool b) { m_topQuirk = b; }
404 void setBottomQuirk(bool b) { m_bottomQuirk = b; }
405 void setDeterminedTopQuirk(bool b) { m_determinedTopQuirk = b; }
406 void setPosMargin(int p) { m_posMargin = p; }
407 void setNegMargin(int n) { m_negMargin = n; }
408 void setPosMarginIfLarger(int p) { if (p > m_posMargin) m_posMargin = p; }
409 void setNegMarginIfLarger(int n) { if (n > m_negMargin) m_negMargin = n; }
411 void setMargin(int p, int n) { m_posMargin = p; m_negMargin = n; }
413 bool atTopOfBlock() const { return m_atTopOfBlock; }
414 bool canCollapseWithTop() const { return m_atTopOfBlock && m_canCollapseTopWithChildren; }
415 bool canCollapseWithBottom() const { return m_atBottomOfBlock && m_canCollapseBottomWithChildren; }
416 bool canCollapseTopWithChildren() const { return m_canCollapseTopWithChildren; }
417 bool canCollapseBottomWithChildren() const { return m_canCollapseBottomWithChildren; }
418 bool selfCollapsingBlockClearedFloat() const { return m_selfCollapsingBlockClearedFloat; }
419 bool quirkContainer() const { return m_quirkContainer; }
420 bool determinedTopQuirk() const { return m_determinedTopQuirk; }
421 bool topQuirk() const { return m_topQuirk; }
422 bool bottomQuirk() const { return m_bottomQuirk; }
423 int posMargin() const { return m_posMargin; }
424 int negMargin() const { return m_negMargin; }
425 int margin() const { return m_posMargin - m_negMargin; }
428 void adjustPositionedBlock(RenderObject* child, const MarginInfo&);
429 void adjustFloatingBlock(const MarginInfo&);
430 RenderObject* handleSpecialChild(RenderObject* child, const MarginInfo&, CompactInfo&, bool& handled);
431 RenderObject* handleFloatingChild(RenderObject* child, const MarginInfo&, bool& handled);
432 RenderObject* handlePositionedChild(RenderObject* child, const MarginInfo&, bool& handled);
433 RenderObject* handleCompactChild(RenderObject* child, CompactInfo&, bool& handled);
434 RenderObject* handleRunInChild(RenderObject* child, bool& handled);
435 void collapseMargins(RenderObject* child, MarginInfo&, int yPosEstimate);
436 void clearFloatsIfNeeded(RenderObject* child, MarginInfo&, int oldTopPosMargin, int oldTopNegMargin);
437 void insertCompactIfNeeded(RenderObject* child, CompactInfo&);
438 int estimateVerticalPosition(RenderObject* child, const MarginInfo&);
439 void determineHorizontalPosition(RenderObject* child);
440 void handleBottomOfBlock(int top, int bottom, MarginInfo&);
441 void setCollapsedBottomMargin(const MarginInfo&);
442 // End helper functions and structs used by layoutBlockChildren.
445 typedef ListHashSet<RenderObject*>::const_iterator Iterator;
446 DeprecatedPtrList<FloatingObject>* m_floatingObjects;
447 ListHashSet<RenderObject*>* m_positionedObjects;
449 // Allocated only when some of these fields have non-default values
451 MaxMargin(const RenderBlock* o)
452 : m_topPos(topPosDefault(o))
453 , m_topNeg(topNegDefault(o))
454 , m_bottomPos(bottomPosDefault(o))
455 , m_bottomNeg(bottomNegDefault(o))
458 static int topPosDefault(const RenderBlock* o) { return o->marginTop() > 0 ? o->marginTop() : 0; }
459 static int topNegDefault(const RenderBlock* o) { return o->marginTop() < 0 ? -o->marginTop() : 0; }
460 static int bottomPosDefault(const RenderBlock* o) { return o->marginBottom() > 0 ? o->marginBottom() : 0; }
461 static int bottomNegDefault(const RenderBlock* o) { return o->marginBottom() < 0 ? -o->marginBottom() : 0; }
469 MaxMargin* m_maxMargin;
472 // How much content overflows out of our block vertically or horizontally.
473 int m_overflowHeight;
479 } // namespace WebCore
481 #endif // RenderBlock_h