Bug 417561, disable tagging of Talkback now we have prebuilt packages, r=rhelmer
[mozilla-1.9.git] / layout / generic / nsAbsoluteContainingBlock.cpp
blob97bac77a73c15dc8f5876ea5245eb4c7913fee36
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 ***** */
39 * code for managing absolutely positioned children of a rendering
40 * object that is a containing block for them
43 #include "nsCOMPtr.h"
44 #include "nsAbsoluteContainingBlock.h"
45 #include "nsContainerFrame.h"
46 #include "nsIPresShell.h"
47 #include "nsHTMLContainerFrame.h"
48 #include "nsHTMLParts.h"
49 #include "nsPresContext.h"
51 #ifdef DEBUG
52 #include "nsBlockFrame.h"
53 #endif
56 nsresult
57 nsAbsoluteContainingBlock::FirstChild(const nsIFrame* aDelegatingFrame,
58 nsIAtom* aListName,
59 nsIFrame** aFirstChild) const
61 NS_PRECONDITION(GetChildListName() == aListName, "unexpected child list name");
62 *aFirstChild = mAbsoluteFrames.FirstChild();
63 return NS_OK;
66 nsresult
67 nsAbsoluteContainingBlock::SetInitialChildList(nsIFrame* aDelegatingFrame,
68 nsIAtom* aListName,
69 nsIFrame* aChildList)
71 NS_PRECONDITION(GetChildListName() == aListName, "unexpected child list name");
72 #ifdef NS_DEBUG
73 nsFrame::VerifyDirtyBitSet(aChildList);
74 #endif
75 mAbsoluteFrames.SetFrames(aChildList);
76 return NS_OK;
79 nsresult
80 nsAbsoluteContainingBlock::AppendFrames(nsIFrame* aDelegatingFrame,
81 nsIAtom* aListName,
82 nsIFrame* aFrameList)
84 NS_ASSERTION(GetChildListName() == aListName, "unexpected child list");
86 // Append the frames to our list of absolutely positioned frames
87 #ifdef NS_DEBUG
88 nsFrame::VerifyDirtyBitSet(aFrameList);
89 #endif
90 mAbsoluteFrames.AppendFrames(nsnull, aFrameList);
92 // no damage to intrinsic widths, since absolutely positioned frames can't
93 // change them
94 return aDelegatingFrame->PresContext()->PresShell()->
95 FrameNeedsReflow(aDelegatingFrame, nsIPresShell::eResize,
96 NS_FRAME_HAS_DIRTY_CHILDREN);
99 nsresult
100 nsAbsoluteContainingBlock::InsertFrames(nsIFrame* aDelegatingFrame,
101 nsIAtom* aListName,
102 nsIFrame* aPrevFrame,
103 nsIFrame* aFrameList)
105 NS_ASSERTION(GetChildListName() == aListName, "unexpected child list");
106 NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == aDelegatingFrame,
107 "inserting after sibling frame with different parent");
109 #ifdef NS_DEBUG
110 nsFrame::VerifyDirtyBitSet(aFrameList);
111 #endif
112 mAbsoluteFrames.InsertFrames(nsnull, aPrevFrame, aFrameList);
114 // no damage to intrinsic widths, since absolutely positioned frames can't
115 // change them
116 return aDelegatingFrame->PresContext()->PresShell()->
117 FrameNeedsReflow(aDelegatingFrame, nsIPresShell::eResize,
118 NS_FRAME_HAS_DIRTY_CHILDREN);
121 nsresult
122 nsAbsoluteContainingBlock::RemoveFrame(nsIFrame* aDelegatingFrame,
123 nsIAtom* aListName,
124 nsIFrame* aOldFrame)
126 NS_ASSERTION(GetChildListName() == aListName, "unexpected child list");
127 nsIFrame* nif = aOldFrame->GetNextInFlow();
128 if (nif) {
129 static_cast<nsContainerFrame*>(nif->GetParent())
130 ->DeleteNextInFlowChild(aOldFrame->PresContext(), nif);
133 PRBool result = mAbsoluteFrames.DestroyFrame(aOldFrame);
134 NS_ASSERTION(result, "didn't find frame to delete");
136 return result ? NS_OK : NS_ERROR_FAILURE;
139 nsresult
140 nsAbsoluteContainingBlock::Reflow(nsContainerFrame* aDelegatingFrame,
141 nsPresContext* aPresContext,
142 const nsHTMLReflowState& aReflowState,
143 nsReflowStatus& aReflowStatus,
144 nscoord aContainingBlockWidth,
145 nscoord aContainingBlockHeight,
146 PRBool aConstrainHeight,
147 PRBool aCBWidthChanged,
148 PRBool aCBHeightChanged,
149 nsRect* aChildBounds)
151 // Initialize OUT parameter
152 if (aChildBounds)
153 aChildBounds->SetRect(0, 0, 0, 0);
154 nsReflowStatus reflowStatus = NS_FRAME_COMPLETE;
156 PRBool reflowAll = aReflowState.ShouldReflowAllKids();
158 nsIFrame* kidFrame;
159 nsOverflowContinuationTracker tracker(aPresContext, aDelegatingFrame, PR_TRUE);
160 for (kidFrame = mAbsoluteFrames.FirstChild(); kidFrame; kidFrame = kidFrame->GetNextSibling()) {
161 if (reflowAll ||
162 NS_SUBTREE_DIRTY(kidFrame) ||
163 FrameDependsOnContainer(kidFrame, aCBWidthChanged, aCBHeightChanged)) {
164 // Reflow the frame
165 nsReflowStatus kidStatus = NS_FRAME_COMPLETE;
166 ReflowAbsoluteFrame(aDelegatingFrame, aPresContext, aReflowState,
167 aContainingBlockWidth, aContainingBlockHeight,
168 aConstrainHeight, kidFrame, kidStatus, aChildBounds);
169 nsIFrame* nextFrame = kidFrame->GetNextInFlow();
170 if (!NS_FRAME_IS_FULLY_COMPLETE(kidStatus)) {
171 // Need a continuation
172 if (!nextFrame) {
173 nsresult rv = nsHTMLContainerFrame::CreateNextInFlow(aPresContext,
174 aDelegatingFrame, kidFrame, nextFrame);
175 NS_ENSURE_SUCCESS(rv, rv);
176 kidFrame->SetNextSibling(nextFrame->GetNextSibling());
177 nextFrame->SetNextSibling(nsnull);
179 // Add it as an overflow container.
180 //XXXfr This is a hack to fix some of our printing dataloss.
181 // See bug 154892. Not sure how to do it "right" yet; probably want
182 // to keep continuations within an nsAbsoluteContainingBlock eventually.
183 tracker.Insert(nextFrame, kidStatus);
184 reflowStatus = NS_FRAME_MERGE_INCOMPLETE(reflowStatus, kidStatus);
186 else {
187 // Delete any continuations
188 if (nextFrame) {
189 tracker.Finish(kidFrame);
190 static_cast<nsContainerFrame*>(nextFrame->GetParent())
191 ->DeleteNextInFlowChild(aPresContext, nextFrame);
195 else {
196 tracker.Skip(kidFrame, reflowStatus);
197 if (aChildBounds) {
198 aChildBounds->UnionRect(*aChildBounds, kidFrame->GetOverflowRect() +
199 kidFrame->GetPosition());
203 // Abspos frames can't cause their parent to be incomplete,
204 // only overflow incomplete.
205 if (NS_FRAME_IS_NOT_COMPLETE(reflowStatus))
206 NS_FRAME_SET_OVERFLOW_INCOMPLETE(reflowStatus);
208 aReflowStatus = NS_FRAME_MERGE_INCOMPLETE(reflowStatus, aReflowStatus);
209 return NS_OK;
212 static inline PRBool IsFixedPaddingSize(nsStyleUnit aUnit) {
213 return aUnit == eStyleUnit_Coord || aUnit == eStyleUnit_Chars;
215 static inline PRBool IsFixedMarginSize(nsStyleUnit aUnit) {
216 return aUnit == eStyleUnit_Coord || aUnit == eStyleUnit_Chars;
218 static inline PRBool IsFixedMaxSize(nsStyleUnit aUnit) {
219 return aUnit == eStyleUnit_None || aUnit == eStyleUnit_Coord ||
220 aUnit == eStyleUnit_Chars;
222 static inline PRBool IsFixedOffset(nsStyleUnit aUnit) {
223 return aUnit == eStyleUnit_Coord || aUnit == eStyleUnit_Chars;
225 static inline PRBool IsFixedHeight(nsStyleUnit aUnit) {
226 return aUnit == eStyleUnit_Coord || aUnit == eStyleUnit_Chars;
229 static inline PRBool IsFixedWidth(const nsStyleCoord& aCoord)
231 return aCoord.GetUnit() == eStyleUnit_Coord ||
232 aCoord.GetUnit() == eStyleUnit_Chars ||
233 (aCoord.GetUnit() == eStyleUnit_Enumerated &&
234 (aCoord.GetIntValue() == NS_STYLE_WIDTH_MAX_CONTENT ||
235 aCoord.GetIntValue() == NS_STYLE_WIDTH_MIN_CONTENT));
238 static inline PRBool IsFixedMaxWidth(const nsStyleCoord& aCoord)
240 return aCoord.GetUnit() == eStyleUnit_None ||
241 IsFixedWidth(aCoord);
244 PRBool
245 nsAbsoluteContainingBlock::FrameDependsOnContainer(nsIFrame* f,
246 PRBool aCBWidthChanged,
247 PRBool aCBHeightChanged)
249 const nsStylePosition* pos = f->GetStylePosition();
250 // See if f's position might have changed because it depends on a
251 // placeholder's position
252 // This can happen in the following cases:
253 // 1) Vertical positioning. "top" must be auto and "bottom" must be auto
254 // (otherwise the vertical position is completely determined by
255 // whichever of them is not auto and the height).
256 // 2) Horizontal positioning. "left" must be auto and "right" must be auto
257 // (otherwise the horizontal position is completely determined by
258 // whichever of them is not auto and the width).
259 // See nsHTMLReflowState::InitAbsoluteConstraints -- these are the
260 // only cases when we call CalculateHypotheticalBox().
261 if ((pos->mOffset.GetTopUnit() == eStyleUnit_Auto &&
262 pos->mOffset.GetBottomUnit() == eStyleUnit_Auto) ||
263 (pos->mOffset.GetLeftUnit() == eStyleUnit_Auto &&
264 pos->mOffset.GetRightUnit() == eStyleUnit_Auto)) {
265 return PR_TRUE;
267 if (!aCBWidthChanged && !aCBHeightChanged) {
268 // skip getting style data
269 return PR_FALSE;
271 const nsStylePadding* padding = f->GetStylePadding();
272 const nsStyleMargin* margin = f->GetStyleMargin();
273 if (aCBWidthChanged) {
274 // See if f's width might have changed.
275 // If border-left, border-right, padding-left, padding-right,
276 // width, min-width, and max-width are all lengths, 'none', or enumerated,
277 // then our frame width does not depend on the parent width.
278 // Note that borders never depend on the parent width
279 // XXX All of the enumerated values except -moz-available are ok too.
280 if (!IsFixedWidth(pos->mWidth) ||
281 !IsFixedWidth(pos->mMinWidth) ||
282 !IsFixedMaxWidth(pos->mMaxWidth) ||
283 !IsFixedPaddingSize(padding->mPadding.GetLeftUnit()) ||
284 !IsFixedPaddingSize(padding->mPadding.GetRightUnit())) {
285 return PR_TRUE;
288 // See if f's position might have changed. If we're RTL then the
289 // rules are slightly different. We'll assume percentage or auto
290 // margins will always induce a dependency on the size
291 if (!IsFixedMarginSize(margin->mMargin.GetLeftUnit()) ||
292 !IsFixedMarginSize(margin->mMargin.GetRightUnit())) {
293 return PR_TRUE;
295 if (f->GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) {
296 // Note that even if 'left' is a length, our position can
297 // still depend on the containing block width, because if
298 // 'right' is also a length we will discard 'left' and be
299 // positioned relative to the containing block right edge.
300 // 'left' length and 'right' auto is the only combination
301 // we can be sure of.
302 if (!IsFixedOffset(pos->mOffset.GetLeftUnit()) ||
303 pos->mOffset.GetRightUnit() != eStyleUnit_Auto) {
304 return PR_TRUE;
306 } else {
307 if (!IsFixedOffset(pos->mOffset.GetLeftUnit())) {
308 return PR_TRUE;
312 if (aCBHeightChanged) {
313 // See if f's height might have changed.
314 // If border-top, border-bottom, padding-top, padding-bottom,
315 // min-height, and max-height are all lengths or 'none',
316 // and height is a length or height and bottom are auto and top is not auto,
317 // then our frame height does not depend on the parent height.
318 // Note that borders never depend on the parent height
319 if (!(IsFixedHeight(pos->mHeight.GetUnit()) ||
320 (pos->mHeight.GetUnit() == eStyleUnit_Auto &&
321 pos->mOffset.GetBottomUnit() == eStyleUnit_Auto &&
322 pos->mOffset.GetTopUnit() != eStyleUnit_Auto)) ||
323 !IsFixedHeight(pos->mMinHeight.GetUnit()) ||
324 !IsFixedMaxSize(pos->mMaxHeight.GetUnit()) ||
325 !IsFixedPaddingSize(padding->mPadding.GetTopUnit()) ||
326 !IsFixedPaddingSize(padding->mPadding.GetBottomUnit())) {
327 return PR_TRUE;
330 // See if f's position might have changed.
331 if (!IsFixedMarginSize(margin->mMargin.GetTopUnit()) ||
332 !IsFixedMarginSize(margin->mMargin.GetBottomUnit())) {
333 return PR_TRUE;
335 if (!IsFixedOffset(pos->mOffset.GetTopUnit())) {
336 return PR_TRUE;
339 return PR_FALSE;
342 void
343 nsAbsoluteContainingBlock::DestroyFrames(nsIFrame* aDelegatingFrame)
345 mAbsoluteFrames.DestroyFrames();
348 // XXX Optimize the case where it's a resize reflow and the absolutely
349 // positioned child has the exact same size and position and skip the
350 // reflow...
352 // When bug 154892 is checked in, make sure that when
353 // GetChildListName() == nsGkAtoms::fixedList, the height is unconstrained.
354 // since we don't allow replicated frames to split.
356 nsresult
357 nsAbsoluteContainingBlock::ReflowAbsoluteFrame(nsIFrame* aDelegatingFrame,
358 nsPresContext* aPresContext,
359 const nsHTMLReflowState& aReflowState,
360 nscoord aContainingBlockWidth,
361 nscoord aContainingBlockHeight,
362 PRBool aConstrainHeight,
363 nsIFrame* aKidFrame,
364 nsReflowStatus& aStatus,
365 nsRect* aChildBounds)
367 #ifdef DEBUG
368 if (nsBlockFrame::gNoisyReflow) {
369 nsFrame::IndentBy(stdout,nsBlockFrame::gNoiseIndent);
370 printf("abs pos ");
371 if (nsnull != aKidFrame) {
372 nsIFrameDebug* frameDebug;
373 if (NS_SUCCEEDED(CallQueryInterface(aKidFrame, &frameDebug))) {
374 nsAutoString name;
375 frameDebug->GetFrameName(name);
376 printf("%s ", NS_LossyConvertUTF16toASCII(name).get());
380 char width[16];
381 char height[16];
382 PrettyUC(aReflowState.availableWidth, width);
383 PrettyUC(aReflowState.availableHeight, height);
384 printf(" a=%s,%s ", width, height);
385 PrettyUC(aReflowState.ComputedWidth(), width);
386 PrettyUC(aReflowState.ComputedHeight(), height);
387 printf("c=%s,%s \n", width, height);
389 AutoNoisyIndenter indent(nsBlockFrame::gNoisy);
390 #endif // DEBUG
392 // Store position and overflow rect so taht we can invalidate the correct
393 // area if the position changes
394 nsRect oldOverflowRect(aKidFrame->GetOverflowRect() +
395 aKidFrame->GetPosition());
396 nsRect oldRect = aKidFrame->GetRect();
398 nsresult rv;
399 // Get the border values
400 const nsMargin& border = aReflowState.mStyleBorder->GetBorder();
402 nscoord availWidth = aContainingBlockWidth;
403 if (availWidth == -1) {
404 NS_ASSERTION(aReflowState.ComputedWidth() != NS_UNCONSTRAINEDSIZE,
405 "Must have a useful width _somewhere_");
406 availWidth =
407 aReflowState.ComputedWidth() + aReflowState.mComputedPadding.LeftRight();
410 nsHTMLReflowMetrics kidDesiredSize;
411 nsHTMLReflowState kidReflowState(aPresContext, aReflowState, aKidFrame,
412 nsSize(availWidth, NS_UNCONSTRAINEDSIZE),
413 aContainingBlockWidth,
414 aContainingBlockHeight);
416 // Send the WillReflow() notification and position the frame
417 aKidFrame->WillReflow(aPresContext);
419 PRBool constrainHeight = (aReflowState.availableHeight != NS_UNCONSTRAINEDSIZE)
420 && aConstrainHeight
421 // Don't split if told not to (e.g. for fixed frames)
422 && (aDelegatingFrame->GetType() != nsGkAtoms::positionedInlineFrame)
423 //XXX we don't handle splitting frames for inline absolute containing blocks yet
424 && (aKidFrame->GetRect().y <= aReflowState.availableHeight);
425 // Don't split things below the fold. (Ideally we shouldn't *have*
426 // anything totally below the fold, but we can't position frames
427 // across next-in-flow breaks yet.
428 if (constrainHeight) {
429 kidReflowState.availableHeight = aReflowState.availableHeight - aKidFrame->GetRect().y;
432 // Do the reflow
433 rv = aKidFrame->Reflow(aPresContext, kidDesiredSize, kidReflowState, aStatus);
435 // If we're solving for 'left' or 'top', then compute it now that we know the
436 // width/height
437 if ((NS_AUTOOFFSET == kidReflowState.mComputedOffsets.left) ||
438 (NS_AUTOOFFSET == kidReflowState.mComputedOffsets.top)) {
439 if (-1 == aContainingBlockWidth) {
440 // Get the containing block width/height
441 kidReflowState.ComputeContainingBlockRectangle(aPresContext,
442 &aReflowState,
443 aContainingBlockWidth,
444 aContainingBlockHeight);
447 if (NS_AUTOOFFSET == kidReflowState.mComputedOffsets.left) {
448 NS_ASSERTION(NS_AUTOOFFSET != kidReflowState.mComputedOffsets.right,
449 "Can't solve for both left and right");
450 kidReflowState.mComputedOffsets.left = aContainingBlockWidth -
451 kidReflowState.mComputedOffsets.right -
452 kidReflowState.mComputedMargin.right -
453 kidDesiredSize.width -
454 kidReflowState.mComputedMargin.left;
456 if (NS_AUTOOFFSET == kidReflowState.mComputedOffsets.top) {
457 kidReflowState.mComputedOffsets.top = aContainingBlockHeight -
458 kidReflowState.mComputedOffsets.bottom -
459 kidReflowState.mComputedMargin.bottom -
460 kidDesiredSize.height -
461 kidReflowState.mComputedMargin.top;
465 // Position the child relative to our padding edge
466 nsRect rect(border.left + kidReflowState.mComputedOffsets.left + kidReflowState.mComputedMargin.left,
467 border.top + kidReflowState.mComputedOffsets.top + kidReflowState.mComputedMargin.top,
468 kidDesiredSize.width, kidDesiredSize.height);
469 aKidFrame->SetRect(rect);
471 nsIView* view = aKidFrame->GetView();
472 if (view) {
473 // Size and position the view and set its opacity, visibility, content
474 // transparency, and clip
475 nsContainerFrame::SyncFrameViewAfterReflow(aPresContext, aKidFrame,
476 view,
477 &kidDesiredSize.mOverflowArea);
478 } else {
479 nsContainerFrame::PositionChildViews(aKidFrame);
482 if (oldRect.TopLeft() != rect.TopLeft() ||
483 (aDelegatingFrame->GetStateBits() & NS_FRAME_FIRST_REFLOW) ||
484 (kidDesiredSize.mOverflowArea + rect.TopLeft() != oldOverflowRect &&
485 (kidDesiredSize.mOverflowArea + rect.TopLeft() != rect || oldRect != oldOverflowRect))) {
486 // The frame moved; we have to invalidate the whole frame
487 // because the children may have moved after they were reflowed
488 // We also have to invalidate when we have overflow and the overflow
489 // changes because the change might be caused by clipping
490 // XXX This could be optimized in some cases, especially clipping changes
491 aKidFrame->GetParent()->Invalidate(oldOverflowRect);
492 aKidFrame->GetParent()->Invalidate(kidDesiredSize.mOverflowArea +
493 rect.TopLeft());
494 } else if (oldRect.Size() != rect.Size()) {
495 // Invalidate the area where the frame changed size.
496 nscoord innerWidth = PR_MIN(oldRect.width, rect.width);
497 nscoord innerHeight = PR_MIN(oldRect.height, rect.height);
498 nscoord outerWidth = PR_MAX(oldRect.width, rect.width);
499 nscoord outerHeight = PR_MAX(oldRect.height, rect.height);
500 aKidFrame->GetParent()->Invalidate(
501 nsRect(rect.x + innerWidth, rect.y, outerWidth - innerWidth, outerHeight));
502 // Invalidate the horizontal strip
503 aKidFrame->GetParent()->Invalidate(
504 nsRect(rect.x, rect.y + innerHeight, outerWidth, outerHeight - innerHeight));
506 aKidFrame->DidReflow(aPresContext, &kidReflowState, NS_FRAME_REFLOW_FINISHED);
508 #ifdef DEBUG
509 if (nsBlockFrame::gNoisyReflow) {
510 nsFrame::IndentBy(stdout,nsBlockFrame::gNoiseIndent - 1);
511 printf("abs pos ");
512 if (nsnull != aKidFrame) {
513 nsIFrameDebug* frameDebug;
514 if (NS_SUCCEEDED(CallQueryInterface(aKidFrame, &frameDebug))) {
515 nsAutoString name;
516 frameDebug->GetFrameName(name);
517 printf("%s ", NS_LossyConvertUTF16toASCII(name).get());
520 printf("%p rect=%d,%d,%d,%d", aKidFrame, rect.x, rect.y, rect.width, rect.height);
521 printf("\n");
523 #endif
525 if (aChildBounds)
526 aChildBounds->UnionRect(*aChildBounds, kidDesiredSize.mOverflowArea +
527 rect.TopLeft());
529 return rv;
532 #ifdef DEBUG
533 void nsAbsoluteContainingBlock::PrettyUC(nscoord aSize,
534 char* aBuf)
536 if (NS_UNCONSTRAINEDSIZE == aSize) {
537 strcpy(aBuf, "UC");
539 else {
540 if((PRInt32)0xdeadbeef == aSize)
542 strcpy(aBuf, "deadbeef");
544 else {
545 sprintf(aBuf, "%d", aSize);
549 #endif