Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / generic / nsHTMLReflowState.h
blob261c9abfbf5f2831624194d49fa40378b778d7d8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* struct containing the input to nsIFrame::Reflow */
40 #ifndef nsHTMLReflowState_h___
41 #define nsHTMLReflowState_h___
43 #include "nsMargin.h"
44 #include "nsStyleCoord.h"
45 #include "nsIFrame.h"
47 class nsPresContext;
48 class nsIRenderingContext;
49 class nsFloatManager;
50 class nsLineLayout;
51 class nsIPercentHeightObserver;
53 struct nsStyleDisplay;
54 struct nsStyleVisibility;
55 struct nsStylePosition;
56 struct nsStyleBorder;
57 struct nsStyleMargin;
58 struct nsStylePadding;
59 struct nsStyleText;
60 struct nsHypotheticalBox;
62 template <class NumericType>
63 NumericType
64 NS_CSS_MINMAX(NumericType aValue, NumericType aMinValue, NumericType aMaxValue)
66 NumericType result = aValue;
67 if (aMaxValue < result)
68 result = aMaxValue;
69 if (aMinValue > result)
70 result = aMinValue;
71 return result;
74 /**
75 * Constant used to indicate an unconstrained size.
77 * @see #Reflow()
79 #define NS_UNCONSTRAINEDSIZE NS_MAXSIZE
81 /**
82 * CSS Frame type. Included as part of the reflow state.
84 typedef PRUint32 nsCSSFrameType;
86 #define NS_CSS_FRAME_TYPE_UNKNOWN 0
87 #define NS_CSS_FRAME_TYPE_INLINE 1
88 #define NS_CSS_FRAME_TYPE_BLOCK 2 /* block-level in normal flow */
89 #define NS_CSS_FRAME_TYPE_FLOATING 3
90 #define NS_CSS_FRAME_TYPE_ABSOLUTE 4
91 #define NS_CSS_FRAME_TYPE_INTERNAL_TABLE 5 /* row group frame, row frame, cell frame, ... */
93 /**
94 * Bit-flag that indicates whether the element is replaced. Applies to inline,
95 * block-level, floating, and absolutely positioned elements
97 #define NS_CSS_FRAME_TYPE_REPLACED 0x08000
99 /**
100 * Bit-flag that indicates that the element is replaced and contains a block
101 * (eg some form controls). Applies to inline, block-level, floating, and
102 * absolutely positioned elements. Mutually exclusive with
103 * NS_CSS_FRAME_TYPE_REPLACED.
105 #define NS_CSS_FRAME_TYPE_REPLACED_CONTAINS_BLOCK 0x10000
108 * Helper macros for telling whether items are replaced
110 #define NS_FRAME_IS_REPLACED_NOBLOCK(_ft) \
111 (NS_CSS_FRAME_TYPE_REPLACED == ((_ft) & NS_CSS_FRAME_TYPE_REPLACED))
113 #define NS_FRAME_IS_REPLACED(_ft) \
114 (NS_FRAME_IS_REPLACED_NOBLOCK(_ft) || \
115 NS_FRAME_IS_REPLACED_CONTAINS_BLOCK(_ft))
117 #define NS_FRAME_REPLACED(_ft) \
118 (NS_CSS_FRAME_TYPE_REPLACED | (_ft))
120 #define NS_FRAME_IS_REPLACED_CONTAINS_BLOCK(_ft) \
121 (NS_CSS_FRAME_TYPE_REPLACED_CONTAINS_BLOCK == \
122 ((_ft) & NS_CSS_FRAME_TYPE_REPLACED_CONTAINS_BLOCK))
124 #define NS_FRAME_REPLACED_CONTAINS_BLOCK(_ft) \
125 (NS_CSS_FRAME_TYPE_REPLACED_CONTAINS_BLOCK | (_ft))
128 * A macro to extract the type. Masks off the 'replaced' bit-flag
130 #define NS_FRAME_GET_TYPE(_ft) \
131 ((_ft) & ~(NS_CSS_FRAME_TYPE_REPLACED | \
132 NS_CSS_FRAME_TYPE_REPLACED_CONTAINS_BLOCK))
134 #define NS_INTRINSICSIZE NS_UNCONSTRAINEDSIZE
135 #define NS_AUTOHEIGHT NS_UNCONSTRAINEDSIZE
136 #define NS_AUTOMARGIN NS_UNCONSTRAINEDSIZE
137 #define NS_AUTOOFFSET NS_UNCONSTRAINEDSIZE
138 // NOTE: there are assumptions all over that these have the same value, namely NS_UNCONSTRAINEDSIZE
139 // if any are changed to be a value other than NS_UNCONSTRAINEDSIZE
140 // at least update AdjustComputedHeight/Width and test ad nauseum
142 // A base class of nsHTMLReflowState that computes only the padding,
143 // border, and margin, since those values are needed more often.
144 struct nsCSSOffsetState {
145 public:
146 // the frame being reflowed
147 nsIFrame* frame;
149 // rendering context to use for measurement
150 nsIRenderingContext* rendContext;
152 // Computed margin values
153 nsMargin mComputedMargin;
155 // Cached copy of the border + padding values
156 nsMargin mComputedBorderPadding;
158 // Computed padding values
159 nsMargin mComputedPadding;
161 // Callers using this constructor must call InitOffsets on their own.
162 nsCSSOffsetState(nsIFrame *aFrame, nsIRenderingContext *aRenderingContext)
163 : frame(aFrame)
164 , rendContext(aRenderingContext)
168 nsCSSOffsetState(nsIFrame *aFrame, nsIRenderingContext *aRenderingContext,
169 nscoord aContainingBlockWidth)
170 : frame(aFrame)
171 , rendContext(aRenderingContext)
173 InitOffsets(aContainingBlockWidth);
176 #ifdef DEBUG
177 // Reflow trace methods. Defined in nsFrame.cpp so they have access
178 // to the display-reflow infrastructure.
179 static void* DisplayInitOffsetsEnter(nsIFrame* aFrame,
180 nsCSSOffsetState* aState,
181 nscoord aCBWidth,
182 const nsMargin* aBorder,
183 const nsMargin* aPadding);
184 static void DisplayInitOffsetsExit(nsIFrame* aFrame,
185 nsCSSOffsetState* aState,
186 void* aValue);
187 #endif
189 private:
191 * Computes margin values from the specified margin style information, and
192 * fills in the mComputedMargin member.
193 * @return PR_TRUE if the margin is dependent on the containing block width
195 PRBool ComputeMargin(nscoord aContainingBlockWidth);
198 * Computes padding values from the specified padding style information, and
199 * fills in the mComputedPadding member.
200 * @return PR_TRUE if the padding is dependent on the containing block width
202 PRBool ComputePadding(nscoord aContainingBlockWidth);
204 protected:
206 void InitOffsets(nscoord aContainingBlockWidth,
207 const nsMargin *aBorder = nsnull,
208 const nsMargin *aPadding = nsnull);
211 * Convert nsStyleCoord to nscoord when percentages depend on the
212 * containing block width, and enumerated values are for width,
213 * min-width, or max-width. Does not handle auto widths.
215 inline nscoord ComputeWidthValue(nscoord aContainingBlockWidth,
216 nscoord aContentEdgeToBoxSizing,
217 nscoord aBoxSizingToMarginEdge,
218 const nsStyleCoord& aCoord);
219 // same as previous, but using mComputedBorderPadding, mComputedPadding,
220 // and mComputedMargin
221 nscoord ComputeWidthValue(nscoord aContainingBlockWidth,
222 PRUint8 aBoxSizing,
223 const nsStyleCoord& aCoord);
227 * State passed to a frame during reflow or intrinsic size calculation.
229 * XXX Refactor so only a base class (nsSizingState?) is used for intrinsic
230 * size calculation.
232 * @see nsIFrame#Reflow()
234 struct nsHTMLReflowState : public nsCSSOffsetState {
235 // the reflow states are linked together. this is the pointer to the
236 // parent's reflow state
237 const nsHTMLReflowState* parentReflowState;
239 // pointer to the float manager associated with this area
240 nsFloatManager* mFloatManager;
242 // LineLayout object (only for inline reflow; set to NULL otherwise)
243 nsLineLayout* mLineLayout;
245 // The appropriate reflow state for the containing block (for
246 // percentage widths, etc.) of this reflow state's frame.
247 const nsHTMLReflowState *mCBReflowState;
249 // the available width in which to reflow the frame. The space
250 // represents the amount of room for the frame's border, padding,
251 // and content area (not the margin area. The parent frame deals
252 // with the child frame's margins). The frame size you choose should
253 // fit within the available width.
254 nscoord availableWidth;
256 // A value of NS_UNCONSTRAINEDSIZE for the available height means
257 // you can choose whatever size you want. In galley mode the
258 // available height is always NS_UNCONSTRAINEDSIZE, and only page
259 // mode or multi-column layout involves a constrained height. The
260 // element's the top border and padding, and content, must fit. If the
261 // element is complete after reflow then its bottom border, padding
262 // and margin (and similar for its complete ancestors) will need to
263 // fit in this height.
264 nscoord availableHeight;
266 // The type of frame, from css's perspective. This value is
267 // initialized by the Init method below.
268 nsCSSFrameType mFrameType;
270 // The amount the in-flow position of the block is moving vertically relative
271 // to its previous in-flow position (i.e. the amount the line containing the
272 // block is moving).
273 // This should be zero for anything which is not a block outside, and it
274 // should be zero for anything which has a non-block parent.
275 // The intended use of this value is to allow the accurate determination
276 // of the potential impact of a float
277 // This takes on an arbitrary value the first time a block is reflowed
278 nscoord mBlockDelta;
280 private:
281 // The computed width specifies the frame's content area width, and it does
282 // not apply to inline non-replaced elements
284 // For replaced inline frames, a value of NS_INTRINSICSIZE means you should
285 // use your intrinsic width as the computed width
287 // For block-level frames, the computed width is based on the width of the
288 // containing block, the margin/border/padding areas, and the min/max width.
289 nscoord mComputedWidth;
291 // The computed height specifies the frame's content height, and it does
292 // not apply to inline non-replaced elements
294 // For replaced inline frames, a value of NS_INTRINSICSIZE means you should
295 // use your intrinsic height as the computed height
297 // For non-replaced block-level frames in the flow and floated, a value of
298 // NS_AUTOHEIGHT means you choose a height to shrink wrap around the normal
299 // flow child frames. The height must be within the limit of the min/max
300 // height if there is such a limit
302 // For replaced block-level frames, a value of NS_INTRINSICSIZE
303 // means you use your intrinsic height as the computed height
304 nscoord mComputedHeight;
306 public:
307 // Computed values for 'left/top/right/bottom' offsets. Only applies to
308 // 'positioned' elements
309 nsMargin mComputedOffsets;
311 // Computed values for 'min-width/max-width' and 'min-height/max-height'
312 // XXXldb The width ones here should go; they should be needed only
313 // internally.
314 nscoord mComputedMinWidth, mComputedMaxWidth;
315 nscoord mComputedMinHeight, mComputedMaxHeight;
317 // Cached pointers to the various style structs used during intialization
318 const nsStyleDisplay* mStyleDisplay;
319 const nsStyleVisibility* mStyleVisibility;
320 const nsStylePosition* mStylePosition;
321 const nsStyleBorder* mStyleBorder;
322 const nsStyleMargin* mStyleMargin;
323 const nsStylePadding* mStylePadding;
324 const nsStyleText* mStyleText;
326 // a frame (e.g. nsTableCellFrame) which may need to generate a special
327 // reflow for percent height calculations
328 nsIPercentHeightObserver* mPercentHeightObserver;
330 // CSS margin collapsing sometimes requires us to reflow
331 // optimistically assuming that margins collapse to see if clearance
332 // is required. When we discover that clearance is required, we
333 // store the frame in which clearance was discovered to the location
334 // requested here.
335 nsIFrame** mDiscoveredClearance;
337 // This value keeps track of how deeply nested a given reflow state
338 // is from the top of the frame tree.
339 PRInt16 mReflowDepth;
341 struct ReflowStateFlags {
342 PRUint16 mSpecialHeightReflow:1; // used by tables to communicate special reflow (in process) to handle
343 // percent height frames inside cells which may not have computed heights
344 PRUint16 mNextInFlowUntouched:1; // nothing in the frame's next-in-flow (or its descendants)
345 // is changing
346 PRUint16 mIsTopOfPage:1; // Is the current context at the top of a
347 // page? When true, we force something
348 // that's too tall for a page/column to
349 // fit anyway to avoid infinite loops.
350 PRUint16 mBlinks:1; // Keep track of text-decoration: blink
351 PRUint16 mHasClearance:1; // Block has clearance
352 PRUint16 mAssumingHScrollbar:1; // parent frame is an nsIScrollableFrame and it
353 // is assuming a horizontal scrollbar
354 PRUint16 mAssumingVScrollbar:1; // parent frame is an nsIScrollableFrame and it
355 // is assuming a vertical scrollbar
357 PRUint16 mHResize:1; // Is frame (a) not dirty and (b) a
358 // different width than before?
360 PRUint16 mVResize:1; // Is frame (a) not dirty and (b) a
361 // different height than before or
362 // (potentially) in a context where
363 // percent heights have a different
364 // basis?
365 PRUint16 mTableIsSplittable:1; // tables are splittable, this should happen only inside a page
366 // and never insider a column frame
367 PRUint16 mHeightDependsOnAncestorCell:1; // Does frame height depend on
368 // an ancestor table-cell?
370 } mFlags;
372 // Note: The copy constructor is written by the compiler automatically. You
373 // can use that and then override specific values if you want, or you can
374 // call Init as desired...
376 // Initialize a <b>root</b> reflow state with a rendering context to
377 // use for measuring things.
378 nsHTMLReflowState(nsPresContext* aPresContext,
379 nsIFrame* aFrame,
380 nsIRenderingContext* aRenderingContext,
381 const nsSize& aAvailableSpace);
383 // Initialize a reflow state for a child frames reflow. Some state
384 // is copied from the parent reflow state; the remaining state is
385 // computed.
386 nsHTMLReflowState(nsPresContext* aPresContext,
387 const nsHTMLReflowState& aParentReflowState,
388 nsIFrame* aFrame,
389 const nsSize& aAvailableSpace,
390 // These two are used by absolute positioning code
391 // to override default containing block w & h:
392 nscoord aContainingBlockWidth = -1,
393 nscoord aContainingBlockHeight = -1,
394 PRBool aInit = PR_TRUE);
396 // This method initializes various data members. It is automatically
397 // called by the various constructors
398 void Init(nsPresContext* aPresContext,
399 nscoord aContainingBlockWidth = -1,
400 nscoord aContainingBlockHeight = -1,
401 const nsMargin* aBorder = nsnull,
402 const nsMargin* aPadding = nsnull);
404 * Find the content width of the containing block of aReflowState
406 static nscoord
407 GetContainingBlockContentWidth(const nsHTMLReflowState* aReflowState);
410 * Find the containing block of aFrame. This may return null if
411 * there isn't one (but that should really only happen for root
412 * frames).
414 static nsIFrame* GetContainingBlockFor(const nsIFrame* aFrame);
417 * Calculate the used line-height property. The return value will be >= 0.
419 nscoord CalcLineHeight() const;
422 * Same as CalcLineHeight() above, but doesn't need a reflow state.
424 * @param aBlockHeight The computed height of the content rect of the block
425 * that the line should fill.
426 * Only used with line-height:-moz-block-height.
427 * NS_AUTOHEIGHT results in a normal line-height for
428 * line-height:-moz-block-height.
430 static nscoord CalcLineHeight(nsStyleContext* aStyleContext,
431 nscoord aBlockHeight);
434 void ComputeContainingBlockRectangle(nsPresContext* aPresContext,
435 const nsHTMLReflowState* aContainingBlockRS,
436 nscoord& aContainingBlockWidth,
437 nscoord& aContainingBlockHeight);
440 * Apply the mComputed(Min/Max)(Width/Height) values to the content
441 * size computed so far. If a passed-in pointer is null, we skip
442 * adjusting that dimension.
444 void ApplyMinMaxConstraints(nscoord* aContentWidth, nscoord* aContentHeight) const;
446 PRBool ShouldReflowAllKids() const {
447 // Note that we could make a stronger optimization for mVResize if
448 // we use it in a ShouldReflowChild test that replaces the current
449 // checks of NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN, if it
450 // were tested there along with NS_FRAME_CONTAINS_RELATIVE_HEIGHT.
451 // This would need to be combined with a slight change in which
452 // frames NS_FRAME_CONTAINS_RELATIVE_HEIGHT is marked on.
453 return (frame->GetStateBits() & NS_FRAME_IS_DIRTY) ||
454 mFlags.mHResize ||
455 (mFlags.mVResize &&
456 (frame->GetStateBits() & NS_FRAME_CONTAINS_RELATIVE_HEIGHT));
459 nscoord ComputedWidth() const { return mComputedWidth; }
460 // This method doesn't apply min/max computed widths to the value passed in.
461 void SetComputedWidth(nscoord aComputedWidth);
463 nscoord ComputedHeight() const { return mComputedHeight; }
464 // This method doesn't apply min/max computed heights to the value passed in.
465 void SetComputedHeight(nscoord aComputedHeight);
467 void SetComputedHeightWithoutResettingResizeFlags(nscoord aComputedHeight) {
468 // Viewport frames reset the computed height on a copy of their reflow
469 // state when reflowing fixed-pos kids. In that case we actually don't
470 // want to mess with the resize flags, because comparing the frame's rect
471 // to the munged computed width is pointless.
472 mComputedHeight = aComputedHeight;
475 void SetTruncated(const nsHTMLReflowMetrics& aMetrics, nsReflowStatus* aStatus) const;
477 PRBool WillReflowAgainForClearance() const {
478 return mDiscoveredClearance && *mDiscoveredClearance;
481 #ifdef DEBUG
482 // Reflow trace methods. Defined in nsFrame.cpp so they have access
483 // to the display-reflow infrastructure.
484 static void* DisplayInitConstraintsEnter(nsIFrame* aFrame,
485 nsHTMLReflowState* aState,
486 nscoord aCBWidth,
487 nscoord aCBHeight,
488 const nsMargin* aBorder,
489 const nsMargin* aPadding);
490 static void DisplayInitConstraintsExit(nsIFrame* aFrame,
491 nsHTMLReflowState* aState,
492 void* aValue);
493 static void* DisplayInitFrameTypeEnter(nsIFrame* aFrame,
494 nsHTMLReflowState* aState);
495 static void DisplayInitFrameTypeExit(nsIFrame* aFrame,
496 nsHTMLReflowState* aState,
497 void* aValue);
498 #endif
500 protected:
501 void InitFrameType();
502 void InitCBReflowState();
503 void InitResizeFlags(nsPresContext* aPresContext);
505 void InitConstraints(nsPresContext* aPresContext,
506 nscoord aContainingBlockWidth,
507 nscoord aContainingBlockHeight,
508 const nsMargin* aBorder,
509 const nsMargin* aPadding);
511 // Returns the nearest containing block or block frame (whether or not
512 // it is a containing block) for the specified frame. Also returns
513 // the left edge and width of the containing block's content area.
514 // These are returned in the coordinate space of the containing block.
515 nsIFrame* GetHypotheticalBoxContainer(nsIFrame* aFrame,
516 nscoord& aCBLeftEdge,
517 nscoord& aCBWidth);
519 void CalculateHypotheticalBox(nsPresContext* aPresContext,
520 nsIFrame* aPlaceholderFrame,
521 nsIFrame* aContainingBlock,
522 nscoord aBlockLeftContentEdge,
523 nscoord aBlockContentWidth,
524 const nsHTMLReflowState* cbrs,
525 nsHypotheticalBox& aHypotheticalBox);
527 void InitAbsoluteConstraints(nsPresContext* aPresContext,
528 const nsHTMLReflowState* cbrs,
529 nscoord aContainingBlockWidth,
530 nscoord aContainingBlockHeight);
532 void ComputeRelativeOffsets(const nsHTMLReflowState* cbrs,
533 nscoord aContainingBlockWidth,
534 nscoord aContainingBlockHeight,
535 nsPresContext* aPresContext);
537 // Calculates the computed values for the 'min-Width', 'max-Width',
538 // 'min-Height', and 'max-Height' properties, and stores them in the assorted
539 // data members
540 void ComputeMinMaxValues(nscoord aContainingBlockWidth,
541 nscoord aContainingBlockHeight,
542 const nsHTMLReflowState* aContainingBlockRS);
544 void CalculateHorizBorderPaddingMargin(nscoord aContainingBlockWidth,
545 nscoord* aInsideBoxSizing,
546 nscoord* aOutsideBoxSizing);
548 void CalculateBlockSideMargins(nscoord aAvailWidth,
549 nscoord aComputedWidth);
552 #endif /* nsHTMLReflowState_h___ */