Bug 54488 - "[Mac] Non-draggable widgets in background windows should look disabled...
[mozilla-central.git] / layout / generic / nsAbsoluteContainingBlock.cpp
blob9980ae526d17212dc792667a49e926bc6661a86f
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
55 nsresult
56 nsAbsoluteContainingBlock::SetInitialChildList(nsIFrame* aDelegatingFrame,
57 nsIAtom* aListName,
58 nsIFrame* aChildList)
60 NS_PRECONDITION(GetChildListName() == aListName, "unexpected child list name");
61 #ifdef NS_DEBUG
62 nsFrame::VerifyDirtyBitSet(aChildList);
63 #endif
64 mAbsoluteFrames.SetFrames(aChildList);
65 return NS_OK;
68 nsresult
69 nsAbsoluteContainingBlock::AppendFrames(nsIFrame* aDelegatingFrame,
70 nsIAtom* aListName,
71 nsIFrame* aFrameList)
73 NS_ASSERTION(GetChildListName() == aListName, "unexpected child list");
75 // Append the frames to our list of absolutely positioned frames
76 #ifdef NS_DEBUG
77 nsFrame::VerifyDirtyBitSet(aFrameList);
78 #endif
79 mAbsoluteFrames.AppendFrames(nsnull, aFrameList);
81 // no damage to intrinsic widths, since absolutely positioned frames can't
82 // change them
83 return aDelegatingFrame->PresContext()->PresShell()->
84 FrameNeedsReflow(aDelegatingFrame, nsIPresShell::eResize,
85 NS_FRAME_HAS_DIRTY_CHILDREN);
88 nsresult
89 nsAbsoluteContainingBlock::InsertFrames(nsIFrame* aDelegatingFrame,
90 nsIAtom* aListName,
91 nsIFrame* aPrevFrame,
92 nsIFrame* aFrameList)
94 NS_ASSERTION(GetChildListName() == aListName, "unexpected child list");
95 NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == aDelegatingFrame,
96 "inserting after sibling frame with different parent");
98 #ifdef NS_DEBUG
99 nsFrame::VerifyDirtyBitSet(aFrameList);
100 #endif
101 mAbsoluteFrames.InsertFrames(nsnull, aPrevFrame, aFrameList);
103 // no damage to intrinsic widths, since absolutely positioned frames can't
104 // change them
105 return aDelegatingFrame->PresContext()->PresShell()->
106 FrameNeedsReflow(aDelegatingFrame, nsIPresShell::eResize,
107 NS_FRAME_HAS_DIRTY_CHILDREN);
110 nsresult
111 nsAbsoluteContainingBlock::RemoveFrame(nsIFrame* aDelegatingFrame,
112 nsIAtom* aListName,
113 nsIFrame* aOldFrame)
115 NS_ASSERTION(GetChildListName() == aListName, "unexpected child list");
116 nsIFrame* nif = aOldFrame->GetNextInFlow();
117 if (nif) {
118 static_cast<nsContainerFrame*>(nif->GetParent())
119 ->DeleteNextInFlowChild(aOldFrame->PresContext(), nif);
122 PRBool result = mAbsoluteFrames.DestroyFrame(aOldFrame);
123 NS_ASSERTION(result, "didn't find frame to delete");
125 return result ? NS_OK : NS_ERROR_FAILURE;
128 nsresult
129 nsAbsoluteContainingBlock::Reflow(nsContainerFrame* aDelegatingFrame,
130 nsPresContext* aPresContext,
131 const nsHTMLReflowState& aReflowState,
132 nsReflowStatus& aReflowStatus,
133 nscoord aContainingBlockWidth,
134 nscoord aContainingBlockHeight,
135 PRBool aConstrainHeight,
136 PRBool aCBWidthChanged,
137 PRBool aCBHeightChanged,
138 nsRect* aChildBounds)
140 // Initialize OUT parameter
141 if (aChildBounds)
142 aChildBounds->SetRect(0, 0, 0, 0);
143 nsReflowStatus reflowStatus = NS_FRAME_COMPLETE;
145 PRBool reflowAll = aReflowState.ShouldReflowAllKids();
147 nsIFrame* kidFrame;
148 nsOverflowContinuationTracker tracker(aPresContext, aDelegatingFrame, PR_TRUE);
149 for (kidFrame = mAbsoluteFrames.FirstChild(); kidFrame; kidFrame = kidFrame->GetNextSibling()) {
150 if (reflowAll ||
151 NS_SUBTREE_DIRTY(kidFrame) ||
152 FrameDependsOnContainer(kidFrame, aCBWidthChanged, aCBHeightChanged)) {
153 // Reflow the frame
154 nsReflowStatus kidStatus = NS_FRAME_COMPLETE;
155 ReflowAbsoluteFrame(aDelegatingFrame, aPresContext, aReflowState,
156 aContainingBlockWidth, aContainingBlockHeight,
157 aConstrainHeight, kidFrame, kidStatus, aChildBounds);
158 nsIFrame* nextFrame = kidFrame->GetNextInFlow();
159 if (!NS_FRAME_IS_FULLY_COMPLETE(kidStatus)) {
160 // Need a continuation
161 if (!nextFrame) {
162 nsresult rv = nsHTMLContainerFrame::CreateNextInFlow(aPresContext,
163 aDelegatingFrame, kidFrame, nextFrame);
164 NS_ENSURE_SUCCESS(rv, rv);
165 kidFrame->SetNextSibling(nextFrame->GetNextSibling());
166 nextFrame->SetNextSibling(nsnull);
168 // Add it as an overflow container.
169 //XXXfr This is a hack to fix some of our printing dataloss.
170 // See bug 154892. Not sure how to do it "right" yet; probably want
171 // to keep continuations within an nsAbsoluteContainingBlock eventually.
172 tracker.Insert(nextFrame, kidStatus);
173 NS_MergeReflowStatusInto(&reflowStatus, kidStatus);
175 else {
176 // Delete any continuations
177 if (nextFrame) {
178 tracker.Finish(kidFrame);
179 static_cast<nsContainerFrame*>(nextFrame->GetParent())
180 ->DeleteNextInFlowChild(aPresContext, nextFrame);
184 else {
185 tracker.Skip(kidFrame, reflowStatus);
186 if (aChildBounds) {
187 aChildBounds->UnionRect(*aChildBounds, kidFrame->GetOverflowRect() +
188 kidFrame->GetPosition());
192 // Abspos frames can't cause their parent to be incomplete,
193 // only overflow incomplete.
194 if (NS_FRAME_IS_NOT_COMPLETE(reflowStatus))
195 NS_FRAME_SET_OVERFLOW_INCOMPLETE(reflowStatus);
197 NS_MergeReflowStatusInto(&aReflowStatus, reflowStatus);
198 return NS_OK;
201 static inline PRBool IsFixedPaddingSize(nsStyleUnit aUnit) {
202 return aUnit == eStyleUnit_Coord;
204 static inline PRBool IsFixedMarginSize(nsStyleUnit aUnit) {
205 return aUnit == eStyleUnit_Coord;
207 static inline PRBool IsFixedMaxSize(nsStyleUnit aUnit) {
208 return aUnit == eStyleUnit_None || aUnit == eStyleUnit_Coord;
210 static inline PRBool IsFixedOffset(nsStyleUnit aUnit) {
211 return aUnit == eStyleUnit_Coord;
213 static inline PRBool IsFixedHeight(nsStyleUnit aUnit) {
214 return aUnit == eStyleUnit_Coord;
217 static inline PRBool IsFixedWidth(const nsStyleCoord& aCoord)
219 return aCoord.GetUnit() == eStyleUnit_Coord ||
220 (aCoord.GetUnit() == eStyleUnit_Enumerated &&
221 (aCoord.GetIntValue() == NS_STYLE_WIDTH_MAX_CONTENT ||
222 aCoord.GetIntValue() == NS_STYLE_WIDTH_MIN_CONTENT));
225 static inline PRBool IsFixedMaxWidth(const nsStyleCoord& aCoord)
227 return aCoord.GetUnit() == eStyleUnit_None ||
228 IsFixedWidth(aCoord);
231 PRBool
232 nsAbsoluteContainingBlock::FrameDependsOnContainer(nsIFrame* f,
233 PRBool aCBWidthChanged,
234 PRBool aCBHeightChanged)
236 const nsStylePosition* pos = f->GetStylePosition();
237 // See if f's position might have changed because it depends on a
238 // placeholder's position
239 // This can happen in the following cases:
240 // 1) Vertical positioning. "top" must be auto and "bottom" must be auto
241 // (otherwise the vertical position is completely determined by
242 // whichever of them is not auto and the height).
243 // 2) Horizontal positioning. "left" must be auto and "right" must be auto
244 // (otherwise the horizontal position is completely determined by
245 // whichever of them is not auto and the width).
246 // See nsHTMLReflowState::InitAbsoluteConstraints -- these are the
247 // only cases when we call CalculateHypotheticalBox().
248 if ((pos->mOffset.GetTopUnit() == eStyleUnit_Auto &&
249 pos->mOffset.GetBottomUnit() == eStyleUnit_Auto) ||
250 (pos->mOffset.GetLeftUnit() == eStyleUnit_Auto &&
251 pos->mOffset.GetRightUnit() == eStyleUnit_Auto)) {
252 return PR_TRUE;
254 if (!aCBWidthChanged && !aCBHeightChanged) {
255 // skip getting style data
256 return PR_FALSE;
258 const nsStylePadding* padding = f->GetStylePadding();
259 const nsStyleMargin* margin = f->GetStyleMargin();
260 if (aCBWidthChanged) {
261 // See if f's width might have changed.
262 // If border-left, border-right, padding-left, padding-right,
263 // width, min-width, and max-width are all lengths, 'none', or enumerated,
264 // then our frame width does not depend on the parent width.
265 // Note that borders never depend on the parent width
266 // XXX All of the enumerated values except -moz-available are ok too.
267 if (!IsFixedWidth(pos->mWidth) ||
268 !IsFixedWidth(pos->mMinWidth) ||
269 !IsFixedMaxWidth(pos->mMaxWidth) ||
270 !IsFixedPaddingSize(padding->mPadding.GetLeftUnit()) ||
271 !IsFixedPaddingSize(padding->mPadding.GetRightUnit())) {
272 return PR_TRUE;
275 // See if f's position might have changed. If we're RTL then the
276 // rules are slightly different. We'll assume percentage or auto
277 // margins will always induce a dependency on the size
278 if (!IsFixedMarginSize(margin->mMargin.GetLeftUnit()) ||
279 !IsFixedMarginSize(margin->mMargin.GetRightUnit())) {
280 return PR_TRUE;
282 if (f->GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) {
283 // Note that even if 'left' is a length, our position can
284 // still depend on the containing block width, because if
285 // 'right' is also a length we will discard 'left' and be
286 // positioned relative to the containing block right edge.
287 // 'left' length and 'right' auto is the only combination
288 // we can be sure of.
289 if (!IsFixedOffset(pos->mOffset.GetLeftUnit()) ||
290 pos->mOffset.GetRightUnit() != eStyleUnit_Auto) {
291 return PR_TRUE;
293 } else {
294 if (!IsFixedOffset(pos->mOffset.GetLeftUnit())) {
295 return PR_TRUE;
299 if (aCBHeightChanged) {
300 // See if f's height might have changed.
301 // If border-top, border-bottom, padding-top, padding-bottom,
302 // min-height, and max-height are all lengths or 'none',
303 // and height is a length or height and bottom are auto and top is not auto,
304 // then our frame height does not depend on the parent height.
305 // Note that borders never depend on the parent height
306 if (!(IsFixedHeight(pos->mHeight.GetUnit()) ||
307 (pos->mHeight.GetUnit() == eStyleUnit_Auto &&
308 pos->mOffset.GetBottomUnit() == eStyleUnit_Auto &&
309 pos->mOffset.GetTopUnit() != eStyleUnit_Auto)) ||
310 !IsFixedHeight(pos->mMinHeight.GetUnit()) ||
311 !IsFixedMaxSize(pos->mMaxHeight.GetUnit()) ||
312 !IsFixedPaddingSize(padding->mPadding.GetTopUnit()) ||
313 !IsFixedPaddingSize(padding->mPadding.GetBottomUnit())) {
314 return PR_TRUE;
317 // See if f's position might have changed.
318 if (!IsFixedMarginSize(margin->mMargin.GetTopUnit()) ||
319 !IsFixedMarginSize(margin->mMargin.GetBottomUnit())) {
320 return PR_TRUE;
322 if (!IsFixedOffset(pos->mOffset.GetTopUnit())) {
323 return PR_TRUE;
326 return PR_FALSE;
329 void
330 nsAbsoluteContainingBlock::DestroyFrames(nsIFrame* aDelegatingFrame)
332 mAbsoluteFrames.DestroyFrames();
335 // XXX Optimize the case where it's a resize reflow and the absolutely
336 // positioned child has the exact same size and position and skip the
337 // reflow...
339 // When bug 154892 is checked in, make sure that when
340 // GetChildListName() == nsGkAtoms::fixedList, the height is unconstrained.
341 // since we don't allow replicated frames to split.
343 nsresult
344 nsAbsoluteContainingBlock::ReflowAbsoluteFrame(nsIFrame* aDelegatingFrame,
345 nsPresContext* aPresContext,
346 const nsHTMLReflowState& aReflowState,
347 nscoord aContainingBlockWidth,
348 nscoord aContainingBlockHeight,
349 PRBool aConstrainHeight,
350 nsIFrame* aKidFrame,
351 nsReflowStatus& aStatus,
352 nsRect* aChildBounds)
354 #ifdef DEBUG
355 if (nsBlockFrame::gNoisyReflow) {
356 nsFrame::IndentBy(stdout,nsBlockFrame::gNoiseIndent);
357 printf("abs pos ");
358 if (nsnull != aKidFrame) {
359 nsIFrameDebug* frameDebug;
360 if (NS_SUCCEEDED(CallQueryInterface(aKidFrame, &frameDebug))) {
361 nsAutoString name;
362 frameDebug->GetFrameName(name);
363 printf("%s ", NS_LossyConvertUTF16toASCII(name).get());
367 char width[16];
368 char height[16];
369 PrettyUC(aReflowState.availableWidth, width);
370 PrettyUC(aReflowState.availableHeight, height);
371 printf(" a=%s,%s ", width, height);
372 PrettyUC(aReflowState.ComputedWidth(), width);
373 PrettyUC(aReflowState.ComputedHeight(), height);
374 printf("c=%s,%s \n", width, height);
376 AutoNoisyIndenter indent(nsBlockFrame::gNoisy);
377 #endif // DEBUG
379 // Store position and overflow rect so taht we can invalidate the correct
380 // area if the position changes
381 nsRect oldOverflowRect(aKidFrame->GetOverflowRect() +
382 aKidFrame->GetPosition());
383 nsRect oldRect = aKidFrame->GetRect();
385 nsresult rv;
386 // Get the border values
387 const nsMargin& border = aReflowState.mStyleBorder->GetActualBorder();
389 nscoord availWidth = aContainingBlockWidth;
390 if (availWidth == -1) {
391 NS_ASSERTION(aReflowState.ComputedWidth() != NS_UNCONSTRAINEDSIZE,
392 "Must have a useful width _somewhere_");
393 availWidth =
394 aReflowState.ComputedWidth() + aReflowState.mComputedPadding.LeftRight();
397 nsHTMLReflowMetrics kidDesiredSize;
398 nsHTMLReflowState kidReflowState(aPresContext, aReflowState, aKidFrame,
399 nsSize(availWidth, NS_UNCONSTRAINEDSIZE),
400 aContainingBlockWidth,
401 aContainingBlockHeight);
403 // Send the WillReflow() notification and position the frame
404 aKidFrame->WillReflow(aPresContext);
406 PRBool constrainHeight = (aReflowState.availableHeight != NS_UNCONSTRAINEDSIZE)
407 && aConstrainHeight
408 // Don't split if told not to (e.g. for fixed frames)
409 && (aDelegatingFrame->GetType() != nsGkAtoms::positionedInlineFrame)
410 //XXX we don't handle splitting frames for inline absolute containing blocks yet
411 && (aKidFrame->GetRect().y <= aReflowState.availableHeight);
412 // Don't split things below the fold. (Ideally we shouldn't *have*
413 // anything totally below the fold, but we can't position frames
414 // across next-in-flow breaks yet.
415 if (constrainHeight) {
416 kidReflowState.availableHeight = aReflowState.availableHeight - border.top
417 - kidReflowState.mComputedMargin.top;
418 if (NS_AUTOOFFSET != kidReflowState.mComputedOffsets.top)
419 kidReflowState.availableHeight -= kidReflowState.mComputedOffsets.top;
422 // Do the reflow
423 rv = aKidFrame->Reflow(aPresContext, kidDesiredSize, kidReflowState, aStatus);
425 // If we're solving for 'left' or 'top', then compute it now that we know the
426 // width/height
427 if ((NS_AUTOOFFSET == kidReflowState.mComputedOffsets.left) ||
428 (NS_AUTOOFFSET == kidReflowState.mComputedOffsets.top)) {
429 if (-1 == aContainingBlockWidth) {
430 // Get the containing block width/height
431 kidReflowState.ComputeContainingBlockRectangle(aPresContext,
432 &aReflowState,
433 aContainingBlockWidth,
434 aContainingBlockHeight);
437 if (NS_AUTOOFFSET == kidReflowState.mComputedOffsets.left) {
438 NS_ASSERTION(NS_AUTOOFFSET != kidReflowState.mComputedOffsets.right,
439 "Can't solve for both left and right");
440 kidReflowState.mComputedOffsets.left = aContainingBlockWidth -
441 kidReflowState.mComputedOffsets.right -
442 kidReflowState.mComputedMargin.right -
443 kidDesiredSize.width -
444 kidReflowState.mComputedMargin.left;
446 if (NS_AUTOOFFSET == kidReflowState.mComputedOffsets.top) {
447 kidReflowState.mComputedOffsets.top = aContainingBlockHeight -
448 kidReflowState.mComputedOffsets.bottom -
449 kidReflowState.mComputedMargin.bottom -
450 kidDesiredSize.height -
451 kidReflowState.mComputedMargin.top;
455 // Position the child relative to our padding edge
456 nsRect rect(border.left + kidReflowState.mComputedOffsets.left + kidReflowState.mComputedMargin.left,
457 border.top + kidReflowState.mComputedOffsets.top + kidReflowState.mComputedMargin.top,
458 kidDesiredSize.width, kidDesiredSize.height);
459 aKidFrame->SetRect(rect);
461 nsIView* view = aKidFrame->GetView();
462 if (view) {
463 // Size and position the view and set its opacity, visibility, content
464 // transparency, and clip
465 nsContainerFrame::SyncFrameViewAfterReflow(aPresContext, aKidFrame,
466 view,
467 &kidDesiredSize.mOverflowArea);
468 } else {
469 nsContainerFrame::PositionChildViews(aKidFrame);
472 if (oldRect.TopLeft() != rect.TopLeft() ||
473 (aDelegatingFrame->GetStateBits() & NS_FRAME_FIRST_REFLOW) ||
474 (kidDesiredSize.mOverflowArea + rect.TopLeft() != oldOverflowRect &&
475 (kidDesiredSize.mOverflowArea + rect.TopLeft() != rect || oldRect != oldOverflowRect))) {
476 // The frame moved; we have to invalidate the whole frame
477 // because the children may have moved after they were reflowed
478 // We also have to invalidate when we have overflow and the overflow
479 // changes because the change might be caused by clipping
480 // XXX This could be optimized in some cases, especially clipping changes
481 aKidFrame->GetParent()->Invalidate(oldOverflowRect);
482 aKidFrame->GetParent()->Invalidate(kidDesiredSize.mOverflowArea +
483 rect.TopLeft());
484 } else if (oldRect.Size() != rect.Size()) {
485 // Invalidate the area where the frame changed size.
486 nscoord innerWidth = PR_MIN(oldRect.width, rect.width);
487 nscoord innerHeight = PR_MIN(oldRect.height, rect.height);
488 nscoord outerWidth = PR_MAX(oldRect.width, rect.width);
489 nscoord outerHeight = PR_MAX(oldRect.height, rect.height);
490 aKidFrame->GetParent()->Invalidate(
491 nsRect(rect.x + innerWidth, rect.y, outerWidth - innerWidth, outerHeight));
492 // Invalidate the horizontal strip
493 aKidFrame->GetParent()->Invalidate(
494 nsRect(rect.x, rect.y + innerHeight, outerWidth, outerHeight - innerHeight));
496 aKidFrame->DidReflow(aPresContext, &kidReflowState, NS_FRAME_REFLOW_FINISHED);
498 #ifdef DEBUG
499 if (nsBlockFrame::gNoisyReflow) {
500 nsFrame::IndentBy(stdout,nsBlockFrame::gNoiseIndent - 1);
501 printf("abs pos ");
502 if (nsnull != aKidFrame) {
503 nsIFrameDebug* frameDebug;
504 if (NS_SUCCEEDED(CallQueryInterface(aKidFrame, &frameDebug))) {
505 nsAutoString name;
506 frameDebug->GetFrameName(name);
507 printf("%s ", NS_LossyConvertUTF16toASCII(name).get());
510 printf("%p rect=%d,%d,%d,%d", aKidFrame, rect.x, rect.y, rect.width, rect.height);
511 printf("\n");
513 #endif
515 if (aChildBounds)
516 aChildBounds->UnionRect(*aChildBounds, kidDesiredSize.mOverflowArea +
517 rect.TopLeft());
519 return rv;
522 #ifdef DEBUG
523 void nsAbsoluteContainingBlock::PrettyUC(nscoord aSize,
524 char* aBuf)
526 if (NS_UNCONSTRAINEDSIZE == aSize) {
527 strcpy(aBuf, "UC");
529 else {
530 if((PRInt32)0xdeadbeef == aSize)
532 strcpy(aBuf, "deadbeef");
534 else {
535 sprintf(aBuf, "%d", aSize);
539 #endif