Bug 54488 - "[Mac] Non-draggable widgets in background windows should look disabled...
[mozilla-central.git] / layout / generic / nsInlineFrame.cpp
blob0676be3337f7b2a8d05ba48445f434add65681d0
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 Communicator client 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 /* rendering object for CSS display:inline objects */
40 #include "nsCOMPtr.h"
41 #include "nsInlineFrame.h"
42 #include "nsBlockFrame.h"
43 #include "nsGkAtoms.h"
44 #include "nsHTMLParts.h"
45 #include "nsStyleContext.h"
46 #include "nsIPresShell.h"
47 #include "nsPresContext.h"
48 #include "nsIRenderingContext.h"
49 #include "nsIFontMetrics.h"
50 #include "nsAbsoluteContainingBlock.h"
51 #include "nsCSSAnonBoxes.h"
52 #include "nsAutoPtr.h"
53 #include "nsFrameManager.h"
54 #ifdef ACCESSIBILITY
55 #include "nsIServiceManager.h"
56 #include "nsIAccessibilityService.h"
57 #endif
58 #include "nsDisplayList.h"
60 #ifdef DEBUG
61 #undef NOISY_PUSHING
62 #endif
65 NS_DEFINE_IID(kInlineFrameCID, NS_INLINE_FRAME_CID);
68 //////////////////////////////////////////////////////////////////////
70 // Basic nsInlineFrame methods
72 nsIFrame*
73 NS_NewInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
75 return new (aPresShell) nsInlineFrame(aContext);
78 NS_IMETHODIMP
79 nsInlineFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr)
81 NS_PRECONDITION(aInstancePtr, "null out param");
83 if (aIID.Equals(kInlineFrameCID)) {
84 *aInstancePtr = this;
85 return NS_OK;
88 return nsInlineFrameSuper::QueryInterface(aIID, aInstancePtr);
91 #ifdef DEBUG
92 NS_IMETHODIMP
93 nsInlineFrame::GetFrameName(nsAString& aResult) const
95 return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult);
97 #endif
99 nsIAtom*
100 nsInlineFrame::GetType() const
102 return nsGkAtoms::inlineFrame;
105 static inline PRBool
106 IsPaddingZero(nsStyleUnit aUnit, const nsStyleCoord &aCoord)
108 return ((aUnit == eStyleUnit_Coord && aCoord.GetCoordValue() == 0) ||
109 (aUnit == eStyleUnit_Percent && aCoord.GetPercentValue() == 0.0));
112 static inline PRBool
113 IsMarginZero(nsStyleUnit aUnit, const nsStyleCoord &aCoord)
115 return (aUnit == eStyleUnit_Auto ||
116 (aUnit == eStyleUnit_Coord && aCoord.GetCoordValue() == 0) ||
117 (aUnit == eStyleUnit_Percent && aCoord.GetPercentValue() == 0.0));
120 /* virtual */ PRBool
121 nsInlineFrame::IsSelfEmpty()
123 #if 0
124 // I used to think inline frames worked this way, but it seems they
125 // don't. At least not in our codebase.
126 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
127 return PR_FALSE;
129 #endif
130 const nsStyleMargin* margin = GetStyleMargin();
131 const nsStyleBorder* border = GetStyleBorder();
132 const nsStylePadding* padding = GetStylePadding();
133 // XXX Top and bottom removed, since they shouldn't affect things, but this
134 // doesn't really match with nsLineLayout.cpp's setting of
135 // ZeroEffectiveSpanBox, anymore, so what should this really be?
136 if (border->GetActualBorderWidth(NS_SIDE_RIGHT) != 0 ||
137 border->GetActualBorderWidth(NS_SIDE_LEFT) != 0 ||
138 !IsPaddingZero(padding->mPadding.GetRightUnit(),
139 padding->mPadding.GetRight()) ||
140 !IsPaddingZero(padding->mPadding.GetLeftUnit(),
141 padding->mPadding.GetLeft()) ||
142 !IsMarginZero(margin->mMargin.GetRightUnit(),
143 margin->mMargin.GetRight()) ||
144 !IsMarginZero(margin->mMargin.GetLeftUnit(),
145 margin->mMargin.GetLeft())) {
146 return PR_FALSE;
148 return PR_TRUE;
151 PRBool
152 nsInlineFrame::IsEmpty()
154 if (!IsSelfEmpty()) {
155 return PR_FALSE;
158 for (nsIFrame *kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
159 if (!kid->IsEmpty())
160 return PR_FALSE;
163 return PR_TRUE;
166 PRBool
167 nsInlineFrame::PeekOffsetCharacter(PRBool aForward, PRInt32* aOffset)
169 // Override the implementation in nsFrame, to skip empty inline frames
170 NS_ASSERTION (aOffset && *aOffset <= 1, "aOffset out of range");
171 PRInt32 startOffset = *aOffset;
172 if (startOffset < 0)
173 startOffset = 1;
174 if (aForward == (startOffset == 0)) {
175 // We're before the frame and moving forward, or after it and moving backwards:
176 // skip to the other side, but keep going.
177 *aOffset = 1 - startOffset;
179 return PR_FALSE;
182 NS_IMETHODIMP
183 nsInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
184 const nsRect& aDirtyRect,
185 const nsDisplayListSet& aLists)
187 nsresult rv = nsHTMLContainerFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
188 NS_ENSURE_SUCCESS(rv, rv);
190 // The sole purpose of this is to trigger display of the selection
191 // window for Named Anchors, which don't have any children and
192 // normally don't have any size, but in Editor we use CSS to display
193 // an image to represent this "hidden" element.
194 if (!mFrames.FirstChild()) {
195 rv = DisplaySelectionOverlay(aBuilder, aLists);
197 return rv;
200 //////////////////////////////////////////////////////////////////////
201 // Reflow methods
203 /* virtual */ void
204 nsInlineFrame::AddInlineMinWidth(nsIRenderingContext *aRenderingContext,
205 nsIFrame::InlineMinWidthData *aData)
207 DoInlineIntrinsicWidth(aRenderingContext, aData, nsLayoutUtils::MIN_WIDTH);
210 /* virtual */ void
211 nsInlineFrame::AddInlinePrefWidth(nsIRenderingContext *aRenderingContext,
212 nsIFrame::InlinePrefWidthData *aData)
214 DoInlineIntrinsicWidth(aRenderingContext, aData, nsLayoutUtils::PREF_WIDTH);
217 /* virtual */ nsSize
218 nsInlineFrame::ComputeSize(nsIRenderingContext *aRenderingContext,
219 nsSize aCBSize, nscoord aAvailableWidth,
220 nsSize aMargin, nsSize aBorder, nsSize aPadding,
221 PRBool aShrinkWrap)
223 // Inlines and text don't compute size before reflow.
224 return nsSize(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
227 nsRect
228 nsInlineFrame::ComputeTightBounds(gfxContext* aContext) const
230 // be conservative
231 if (GetStyleContext()->HasTextDecorations())
232 return GetOverflowRect();
233 return ComputeSimpleTightBounds(aContext);
236 void
237 nsInlineFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
238 nsIFrame* aFrame,
239 PRBool aReparentSiblings)
241 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
242 aOurLineContainer->GetPrevContinuation(),
243 "Don't call this when we have no continuation, it's a waste");
244 if (!aFrame) {
245 NS_ASSERTION(aReparentSiblings, "Why did we get called?");
246 return;
249 nsIFrame* ancestor = aFrame;
250 nsIFrame* ancestorBlockChild;
251 do {
252 ancestorBlockChild = ancestor;
253 ancestor = ancestor->GetParent();
254 if (!ancestor)
255 return;
256 } while (!ancestor->IsFloatContainingBlock());
258 if (ancestor == aOurLineContainer)
259 return;
261 nsBlockFrame* ourBlock = nsLayoutUtils::GetAsBlock(aOurLineContainer);
262 NS_ASSERTION(ourBlock, "Not a block, but broke vertically?");
263 nsBlockFrame* frameBlock = nsLayoutUtils::GetAsBlock(ancestor);
264 NS_ASSERTION(frameBlock, "ancestor not a block");
266 nsFrameList blockChildren(ancestor->GetFirstChild(nsnull));
267 PRBool isOverflow = !blockChildren.ContainsFrame(ancestorBlockChild);
269 while (PR_TRUE) {
270 ourBlock->ReparentFloats(aFrame, frameBlock, isOverflow, PR_FALSE);
272 if (!aReparentSiblings)
273 return;
274 nsIFrame* next = aFrame->GetNextSibling();
275 if (!next)
276 return;
277 if (next->GetParent() == aFrame->GetParent()) {
278 aFrame = next;
279 continue;
281 // This is paranoid and will hardly ever get hit ... but we can't actually
282 // trust that the frames in the sibling chain all have the same parent,
283 // because lazy reparenting may be going on. If we find a different
284 // parent we need to redo our analysis.
285 ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
286 return;
290 NS_IMETHODIMP
291 nsInlineFrame::Reflow(nsPresContext* aPresContext,
292 nsHTMLReflowMetrics& aMetrics,
293 const nsHTMLReflowState& aReflowState,
294 nsReflowStatus& aStatus)
296 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
297 DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
298 if (nsnull == aReflowState.mLineLayout) {
299 return NS_ERROR_INVALID_ARG;
302 PRBool lazilySetParentPointer = PR_FALSE;
304 nsIFrame* lineContainer = aReflowState.mLineLayout->GetLineContainerFrame();
306 // Check for an overflow list with our prev-in-flow
307 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
308 if (nsnull != prevInFlow) {
309 nsIFrame* prevOverflowFrames = prevInFlow->GetOverflowFrames(aPresContext, PR_TRUE);
311 if (prevOverflowFrames) {
312 // When pushing and pulling frames we need to check for whether any
313 // views need to be reparented.
314 nsHTMLContainerFrame::ReparentFrameViewList(aPresContext, prevOverflowFrames,
315 prevInFlow, this);
317 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
318 // If it's the initial reflow, then our child list must be empty, so
319 // just set the child list rather than calling InsertFrame(). This avoids
320 // having to get the last child frame in the list.
321 // Note that we don't set the parent pointer for the new frames. Instead wait
322 // to do this until we actually reflow the frame. If the overflow list contains
323 // thousands of frames this is a big performance issue (see bug #5588)
324 NS_ASSERTION(mFrames.IsEmpty(), "child list is not empty for initial reflow");
325 mFrames.SetFrames(prevOverflowFrames);
326 lazilySetParentPointer = PR_TRUE;
327 } else {
328 // Assign all floats to our block if necessary
329 if (lineContainer && lineContainer->GetPrevContinuation()) {
330 ReparentFloatsForInlineChild(lineContainer, prevOverflowFrames, PR_TRUE);
332 // Insert the new frames at the beginning of the child list
333 // and set their parent pointer
334 mFrames.InsertFrames(this, nsnull, prevOverflowFrames);
339 // It's also possible that we have an overflow list for ourselves
340 #ifdef DEBUG
341 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
342 // If it's our initial reflow, then we should not have an overflow list.
343 // However, add an assertion in case we get reflowed more than once with
344 // the initial reflow reason
345 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_FALSE);
346 NS_ASSERTION(!overflowFrames, "overflow list is not empty for initial reflow");
348 #endif
349 if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
350 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_TRUE);
351 if (overflowFrames) {
352 NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
354 // Because we lazily set the parent pointer of child frames we get from
355 // our prev-in-flow's overflow list, it's possible that we have not set
356 // the parent pointer for these frames.
357 mFrames.AppendFrames(this, overflowFrames);
361 if (IsFrameTreeTooDeep(aReflowState, aMetrics)) {
362 #ifdef DEBUG_kipp
364 extern char* nsPresShell_ReflowStackPointerTop;
365 char marker;
366 char* newsp = (char*) &marker;
367 printf("XXX: frame tree is too deep; approx stack size = %d\n",
368 nsPresShell_ReflowStackPointerTop - newsp);
370 #endif
371 aStatus = NS_FRAME_COMPLETE;
372 return NS_OK;
375 // Set our own reflow state (additional state above and beyond
376 // aReflowState)
377 InlineReflowState irs;
378 irs.mPrevFrame = nsnull;
379 irs.mLineContainer = lineContainer;
380 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
381 irs.mSetParentPointer = lazilySetParentPointer;
383 nsresult rv;
384 if (mFrames.IsEmpty()) {
385 // Try to pull over one frame before starting so that we know
386 // whether we have an anonymous block or not.
387 PRBool complete;
388 (void) PullOneFrame(aPresContext, irs, &complete);
391 rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
393 // Note: the line layout code will properly compute our
394 // NS_FRAME_OUTSIDE_CHILDREN state for us.
396 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
397 return rv;
400 /* virtual */ PRBool
401 nsInlineFrame::CanContinueTextRun() const
403 // We can continue a text run through an inline frame
404 return PR_TRUE;
407 nsresult
408 nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
409 const nsHTMLReflowState& aReflowState,
410 InlineReflowState& irs,
411 nsHTMLReflowMetrics& aMetrics,
412 nsReflowStatus& aStatus)
414 nsresult rv = NS_OK;
415 aStatus = NS_FRAME_COMPLETE;
417 nsLineLayout* lineLayout = aReflowState.mLineLayout;
418 PRBool ltr = (NS_STYLE_DIRECTION_LTR == aReflowState.mStyleVisibility->mDirection);
419 nscoord leftEdge = 0;
420 if (nsnull == GetPrevContinuation()) {
421 leftEdge = ltr ? aReflowState.mComputedBorderPadding.left
422 : aReflowState.mComputedBorderPadding.right;
424 nscoord availableWidth = aReflowState.availableWidth;
425 NS_ASSERTION(availableWidth != NS_UNCONSTRAINEDSIZE,
426 "should no longer use available widths");
427 // Subtract off left and right border+padding from availableWidth
428 availableWidth -= leftEdge;
429 availableWidth -= ltr ? aReflowState.mComputedBorderPadding.right
430 : aReflowState.mComputedBorderPadding.left;
431 lineLayout->BeginSpan(this, &aReflowState, leftEdge, leftEdge + availableWidth);
433 // First reflow our current children
434 nsIFrame* frame = mFrames.FirstChild();
435 PRBool done = PR_FALSE;
436 while (nsnull != frame) {
437 PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
439 // Check if we should lazily set the child frame's parent pointer
440 if (irs.mSetParentPointer) {
441 PRBool havePrevBlock =
442 irs.mLineContainer && irs.mLineContainer->GetPrevContinuation();
443 // If our block is the first in flow, then any floats under the pulled
444 // frame must already belong to our block.
445 if (havePrevBlock) {
446 // This has to happen before we update frame's parent; we need to
447 // know frame's ancestry under its old block.
448 // The blockChildren.ContainsFrame check performed by
449 // ReparentFloatsForInlineChild here may be slow, but we can't
450 // easily avoid it because we don't know where 'frame' originally
451 // came from. If we really really have to optimize this we could
452 // cache whether frame->GetParent() is under its containing blocks
453 // overflowList or not.
454 ReparentFloatsForInlineChild(irs.mLineContainer, frame, PR_FALSE);
456 frame->SetParent(this);
457 // We also need to check if frame has a next-in-flow. If it does, then set
458 // its parent frame pointer, too. Otherwise, if we reflow frame and it's
459 // complete we'll fail when deleting its next-in-flow which is no longer
460 // needed. This scenario doesn't happen often, but it can happen
461 nsIFrame* nextInFlow = frame->GetNextInFlow();
462 while (nextInFlow) {
463 // Since we only do lazy setting of parent pointers for the frame's
464 // initial reflow, this frame can't have a next-in-flow. That means
465 // the continuing child frame must be in our child list as well. If
466 // not, then something is wrong
467 NS_ASSERTION(mFrames.ContainsFrame(nextInFlow), "unexpected flow");
468 if (havePrevBlock) {
469 ReparentFloatsForInlineChild(irs.mLineContainer, nextInFlow, PR_FALSE);
471 nextInFlow->SetParent(this);
472 nextInFlow = nextInFlow->GetNextInFlow();
475 rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
476 if (NS_FAILED(rv)) {
477 done = PR_TRUE;
478 break;
480 if (NS_INLINE_IS_BREAK(aStatus) ||
481 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
482 done = PR_TRUE;
483 break;
485 irs.mPrevFrame = frame;
486 frame = frame->GetNextSibling();
489 // Attempt to pull frames from our next-in-flow until we can't
490 if (!done && (nsnull != GetNextInFlow())) {
491 while (!done) {
492 PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
493 PRBool isComplete;
494 if (!frame) { // Could be non-null if we pulled a first-letter frame and
495 // it created a continuation, since we don't push those.
496 frame = PullOneFrame(aPresContext, irs, &isComplete);
498 #ifdef NOISY_PUSHING
499 printf("%p pulled up %p\n", this, frame);
500 #endif
501 if (nsnull == frame) {
502 if (!isComplete) {
503 aStatus = NS_FRAME_NOT_COMPLETE;
505 break;
507 rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
508 if (NS_FAILED(rv)) {
509 done = PR_TRUE;
510 break;
512 if (NS_INLINE_IS_BREAK(aStatus) ||
513 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
514 done = PR_TRUE;
515 break;
517 irs.mPrevFrame = frame;
518 frame = frame->GetNextSibling();
521 #ifdef DEBUG
522 if (NS_FRAME_IS_COMPLETE(aStatus)) {
523 // We can't be complete AND have overflow frames!
524 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_FALSE);
525 NS_ASSERTION(!overflowFrames, "whoops");
527 #endif
529 // If after reflowing our children they take up no area then make
530 // sure that we don't either.
532 // Note: CSS demands that empty inline elements still affect the
533 // line-height calculations. However, continuations of an inline
534 // that are empty we force to empty so that things like collapsed
535 // whitespace in an inline element don't affect the line-height.
536 aMetrics.width = lineLayout->EndSpan(this);
538 // Compute final width
539 if (nsnull == GetPrevContinuation()) {
540 aMetrics.width += ltr ? aReflowState.mComputedBorderPadding.left
541 : aReflowState.mComputedBorderPadding.right;
543 if (NS_FRAME_IS_COMPLETE(aStatus) && (!GetNextContinuation() || GetNextInFlow())) {
544 aMetrics.width += ltr ? aReflowState.mComputedBorderPadding.right
545 : aReflowState.mComputedBorderPadding.left;
548 nsLayoutUtils::SetFontFromStyle(aReflowState.rendContext, mStyleContext);
549 nsCOMPtr<nsIFontMetrics> fm;
550 aReflowState.rendContext->GetFontMetrics(*getter_AddRefs(fm));
552 if (fm) {
553 // Compute final height of the frame.
555 // Do things the standard css2 way -- though it's hard to find it
556 // in the css2 spec! It's actually found in the css1 spec section
557 // 4.4 (you will have to read between the lines to really see
558 // it).
560 // The height of our box is the sum of our font size plus the top
561 // and bottom border and padding. The height of children do not
562 // affect our height.
563 fm->GetMaxAscent(aMetrics.ascent);
564 fm->GetHeight(aMetrics.height);
565 } else {
566 NS_WARNING("Cannot get font metrics - defaulting sizes to 0");
567 aMetrics.ascent = aMetrics.height = 0;
569 aMetrics.ascent += aReflowState.mComputedBorderPadding.top;
570 aMetrics.height += aReflowState.mComputedBorderPadding.top +
571 aReflowState.mComputedBorderPadding.bottom;
573 // For now our overflow area is zero. The real value will be
574 // computed in |nsLineLayout::RelativePositionFrames|.
575 aMetrics.mOverflowArea.SetRect(0, 0, 0, 0);
577 #ifdef NOISY_FINAL_SIZE
578 ListTag(stdout);
579 printf(": metrics=%d,%d ascent=%d\n",
580 aMetrics.width, aMetrics.height, aMetrics.ascent);
581 #endif
583 return rv;
586 nsresult
587 nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
588 const nsHTMLReflowState& aReflowState,
589 InlineReflowState& irs,
590 nsIFrame* aFrame,
591 nsReflowStatus& aStatus)
593 nsLineLayout* lineLayout = aReflowState.mLineLayout;
594 PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
595 PRBool pushedFrame;
596 nsresult rv =
597 lineLayout->ReflowFrame(aFrame, aStatus, nsnull, pushedFrame);
599 if (NS_FAILED(rv)) {
600 return rv;
602 if (NS_INLINE_IS_BREAK(aStatus)) {
603 if (NS_INLINE_IS_BREAK_BEFORE(aStatus)) {
604 if (aFrame != mFrames.FirstChild()) {
605 // Change break-before status into break-after since we have
606 // already placed at least one child frame. This preserves the
607 // break-type so that it can be propagated upward.
608 aStatus = NS_FRAME_NOT_COMPLETE |
609 NS_INLINE_BREAK | NS_INLINE_BREAK_AFTER |
610 (aStatus & NS_INLINE_BREAK_TYPE_MASK);
611 PushFrames(aPresContext, aFrame, irs.mPrevFrame);
613 else {
614 // Preserve reflow status when breaking-before our first child
615 // and propagate it upward without modification.
616 // Note: if we're lazily setting the frame pointer for our child
617 // frames, then we need to set it now. Don't return and leave the
618 // remaining child frames in our child list with the wrong parent
619 // frame pointer...
620 if (irs.mSetParentPointer) {
621 if (irs.mLineContainer && irs.mLineContainer->GetPrevContinuation()) {
622 ReparentFloatsForInlineChild(irs.mLineContainer, aFrame->GetNextSibling(),
623 PR_TRUE);
625 for (nsIFrame* f = aFrame->GetNextSibling(); f; f = f->GetNextSibling()) {
626 f->SetParent(this);
631 else {
632 // Break-after
633 if (NS_FRAME_IS_NOT_COMPLETE(aStatus)) {
634 nsIFrame* newFrame;
635 rv = CreateNextInFlow(aPresContext, this, aFrame, newFrame);
636 if (NS_FAILED(rv)) {
637 return rv;
640 nsIFrame* nextFrame = aFrame->GetNextSibling();
641 if (nextFrame) {
642 NS_FRAME_SET_INCOMPLETE(aStatus);
643 PushFrames(aPresContext, nextFrame, aFrame);
645 else if (nsnull != GetNextInFlow()) {
646 // We must return an incomplete status if there are more child
647 // frames remaining in a next-in-flow that follows this frame.
648 nsInlineFrame* nextInFlow = (nsInlineFrame*) GetNextInFlow();
649 while (nsnull != nextInFlow) {
650 if (nextInFlow->mFrames.NotEmpty()) {
651 NS_FRAME_SET_INCOMPLETE(aStatus);
652 break;
654 nextInFlow = (nsInlineFrame*) nextInFlow->GetNextInFlow();
659 else if (NS_FRAME_IS_NOT_COMPLETE(aStatus)) {
660 if (nsGkAtoms::placeholderFrame == aFrame->GetType()) {
661 nsBlockReflowState* blockRS = lineLayout->mBlockRS;
662 blockRS->mBlock->SplitPlaceholder(*blockRS, aFrame);
663 // Allow the parent to continue reflowing
664 aStatus = NS_FRAME_COMPLETE;
666 else {
667 nsIFrame* newFrame;
668 rv = CreateNextInFlow(aPresContext, this, aFrame, newFrame);
669 if (NS_FAILED(rv)) {
670 return rv;
672 if (!reflowingFirstLetter) {
673 nsIFrame* nextFrame = aFrame->GetNextSibling();
674 if (nextFrame) {
675 PushFrames(aPresContext, nextFrame, aFrame);
680 return rv;
683 nsIFrame*
684 nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
685 InlineReflowState& irs,
686 PRBool* aIsComplete)
688 PRBool isComplete = PR_TRUE;
690 nsIFrame* frame = nsnull;
691 nsInlineFrame* nextInFlow = irs.mNextInFlow;
692 while (nsnull != nextInFlow) {
693 frame = nextInFlow->mFrames.FirstChild();
694 if (nsnull != frame) {
695 // If our block has no next continuation, then any floats belonging to
696 // the pulled frame must belong to our block already. This check ensures
697 // we do no extra work in the common non-vertical-breaking case.
698 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
699 // The blockChildren.ContainsFrame check performed by
700 // ReparentFloatsForInlineChild will be fast because frame's ancestor
701 // will be the first child of its containing block.
702 ReparentFloatsForInlineChild(irs.mLineContainer, frame, PR_FALSE);
704 nextInFlow->mFrames.RemoveFirstChild();
705 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
706 isComplete = PR_FALSE;
707 nsHTMLContainerFrame::ReparentFrameView(aPresContext, frame, nextInFlow, this);
708 break;
710 nextInFlow = (nsInlineFrame*) nextInFlow->GetNextInFlow();
711 irs.mNextInFlow = nextInFlow;
714 *aIsComplete = isComplete;
715 return frame;
718 void
719 nsInlineFrame::PushFrames(nsPresContext* aPresContext,
720 nsIFrame* aFromChild,
721 nsIFrame* aPrevSibling)
723 NS_PRECONDITION(nsnull != aFromChild, "null pointer");
724 NS_PRECONDITION(nsnull != aPrevSibling, "pushing first child");
725 NS_PRECONDITION(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
727 #ifdef NOISY_PUSHING
728 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n",
729 this, aFromChild, aPrevSibling);
730 #endif
731 // Disconnect aFromChild from its previous sibling
732 aPrevSibling->SetNextSibling(nsnull);
734 // Add the frames to our overflow list (let our next in flow drain
735 // our overflow list when it is ready)
736 SetOverflowFrames(aPresContext, aFromChild);
740 //////////////////////////////////////////////////////////////////////
742 PRIntn
743 nsInlineFrame::GetSkipSides() const
745 PRIntn skip = 0;
746 if (!IsLeftMost()) {
747 nsInlineFrame* prev = (nsInlineFrame*) GetPrevContinuation();
748 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
749 (prev && (prev->mRect.height || prev->mRect.width))) {
750 // Prev continuation is not empty therefore we don't render our left
751 // border edge.
752 skip |= 1 << NS_SIDE_LEFT;
754 else {
755 // If the prev continuation is empty, then go ahead and let our left
756 // edge border render.
759 if (!IsRightMost()) {
760 nsInlineFrame* next = (nsInlineFrame*) GetNextContinuation();
761 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
762 (next && (next->mRect.height || next->mRect.width))) {
763 // Next continuation is not empty therefore we don't render our right
764 // border edge.
765 skip |= 1 << NS_SIDE_RIGHT;
767 else {
768 // If the next continuation is empty, then go ahead and let our right
769 // edge border render.
772 return skip;
775 #ifdef ACCESSIBILITY
776 NS_IMETHODIMP nsInlineFrame::GetAccessible(nsIAccessible** aAccessible)
778 // Broken image accessibles are created here, because layout
779 // replaces the image or image control frame with an inline frame
780 *aAccessible = nsnull;
781 nsIAtom *tagAtom = mContent->Tag();
782 if ((tagAtom == nsGkAtoms::img || tagAtom == nsGkAtoms::input ||
783 tagAtom == nsGkAtoms::label) && mContent->IsNodeOfType(nsINode::eHTML)) {
784 // Only get accessibility service if we're going to use it
785 nsCOMPtr<nsIAccessibilityService> accService(do_GetService("@mozilla.org/accessibilityService;1"));
786 if (!accService)
787 return NS_ERROR_FAILURE;
788 if (tagAtom == nsGkAtoms::input) // Broken <input type=image ... />
789 return accService->CreateHTMLButtonAccessible(static_cast<nsIFrame*>(this), aAccessible);
790 else if (tagAtom == nsGkAtoms::img) // Create accessible for broken <img>
791 return accService->CreateHTMLImageAccessible(static_cast<nsIFrame*>(this), aAccessible);
792 else if (tagAtom == nsGkAtoms::label) // Creat accessible for <label>
793 return accService->CreateHTMLLabelAccessible(static_cast<nsIFrame*>(this), aAccessible);
796 return NS_ERROR_FAILURE;
798 #endif
800 //////////////////////////////////////////////////////////////////////
802 // nsLineFrame implementation
804 static void
805 ReParentChildListStyle(nsPresContext* aPresContext,
806 nsFrameList& aFrameList,
807 nsIFrame* aParentFrame)
809 nsFrameManager *frameManager = aPresContext->FrameManager();
811 for (nsIFrame* kid = aFrameList.FirstChild(); kid;
812 kid = kid->GetNextSibling()) {
813 NS_ASSERTION(kid->GetParent() == aParentFrame, "Bogus parentage");
814 frameManager->ReParentStyleContext(kid);
818 nsIFrame*
819 NS_NewFirstLineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
821 return new (aPresShell) nsFirstLineFrame(aContext);
824 #ifdef DEBUG
825 NS_IMETHODIMP
826 nsFirstLineFrame::GetFrameName(nsAString& aResult) const
828 return MakeFrameName(NS_LITERAL_STRING("Line"), aResult);
830 #endif
832 nsIAtom*
833 nsFirstLineFrame::GetType() const
835 return nsGkAtoms::lineFrame;
838 void
839 nsFirstLineFrame::StealFramesFrom(nsIFrame* aFrame)
841 nsIFrame* prevFrame = mFrames.GetPrevSiblingFor(aFrame);
842 if (prevFrame) {
843 prevFrame->SetNextSibling(nsnull);
845 else {
846 mFrames.SetFrames(nsnull);
850 nsIFrame*
851 nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext, InlineReflowState& irs,
852 PRBool* aIsComplete)
854 nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs, aIsComplete);
855 if (frame && !GetPrevInFlow()) {
856 // We are a first-line frame. Fixup the child frames
857 // style-context that we just pulled.
858 NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
859 aPresContext->FrameManager()->ReParentStyleContext(frame);
861 return frame;
864 NS_IMETHODIMP
865 nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
866 nsHTMLReflowMetrics& aMetrics,
867 const nsHTMLReflowState& aReflowState,
868 nsReflowStatus& aStatus)
870 if (nsnull == aReflowState.mLineLayout) {
871 return NS_ERROR_INVALID_ARG;
874 nsIFrame* lineContainer = aReflowState.mLineLayout->GetLineContainerFrame();
876 // Check for an overflow list with our prev-in-flow
877 nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)GetPrevInFlow();
878 if (nsnull != prevInFlow) {
879 nsIFrame* prevOverflowFrames = prevInFlow->GetOverflowFrames(aPresContext, PR_TRUE);
880 if (prevOverflowFrames) {
881 nsFrameList frames(prevOverflowFrames);
883 // Assign all floats to our block if necessary
884 if (lineContainer && lineContainer->GetPrevContinuation()) {
885 ReparentFloatsForInlineChild(lineContainer, prevOverflowFrames, PR_TRUE);
887 mFrames.InsertFrames(this, nsnull, prevOverflowFrames);
888 ReParentChildListStyle(aPresContext, frames, this);
892 // It's also possible that we have an overflow list for ourselves
893 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_TRUE);
894 if (overflowFrames) {
895 NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
896 nsFrameList frames(overflowFrames);
898 mFrames.AppendFrames(nsnull, overflowFrames);
899 ReParentChildListStyle(aPresContext, frames, this);
902 // Set our own reflow state (additional state above and beyond
903 // aReflowState)
904 InlineReflowState irs;
905 irs.mPrevFrame = nsnull;
906 irs.mLineContainer = lineContainer;
907 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
909 nsresult rv;
910 PRBool wasEmpty = mFrames.IsEmpty();
911 if (wasEmpty) {
912 // Try to pull over one frame before starting so that we know
913 // whether we have an anonymous block or not.
914 PRBool complete;
915 PullOneFrame(aPresContext, irs, &complete);
918 if (nsnull == GetPrevInFlow()) {
919 // XXX This is pretty sick, but what we do here is to pull-up, in
920 // advance, all of the next-in-flows children. We re-resolve their
921 // style while we are at at it so that when we reflow they have
922 // the right style.
924 // All of this is so that text-runs reflow properly.
925 irs.mPrevFrame = mFrames.LastChild();
926 for (;;) {
927 PRBool complete;
928 nsIFrame* frame = PullOneFrame(aPresContext, irs, &complete);
929 if (!frame) {
930 break;
932 irs.mPrevFrame = frame;
934 irs.mPrevFrame = nsnull;
936 else {
937 // XXX do this in the Init method instead
938 // For continuations, we need to check and see if our style
939 // context is right. If its the same as the first-in-flow, then
940 // we need to fix it up (that way :first-line style doesn't leak
941 // into this continuation since we aren't the first line).
942 nsFirstLineFrame* first = (nsFirstLineFrame*) GetFirstInFlow();
943 if (mStyleContext == first->mStyleContext) {
944 // Fixup our style context and our children. First get the
945 // proper parent context.
946 nsStyleContext* parentContext = first->GetParent()->GetStyleContext();
947 if (parentContext) {
948 // Create a new style context that is a child of the parent
949 // style context thus removing the :first-line style. This way
950 // we behave as if an anonymous (unstyled) span was the child
951 // of the parent frame.
952 nsRefPtr<nsStyleContext> newSC;
953 newSC = aPresContext->StyleSet()->
954 ResolvePseudoStyleFor(nsnull,
955 nsCSSAnonBoxes::mozLineFrame, parentContext);
956 if (newSC) {
957 // Switch to the new style context.
958 SetStyleContext(newSC);
960 // Re-resolve all children
961 ReParentChildListStyle(aPresContext, mFrames, this);
967 NS_ASSERTION(!aReflowState.mLineLayout->GetInFirstLine(),
968 "Nested first-line frames? BOGUS");
969 aReflowState.mLineLayout->SetInFirstLine(PR_TRUE);
970 rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
971 aReflowState.mLineLayout->SetInFirstLine(PR_FALSE);
973 // Note: the line layout code will properly compute our overflow state for us
975 return rv;
978 //////////////////////////////////////////////////////////////////////
980 nsIFrame*
981 NS_NewPositionedInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
983 return new (aPresShell) nsPositionedInlineFrame(aContext);
986 void
987 nsPositionedInlineFrame::Destroy()
989 mAbsoluteContainer.DestroyFrames(this);
990 nsInlineFrame::Destroy();
993 NS_IMETHODIMP
994 nsPositionedInlineFrame::SetInitialChildList(nsIAtom* aListName,
995 nsIFrame* aChildList)
997 nsresult rv;
999 if (nsGkAtoms::absoluteList == aListName) {
1000 rv = mAbsoluteContainer.SetInitialChildList(this, aListName, aChildList);
1001 } else {
1002 rv = nsInlineFrame::SetInitialChildList(aListName, aChildList);
1005 return rv;
1008 NS_IMETHODIMP
1009 nsPositionedInlineFrame::AppendFrames(nsIAtom* aListName,
1010 nsIFrame* aFrameList)
1012 nsresult rv;
1014 if (nsGkAtoms::absoluteList == aListName) {
1015 rv = mAbsoluteContainer.AppendFrames(this, aListName, aFrameList);
1016 } else {
1017 rv = nsInlineFrame::AppendFrames(aListName, aFrameList);
1020 return rv;
1023 NS_IMETHODIMP
1024 nsPositionedInlineFrame::InsertFrames(nsIAtom* aListName,
1025 nsIFrame* aPrevFrame,
1026 nsIFrame* aFrameList)
1028 nsresult rv;
1030 if (nsGkAtoms::absoluteList == aListName) {
1031 rv = mAbsoluteContainer.InsertFrames(this, aListName, aPrevFrame,
1032 aFrameList);
1033 } else {
1034 rv = nsInlineFrame::InsertFrames(aListName, aPrevFrame, aFrameList);
1037 return rv;
1040 NS_IMETHODIMP
1041 nsPositionedInlineFrame::RemoveFrame(nsIAtom* aListName,
1042 nsIFrame* aOldFrame)
1044 nsresult rv;
1046 if (nsGkAtoms::absoluteList == aListName) {
1047 rv = mAbsoluteContainer.RemoveFrame(this, aListName, aOldFrame);
1048 } else {
1049 rv = nsInlineFrame::RemoveFrame(aListName, aOldFrame);
1052 return rv;
1055 NS_IMETHODIMP
1056 nsPositionedInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
1057 const nsRect& aDirtyRect,
1058 const nsDisplayListSet& aLists)
1060 aBuilder->MarkFramesForDisplayList(this, mAbsoluteContainer.GetFirstChild(), aDirtyRect);
1061 return nsHTMLContainerFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
1064 nsIAtom*
1065 nsPositionedInlineFrame::GetAdditionalChildListName(PRInt32 aIndex) const
1067 if (0 == aIndex) {
1068 return nsGkAtoms::absoluteList;
1070 return nsnull;
1073 nsIFrame*
1074 nsPositionedInlineFrame::GetFirstChild(nsIAtom* aListName) const
1076 if (nsGkAtoms::absoluteList == aListName)
1077 return mAbsoluteContainer.GetFirstChild();
1079 return nsInlineFrame::GetFirstChild(aListName);
1082 nsIAtom*
1083 nsPositionedInlineFrame::GetType() const
1085 return nsGkAtoms::positionedInlineFrame;
1088 NS_IMETHODIMP
1089 nsPositionedInlineFrame::Reflow(nsPresContext* aPresContext,
1090 nsHTMLReflowMetrics& aDesiredSize,
1091 const nsHTMLReflowState& aReflowState,
1092 nsReflowStatus& aStatus)
1094 nsresult rv = NS_OK;
1096 // Don't bother optimizing for fast incremental reflow of absolute
1097 // children of an inline
1099 // Let the inline frame do its reflow first
1100 rv = nsInlineFrame::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
1102 // Let the absolutely positioned container reflow any absolutely positioned
1103 // child frames that need to be reflowed
1104 // We want to do this under either of two conditions:
1105 // 1. If we didn't do the incremental reflow above.
1106 // 2. If our size changed.
1107 // Even though it's the padding edge that's the containing block, we
1108 // can use our rect (the border edge) since if the border style
1109 // changed, the reflow would have been targeted at us so we'd satisfy
1110 // condition 1.
1111 if (NS_SUCCEEDED(rv) &&
1112 mAbsoluteContainer.HasAbsoluteFrames()) {
1113 // The containing block for the abs pos kids is formed by our padding edge.
1114 nsMargin computedBorder =
1115 aReflowState.mComputedBorderPadding - aReflowState.mComputedPadding;
1116 nscoord containingBlockWidth =
1117 aDesiredSize.width - computedBorder.LeftRight();
1118 nscoord containingBlockHeight =
1119 aDesiredSize.height - computedBorder.TopBottom();
1121 // Factor the absolutely positioned child bounds into the overflow area
1122 // Don't include this frame's bounds, nor its inline descendants' bounds,
1123 // and don't store the overflow property.
1124 // That will all be done by nsLineLayout::RelativePositionFrames.
1125 rv = mAbsoluteContainer.Reflow(this, aPresContext, aReflowState, aStatus,
1126 containingBlockWidth, containingBlockHeight,
1127 PR_TRUE, PR_TRUE, PR_TRUE, // XXX could be optimized
1128 &aDesiredSize.mOverflowArea);
1131 return rv;