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 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.
25 #ifndef RENDER_BLOCK_H
26 #define RENDER_BLOCK_H
30 #include "render_flow.h"
38 class RenderBlock : public RenderFlow
41 RenderBlock(DOM::NodeImpl* node);
42 virtual ~RenderBlock();
44 virtual const char *renderName() const;
46 // These two functions are overridden for inline-block.
47 virtual short lineHeight(bool b, bool isRootLineBox=false) const;
48 virtual short baselinePosition(bool b, bool isRootLineBox=false) const;
50 virtual bool isRenderBlock() const { return true; }
51 virtual bool isBlockFlow() const { return (!isInline() || isReplaced()) && !isTable(); }
52 virtual bool isInlineFlow() const { return isInline() && !isReplaced(); }
53 virtual bool isInlineBlockOrInlineTable() const { return isInline() && isReplaced(); }
55 virtual bool childrenInline() const { return m_childrenInline; }
56 virtual void setChildrenInline(bool b) { m_childrenInline = b; }
57 void makeChildrenNonInline(RenderObject* insertionPoint = 0);
59 // The height (and width) of a block when you include overflow spillage out of the bottom
60 // of the block (e.g., a <div style="height:25px"> that has a 100px tall image inside
61 // it would have an overflow height of borderTop() + paddingTop() + 100px.
62 virtual int overflowHeight(bool includeInterior=true) const;
63 virtual int overflowWidth(bool includeInterior=true) const;
64 virtual int overflowLeft(bool includeInterior=true) const;
65 virtual int overflowTop(bool includeInterior=true) const;
66 virtual QRect overflowRect(bool includeInterior=true) const;
67 virtual void setOverflowHeight(int h) { m_overflowHeight = h; }
68 virtual void setOverflowWidth(int w) { m_overflowWidth = w; }
70 virtual bool isSelfCollapsingBlock() const;
71 virtual bool isTopMarginQuirk() const { return m_topMarginQuirk; }
72 virtual bool isBottomMarginQuirk() const { return m_bottomMarginQuirk; }
74 virtual int maxTopMargin(bool positive) const {
76 return m_maxTopPosMargin;
78 return m_maxTopNegMargin;
80 virtual int maxBottomMargin(bool positive) const {
82 return m_maxBottomPosMargin;
84 return m_maxBottomNegMargin;
87 void initMaxMarginValues() {
89 m_maxTopPosMargin = m_marginTop;
91 m_maxTopNegMargin = -m_marginTop;
92 if (m_marginBottom >= 0)
93 m_maxBottomPosMargin = m_marginBottom;
95 m_maxBottomNegMargin = -m_marginBottom;
98 virtual void addChildToFlow(RenderObject* newChild, RenderObject* beforeChild);
99 virtual void removeChild(RenderObject *oldChild);
101 virtual void repaintObjectsBeforeLayout();
102 virtual void repaintFloatingDescendants();
103 virtual void getAbsoluteRepaintRectIncludingFloats(QRect& bounds, QRect& fullBounds);
105 virtual void setStyle(RenderStyle* _style);
107 virtual void layout();
108 virtual void layoutBlock(bool relayoutChildren);
109 void layoutBlockChildren(bool relayoutChildren);
110 QRect layoutInlineChildren(bool relayoutChildren);
112 void layoutPositionedObjects( bool relayoutChildren );
113 void insertPositionedObject(RenderObject *o);
114 void removePositionedObject(RenderObject *o);
116 // Called to lay out the legend for a fieldset.
117 virtual RenderObject* layoutLegend(bool relayoutChildren) { return 0; };
119 // the implementation of the following functions is in bidi.cpp
120 void bidiReorderLine(const BidiIterator &start, const BidiIterator &end, BidiState &bidi );
121 RootInlineBox* determineStartPosition(bool fullLayout, BidiIterator &start, BidiState &bidi);
122 RootInlineBox* determineEndPosition(RootInlineBox* startBox, BidiIterator& cleanLineStart, int& yPos);
123 bool matchedEndLine(const BidiIterator& start, const BidiIterator& endLineStart,
124 RootInlineBox*& endLine, int& endYPos);
125 int skipWhitespace(BidiIterator &, BidiState &);
126 BidiIterator findNextLineBreak(BidiIterator &start, BidiState &info );
127 RootInlineBox* constructLine(const BidiIterator& start, const BidiIterator& end);
128 InlineFlowBox* createLineBoxes(RenderObject* obj);
129 void computeHorizontalPositionsForLine(RootInlineBox* lineBox, BidiState &bidi);
130 void computeVerticalPositionsForLine(RootInlineBox* lineBox);
131 void checkLinesForOverflow();
132 void deleteEllipsisLineBoxes();
133 void checkLinesForTextOverflow();
134 // end bidi.cpp functions
136 virtual void paint(PaintInfo& i, int tx, int ty);
137 void paintObject(PaintInfo& i, int tx, int ty);
138 void paintFloats(PaintInfo& i, int _tx, int _ty, bool paintSelection = false);
139 void paintChildren(PaintInfo& i, int _tx, int _ty);
140 void paintEllipsisBoxes(PaintInfo& i, int _tx, int _ty);
141 void paintSelection(PaintInfo& i, int _tx, int _ty);
143 void insertFloatingObject(RenderObject *o);
144 void removeFloatingObject(RenderObject *o);
146 // called from lineWidth, to position the floats added in the last line.
147 void positionNewFloats();
149 int getClearDelta(RenderObject *child);
150 virtual void markAllDescendantsWithFloatsForLayout(RenderObject* floatToRemove = 0);
151 void markPositionedObjectsForLayout();
153 virtual bool containsFloats() { return m_floatingObjects!=0; }
154 virtual bool containsFloat(RenderObject* o);
156 virtual bool hasOverhangingFloats() { return floatBottom() > m_height; }
157 void addIntrudingFloats(RenderBlock* prev, int xoffset, int yoffset);
158 void addOverhangingFloats(RenderBlock* child, int xoffset, int yoffset);
160 int nearestFloatBottom(int height) const;
161 int floatBottom() const;
162 inline int leftBottom();
163 inline int rightBottom();
164 virtual QRect floatRect() const;
166 virtual int lineWidth(int y) const;
167 virtual int lowestPosition(bool includeOverflowInterior=true, bool includeSelf=true) const;
168 virtual int rightmostPosition(bool includeOverflowInterior=true, bool includeSelf=true) const;
169 virtual int leftmostPosition(bool includeOverflowInterior=true, bool includeSelf=true) const;
171 int rightOffset() const;
172 int rightRelOffset(int y, int fixedOffset, bool applyTextIndent = true,
173 int *heightRemaining = 0) const;
174 int rightOffset(int y) const { return rightRelOffset(y, rightOffset(), true); }
176 int leftOffset() const;
177 int leftRelOffset(int y, int fixedOffset, bool applyTextIndent = true,
178 int *heightRemaining = 0) const;
179 int leftOffset(int y) const { return leftRelOffset(y, leftOffset(), true); }
181 virtual bool nodeAtPoint(NodeInfo& info, int x, int y, int tx, int ty,
182 HitTestAction hitTestAction);
184 bool isPointInScrollbar(int x, int y, int tx, int ty);
186 virtual DOM::Position positionForCoordinates(int x, int y, EAffinity * = 0);
188 virtual void calcMinMaxWidth();
189 void calcInlineMinMaxWidth();
190 void calcBlockMinMaxWidth();
192 virtual int getBaselineOfFirstLineBox() const;
194 RootInlineBox* firstRootBox() { return static_cast<RootInlineBox*>(m_firstLineBox); }
195 RootInlineBox* lastRootBox() { return static_cast<RootInlineBox*>(m_lastLineBox); }
197 // Obtains the nearest enclosing block (including this block) that contributes a first-line style to our inline
199 virtual RenderBlock* firstLineBlock() const;
200 virtual void updateFirstLetter();
202 bool inRootBlockContext() const;
204 void setHasMarkupTruncation(bool b=true) { m_hasMarkupTruncation = b; }
205 bool hasMarkupTruncation() const { return m_hasMarkupTruncation; }
207 virtual bool hasSelectedChildren() const { return m_selectionState != SelectionNone; }
208 virtual SelectionState selectionState() const { return m_selectionState; }
209 virtual void setSelectionState(SelectionState s);
211 struct BlockSelectionInfo {
212 RenderBlock* m_block;
214 SelectionState m_state;
216 BlockSelectionInfo() { m_block = 0; m_state = SelectionNone; }
217 BlockSelectionInfo(RenderBlock* b) {
219 m_state = m_block->selectionState();
220 m_rects = m_block->selectionGapRects();
223 GapRects rects() const { return m_rects; }
224 SelectionState state() const { return m_state; }
225 RenderBlock* block() const { return m_block; }
228 virtual QRect selectionRect() { return selectionGapRects(); }
229 GapRects selectionGapRects();
230 virtual bool shouldPaintSelectionGaps() const;
231 bool isSelectionRoot() const;
232 GapRects fillSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
233 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* i = 0);
234 GapRects fillInlineSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
235 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* i);
236 GapRects fillBlockSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
237 int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* i);
238 QRect fillVerticalSelectionGap(int lastTop, int lastLeft, int lastRight,
239 int bottomY, RenderBlock* rootBlock, int blockX, int blockY, const PaintInfo* i);
240 QRect fillLeftSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty, const PaintInfo* i);
241 QRect fillRightSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty, const PaintInfo* i);
242 QRect fillHorizontalSelectionGap(RenderObject* selObj, int xPos, int yPos, int width, int height, const PaintInfo* i);
244 void getHorizontalSelectionGapInfo(SelectionState state, bool& leftGap, bool& rightGap);
245 int leftSelectionOffset(RenderBlock* rootBlock, int y);
246 int rightSelectionOffset(RenderBlock* rootBlock, int y);
249 virtual void printTree(int indent=0) const;
250 virtual void dump(QTextStream *stream, QString ind = "") const;
253 // Helper methods for computing line counts and heights for line counts.
254 RootInlineBox* lineAtIndex(int i);
256 int heightForLineCount(int l);
257 void clearTruncation();
263 DOM::Position positionForBox(InlineBox *box, bool start=true) const;
264 DOM::Position positionForRenderer(RenderObject *renderer, bool start=true) const;
265 bool skipNonBreakingSpace(BidiIterator &it);
268 struct FloatingObject {
274 FloatingObject(Type _type) {
289 Type type : 1; // left or right aligned
293 // The following helper functions and structs are used by layoutBlockChildren.
295 // A compact child that needs to be collapsed into the margin of the following block.
296 RenderObject* m_compact;
298 // The block with the open margin that the compact child is going to place itself within.
299 RenderObject* m_block;
302 RenderObject* compact() const { return m_compact; }
303 RenderObject* block() const { return m_block; }
304 bool matches(RenderObject* child) const { return m_compact && m_block == child; }
306 void clear() { set(0, 0); }
307 void set(RenderObject* c, RenderObject* b) { m_compact = c; m_block = b; }
309 CompactInfo() { clear(); }
313 // Collapsing flags for whether we can collapse our margins with our children's margins.
314 bool m_canCollapseWithChildren : 1;
315 bool m_canCollapseTopWithChildren : 1;
316 bool m_canCollapseBottomWithChildren : 1;
318 // Whether or not we are a quirky container, i.e., do we collapse away top and bottom
319 // margins in our container. Table cells and the body are the common examples. We
320 // also have a custom style property for Safari RSS to deal with TypePad blog articles.
321 bool m_quirkContainer : 1;
323 // This flag tracks whether we are still looking at child margins that can all collapse together at the beginning of a block.
324 // They may or may not collapse with the top margin of the block (|m_canCollapseTopWithChildren| tells us that), but they will
325 // always be collapsing with one another. This variable can remain set to true through multiple iterations
326 // as long as we keep encountering self-collapsing blocks.
327 bool m_atTopOfBlock : 1;
329 // This flag is set when we know we're examining bottom margins and we know we're at the bottom of the block.
330 bool m_atBottomOfBlock : 1;
332 // If our last normal flow child was a self-collapsing block that cleared a float,
333 // we track it in this variable.
334 bool m_selfCollapsingBlockClearedFloat : 1;
336 // These variables are used to detect quirky margins that we need to collapse away (in table cells
337 // and in the body element).
339 bool m_bottomQuirk : 1;
340 bool m_determinedTopQuirk : 1;
342 // These flags track the previous maximal positive and negative margins.
347 MarginInfo(RenderBlock* b, int top, int bottom);
349 void setAtTopOfBlock(bool b) { m_atTopOfBlock = b; }
350 void setAtBottomOfBlock(bool b) { m_atBottomOfBlock = b; }
351 void clearMargin() { m_posMargin = m_negMargin = 0; }
352 void setSelfCollapsingBlockClearedFloat(bool b) { m_selfCollapsingBlockClearedFloat = false; }
353 void setTopQuirk(bool b) { m_topQuirk = b; }
354 void setBottomQuirk(bool b) { m_bottomQuirk = b; }
355 void setDeterminedTopQuirk(bool b) { m_determinedTopQuirk = b; }
356 void setPosMargin(int p) { m_posMargin = p; }
357 void setNegMargin(int n) { m_negMargin = n; }
358 void setPosMarginIfLarger(int p) { if (p > m_posMargin) m_posMargin = p; }
359 void setNegMarginIfLarger(int n) { if (n > m_negMargin) m_negMargin = n; }
361 void setMargin(int p, int n) { m_posMargin = p; m_negMargin = n; }
363 bool atTopOfBlock() const { return m_atTopOfBlock; }
364 bool canCollapseWithTop() const { return m_atTopOfBlock && m_canCollapseTopWithChildren; }
365 bool canCollapseWithBottom() const { return m_atBottomOfBlock && m_canCollapseBottomWithChildren; }
366 bool canCollapseTopWithChildren() const { return m_canCollapseTopWithChildren; }
367 bool canCollapseBottomWithChildren() const { return m_canCollapseBottomWithChildren; }
368 bool selfCollapsingBlockClearedFloat() const { return m_selfCollapsingBlockClearedFloat; }
369 bool quirkContainer() const { return m_quirkContainer; }
370 bool determinedTopQuirk() const { return m_determinedTopQuirk; }
371 bool topQuirk() const { return m_topQuirk; }
372 bool bottomQuirk() const { return m_bottomQuirk; }
373 int posMargin() const { return m_posMargin; }
374 int negMargin() const { return m_negMargin; }
375 int margin() const { return m_posMargin - m_negMargin; }
378 void adjustPositionedBlock(RenderObject* child, const MarginInfo& marginInfo);
379 void adjustFloatingBlock(const MarginInfo& marginInfo);
380 RenderObject* handleSpecialChild(RenderObject* child, const MarginInfo& marginInfo, CompactInfo& compactInfo, bool& handled);
381 RenderObject* handleFloatingChild(RenderObject* child, const MarginInfo& marginInfo, bool& handled);
382 RenderObject* handlePositionedChild(RenderObject* child, const MarginInfo& marginInfo, bool& handled);
383 RenderObject* handleCompactChild(RenderObject* child, CompactInfo& compactInfo, bool& handled);
384 RenderObject* handleRunInChild(RenderObject* child, bool& handled);
385 void collapseMargins(RenderObject* child, MarginInfo& marginInfo, int yPosEstimate);
386 void clearFloatsIfNeeded(RenderObject* child, MarginInfo& marginInfo, int oldTopPosMargin, int oldTopNegMargin);
387 void insertCompactIfNeeded(RenderObject* child, CompactInfo& compactInfo);
388 int estimateVerticalPosition(RenderObject* child, const MarginInfo& info);
389 void determineHorizontalPosition(RenderObject* child);
390 void handleBottomOfBlock(int top, int bottom, MarginInfo& marginInfo);
391 void setCollapsedBottomMargin(const MarginInfo& marginInfo);
392 // End helper functions and structs used by layoutBlockChildren.
395 QPtrList<FloatingObject>* m_floatingObjects;
396 QPtrList<RenderObject>* m_positionedObjects;
398 bool m_childrenInline : 1;
400 bool m_firstLine : 1;
401 EClear m_clearStatus : 2;
402 bool m_topMarginQuirk : 1;
403 bool m_bottomMarginQuirk : 1;
404 bool m_hasMarkupTruncation : 1;
405 SelectionState m_selectionState : 3;
407 int m_maxTopPosMargin;
408 int m_maxTopNegMargin;
409 int m_maxBottomPosMargin;
410 int m_maxBottomNegMargin;
412 // How much content overflows out of our block vertically or horizontally (all we support
413 // for now is spillage out of the bottom and the right, which are the common cases).
414 // XXX Generalize to work with top and left as well.
415 int m_overflowHeight;
418 // Left and top overflow. Does not affect scrolling dimensions, but we do at least use it
419 // when dirty rect checking and hit testing.
426 #endif // RENDER_BLOCK_H