Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / layout / generic / nsHTMLReflowState.h
blob5c0e0bf56603a6ee9bda9a8378930055ef4d5685
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 nsSpaceManager;
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 // Destructor for usedPaddingProperty
177 static void DestroyMarginFunc(void* aFrame,
178 nsIAtom* aPropertyName,
179 void* aPropertyValue,
180 void* aDtorData);
182 private:
183 // Computes margin values from the specified margin style information, and
184 // fills in the mComputedMargin member
185 void ComputeMargin(nscoord aContainingBlockWidth);
187 // Computes padding values from the specified padding style information, and
188 // fills in the mComputedPadding member
189 void ComputePadding(nscoord aContainingBlockWidth);
191 protected:
193 void InitOffsets(nscoord aContainingBlockWidth,
194 const nsMargin *aBorder = nsnull,
195 const nsMargin *aPadding = nsnull);
198 * Convert nsStyleCoord to nscoord when percentages depend on the
199 * containing block width, and enumerated values are for width,
200 * min-width, or max-width. Does not handle auto widths.
202 inline nscoord ComputeWidthValue(nscoord aContainingBlockWidth,
203 nscoord aContentEdgeToBoxSizing,
204 nscoord aBoxSizingToMarginEdge,
205 const nsStyleCoord& aCoord);
206 // same as previous, but using mComputedBorderPadding, mComputedPadding,
207 // and mComputedMargin
208 nscoord ComputeWidthValue(nscoord aContainingBlockWidth,
209 PRUint8 aBoxSizing,
210 const nsStyleCoord& aCoord);
214 * State passed to a frame during reflow or intrinsic size calculation.
216 * XXX Refactor so only a base class (nsSizingState?) is used for intrinsic
217 * size calculation.
219 * @see nsIFrame#Reflow()
221 struct nsHTMLReflowState : public nsCSSOffsetState {
222 // the reflow states are linked together. this is the pointer to the
223 // parent's reflow state
224 const nsHTMLReflowState* parentReflowState;
226 // the available width in which to reflow the frame. The space
227 // represents the amount of room for the frame's border, padding,
228 // and content area (not the margin area. The parent frame deals
229 // with the child frame's margins). The frame size you choose should
230 // fit within the available width.
231 nscoord availableWidth;
233 // A value of NS_UNCONSTRAINEDSIZE for the available height means
234 // you can choose whatever size you want. In galley mode the
235 // available height is always NS_UNCONSTRAINEDSIZE, and only page
236 // mode or multi-column layout involves a constrained height. The
237 // element's the top border and padding, and content, must fit. If the
238 // element is complete after reflow then its bottom border, padding
239 // and margin (and similar for its complete ancestors) will need to
240 // fit in this height.
241 nscoord availableHeight;
243 // The type of frame, from css's perspective. This value is
244 // initialized by the Init method below.
245 nsCSSFrameType mFrameType;
247 // pointer to the space manager associated with this area
248 nsSpaceManager* mSpaceManager;
250 // The amount the in-flow position of the block is moving vertically relative
251 // to its previous in-flow position (i.e. the amount the line containing the
252 // block is moving).
253 // This should be zero for anything which is not a block outside, and it
254 // should be zero for anything which has a non-block parent.
255 // The intended use of this value is to allow the accurate determination
256 // of the potential impact of a float
257 // This takes on an arbitrary value the first time a block is reflowed
258 nscoord mBlockDelta;
260 // LineLayout object (only for inline reflow; set to NULL otherwise)
261 nsLineLayout* mLineLayout;
263 // The appropriate reflow state for the containing block (for
264 // percentage widths, etc.) of this reflow state's frame.
265 const nsHTMLReflowState *mCBReflowState;
267 private:
268 // The computed width specifies the frame's content area width, and it does
269 // not apply to inline non-replaced elements
271 // For replaced inline frames, a value of NS_INTRINSICSIZE means you should
272 // use your intrinsic width as the computed width
274 // For block-level frames, the computed width is based on the width of the
275 // containing block, the margin/border/padding areas, and the min/max width.
276 nscoord mComputedWidth;
278 // The computed height specifies the frame's content height, and it does
279 // not apply to inline non-replaced elements
281 // For replaced inline frames, a value of NS_INTRINSICSIZE means you should
282 // use your intrinsic height as the computed height
284 // For non-replaced block-level frames in the flow and floated, a value of
285 // NS_AUTOHEIGHT means you choose a height to shrink wrap around the normal
286 // flow child frames. The height must be within the limit of the min/max
287 // height if there is such a limit
289 // For replaced block-level frames, a value of NS_INTRINSICSIZE
290 // means you use your intrinsic height as the computed height
291 nscoord mComputedHeight;
293 public:
294 // Computed values for 'left/top/right/bottom' offsets. Only applies to
295 // 'positioned' elements
296 nsMargin mComputedOffsets;
298 // Computed values for 'min-width/max-width' and 'min-height/max-height'
299 // XXXldb The width ones here should go; they should be needed only
300 // internally.
301 nscoord mComputedMinWidth, mComputedMaxWidth;
302 nscoord mComputedMinHeight, mComputedMaxHeight;
304 // Cached pointers to the various style structs used during intialization
305 const nsStyleDisplay* mStyleDisplay;
306 const nsStyleVisibility* mStyleVisibility;
307 const nsStylePosition* mStylePosition;
308 const nsStyleBorder* mStyleBorder;
309 const nsStyleMargin* mStyleMargin;
310 const nsStylePadding* mStylePadding;
311 const nsStyleText* mStyleText;
313 // a frame (e.g. nsTableCellFrame) which may need to generate a special
314 // reflow for percent height calculations
315 nsIPercentHeightObserver* mPercentHeightObserver;
317 // a frame (e.g. nsTableFrame) which initiates a special reflow for percent height calculations
318 nsIFrame* mPercentHeightReflowInitiator;
320 // CSS margin collapsing sometimes requires us to reflow
321 // optimistically assuming that margins collapse to see if clearance
322 // is required. When we discover that clearance is required, we
323 // store the frame in which clearance was discovered to the location
324 // requested here.
325 nsIFrame** mDiscoveredClearance;
327 // This value keeps track of how deeply nested a given reflow state
328 // is from the top of the frame tree.
329 PRInt16 mReflowDepth;
331 struct ReflowStateFlags {
332 PRUint16 mSpecialHeightReflow:1; // used by tables to communicate special reflow (in process) to handle
333 // percent height frames inside cells which may not have computed heights
334 PRUint16 mNextInFlowUntouched:1; // nothing in the frame's next-in-flow (or its descendants)
335 // is changing
336 PRUint16 mIsTopOfPage:1; // is the current context at the top of a page?
337 PRUint16 mBlinks:1; // Keep track of text-decoration: blink
338 PRUint16 mHasClearance:1; // Block has clearance
339 PRUint16 mAssumingHScrollbar:1; // parent frame is an nsIScrollableFrame and it
340 // is assuming a horizontal scrollbar
341 PRUint16 mAssumingVScrollbar:1; // parent frame is an nsIScrollableFrame and it
342 // is assuming a vertical scrollbar
344 PRUint16 mHResize:1; // Is frame (a) not dirty and (b) a
345 // different width than before?
347 PRUint16 mVResize:1; // Is frame (a) not dirty and (b) a
348 // different height than before or
349 // (potentially) in a context where
350 // percent heights have a different
351 // basis?
352 PRUint16 mTableIsSplittable:1; // tables are splittable, this should happen only inside a page
353 // and never insider a column frame
354 PRUint16 mHeightDependsOnAncestorCell:1; // Does frame height depend on
355 // an ancestor table-cell?
357 } mFlags;
359 // Note: The copy constructor is written by the compiler automatically. You
360 // can use that and then override specific values if you want, or you can
361 // call Init as desired...
363 // Initialize a <b>root</b> reflow state with a rendering context to
364 // use for measuring things.
365 nsHTMLReflowState(nsPresContext* aPresContext,
366 nsIFrame* aFrame,
367 nsIRenderingContext* aRenderingContext,
368 const nsSize& aAvailableSpace);
370 // Initialize a reflow state for a child frames reflow. Some state
371 // is copied from the parent reflow state; the remaining state is
372 // computed.
373 nsHTMLReflowState(nsPresContext* aPresContext,
374 const nsHTMLReflowState& aParentReflowState,
375 nsIFrame* aFrame,
376 const nsSize& aAvailableSpace,
377 // These two are used by absolute positioning code
378 // to override default containing block w & h:
379 nscoord aContainingBlockWidth = -1,
380 nscoord aContainingBlockHeight = -1,
381 PRBool aInit = PR_TRUE);
383 // This method initializes various data members. It is automatically
384 // called by the various constructors
385 void Init(nsPresContext* aPresContext,
386 nscoord aContainingBlockWidth = -1,
387 nscoord aContainingBlockHeight = -1,
388 const nsMargin* aBorder = nsnull,
389 const nsMargin* aPadding = nsnull);
391 * Find the content width of the containing block of aReflowState
393 static nscoord
394 GetContainingBlockContentWidth(const nsHTMLReflowState* aReflowState);
397 * Find the containing block of aFrame. This may return null if
398 * there isn't one (but that should really only happen for root
399 * frames).
401 static nsIFrame* GetContainingBlockFor(const nsIFrame* aFrame);
404 * Calculate the raw line-height property for the given frame. The return
405 * value will be >= 0.
407 static nscoord CalcLineHeight(nsIFrame* aFrame)
409 return CalcLineHeight(aFrame->GetStyleContext());
413 * Same as above, but doesn't need a frame.
415 static nscoord CalcLineHeight(nsStyleContext* aStyleContext);
418 void ComputeContainingBlockRectangle(nsPresContext* aPresContext,
419 const nsHTMLReflowState* aContainingBlockRS,
420 nscoord& aContainingBlockWidth,
421 nscoord& aContainingBlockHeight);
424 * Apply the mComputed(Min/Max)(Width/Height) values to the content
425 * size computed so far. If a passed-in pointer is null, we skip
426 * adjusting that dimension.
428 void ApplyMinMaxConstraints(nscoord* aContentWidth, nscoord* aContentHeight) const;
430 PRBool ShouldReflowAllKids() const {
431 // Note that we could make a stronger optimization for mVResize if
432 // we use it in a ShouldReflowChild test that replaces the current
433 // checks of NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN, if it
434 // were tested there along with NS_FRAME_CONTAINS_RELATIVE_HEIGHT.
435 // This would need to be combined with a slight change in which
436 // frames NS_FRAME_CONTAINS_RELATIVE_HEIGHT is marked on.
437 return (frame->GetStateBits() & NS_FRAME_IS_DIRTY) ||
438 mFlags.mHResize ||
439 (mFlags.mVResize &&
440 (frame->GetStateBits() & NS_FRAME_CONTAINS_RELATIVE_HEIGHT));
443 nscoord ComputedWidth() const { return mComputedWidth; }
444 // This method doesn't apply min/max computed widths to the value passed in.
445 void SetComputedWidth(nscoord aComputedWidth);
447 nscoord ComputedHeight() const { return mComputedHeight; }
448 // This method doesn't apply min/max computed heights to the value passed in.
449 void SetComputedHeight(nscoord aComputedHeight);
451 void SetTruncated(const nsHTMLReflowMetrics& aMetrics, nsReflowStatus* aStatus) const;
453 PRBool WillReflowAgainForClearance() const {
454 return mDiscoveredClearance && *mDiscoveredClearance;
457 protected:
458 void InitFrameType();
459 void InitCBReflowState();
460 void InitResizeFlags(nsPresContext* aPresContext);
462 void InitConstraints(nsPresContext* aPresContext,
463 nscoord aContainingBlockWidth,
464 nscoord aContainingBlockHeight,
465 const nsMargin* aBorder,
466 const nsMargin* aPadding);
468 // Returns the nearest containing block frame for the specified frame. Also
469 // returns the left edge and width of the containing block's content area.
470 // These are returned in the coordinate space of the containing block.
471 nsIFrame* GetNearestContainingBlock(nsIFrame* aFrame, nscoord& aCBLeftEdge,
472 nscoord& aCBWidth);
474 void CalculateHypotheticalBox(nsPresContext* aPresContext,
475 nsIFrame* aPlaceholderFrame,
476 nsIFrame* aContainingBlock,
477 nscoord aBlockLeftContentEdge,
478 nscoord aBlockContentWidth,
479 const nsHTMLReflowState* cbrs,
480 nsHypotheticalBox& aHypotheticalBox);
482 void InitAbsoluteConstraints(nsPresContext* aPresContext,
483 const nsHTMLReflowState* cbrs,
484 nscoord aContainingBlockWidth,
485 nscoord aContainingBlockHeight);
487 void ComputeRelativeOffsets(const nsHTMLReflowState* cbrs,
488 nscoord aContainingBlockWidth,
489 nscoord aContainingBlockHeight,
490 nsPresContext* aPresContext);
492 // Calculates the computed values for the 'min-Width', 'max-Width',
493 // 'min-Height', and 'max-Height' properties, and stores them in the assorted
494 // data members
495 void ComputeMinMaxValues(nscoord aContainingBlockWidth,
496 nscoord aContainingBlockHeight,
497 const nsHTMLReflowState* aContainingBlockRS);
499 void CalculateHorizBorderPaddingMargin(nscoord aContainingBlockWidth,
500 nscoord* aInsideBoxSizing,
501 nscoord* aOutsideBoxSizing);
503 void CalculateBlockSideMargins(nscoord aAvailWidth,
504 nscoord aComputedWidth);
507 #endif /* nsHTMLReflowState_h___ */