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 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
27 #include "DeprecatedPtrList.h"
29 #include "RenderFlow.h"
30 #include "RootInlineBox.h"
36 enum CaretType { CursorCaret, DragCaret };
38 class RenderBlock : public RenderFlow {
40 typedef Vector<UChar, 1024> CharacterBuffer;
41 static void bidiReorderCharacters(Document*, RenderStyle*, CharacterBuffer&);
44 virtual ~RenderBlock();
46 virtual const char* renderName() const;
48 // These two functions are overridden for inline-block.
49 virtual short lineHeight(bool firstLine, bool isRootLineBox = false) const;
50 virtual short baselinePosition(bool firstLine, bool isRootLineBox = false) const;
52 virtual bool isRenderBlock() const { return true; }
53 virtual bool isBlockFlow() const { return (!isInline() || isReplaced()) && !isTable(); }
54 virtual bool isInlineFlow() const { return isInline() && !isReplaced(); }
55 virtual bool isInlineBlockOrInlineTable() const { return isInline() && isReplaced(); }
57 virtual bool childrenInline() const { return m_childrenInline; }
58 virtual void setChildrenInline(bool b) { m_childrenInline = b; }
59 void makeChildrenNonInline(RenderObject* insertionPoint = 0);
60 void deleteLineBoxTree();
62 // The height (and width) of a block when you include overflow spillage out of the bottom
63 // of the block (e.g., a <div style="height:25px"> that has a 100px tall image inside
64 // it would have an overflow height of borderTop() + paddingTop() + 100px.
65 virtual int overflowHeight(bool includeInterior = true) const;
66 virtual int overflowWidth(bool includeInterior = true) const;
67 virtual int overflowLeft(bool includeInterior = true) const;
68 virtual int overflowTop(bool includeInterior = true) const;
69 virtual IntRect overflowRect(bool includeInterior = true) const;
70 virtual void setOverflowHeight(int h) { m_overflowHeight = h; }
71 virtual void setOverflowWidth(int w) { m_overflowWidth = w; }
73 void addVisualOverflow(const IntRect&);
75 virtual bool isSelfCollapsingBlock() const;
76 virtual bool isTopMarginQuirk() const { return m_topMarginQuirk; }
77 virtual bool isBottomMarginQuirk() const { return m_bottomMarginQuirk; }
79 virtual int maxTopMargin(bool positive) const { return positive ? m_maxTopPosMargin : m_maxTopNegMargin; }
80 virtual int maxBottomMargin(bool positive) const { return positive ? m_maxBottomPosMargin : m_maxBottomNegMargin; }
82 void initMaxMarginValues()
84 int margTop = marginTop();
86 m_maxTopPosMargin = margTop;
87 m_maxTopNegMargin = 0;
89 m_maxTopNegMargin = -margTop;
90 m_maxTopPosMargin = 0;
92 int margBottom = marginBottom();
93 if (margBottom >= 0) {
94 m_maxBottomPosMargin = margBottom;
95 m_maxBottomNegMargin = 0;
97 m_maxBottomNegMargin = -margBottom;
98 m_maxBottomPosMargin = 0;
102 virtual void addChildToFlow(RenderObject* newChild, RenderObject* beforeChild);
103 virtual void removeChild(RenderObject*);
105 virtual void repaintObjectsBeforeLayout();
106 virtual void repaintOverhangingFloats(bool paintAllDescendants);
108 virtual void setStyle(RenderStyle*);
110 virtual void layout();
111 virtual void layoutBlock(bool relayoutChildren);
112 void layoutBlockChildren(bool relayoutChildren);
113 IntRect layoutInlineChildren(bool relayoutChildren);
115 void layoutPositionedObjects(bool relayoutChildren);
116 void insertPositionedObject(RenderObject*);
117 void removePositionedObject(RenderObject*);
118 virtual void removePositionedObjects(RenderBlock*);
120 virtual void positionListMarker() { }
122 virtual void borderFitAdjust(int& x, int& w) const; // Shrink the box in which the border paints if border-fit is set.
124 // Called to lay out the legend for a fieldset.
125 virtual RenderObject* layoutLegend(bool relayoutChildren) { return 0; };
127 // the implementation of the following functions is in bidi.cpp
128 void bidiReorderLine(const BidiIterator& start, const BidiIterator& end, BidiState& bidi);
129 RootInlineBox* determineStartPosition(bool fullLayout, BidiIterator& start, BidiState& bidi);
130 RootInlineBox* determineEndPosition(RootInlineBox* startBox, BidiIterator& cleanLineStart,
131 BidiStatus& cleanLineBidiStatus, BidiContext*& cleanLineBidiContext,
133 bool matchedEndLine(const BidiIterator& start, const BidiStatus& status, BidiContext* context,
134 const BidiIterator& endLineStart, const BidiStatus& endLineStatus, BidiContext* endLineContext,
135 RootInlineBox*& endLine, int& endYPos, int& repaintBottom, int& repaintTop);
136 int skipWhitespace(BidiIterator&, BidiState&);
137 BidiIterator findNextLineBreak(BidiIterator& start, BidiState& info);
138 RootInlineBox* constructLine(const BidiIterator& start, const BidiIterator& end);
139 InlineFlowBox* createLineBoxes(RenderObject*);
140 int tabWidth(bool isWhitespacePre);
141 void computeHorizontalPositionsForLine(RootInlineBox*, BidiState&);
142 void computeVerticalPositionsForLine(RootInlineBox*);
143 void checkLinesForOverflow();
144 void deleteEllipsisLineBoxes();
145 void checkLinesForTextOverflow();
146 // end bidi.cpp functions
148 virtual void paint(PaintInfo&, int tx, int ty);
149 virtual void paintObject(PaintInfo&, int tx, int ty);
150 void paintFloats(PaintInfo&, int tx, int ty, bool paintSelection = false);
151 void paintContents(PaintInfo&, int tx, int ty);
152 void paintColumns(PaintInfo&, int tx, int ty, bool paintFloats = false);
153 void paintChildren(PaintInfo&, int tx, int ty);
154 void paintEllipsisBoxes(PaintInfo&, int tx, int ty);
155 void paintSelection(PaintInfo&, int tx, int ty);
156 void paintCaret(PaintInfo&, CaretType);
158 void insertFloatingObject(RenderObject*);
159 void removeFloatingObject(RenderObject*);
160 void setPaintsFloatingObject(RenderObject*, bool);
162 // called from lineWidth, to position the floats added in the last line.
163 void positionNewFloats();
165 int getClearDelta(RenderObject* child);
166 virtual void markAllDescendantsWithFloatsForLayout(RenderObject* floatToRemove = 0);
167 void markPositionedObjectsForLayout();
169 // FIXME: containsFloats() should not return true if the floating objects list
170 // is empty. However, layoutInlineChildren() relies on the current behavior.
171 // http://bugs.webkit.org/show_bug.cgi?id=7395#c3
172 virtual bool containsFloats() { return m_floatingObjects; }
173 virtual bool containsFloat(RenderObject*);
175 virtual bool avoidsFloats() const;
177 virtual bool hasOverhangingFloats() { return !hasColumns() && floatBottom() > m_height; }
178 void addIntrudingFloats(RenderBlock* prev, int xoffset, int yoffset);
179 void addOverhangingFloats(RenderBlock* child, int xoffset, int yoffset);
181 int nearestFloatBottom(int height) const;
182 int floatBottom() const;
183 inline int leftBottom();
184 inline int rightBottom();
185 IntRect floatRect() const;
187 virtual int lineWidth(int y) const;
188 virtual int lowestPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
189 virtual int rightmostPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
190 virtual int leftmostPosition(bool includeOverflowInterior = true, bool includeSelf = true) const;
192 int rightOffset() const;
193 int rightRelOffset(int y, int fixedOffset, bool applyTextIndent = true, int* heightRemaining = 0) const;
194 int rightOffset(int y) const { return rightRelOffset(y, rightOffset(), true); }
196 int leftOffset() const;
197 int leftRelOffset(int y, int fixedOffset, bool applyTextIndent = true, int* heightRemaining = 0) const;
198 int leftOffset(int y) const { return leftRelOffset(y, leftOffset(), true); }
200 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
201 virtual bool hitTestColumns(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
202 virtual bool hitTestContents(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
204 virtual bool isPointInScrollbar(HitTestResult&, int x, int y, int tx, int ty);
206 virtual VisiblePosition positionForCoordinates(int x, int y);
208 // Block flows subclass availableWidth to handle multi column layout (shrinking the width available to children when laying out.)
209 virtual int availableWidth() const;
211 virtual void calcMinMaxWidth();
212 void calcInlineMinMaxWidth();
213 void calcBlockMinMaxWidth();
215 virtual int getBaselineOfFirstLineBox() const;
216 virtual int getBaselineOfLastLineBox() const;
218 RootInlineBox* firstRootBox() const { return static_cast<RootInlineBox*>(firstLineBox()); }
219 RootInlineBox* lastRootBox() const { return static_cast<RootInlineBox*>(lastLineBox()); }
221 // Obtains the nearest enclosing block (including this block) that contributes a first-line style to our inline
223 virtual RenderBlock* firstLineBlock() const;
224 virtual void updateFirstLetter();
226 bool inRootBlockContext() const;
228 void setHasMarkupTruncation(bool b = true) { m_hasMarkupTruncation = b; }
229 bool hasMarkupTruncation() const { return m_hasMarkupTruncation; }
231 virtual bool hasSelectedChildren() const { return m_selectionState != SelectionNone; }
232 virtual SelectionState selectionState() const { return static_cast<SelectionState>(m_selectionState); }
233 virtual void setSelectionState(SelectionState s);
235 struct BlockSelectionInfo {
236 RenderBlock* m_block;
238 SelectionState m_state;
242 , m_state(SelectionNone)
246 BlockSelectionInfo(RenderBlock* b)
248 , m_rects(b->selectionGapRects())
249 , m_state(b->selectionState())
253 RenderBlock* block() const { return m_block; }
254 GapRects rects() const { return m_rects; }
255 SelectionState state() const { return m_state; }
258 virtual IntRect selectionRect() { return selectionGapRects(); }
259 GapRects selectionGapRects();
260 virtual bool shouldPaintSelectionGaps() const;
261 bool isSelectionRoot() const;
262 GapRects fillSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
263 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* = 0);
264 GapRects fillInlineSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
265 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo*);
266 GapRects fillBlockSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
267 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo*);
268 IntRect fillVerticalSelectionGap(int lastTop, int lastLeft, int lastRight, int bottomY, RenderBlock* rootBlock,
269 int blockX, int blockY, const PaintInfo*);
270 IntRect fillLeftSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock,
271 int blockX, int blockY, int tx, int ty, const PaintInfo*);
272 IntRect fillRightSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock,
273 int blockX, int blockY, int tx, int ty, const PaintInfo*);
274 IntRect fillHorizontalSelectionGap(RenderObject* selObj, int xPos, int yPos, int width, int height, const PaintInfo*);
276 void getHorizontalSelectionGapInfo(SelectionState, bool& leftGap, bool& rightGap);
277 int leftSelectionOffset(RenderBlock* rootBlock, int y);
278 int rightSelectionOffset(RenderBlock* rootBlock, int y);
281 virtual void dump(TextStream*, DeprecatedString ind = "") const;
284 // Helper methods for computing line counts and heights for line counts.
285 RootInlineBox* lineAtIndex(int);
287 int heightForLineCount(int);
288 void clearTruncation();
290 virtual bool hasColumns() const { return m_desiredColumnCount > 1; }
291 void adjustRectForColumns(IntRect&) const;
293 void adjustPointToColumnContents(IntPoint&) const;
294 void adjustForBorderFit(int x, int& left, int& right) const; // Helper function for borderFitAdjust
298 virtual bool hasLineIfEmpty() const;
301 Position positionForBox(InlineBox*, bool start = true) const;
302 Position positionForRenderer(RenderObject*, bool start = true) const;
304 int columnGap() const;
305 void calcColumnWidth();
306 int layoutColumns(int endOfContent = -1);
309 struct FloatingObject {
315 FloatingObject(Type type)
326 Type type() { return static_cast<Type>(m_type); }
333 unsigned m_type : 1; // Type (left or right aligned)
337 // The following helper functions and structs are used by layoutBlockChildren.
339 // A compact child that needs to be collapsed into the margin of the following block.
340 RenderObject* m_compact;
342 // The block with the open margin that the compact child is going to place itself within.
343 RenderObject* m_block;
346 RenderObject* compact() const { return m_compact; }
347 RenderObject* block() const { return m_block; }
348 bool matches(RenderObject* child) const { return m_compact && m_block == child; }
350 void clear() { set(0, 0); }
351 void set(RenderObject* c, RenderObject* b) { m_compact = c; m_block = b; }
353 CompactInfo() { clear(); }
357 // Collapsing flags for whether we can collapse our margins with our children's margins.
358 bool m_canCollapseWithChildren : 1;
359 bool m_canCollapseTopWithChildren : 1;
360 bool m_canCollapseBottomWithChildren : 1;
362 // Whether or not we are a quirky container, i.e., do we collapse away top and bottom
363 // margins in our container. Table cells and the body are the common examples. We
364 // also have a custom style property for Safari RSS to deal with TypePad blog articles.
365 bool m_quirkContainer : 1;
367 // This flag tracks whether we are still looking at child margins that can all collapse together at the beginning of a block.
368 // They may or may not collapse with the top margin of the block (|m_canCollapseTopWithChildren| tells us that), but they will
369 // always be collapsing with one another. This variable can remain set to true through multiple iterations
370 // as long as we keep encountering self-collapsing blocks.
371 bool m_atTopOfBlock : 1;
373 // This flag is set when we know we're examining bottom margins and we know we're at the bottom of the block.
374 bool m_atBottomOfBlock : 1;
376 // If our last normal flow child was a self-collapsing block that cleared a float,
377 // we track it in this variable.
378 bool m_selfCollapsingBlockClearedFloat : 1;
380 // These variables are used to detect quirky margins that we need to collapse away (in table cells
381 // and in the body element).
383 bool m_bottomQuirk : 1;
384 bool m_determinedTopQuirk : 1;
386 // These flags track the previous maximal positive and negative margins.
391 MarginInfo(RenderBlock* b, int top, int bottom);
393 void setAtTopOfBlock(bool b) { m_atTopOfBlock = b; }
394 void setAtBottomOfBlock(bool b) { m_atBottomOfBlock = b; }
395 void clearMargin() { m_posMargin = m_negMargin = 0; }
396 void setSelfCollapsingBlockClearedFloat(bool b) { m_selfCollapsingBlockClearedFloat = b; }
397 void setTopQuirk(bool b) { m_topQuirk = b; }
398 void setBottomQuirk(bool b) { m_bottomQuirk = b; }
399 void setDeterminedTopQuirk(bool b) { m_determinedTopQuirk = b; }
400 void setPosMargin(int p) { m_posMargin = p; }
401 void setNegMargin(int n) { m_negMargin = n; }
402 void setPosMarginIfLarger(int p) { if (p > m_posMargin) m_posMargin = p; }
403 void setNegMarginIfLarger(int n) { if (n > m_negMargin) m_negMargin = n; }
405 void setMargin(int p, int n) { m_posMargin = p; m_negMargin = n; }
407 bool atTopOfBlock() const { return m_atTopOfBlock; }
408 bool canCollapseWithTop() const { return m_atTopOfBlock && m_canCollapseTopWithChildren; }
409 bool canCollapseWithBottom() const { return m_atBottomOfBlock && m_canCollapseBottomWithChildren; }
410 bool canCollapseTopWithChildren() const { return m_canCollapseTopWithChildren; }
411 bool canCollapseBottomWithChildren() const { return m_canCollapseBottomWithChildren; }
412 bool selfCollapsingBlockClearedFloat() const { return m_selfCollapsingBlockClearedFloat; }
413 bool quirkContainer() const { return m_quirkContainer; }
414 bool determinedTopQuirk() const { return m_determinedTopQuirk; }
415 bool topQuirk() const { return m_topQuirk; }
416 bool bottomQuirk() const { return m_bottomQuirk; }
417 int posMargin() const { return m_posMargin; }
418 int negMargin() const { return m_negMargin; }
419 int margin() const { return m_posMargin - m_negMargin; }
422 void adjustPositionedBlock(RenderObject* child, const MarginInfo&);
423 void adjustFloatingBlock(const MarginInfo&);
424 RenderObject* handleSpecialChild(RenderObject* child, const MarginInfo&, CompactInfo&, bool& handled);
425 RenderObject* handleFloatingChild(RenderObject* child, const MarginInfo&, bool& handled);
426 RenderObject* handlePositionedChild(RenderObject* child, const MarginInfo&, bool& handled);
427 RenderObject* handleCompactChild(RenderObject* child, CompactInfo&, bool& handled);
428 RenderObject* handleRunInChild(RenderObject* child, bool& handled);
429 void collapseMargins(RenderObject* child, MarginInfo&, int yPosEstimate);
430 void clearFloatsIfNeeded(RenderObject* child, MarginInfo&, int oldTopPosMargin, int oldTopNegMargin);
431 void insertCompactIfNeeded(RenderObject* child, CompactInfo&);
432 int estimateVerticalPosition(RenderObject* child, const MarginInfo&);
433 void determineHorizontalPosition(RenderObject* child);
434 void handleBottomOfBlock(int top, int bottom, MarginInfo&);
435 void setCollapsedBottomMargin(const MarginInfo&);
436 // End helper functions and structs used by layoutBlockChildren.
439 DeprecatedPtrList<FloatingObject>* m_floatingObjects;
440 DeprecatedPtrList<RenderObject>* m_positionedObjects;
442 bool m_childrenInline : 1;
443 bool m_firstLine : 1;
444 unsigned m_clearStatus : 2; // EClear
445 bool m_topMarginQuirk : 1;
446 bool m_bottomMarginQuirk : 1;
447 bool m_hasMarkupTruncation : 1;
448 unsigned m_selectionState : 3; // SelectionState
451 int m_maxTopPosMargin;
452 int m_maxTopNegMargin;
453 int m_maxBottomPosMargin;
454 int m_maxBottomNegMargin;
456 // How much content overflows out of our block vertically or horizontally.
457 int m_overflowHeight;
463 // full width of a tab character
466 // Column information.
467 int m_desiredColumnWidth;
468 unsigned m_desiredColumnCount;
469 Vector<IntRect>* m_columnRects;
472 } // namespace WebCore
474 #endif // RenderBlock_h