Back to index

lightning-sunbird  0.9+nobinonly
nsInlineFrame.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsCOMPtr.h"
00038 #include "nsInlineFrame.h"
00039 #include "nsBlockFrame.h"
00040 #include "nsHTMLAtoms.h"
00041 #include "nsHTMLParts.h"
00042 #include "nsStyleContext.h"
00043 #include "nsIPresShell.h"
00044 #include "nsPresContext.h"
00045 #include "nsIRenderingContext.h"
00046 #include "nsIFontMetrics.h"
00047 #include "nsAbsoluteContainingBlock.h"
00048 #include "nsLayoutAtoms.h"
00049 #include "nsCSSAnonBoxes.h"
00050 #include "nsReflowPath.h"
00051 #include "nsAutoPtr.h"
00052 #include "nsFrameManager.h"
00053 #ifdef ACCESSIBILITY
00054 #include "nsIServiceManager.h"
00055 #include "nsIAccessibilityService.h"
00056 #endif
00057 
00058 #ifdef DEBUG
00059 #undef NOISY_PUSHING
00060 #endif
00061 
00062 
00063 NS_DEFINE_IID(kInlineFrameCID, NS_INLINE_FRAME_CID);
00064 
00065 
00067 
00068 // Basic nsInlineFrame methods
00069 
00070 nsresult
00071 NS_NewInlineFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00072 {
00073   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00074   if (nsnull == aNewFrame) {
00075     return NS_ERROR_NULL_POINTER;
00076   }
00077   nsInlineFrame* it = new (aPresShell) nsInlineFrame;
00078   if (nsnull == it) {
00079     return NS_ERROR_OUT_OF_MEMORY;
00080   }
00081   *aNewFrame = it;
00082   return NS_OK;
00083 }
00084 
00085 nsInlineFrame::nsInlineFrame()
00086 {
00087 }
00088 
00089 NS_IMETHODIMP
00090 nsInlineFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr)
00091 {
00092   if (nsnull == aInstancePtr) {
00093     return NS_ERROR_NULL_POINTER;
00094   }
00095   if (aIID.Equals(kInlineFrameCID)) {
00096     nsInlineFrame* tmp = this;
00097     *aInstancePtr = (void*) tmp;
00098     return NS_OK;
00099   }
00100   return nsInlineFrameSuper::QueryInterface(aIID, aInstancePtr);
00101 }
00102 
00103 #ifdef DEBUG
00104 NS_IMETHODIMP
00105 nsInlineFrame::GetFrameName(nsAString& aResult) const
00106 {
00107   return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult);
00108 }
00109 #endif
00110 
00111 nsIAtom*
00112 nsInlineFrame::GetType() const
00113 {
00114   return nsLayoutAtoms::inlineFrame;
00115 }
00116 
00117 inline PRBool
00118 IsPaddingZero(nsStyleUnit aUnit, nsStyleCoord &aCoord)
00119 {
00120     return (aUnit == eStyleUnit_Null ||
00121             (aUnit == eStyleUnit_Coord && aCoord.GetCoordValue() == 0) ||
00122             (aUnit == eStyleUnit_Percent && aCoord.GetPercentValue() == 0.0));
00123 }
00124 
00125 inline PRBool
00126 IsMarginZero(nsStyleUnit aUnit, nsStyleCoord &aCoord)
00127 {
00128     return (aUnit == eStyleUnit_Null ||
00129             aUnit == eStyleUnit_Auto ||
00130             (aUnit == eStyleUnit_Coord && aCoord.GetCoordValue() == 0) ||
00131             (aUnit == eStyleUnit_Percent && aCoord.GetPercentValue() == 0.0));
00132 }
00133 
00134 /* virtual */ PRBool
00135 nsInlineFrame::IsSelfEmpty()
00136 {
00137 #if 0
00138   // I used to think inline frames worked this way, but it seems they
00139   // don't.  At least not in our codebase.
00140   if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
00141     return PR_FALSE;
00142   }
00143 #endif
00144   const nsStyleMargin* margin = GetStyleMargin();
00145   const nsStyleBorder* border = GetStyleBorder();
00146   const nsStylePadding* padding = GetStylePadding();
00147   nsStyleCoord coord;
00148   // XXX Top and bottom removed, since they shouldn't affect things, but this
00149   // doesn't really match with nsLineLayout.cpp's setting of
00150   // ZeroEffectiveSpanBox, anymore, so what should this really be?
00151   if (border->GetBorderWidth(NS_SIDE_RIGHT) != 0 ||
00152       border->GetBorderWidth(NS_SIDE_LEFT) != 0 ||
00153       !IsPaddingZero(padding->mPadding.GetRightUnit(),
00154                      padding->mPadding.GetRight(coord)) ||
00155       !IsPaddingZero(padding->mPadding.GetLeftUnit(),
00156                      padding->mPadding.GetLeft(coord)) ||
00157       !IsMarginZero(margin->mMargin.GetRightUnit(),
00158                     margin->mMargin.GetRight(coord)) ||
00159       !IsMarginZero(margin->mMargin.GetLeftUnit(),
00160                     margin->mMargin.GetLeft(coord))) {
00161     return PR_FALSE;
00162   }
00163   return PR_TRUE;
00164 }
00165 
00166 PRBool
00167 nsInlineFrame::IsEmpty()
00168 {
00169   if (!IsSelfEmpty()) {
00170     return PR_FALSE;
00171   }
00172 
00173   for (nsIFrame *kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
00174     if (!kid->IsEmpty())
00175       return PR_FALSE;
00176   }
00177 
00178   return PR_TRUE;
00179 }
00180 
00181 NS_IMETHODIMP
00182 nsInlineFrame::AppendFrames(nsIAtom*        aListName,
00183                             nsIFrame*       aFrameList)
00184 {
00185   if (nsnull != aListName) {
00186     return NS_ERROR_INVALID_ARG;
00187   }
00188   if (aFrameList) {
00189     mFrames.AppendFrames(this, aFrameList);
00190 
00191     // Ask the parent frame to reflow me.
00192     ReflowDirtyChild(GetPresContext()->PresShell(), nsnull);
00193   }
00194   return NS_OK;
00195 }
00196 
00197 NS_IMETHODIMP
00198 nsInlineFrame::InsertFrames(nsIAtom*        aListName,
00199                             nsIFrame*       aPrevFrame,
00200                             nsIFrame*       aFrameList)
00201 {
00202   if (nsnull != aListName) {
00203 #ifdef IBMBIDI
00204     if (aListName != nsLayoutAtoms::nextBidi)
00205 #endif
00206     return NS_ERROR_INVALID_ARG;
00207   }
00208   if (aFrameList) {
00209     // Insert frames after aPrevFrame
00210     mFrames.InsertFrames(this, aPrevFrame, aFrameList);
00211 
00212 #ifdef IBMBIDI
00213     if (nsnull == aListName)
00214 #endif
00215     // Ask the parent frame to reflow me.
00216     ReflowDirtyChild(GetPresContext()->PresShell(), nsnull);
00217   }
00218   return NS_OK;
00219 }
00220 
00221 NS_IMETHODIMP
00222 nsInlineFrame::RemoveFrame(nsIAtom*        aListName,
00223                            nsIFrame*       aOldFrame)
00224 {
00225   if (nsnull != aListName) {
00226 #ifdef IBMBIDI
00227     if (nsLayoutAtoms::nextBidi != aListName)
00228 #endif
00229     return NS_ERROR_INVALID_ARG;
00230   }
00231 
00232   if (aOldFrame) {
00233     // Loop and destroy the frame and all of its continuations.
00234     // If the frame we are removing is a brFrame, we need a reflow so
00235     // the line the brFrame was on can attempt to pull up any frames
00236     // that can fit from lines below it.
00237     PRBool generateReflowCommand =
00238       aOldFrame->GetType() == nsLayoutAtoms::brFrame;
00239 
00240     nsInlineFrame* parent = NS_STATIC_CAST(nsInlineFrame*, aOldFrame->GetParent());
00241     while (aOldFrame) {
00242 #ifdef IBMBIDI
00243       if (nsLayoutAtoms::nextBidi != aListName) {
00244 #endif
00245       // If the frame being removed has zero size then don't bother
00246       // generating a reflow command, otherwise make sure we do.
00247       nsRect bbox = aOldFrame->GetRect();
00248       if (bbox.width || bbox.height) {
00249         generateReflowCommand = PR_TRUE;
00250       }
00251 #ifdef IBMBIDI
00252       }
00253 #endif
00254 
00255       // When the parent is an inline frame we have a simple task - just
00256       // remove the frame from its parents list and generate a reflow
00257       // command.
00258       nsIFrame* oldFrameNextInFlow = aOldFrame->GetNextInFlow();
00259       parent->mFrames.DestroyFrame(GetPresContext(), aOldFrame);
00260       aOldFrame = oldFrameNextInFlow;
00261       if (aOldFrame) {
00262         parent = NS_STATIC_CAST(nsInlineFrame*, aOldFrame->GetParent());
00263       }
00264     }
00265 
00266     if (generateReflowCommand) {
00267       // Ask the parent frame to reflow me.
00268       ReflowDirtyChild(GetPresContext()->PresShell(), nsnull);
00269     }
00270   }
00271 
00272   return NS_OK;
00273 }
00274 
00275 NS_IMETHODIMP
00276 nsInlineFrame::ReplaceFrame(nsIAtom*        aListName,
00277                             nsIFrame*       aOldFrame,
00278                             nsIFrame*       aNewFrame)
00279 {
00280   if (aListName) {
00281     NS_ERROR("Don't have any special lists on inline frames!");
00282     return NS_ERROR_INVALID_ARG;
00283   }
00284   if (!aOldFrame || !aNewFrame) {
00285     NS_ERROR("Missing aOldFrame or aNewFrame");
00286     return NS_ERROR_INVALID_ARG;
00287   }
00288 
00289   PRBool retval =
00290     mFrames.ReplaceFrame(this, aOldFrame, aNewFrame, PR_TRUE);
00291   
00292   // Ask the parent frame to reflow me.
00293   ReflowDirtyChild(GetPresContext()->PresShell(), nsnull);
00294 
00295   return retval ? NS_OK : NS_ERROR_FAILURE;
00296 }
00297 
00298 
00299 NS_IMETHODIMP
00300 nsInlineFrame::Paint(nsPresContext*      aPresContext,
00301                      nsIRenderingContext& aRenderingContext,
00302                      const nsRect&        aDirtyRect,
00303                      nsFramePaintLayer    aWhichLayer,
00304                      PRUint32             aFlags)
00305 {
00306   if (NS_FRAME_IS_UNFLOWABLE & mState) {
00307     return NS_OK;
00308   }
00309 
00310   // Paint inline element backgrounds in the foreground layer (bug 36710).
00311   if (aWhichLayer == NS_FRAME_PAINT_LAYER_FOREGROUND) {
00312     PaintSelf(aPresContext, aRenderingContext, aDirtyRect);
00313   }
00314     
00315   // The sole purpose of this is to trigger display of the selection
00316   // window for Named Anchors, which don't have any children and
00317   // normally don't have any size, but in Editor we use CSS to display
00318   // an image to represent this "hidden" element.
00319   if (!mFrames.FirstChild()) {
00320     nsFrame::Paint(aPresContext, aRenderingContext, aDirtyRect,
00321                    aWhichLayer, aFlags);
00322   }
00323 
00324   PaintDecorationsAndChildren(aPresContext, aRenderingContext,
00325                               aDirtyRect, aWhichLayer, PR_FALSE,
00326                               aFlags);
00327   return NS_OK;
00328 }
00329 
00331 // Reflow methods
00332 
00333 void
00334 nsInlineFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
00335                                             nsIFrame* aFrame,
00336                                             PRBool aReparentSiblings)
00337 {
00338   NS_ASSERTION(aOurLineContainer->GetNextInFlow() ||
00339                aOurLineContainer->GetPrevInFlow(),
00340                "Don't call this when we have no continuation, it's a waste");
00341 
00342   if (!aFrame) {
00343     NS_ASSERTION(aReparentSiblings, "Why did we get called?");
00344     return;
00345   }
00346 
00347   nsIFrame* ancestor = aFrame;
00348   nsIFrame* ancestorBlockChild;
00349   do {
00350     ancestorBlockChild = ancestor;
00351     ancestor = ancestor->GetParent();
00352     NS_ASSERTION(ancestor, "No block ancestor!");
00353   } while (!ancestor->IsFloatContainingBlock());
00354 
00355   if (ancestor == aOurLineContainer)
00356     return;
00357 
00358   nsBlockFrame* ourBlock;
00359   nsresult rv = aOurLineContainer->QueryInterface(kBlockFrameCID, (void**)&ourBlock);
00360   NS_ASSERTION(NS_SUCCEEDED(rv), "Not a block, but broke vertically?");
00361   nsBlockFrame* frameBlock;
00362   rv = ancestor->QueryInterface(kBlockFrameCID, (void**)&frameBlock);
00363   NS_ASSERTION(NS_SUCCEEDED(rv), "ancestor not a block");
00364 
00365   nsFrameList blockChildren(ancestor->GetFirstChild(nsnull));
00366   PRBool isOverflow = !blockChildren.ContainsFrame(ancestorBlockChild);
00367 
00368   while (PR_TRUE) {
00369     ourBlock->ReparentFloats(aFrame, frameBlock, isOverflow, PR_FALSE);
00370 
00371     if (!aReparentSiblings)
00372       return;
00373     nsIFrame* next = aFrame->GetNextSibling();
00374     if (!next)
00375       return;
00376     if (next->GetParent() == aFrame->GetParent()) {
00377       aFrame = next;
00378       continue;
00379     }
00380     // This is paranoid and will hardly ever get hit ... but we can't actually
00381     // trust that the frames in the sibling chain all have the same parent,
00382     // because lazy reparenting may be going on. If we find a different
00383     // parent we need to redo our analysis.
00384     ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
00385     return;
00386   }
00387 }
00388 
00389 NS_IMETHODIMP
00390 nsInlineFrame::Reflow(nsPresContext*          aPresContext,
00391                       nsHTMLReflowMetrics&     aMetrics,
00392                       const nsHTMLReflowState& aReflowState,
00393                       nsReflowStatus&          aStatus)
00394 {
00395   DO_GLOBAL_REFLOW_COUNT("nsInlineFrame", aReflowState.reason);
00396   DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
00397   if (nsnull == aReflowState.mLineLayout) {
00398     return NS_ERROR_INVALID_ARG;
00399   }
00400 
00401   PRBool  lazilySetParentPointer = PR_FALSE;
00402 
00403   nsIFrame* lineContainer = aReflowState.mLineLayout->GetLineContainerFrame();
00404 
00405   // Check for an overflow list with our prev-in-flow
00406   nsInlineFrame* prevInFlow = (nsInlineFrame*)mPrevInFlow;
00407   if (nsnull != prevInFlow) {
00408     nsIFrame* prevOverflowFrames = prevInFlow->GetOverflowFrames(aPresContext, PR_TRUE);
00409 
00410     if (prevOverflowFrames) {
00411       // When pushing and pulling frames we need to check for whether any
00412       // views need to be reparented.
00413       nsHTMLContainerFrame::ReparentFrameViewList(aPresContext, prevOverflowFrames,
00414                                                   prevInFlow, this);
00415 
00416       if (aReflowState.reason == eReflowReason_Initial) {
00417         // If it's the initial reflow, then our child list must be empty, so
00418         // just set the child list rather than calling InsertFrame(). This avoids
00419         // having to get the last child frame in the list.
00420         // Note that we don't set the parent pointer for the new frames. Instead wait
00421         // to do this until we actually reflow the frame. If the overflow list contains
00422         // thousands of frames this is a big performance issue (see bug #5588)
00423         NS_ASSERTION(mFrames.IsEmpty(), "child list is not empty for initial reflow");
00424         mFrames.SetFrames(prevOverflowFrames);
00425         lazilySetParentPointer = PR_TRUE;
00426       } else {
00427         // Assign all floats to our block if necessary
00428        if (lineContainer && lineContainer->GetPrevInFlow()) {
00429           ReparentFloatsForInlineChild(lineContainer, prevOverflowFrames, PR_TRUE);
00430         }
00431         // Insert the new frames at the beginning of the child list
00432         // and set their parent pointer
00433         mFrames.InsertFrames(this, nsnull, prevOverflowFrames);
00434       }
00435     }
00436   }
00437 
00438   // It's also possible that we have an overflow list for ourselves
00439 #ifdef DEBUG
00440   if (aReflowState.reason == eReflowReason_Initial) {
00441     // If it's our initial reflow, then we should not have an overflow list.
00442     // However, add an assertion in case we get reflowed more than once with
00443     // the initial reflow reason
00444     nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_FALSE);
00445     NS_ASSERTION(!overflowFrames, "overflow list is not empty for initial reflow");
00446   }
00447 #endif
00448   if (aReflowState.reason != eReflowReason_Initial) {
00449     nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_TRUE);
00450     if (overflowFrames) {
00451       NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
00452 
00453       // Because we lazily set the parent pointer of child frames we get from
00454       // our prev-in-flow's overflow list, it's possible that we have not set
00455       // the parent pointer for these frames.
00456       mFrames.AppendFrames(this, overflowFrames);
00457     }
00458   }
00459 
00460   if (IsFrameTreeTooDeep(aReflowState, aMetrics)) {
00461 #ifdef DEBUG_kipp
00462     {
00463       extern char* nsPresShell_ReflowStackPointerTop;
00464       char marker;
00465       char* newsp = (char*) ▮
00466       printf("XXX: frame tree is too deep; approx stack size = %d\n",
00467              nsPresShell_ReflowStackPointerTop - newsp);
00468     }
00469 #endif
00470     aStatus = NS_FRAME_COMPLETE;
00471     return NS_OK;
00472   }
00473 
00474   // Set our own reflow state (additional state above and beyond
00475   // aReflowState)
00476   InlineReflowState irs;
00477   irs.mPrevFrame = nsnull;
00478   irs.mLineContainer = lineContainer;
00479   irs.mNextInFlow = (nsInlineFrame*) mNextInFlow;
00480   irs.mSetParentPointer = lazilySetParentPointer;
00481 
00482   nsresult rv;
00483   if (mFrames.IsEmpty()) {
00484     // Try to pull over one frame before starting so that we know
00485     // whether we have an anonymous block or not.
00486     PRBool complete;
00487     (void) PullOneFrame(aPresContext, irs, &complete);
00488   }
00489 
00490   rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
00491   
00492   // Note: the line layout code will properly compute our
00493   // NS_FRAME_OUTSIDE_CHILDREN state for us.
00494 
00495   NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
00496   return rv;
00497 }
00498 
00499 NS_IMETHODIMP
00500 nsInlineFrame::CanContinueTextRun(PRBool& aContinueTextRun) const
00501 {
00502   // We can continue a text run through an inline frame
00503   aContinueTextRun = PR_TRUE;
00504   return NS_OK;
00505 }
00506 
00507 NS_IMETHODIMP
00508 nsInlineFrame::ReflowDirtyChild(nsIPresShell* aPresShell, nsIFrame* aChild)
00509 {
00510   // The inline container frame does not handle the reflow
00511   // request.  It passes it up to its parent container.
00512 
00513   // If you don't already have dirty children,
00514   if (!(mState & NS_FRAME_HAS_DIRTY_CHILDREN)) {
00515     if (mParent) {
00516       // Record that you are dirty and have dirty children
00517       mState |= NS_FRAME_IS_DIRTY;
00518       mState |= NS_FRAME_HAS_DIRTY_CHILDREN; 
00519 
00520       // Pass the reflow request up to the parent
00521       mParent->ReflowDirtyChild(aPresShell, this);
00522     }
00523     else {
00524       NS_ERROR("No parent to pass the reflow request up to.");
00525     }
00526   }
00527 
00528   return NS_OK;
00529 }
00530 
00531 nsresult
00532 nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
00533                             const nsHTMLReflowState& aReflowState,
00534                             InlineReflowState& irs,
00535                             nsHTMLReflowMetrics& aMetrics,
00536                             nsReflowStatus& aStatus)
00537 {
00538   nsresult rv = NS_OK;
00539   aStatus = NS_FRAME_COMPLETE;
00540 
00541   nsLineLayout* lineLayout = aReflowState.mLineLayout;
00542   nscoord leftEdge = 0;
00543   if (nsnull == mPrevInFlow) {
00544     leftEdge = aReflowState.mComputedBorderPadding.left;
00545   }
00546   nscoord availableWidth = aReflowState.availableWidth;
00547   if (NS_UNCONSTRAINEDSIZE != availableWidth) {
00548     // Subtract off left and right border+padding from availableWidth
00549     availableWidth -= leftEdge;
00550     availableWidth -= aReflowState.mComputedBorderPadding.right;
00551     availableWidth = PR_MAX(0, availableWidth);
00552   }
00553   lineLayout->BeginSpan(this, &aReflowState, leftEdge, leftEdge + availableWidth);
00554 
00555   // First reflow our current children
00556   nsIFrame* frame = mFrames.FirstChild();
00557   PRBool done = PR_FALSE;
00558   while (nsnull != frame) {
00559     PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
00560 
00561     // Check if we should lazily set the child frame's parent pointer
00562     if (irs.mSetParentPointer) {
00563       PRBool havePrevBlock =
00564          irs.mLineContainer && irs.mLineContainer->GetPrevInFlow();
00565       // If our block is the first in flow, then any floats under the pulled
00566       // frame must already belong to our block.
00567       if (havePrevBlock) {
00568         // This has to happen before we update frame's parent; we need to
00569         // know frame's ancestry under its old block.
00570         // The blockChildren.ContainsFrame check performed by
00571         // ReparentFloatsForInlineChild here may be slow, but we can't
00572         // easily avoid it because we don't know where 'frame' originally
00573         // came from. If we really really have to optimize this we could
00574         // cache whether frame->GetParent() is under its containing blocks
00575         // overflowList or not.
00576         ReparentFloatsForInlineChild(irs.mLineContainer, frame, PR_FALSE);
00577       }
00578       frame->SetParent(this);
00579       // We also need to check if frame has a next-in-flow. It it does, then set
00580       // its parent frame pointer, too. Otherwise, if we reflow frame and it's
00581       // complete we'll fail when deleting its next-in-flow which is no longer
00582       // needed. This scenario doesn't happen often, but it can happen
00583       nsIFrame* nextInFlow = frame->GetNextInFlow();
00584       while (nextInFlow) {
00585         // Since we only do lazy setting of parent pointers for the frame's
00586         // initial reflow, this frame can't have a next-in-flow. That means
00587         // the continuing child frame must be in our child list as well. If
00588         // not, then something is wrong
00589         NS_ASSERTION(mFrames.ContainsFrame(nextInFlow), "unexpected flow");
00590         if (havePrevBlock) {
00591           ReparentFloatsForInlineChild(irs.mLineContainer, nextInFlow, PR_FALSE);
00592         }
00593         nextInFlow->SetParent(this);
00594         nextInFlow = nextInFlow->GetNextInFlow();
00595       }
00596     }
00597     rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
00598     if (NS_FAILED(rv)) {
00599       done = PR_TRUE;
00600       break;
00601     }
00602     if (NS_INLINE_IS_BREAK(aStatus) || 
00603         (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
00604       done = PR_TRUE;
00605       break;
00606     }
00607     irs.mPrevFrame = frame;
00608     frame = frame->GetNextSibling();
00609   }
00610 
00611   // Attempt to pull frames from our next-in-flow until we can't
00612   if (!done && (nsnull != mNextInFlow)) {
00613     while (!done) {
00614       PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
00615       PRBool isComplete;
00616       frame = PullOneFrame(aPresContext, irs, &isComplete);
00617 #ifdef NOISY_PUSHING
00618       printf("%p pulled up %p\n", this, frame);
00619 #endif
00620       if (nsnull == frame) {
00621         if (!isComplete) {
00622           aStatus = NS_FRAME_NOT_COMPLETE;
00623         }
00624         break;
00625       }
00626       rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
00627       if (NS_FAILED(rv)) {
00628         done = PR_TRUE;
00629         break;
00630       }
00631       if (NS_INLINE_IS_BREAK(aStatus) || 
00632           (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
00633         done = PR_TRUE;
00634         break;
00635       }
00636       irs.mPrevFrame = frame;
00637     }
00638   }
00639 #ifdef DEBUG
00640   if (NS_FRAME_IS_COMPLETE(aStatus)) {
00641     // We can't be complete AND have overflow frames!
00642     nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_FALSE);
00643     NS_ASSERTION(!overflowFrames, "whoops");
00644   }
00645 #endif
00646 
00647   // If after reflowing our children they take up no area then make
00648   // sure that we don't either.
00649   //
00650   // Note: CSS demands that empty inline elements still affect the
00651   // line-height calculations. However, continuations of an inline
00652   // that are empty we force to empty so that things like collapsed
00653   // whitespace in an inline element don't affect the line-height.
00654   nsSize size;
00655   lineLayout->EndSpan(this, size,
00656                     aMetrics.mComputeMEW ? &aMetrics.mMaxElementWidth : nsnull);
00657   if ((0 == size.height) && (0 == size.width) &&
00658       ((nsnull != mPrevInFlow) || (nsnull != mNextInFlow))) {
00659     // This is a continuation of a previous inline. Therefore make
00660     // sure we don't affect the line-height.
00661     aMetrics.width = 0;
00662     aMetrics.height = 0;
00663     aMetrics.ascent = 0;
00664     aMetrics.descent = 0;
00665     if (aMetrics.mComputeMEW) {
00666       aMetrics.mMaxElementWidth = 0;
00667     }
00668   }
00669   else {
00670     // Compute final width
00671     aMetrics.width = size.width;
00672     if (nsnull == mPrevInFlow) {
00673       aMetrics.width += aReflowState.mComputedBorderPadding.left;
00674     }
00675     if (NS_FRAME_IS_COMPLETE(aStatus)) {
00676       aMetrics.width += aReflowState.mComputedBorderPadding.right;
00677     }
00678 
00679     SetFontFromStyle(aReflowState.rendContext, mStyleContext);
00680     nsCOMPtr<nsIFontMetrics> fm;
00681     aReflowState.rendContext->GetFontMetrics(*getter_AddRefs(fm));
00682 
00683     if (fm) {
00684       // Compute final height of the frame.
00685       //
00686       // Do things the standard css2 way -- though it's hard to find it
00687       // in the css2 spec! It's actually found in the css1 spec section
00688       // 4.4 (you will have to read between the lines to really see
00689       // it).
00690       //
00691       // The height of our box is the sum of our font size plus the top
00692       // and bottom border and padding. The height of children do not
00693       // affect our height.
00694       fm->GetMaxAscent(aMetrics.ascent);
00695       fm->GetMaxDescent(aMetrics.descent);
00696       fm->GetHeight(aMetrics.height);
00697     } else {
00698       NS_WARNING("Cannot get font metrics - defaulting sizes to 0");
00699       aMetrics.ascent = aMetrics.descent = aMetrics.height = 0;
00700     }
00701     aMetrics.ascent += aReflowState.mComputedBorderPadding.top;
00702     aMetrics.descent += aReflowState.mComputedBorderPadding.bottom;
00703     aMetrics.height += aReflowState.mComputedBorderPadding.top +
00704       aReflowState.mComputedBorderPadding.bottom;
00705   }
00706 
00707   // For now our overflow area is zero. The real value will be
00708   // computed during vertical alignment of the line we are on.
00709   aMetrics.mOverflowArea.SetRect(0, 0, 0, 0);
00710 
00711 #ifdef NOISY_FINAL_SIZE
00712   ListTag(stdout);
00713   printf(": metrics=%d,%d ascent=%d descent=%d\n",
00714          aMetrics.width, aMetrics.height, aMetrics.ascent, aMetrics.descent);
00715   if (aMetrics.mComputeMEW) {
00716     printf(" maxElementWidth %d\n", aMetrics.mMaxElementWidth);
00717   }
00718 #endif
00719 
00720   return rv;
00721 }
00722 
00723 static 
00724 void SetContainsPercentAwareChild(nsIFrame *aFrame)
00725 {
00726   aFrame->AddStateBits(NS_INLINE_FRAME_CONTAINS_PERCENT_AWARE_CHILD);
00727 }
00728 
00729 static
00730 void MarkPercentAwareFrame(nsPresContext *aPresContext, 
00731                            nsInlineFrame  *aInline,
00732                            nsIFrame       *aFrame)
00733 {
00734   if (aFrame->GetStateBits() & NS_FRAME_REPLACED_ELEMENT) 
00735   { // aFrame is a replaced element, check it's style
00736     if (nsLineLayout::IsPercentageAwareReplacedElement(aPresContext, aFrame)) {
00737       SetContainsPercentAwareChild(aInline);
00738     }
00739   }
00740   else
00741   {
00742     if (aFrame->GetFirstChild(nsnull))
00743     { // aFrame is an inline container frame, check my frame state
00744       if (aFrame->GetStateBits() & NS_INLINE_FRAME_CONTAINS_PERCENT_AWARE_CHILD) {
00745         SetContainsPercentAwareChild(aInline); // if a child container is effected, so am I
00746       }
00747     }
00748     // else frame is a leaf that we don't care about
00749   }   
00750 }
00751 
00752 nsresult
00753 nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
00754                                  const nsHTMLReflowState& aReflowState,
00755                                  InlineReflowState& irs,
00756                                  nsIFrame* aFrame,
00757                                  nsReflowStatus& aStatus)
00758 {
00759   nsLineLayout* lineLayout = aReflowState.mLineLayout;
00760   PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
00761   PRBool pushedFrame;
00762   nsresult rv = lineLayout->ReflowFrame(aFrame, aStatus, nsnull, pushedFrame);
00763   /* This next block is for bug 28811
00764      Test the child frame for %-awareness, 
00765      and mark this frame with a bit if it is %-aware.
00766      Don't bother if this frame is already marked
00767   */
00768   if (!(mState & NS_INLINE_FRAME_CONTAINS_PERCENT_AWARE_CHILD)) {  
00769     MarkPercentAwareFrame(aPresContext, this, aFrame);
00770   }
00771 
00772   if (NS_FAILED(rv)) {
00773     return rv;
00774   }
00775   if (NS_INLINE_IS_BREAK(aStatus)) {
00776     if (NS_INLINE_IS_BREAK_BEFORE(aStatus)) {
00777       if (aFrame != mFrames.FirstChild()) {
00778         // Change break-before status into break-after since we have
00779         // already placed at least one child frame. This preserves the
00780         // break-type so that it can be propagated upward.
00781         aStatus = NS_FRAME_NOT_COMPLETE |
00782           NS_INLINE_BREAK | NS_INLINE_BREAK_AFTER |
00783           (aStatus & NS_INLINE_BREAK_TYPE_MASK);
00784         PushFrames(aPresContext, aFrame, irs.mPrevFrame);
00785       }
00786       else {
00787         // Preserve reflow status when breaking-before our first child
00788         // and propagate it upward without modification.
00789         // Note: if we're lazily setting the frame pointer for our child 
00790         // frames, then we need to set it now. Don't return and leave the
00791         // remaining child frames in our child list with the wrong parent
00792         // frame pointer...
00793         if (irs.mSetParentPointer) {
00794           if (irs.mLineContainer && irs.mLineContainer->GetPrevInFlow()) {
00795             ReparentFloatsForInlineChild(irs.mLineContainer, aFrame->GetNextSibling(),
00796                                          PR_TRUE);
00797           }
00798           for (nsIFrame* f = aFrame->GetNextSibling(); f; f = f->GetNextSibling()) {
00799             f->SetParent(this);
00800           }
00801         }
00802       }
00803     }
00804     else {
00805       // Break-after
00806       if (NS_FRAME_IS_NOT_COMPLETE(aStatus)) {
00807         nsIFrame* newFrame;
00808         rv = CreateNextInFlow(aPresContext, this, aFrame, newFrame);
00809         if (NS_FAILED(rv)) {
00810           return rv;
00811         }
00812       }
00813       nsIFrame* nextFrame = aFrame->GetNextSibling();
00814       if (nextFrame) {
00815         aStatus |= NS_FRAME_NOT_COMPLETE;
00816         PushFrames(aPresContext, nextFrame, aFrame);
00817       }
00818       else if (nsnull != mNextInFlow) {
00819         // We must return an incomplete status if there are more child
00820         // frames remaining in a next-in-flow that follows this frame.
00821         nsInlineFrame* nextInFlow = (nsInlineFrame*) mNextInFlow;
00822         while (nsnull != nextInFlow) {
00823           if (nextInFlow->mFrames.NotEmpty()) {
00824             aStatus |= NS_FRAME_NOT_COMPLETE;
00825             break;
00826           }
00827           nextInFlow = (nsInlineFrame*) nextInFlow->mNextInFlow;
00828         }
00829       }
00830     }
00831   }
00832   else if (NS_FRAME_IS_NOT_COMPLETE(aStatus)) {
00833     if (nsLayoutAtoms::placeholderFrame == aFrame->GetType()) {
00834       nsBlockReflowState* blockRS = lineLayout->mBlockRS;
00835       blockRS->mBlock->SplitPlaceholder(*blockRS, aFrame);
00836       // Allow the parent to continue reflowing
00837       aStatus = NS_FRAME_COMPLETE;
00838     }
00839     else {
00840       nsIFrame* newFrame;
00841       rv = CreateNextInFlow(aPresContext, this, aFrame, newFrame);
00842       if (NS_FAILED(rv)) {
00843         return rv;
00844       }
00845       if (!reflowingFirstLetter) {
00846         nsIFrame* nextFrame = aFrame->GetNextSibling();
00847         if (nextFrame) {
00848           PushFrames(aPresContext, nextFrame, aFrame);
00849         }
00850       }
00851     }
00852   }
00853   return rv;
00854 }
00855 
00856 nsIFrame*
00857 nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
00858                             InlineReflowState& irs,
00859                             PRBool* aIsComplete)
00860 {
00861   PRBool isComplete = PR_TRUE;
00862 
00863   nsIFrame* frame = nsnull;
00864   nsInlineFrame* nextInFlow = irs.mNextInFlow;
00865   while (nsnull != nextInFlow) {
00866     frame = nextInFlow->mFrames.FirstChild();
00867     if (nsnull != frame) {
00868       // If our block has no next continuation, then any floats belonging to
00869       // the pulled frame must belong to our block already. This check ensures
00870       // we do no extra work in the common non-vertical-breaking case.
00871       if (irs.mLineContainer && irs.mLineContainer->GetNextInFlow()) {
00872         // The blockChildren.ContainsFrame check performed by
00873         // ReparentFloatsForInlineChild will be fast because frame's ancestor
00874         // will be the first child of its containing block.
00875         ReparentFloatsForInlineChild(irs.mLineContainer, frame, PR_FALSE);
00876       }
00877       nextInFlow->mFrames.RemoveFirstChild();
00878       mFrames.InsertFrame(this, irs.mPrevFrame, frame);
00879       isComplete = PR_FALSE;
00880       nsHTMLContainerFrame::ReparentFrameView(aPresContext, frame, nextInFlow, this);
00881       break;
00882     }
00883     nextInFlow = (nsInlineFrame*) nextInFlow->mNextInFlow;
00884     irs.mNextInFlow = nextInFlow;
00885   }
00886 
00887   *aIsComplete = isComplete;
00888   return frame;
00889 }
00890 
00891 void
00892 nsInlineFrame::PushFrames(nsPresContext* aPresContext,
00893                           nsIFrame* aFromChild,
00894                           nsIFrame* aPrevSibling)
00895 {
00896   NS_PRECONDITION(nsnull != aFromChild, "null pointer");
00897   NS_PRECONDITION(nsnull != aPrevSibling, "pushing first child");
00898   NS_PRECONDITION(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
00899 
00900 #ifdef NOISY_PUSHING
00901       printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n", 
00902              this, aFromChild, aPrevSibling);
00903 #endif
00904   // Disconnect aFromChild from its previous sibling
00905   aPrevSibling->SetNextSibling(nsnull);
00906 
00907   // Add the frames to our overflow list (let our next in flow drain
00908   // our overflow list when it is ready)
00909   SetOverflowFrames(aPresContext, aFromChild);
00910 }
00911 
00912 
00914 
00915 PRIntn
00916 nsInlineFrame::GetSkipSides() const
00917 {
00918   PRIntn skip = 0;
00919   if (nsnull != mPrevInFlow) {
00920     nsInlineFrame* prev = (nsInlineFrame*) mPrevInFlow;
00921     if (prev->mRect.height || prev->mRect.width) {
00922       // Prev-in-flow is not empty therefore we don't render our left
00923       // border edge.
00924       skip |= 1 << NS_SIDE_LEFT;
00925     }
00926     else {
00927       // If the prev-in-flow is empty, then go ahead and let our left
00928       // edge border render.
00929     }
00930   }
00931   if (nsnull != mNextInFlow) {
00932     nsInlineFrame* next = (nsInlineFrame*) mNextInFlow;
00933     if (next->mRect.height || next->mRect.width) {
00934       // Next-in-flow is not empty therefore we don't render our right
00935       // border edge.
00936       skip |= 1 << NS_SIDE_RIGHT;
00937     }
00938     else {
00939       // If the next-in-flow is empty, then go ahead and let our right
00940       // edge border render.
00941     }
00942   }
00943   return skip;
00944 }
00945 
00946 #ifdef ACCESSIBILITY
00947 NS_IMETHODIMP nsInlineFrame::GetAccessible(nsIAccessible** aAccessible)
00948 {
00949   // Broken image accessibles are created here, because layout
00950   // replaces the image or image control frame with an inline frame
00951   *aAccessible = nsnull;
00952   nsIAtom *tagAtom = mContent->Tag();
00953   if ((tagAtom == nsHTMLAtoms::img || tagAtom == nsHTMLAtoms::input || 
00954        tagAtom == nsHTMLAtoms::label) && mContent->IsContentOfType(nsIContent::eHTML)) {
00955     // Only get accessibility service if we're going to use it
00956     nsCOMPtr<nsIAccessibilityService> accService(do_GetService("@mozilla.org/accessibilityService;1"));
00957     if (!accService)
00958       return NS_ERROR_FAILURE;
00959     if (tagAtom == nsHTMLAtoms::input)  // Broken <input type=image ... />
00960       return accService->CreateHTMLButtonAccessible(NS_STATIC_CAST(nsIFrame*, this), aAccessible);
00961     else if (tagAtom == nsHTMLAtoms::img)  // Create accessible for broken <img>
00962       return accService->CreateHTMLImageAccessible(NS_STATIC_CAST(nsIFrame*, this), aAccessible);
00963     else if (tagAtom == nsHTMLAtoms::label)  // Creat accessible for <label>
00964       return accService->CreateHTMLLabelAccessible(NS_STATIC_CAST(nsIFrame*, this), aAccessible);
00965   }
00966 
00967   return NS_ERROR_FAILURE;
00968 }
00969 #endif
00970 
00972 
00973 // nsLineFrame implementation
00974 
00975 static void
00976 ReParentChildListStyle(nsPresContext* aPresContext,
00977                        nsFrameList& aFrameList,
00978                        nsIFrame* aParentFrame)
00979 {
00980   nsFrameManager *frameManager = aPresContext->FrameManager();
00981 
00982   for (nsIFrame* kid = aFrameList.FirstChild(); kid;
00983        kid = kid->GetNextSibling()) {
00984     NS_ASSERTION(kid->GetParent() == aParentFrame, "Bogus parentage");
00985     frameManager->ReParentStyleContext(kid);
00986   }
00987 }
00988 
00989 nsresult
00990 NS_NewFirstLineFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00991 {
00992   NS_PRECONDITION(nsnull != aNewFrame, "null ptr");
00993   if (nsnull == aNewFrame) {
00994     return NS_ERROR_NULL_POINTER;
00995   }
00996   nsInlineFrame* it = new (aPresShell) nsFirstLineFrame;
00997   if (nsnull == it) {
00998     return NS_ERROR_OUT_OF_MEMORY;
00999   }
01000   *aNewFrame = it;
01001   return NS_OK;
01002 }
01003 
01004 nsFirstLineFrame::nsFirstLineFrame()
01005 {
01006 }
01007 
01008 #ifdef DEBUG
01009 NS_IMETHODIMP
01010 nsFirstLineFrame::GetFrameName(nsAString& aResult) const
01011 {
01012   return MakeFrameName(NS_LITERAL_STRING("Line"), aResult);
01013 }
01014 #endif
01015 
01016 nsIAtom*
01017 nsFirstLineFrame::GetType() const
01018 {
01019   return nsLayoutAtoms::lineFrame;
01020 }
01021 
01022 void
01023 nsFirstLineFrame::StealFramesFrom(nsIFrame* aFrame)
01024 {
01025   nsIFrame* prevFrame = mFrames.GetPrevSiblingFor(aFrame);
01026   if (prevFrame) {
01027     prevFrame->SetNextSibling(nsnull);
01028   }
01029   else {
01030     mFrames.SetFrames(nsnull);
01031   }
01032 }
01033 
01034 nsIFrame*
01035 nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext, InlineReflowState& irs,
01036                                      PRBool* aIsComplete)
01037 {
01038   nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs, aIsComplete);
01039   if (frame && !mPrevInFlow) {
01040     // We are a first-line frame. Fixup the child frames
01041     // style-context that we just pulled.
01042     NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
01043     aPresContext->FrameManager()->ReParentStyleContext(frame);
01044   }
01045   return frame;
01046 }
01047 
01048 NS_IMETHODIMP
01049 nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
01050                          nsHTMLReflowMetrics& aMetrics,
01051                          const nsHTMLReflowState& aReflowState,
01052                          nsReflowStatus& aStatus)
01053 {
01054   if (nsnull == aReflowState.mLineLayout) {
01055     return NS_ERROR_INVALID_ARG;
01056   }
01057 
01058   nsIFrame* lineContainer = aReflowState.mLineLayout->GetLineContainerFrame();
01059 
01060   // Check for an overflow list with our prev-in-flow
01061   nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)mPrevInFlow;
01062   if (nsnull != prevInFlow) {
01063     nsIFrame* prevOverflowFrames = prevInFlow->GetOverflowFrames(aPresContext, PR_TRUE);
01064     if (prevOverflowFrames) {
01065       nsFrameList frames(prevOverflowFrames);
01066       
01067       // Assign all floats to our block if necessary
01068       if (lineContainer && lineContainer->GetPrevInFlow()) {
01069         ReparentFloatsForInlineChild(lineContainer, prevOverflowFrames, PR_TRUE);
01070       }
01071 
01072       mFrames.InsertFrames(this, nsnull, prevOverflowFrames);
01073       ReParentChildListStyle(aPresContext, frames, this);
01074     }
01075   }
01076 
01077   // It's also possible that we have an overflow list for ourselves
01078   nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_TRUE);
01079   if (overflowFrames) {
01080     NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
01081     nsFrameList frames(overflowFrames);
01082 
01083     mFrames.AppendFrames(nsnull, overflowFrames);
01084     ReParentChildListStyle(aPresContext, frames, this);
01085   }
01086 
01087   // Set our own reflow state (additional state above and beyond
01088   // aReflowState)
01089   InlineReflowState irs;
01090   irs.mPrevFrame = nsnull;
01091   irs.mLineContainer = lineContainer;
01092   irs.mNextInFlow = (nsInlineFrame*) mNextInFlow;
01093 
01094   nsresult rv;
01095   PRBool wasEmpty = mFrames.IsEmpty();
01096   if (wasEmpty) {
01097     // Try to pull over one frame before starting so that we know
01098     // whether we have an anonymous block or not.
01099     PRBool complete;
01100     PullOneFrame(aPresContext, irs, &complete);
01101   }
01102 
01103   if (nsnull == mPrevInFlow) {
01104     // XXX This is pretty sick, but what we do here is to pull-up, in
01105     // advance, all of the next-in-flows children. We re-resolve their
01106     // style while we are at at it so that when we reflow they have
01107     // the right style.
01108     //
01109     // All of this is so that text-runs reflow properly.
01110     irs.mPrevFrame = mFrames.LastChild();
01111     for (;;) {
01112       PRBool complete;
01113       nsIFrame* frame = PullOneFrame(aPresContext, irs, &complete);
01114       if (!frame) {
01115         break;
01116       }
01117       irs.mPrevFrame = frame;
01118     }
01119     irs.mPrevFrame = nsnull;
01120   }
01121   else {
01122 // XXX do this in the Init method instead
01123     // For continuations, we need to check and see if our style
01124     // context is right. If its the same as the first-in-flow, then
01125     // we need to fix it up (that way :first-line style doesn't leak
01126     // into this continuation since we aren't the first line).
01127     nsFirstLineFrame* first = (nsFirstLineFrame*) GetFirstInFlow();
01128     if (mStyleContext == first->mStyleContext) {
01129       // Fixup our style context and our children. First get the
01130       // proper parent context.
01131       nsStyleContext* parentContext = first->GetParent()->GetStyleContext();
01132       if (parentContext) {
01133         // Create a new style context that is a child of the parent
01134         // style context thus removing the :first-line style. This way
01135         // we behave as if an anonymous (unstyled) span was the child
01136         // of the parent frame.
01137         nsRefPtr<nsStyleContext> newSC;
01138         newSC = aPresContext->StyleSet()->
01139           ResolvePseudoStyleFor(nsnull,
01140                                 nsCSSAnonBoxes::mozLineFrame, parentContext);
01141         if (newSC) {
01142           // Switch to the new style context.
01143           SetStyleContext(aPresContext, newSC);
01144 
01145           // Re-resolve all children
01146           ReParentChildListStyle(aPresContext, mFrames, this);
01147         }
01148       }
01149     }
01150   }
01151 
01152   rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
01153 
01154   // Note: the line layout code will properly compute our overflow state for us
01155 
01156   return rv;
01157 }
01158 
01160 
01161 nsresult
01162 NS_NewPositionedInlineFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
01163 {
01164   NS_PRECONDITION(aNewFrame, "null OUT ptr");
01165   if (nsnull == aNewFrame) {
01166     return NS_ERROR_NULL_POINTER;
01167   }
01168   nsPositionedInlineFrame* it = new (aPresShell) nsPositionedInlineFrame();
01169   if (nsnull == it) {
01170     return NS_ERROR_OUT_OF_MEMORY;
01171   }
01172   *aNewFrame = it;
01173   return NS_OK;
01174 }
01175 
01176 NS_IMETHODIMP
01177 nsPositionedInlineFrame::Destroy(nsPresContext* aPresContext)
01178 {
01179   mAbsoluteContainer.DestroyFrames(this, aPresContext);
01180   return nsInlineFrame::Destroy(aPresContext);
01181 }
01182 
01183 NS_IMETHODIMP
01184 nsPositionedInlineFrame::SetInitialChildList(nsPresContext* aPresContext,
01185                                              nsIAtom*        aListName,
01186                                              nsIFrame*       aChildList)
01187 {
01188   nsresult  rv;
01189 
01190   if (mAbsoluteContainer.GetChildListName() == aListName) {
01191     rv = mAbsoluteContainer.SetInitialChildList(this, aPresContext, aListName, aChildList);
01192   } else {
01193     rv = nsInlineFrame::SetInitialChildList(aPresContext, aListName, aChildList);
01194   }
01195 
01196   return rv;
01197 }
01198 
01199 NS_IMETHODIMP
01200 nsPositionedInlineFrame::AppendFrames(nsIAtom*        aListName,
01201                                       nsIFrame*       aFrameList)
01202 {
01203   nsresult  rv;
01204   
01205   if (mAbsoluteContainer.GetChildListName() == aListName) {
01206     rv = mAbsoluteContainer.AppendFrames(this, aListName, aFrameList);
01207   } else {
01208     rv = nsInlineFrame::AppendFrames(aListName, aFrameList);
01209   }
01210 
01211   return rv;
01212 }
01213   
01214 NS_IMETHODIMP
01215 nsPositionedInlineFrame::InsertFrames(nsIAtom*        aListName,
01216                                       nsIFrame*       aPrevFrame,
01217                                       nsIFrame*       aFrameList)
01218 {
01219   nsresult  rv;
01220 
01221   if (mAbsoluteContainer.GetChildListName() == aListName) {
01222     rv = mAbsoluteContainer.InsertFrames(this, aListName, aPrevFrame,
01223                                          aFrameList);
01224   } else {
01225     rv = nsInlineFrame::InsertFrames(aListName, aPrevFrame, aFrameList);
01226   }
01227 
01228   return rv;
01229 }
01230   
01231 NS_IMETHODIMP
01232 nsPositionedInlineFrame::RemoveFrame(nsIAtom*        aListName,
01233                                      nsIFrame*       aOldFrame)
01234 {
01235   nsresult  rv;
01236 
01237   if (mAbsoluteContainer.GetChildListName() == aListName) {
01238     rv = mAbsoluteContainer.RemoveFrame(this, aListName, aOldFrame);
01239   } else {
01240     rv = nsInlineFrame::RemoveFrame(aListName, aOldFrame);
01241   }
01242 
01243   return rv;
01244 }
01245 
01246 NS_IMETHODIMP
01247 nsPositionedInlineFrame::ReplaceFrame(nsIAtom*        aListName,
01248                                       nsIFrame*       aOldFrame,
01249                                       nsIFrame*       aNewFrame)
01250 {
01251   if (mAbsoluteContainer.GetChildListName() == aListName) {
01252     return mAbsoluteContainer.ReplaceFrame(this, aListName, aOldFrame,
01253                                            aNewFrame);
01254   } else {
01255     return nsInlineFrame::ReplaceFrame(aListName, aOldFrame, aNewFrame);
01256   }
01257 }
01258 
01259 nsIAtom*
01260 nsPositionedInlineFrame::GetAdditionalChildListName(PRInt32 aIndex) const
01261 {
01262   if (0 == aIndex) {
01263     return mAbsoluteContainer.GetChildListName();
01264   }
01265   return nsnull;
01266 }
01267 
01268 nsIFrame*
01269 nsPositionedInlineFrame::GetFirstChild(nsIAtom* aListName) const
01270 {
01271   if (mAbsoluteContainer.GetChildListName() == aListName) {
01272     nsIFrame* result = nsnull;
01273     mAbsoluteContainer.FirstChild(this, aListName, &result);
01274     return result;
01275   }
01276 
01277   return nsInlineFrame::GetFirstChild(aListName);
01278 }
01279 
01280 nsIAtom*
01281 nsPositionedInlineFrame::GetType() const
01282 {
01283   return nsLayoutAtoms::positionedInlineFrame;
01284 }
01285 
01286 NS_IMETHODIMP
01287 nsPositionedInlineFrame::Reflow(nsPresContext*          aPresContext,
01288                                 nsHTMLReflowMetrics&     aDesiredSize,
01289                                 const nsHTMLReflowState& aReflowState,
01290                                 nsReflowStatus&          aStatus)
01291 {
01292   nsresult  rv = NS_OK;
01293 
01294   // Don't bother optimizing for fast incremental reflow of absolute
01295   // children of an inline
01296 
01297   // Let the inline frame do its reflow first
01298   rv = nsInlineFrame::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
01299 
01300   // Let the absolutely positioned container reflow any absolutely positioned
01301   // child frames that need to be reflowed
01302   // We want to do this under either of two conditions:
01303   //  1. If we didn't do the incremental reflow above.
01304   //  2. If our size changed.
01305   // Even though it's the padding edge that's the containing block, we
01306   // can use our rect (the border edge) since if the border style
01307   // changed, the reflow would have been targeted at us so we'd satisfy
01308   // condition 1.
01309   if (NS_SUCCEEDED(rv) &&
01310       mAbsoluteContainer.HasAbsoluteFrames()) {
01311     // The containing block for the abs pos kids is formed by our padding edge.
01312     nsMargin computedBorder =
01313       aReflowState.mComputedBorderPadding - aReflowState.mComputedPadding;
01314     nscoord containingBlockWidth =
01315       aDesiredSize.width - computedBorder.LeftRight();
01316     nscoord containingBlockHeight =
01317       aDesiredSize.height - computedBorder.TopBottom();
01318 
01319     // Do any incremental reflows ... would be nice to merge with
01320     // the reflows below but that would be more work, and more risky
01321     if (eReflowReason_Incremental == aReflowState.reason) {
01322       mAbsoluteContainer.IncrementalReflow(this, aPresContext, aReflowState,
01323                                            containingBlockWidth,
01324                                            containingBlockHeight);
01325     }
01326 
01327     // Factor the absolutely positioned child bounds into the overflow area
01328     // Don't include this frame's bounds, nor its inline descendants' bounds,
01329     // and don't store the overflow property.
01330     // That will all be done by nsLineLayout::RelativePositionFrames.
01331     rv = mAbsoluteContainer.Reflow(this, aPresContext, aReflowState,
01332                                    containingBlockWidth, containingBlockHeight,
01333                                    &aDesiredSize.mOverflowArea);
01334   }
01335 
01336   return rv;
01337 }