Back to index

lightning-sunbird  0.9+nobinonly
nsFieldSetFrame.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.org 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 
00038 // YY need to pass isMultiple before create called
00039 
00040 //#include "nsFormControlFrame.h"
00041 #include "nsHTMLContainerFrame.h"
00042 #include "nsLegendFrame.h"
00043 #include "nsIDOMNode.h"
00044 #include "nsIDOMHTMLFieldSetElement.h"
00045 #include "nsIDOMHTMLLegendElement.h"
00046 #include "nsCSSRendering.h"
00047 //#include "nsIDOMHTMLCollection.h"
00048 #include "nsIContent.h"
00049 #include "nsIFrame.h"
00050 #include "nsISupports.h"
00051 #include "nsIAtom.h"
00052 #include "nsPresContext.h"
00053 #include "nsFrameManager.h"
00054 #include "nsHTMLParts.h"
00055 #include "nsHTMLAtoms.h"
00056 #include "nsLayoutAtoms.h"
00057 #include "nsStyleConsts.h"
00058 #include "nsFont.h"
00059 #include "nsCOMPtr.h"
00060 #ifdef ACCESSIBILITY
00061 #include "nsIAccessibilityService.h"
00062 #endif
00063 #include "nsIServiceManager.h"
00064 #include "nsSpaceManager.h"
00065 
00066 class nsLegendFrame;
00067 
00068 class nsFieldSetFrame : public nsHTMLContainerFrame {
00069 public:
00070 
00071   nsFieldSetFrame();
00072 
00073   NS_IMETHOD SetInitialChildList(nsPresContext* aPresContext,
00074                                  nsIAtom*       aListName,
00075                                  nsIFrame*      aChildList);
00076 
00077   NS_IMETHOD Reflow(nsPresContext*           aPresContext,
00078                     nsHTMLReflowMetrics&     aDesiredSize,
00079                     const nsHTMLReflowState& aReflowState,
00080                     nsReflowStatus&          aStatus);
00081                                
00082   NS_IMETHOD Paint(nsPresContext*       aPresContext,
00083                    nsIRenderingContext& aRenderingContext,
00084                    const nsRect&        aDirtyRect,
00085                    nsFramePaintLayer    aWhichLayer,
00086                    PRUint32             aFlags);
00087 
00088   virtual PRBool CanPaintBackground();
00089 
00090   NS_IMETHOD AppendFrames(nsIAtom*       aListName,
00091                           nsIFrame*      aFrameList);
00092   NS_IMETHOD InsertFrames(nsIAtom*       aListName,
00093                           nsIFrame*      aPrevFrame,
00094                           nsIFrame*      aFrameList);
00095   NS_IMETHOD RemoveFrame(nsIAtom*       aListName,
00096                          nsIFrame*      aOldFrame);
00097   NS_IMETHOD ReplaceFrame(nsIAtom*       aListName,
00098                           nsIFrame*      aOldFrame,
00099                           nsIFrame*      aNewFrame);
00100 
00101   NS_IMETHOD  GetFrameForPoint(const nsPoint&    aPoint, 
00102                                nsFramePaintLayer aWhichLayer,
00103                                nsIFrame**        aFrame);
00104 
00105   virtual nsIAtom* GetType() const;
00106   virtual PRBool IsContainingBlock() const;
00107 
00108 #ifdef ACCESSIBILITY  
00109   NS_IMETHOD  GetAccessible(nsIAccessible** aAccessible);
00110 #endif
00111 
00112 #ifdef DEBUG
00113   NS_IMETHOD GetFrameName(nsAString& aResult) const {
00114     return MakeFrameName(NS_LITERAL_STRING("FieldSet"), aResult);
00115   }
00116 #endif
00117 
00118 protected:
00119 
00120   virtual PRIntn GetSkipSides() const;
00121   nsIFrame* MaybeSetLegend(nsIFrame* aFrameList, nsIAtom* aListName);
00122   void ReParentFrameList(nsIFrame* aFrameList);
00123 
00124   nsIFrame* mLegendFrame;
00125   nsIFrame* mContentFrame;
00126   nsRect    mLegendRect;
00127   nscoord   mLegendSpace;
00128 };
00129 
00130 nsresult
00131 NS_NewFieldSetFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame, PRUint32 aStateFlags)
00132 {
00133   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00134   if (nsnull == aNewFrame) {
00135     return NS_ERROR_NULL_POINTER;
00136   }
00137   nsFieldSetFrame* it = new (aPresShell) nsFieldSetFrame;
00138   if (!it) {
00139     return NS_ERROR_OUT_OF_MEMORY;
00140   }
00141 
00142   // set the state flags (if any are provided)
00143   it->AddStateBits(aStateFlags);
00144   
00145   *aNewFrame = it;
00146   return NS_OK;
00147 }
00148 
00149 nsFieldSetFrame::nsFieldSetFrame()
00150   : nsHTMLContainerFrame()
00151 {
00152   mContentFrame = nsnull;
00153   mLegendFrame  = nsnull;
00154   mLegendSpace  = 0;
00155 }
00156 
00157 nsIAtom*
00158 nsFieldSetFrame::GetType() const
00159 {
00160   return nsLayoutAtoms::fieldSetFrame;
00161 }
00162 
00163 PRBool
00164 nsFieldSetFrame::IsContainingBlock() const
00165 {
00166   return PR_TRUE;
00167 }
00168 
00169 NS_IMETHODIMP
00170 nsFieldSetFrame::SetInitialChildList(nsPresContext* aPresContext,
00171                                      nsIAtom*       aListName,
00172                                      nsIFrame*      aChildList)
00173 {
00174   // Get the content and legend frames.
00175   if (aChildList->GetNextSibling()) {
00176     mContentFrame = aChildList->GetNextSibling();
00177     mLegendFrame  = aChildList;
00178   } else {
00179     mContentFrame = aChildList;
00180     mLegendFrame  = nsnull;
00181   }
00182 
00183   // Queue up the frames for the content frame
00184   return nsHTMLContainerFrame::SetInitialChildList(aPresContext, nsnull, aChildList);
00185 }
00186 
00187 // this is identical to nsHTMLContainerFrame::Paint except for the background and border. 
00188 NS_IMETHODIMP
00189 nsFieldSetFrame::Paint(nsPresContext*       aPresContext,
00190                        nsIRenderingContext& aRenderingContext,
00191                        const nsRect&        aDirtyRect,
00192                        nsFramePaintLayer    aWhichLayer,
00193                        PRUint32             aFlags)
00194 {
00195   if (NS_FRAME_PAINT_LAYER_BACKGROUND == aWhichLayer) {
00196     // Paint our background and border
00197     PRBool isVisible;
00198     if (NS_SUCCEEDED(IsVisibleForPainting(aPresContext, aRenderingContext, PR_TRUE, &isVisible)) && 
00199                      isVisible && mRect.width && mRect.height) {
00200       PRIntn skipSides = GetSkipSides();
00201       const nsStyleBorder* borderStyle = GetStyleBorder();
00202       const nsStylePadding* paddingStyle = GetStylePadding();
00203        
00204       nscoord topBorder = borderStyle->GetBorderWidth(NS_SIDE_TOP);
00205 
00206       nscoord yoff = 0;
00207       
00208       // if the border is smaller than the legend. Move the border down
00209       // to be centered on the legend. 
00210       if (topBorder < mLegendRect.height)
00211         yoff = (mLegendRect.height - topBorder)/2;
00212       
00213       nsRect rect(0, yoff, mRect.width, mRect.height - yoff);
00214 
00215       nsCSSRendering::PaintBackground(aPresContext, aRenderingContext, this,
00216                                       aDirtyRect, rect, *borderStyle,
00217                                       *paddingStyle, PR_TRUE);
00218 
00219       if (mLegendFrame) {
00220 
00221         // Use the rect of the legend frame, not mLegendRect, so we draw our
00222         // border under the legend's left and right margins.
00223         const nsRect & legendRect = mLegendFrame->GetRect();
00224       
00225         // we should probably use PaintBorderEdges to do this but for now just use clipping
00226         // to achieve the same effect.
00227 
00228         // draw left side
00229         nsRect clipRect(rect);
00230         clipRect.width = legendRect.x - rect.x;
00231         clipRect.height = topBorder;
00232 
00233         aRenderingContext.PushState();
00234         aRenderingContext.SetClipRect(clipRect, nsClipCombine_kIntersect);
00235         nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, this,
00236                                     aDirtyRect, rect, *borderStyle, mStyleContext, skipSides);
00237   
00238         aRenderingContext.PopState();
00239 
00240 
00241         // draw right side
00242         clipRect = rect;
00243         clipRect.x = legendRect.x + legendRect.width;
00244         clipRect.width -= (legendRect.x + legendRect.width);
00245         clipRect.height = topBorder;
00246 
00247         aRenderingContext.PushState();
00248         aRenderingContext.SetClipRect(clipRect, nsClipCombine_kIntersect);
00249         nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, this,
00250                                     aDirtyRect, rect, *borderStyle, mStyleContext, skipSides);
00251   
00252         aRenderingContext.PopState();
00253 
00254       
00255         // draw bottom
00256         clipRect = rect;
00257         clipRect.y += topBorder;
00258         clipRect.height = mRect.height - (yoff + topBorder);
00259       
00260         aRenderingContext.PushState();
00261         aRenderingContext.SetClipRect(clipRect, nsClipCombine_kIntersect);
00262         nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, this,
00263                                     aDirtyRect, rect, *borderStyle, mStyleContext, skipSides);
00264   
00265         aRenderingContext.PopState();
00266       } else {
00267 
00268         nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, this,
00269                                     aDirtyRect,
00270                                     nsRect(0,0,mRect.width, mRect.height),
00271                                     *borderStyle, mStyleContext, skipSides);
00272       }
00273     }
00274   }
00275 
00276   PaintChildren(aPresContext, aRenderingContext, aDirtyRect, aWhichLayer);
00277 
00278 #ifdef DEBUG
00279   if ((NS_FRAME_PAINT_LAYER_DEBUG == aWhichLayer) && GetShowFrameBorders()) {
00280     if (HasView()) {
00281       aRenderingContext.SetColor(NS_RGB(0,0,255));
00282     }
00283     else {
00284       aRenderingContext.SetColor(NS_RGB(255,0,0));
00285     }
00286     aRenderingContext.DrawRect(0, 0, mRect.width, mRect.height);
00287   }
00288 #endif
00289   DO_GLOBAL_REFLOW_COUNT_DSP("nsFieldSetFrame", &aRenderingContext);
00290   return NS_OK;
00291 }
00292 
00293 PRBool
00294 nsFieldSetFrame::CanPaintBackground()
00295 {
00296   // If we have a legend, we won't be painting our background across our whole
00297   // rect.  So return false here, since we'll usually have a legend.
00298   return PR_FALSE;
00299 }
00300 
00301 NS_IMETHODIMP 
00302 nsFieldSetFrame::Reflow(nsPresContext*           aPresContext,
00303                         nsHTMLReflowMetrics&     aDesiredSize,
00304                         const nsHTMLReflowState& aReflowState,
00305                         nsReflowStatus&          aStatus)
00306 {
00307   DO_GLOBAL_REFLOW_COUNT("nsFieldSetFrame", aReflowState.reason);
00308   DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
00309 
00310   // Initialize OUT parameter
00311   aStatus = NS_FRAME_COMPLETE;
00312 
00313   // Should we create a space manager?
00314   nsAutoSpaceManager autoSpaceManager(NS_CONST_CAST(nsHTMLReflowState &, aReflowState));
00315 
00316   // XXXldb If we start storing the space manager in the frame rather
00317   // than keeping it around only during reflow then we should create it
00318   // only when there are actually floats to manage.  Otherwise things
00319   // like tables will gain significant bloat.
00320   if (NS_BLOCK_SPACE_MGR & mState)
00321     autoSpaceManager.CreateSpaceManagerFor(aPresContext, this);
00322 
00323   
00324   //------------ Handle Incremental Reflow -----------------
00325   PRBool reflowContent = PR_TRUE;
00326   PRBool reflowLegend = PR_TRUE;
00327   nsReflowReason reason = aReflowState.reason;
00328 
00329   if (reason == eReflowReason_Incremental) {
00330       nsHTMLReflowCommand *command = aReflowState.path->mReflowCommand;
00331 
00332       // See if it's targeted at us
00333       if (command) {
00334         nsReflowType  reflowType;
00335         command->GetType(reflowType);
00336 
00337         switch (reflowType) {
00338           case eReflowType_StyleChanged:
00339             reason = eReflowReason_StyleChange;
00340             break;
00341 
00342           case eReflowType_ReflowDirty: 
00343             reason = eReflowReason_Dirty;
00344             break;
00345 
00346           default:
00347             NS_ERROR("Unexpected Reflow Type");
00348         }
00349       } else {
00350            reflowContent = PR_FALSE;
00351            reflowLegend = PR_FALSE;
00352 
00353            nsReflowPath::iterator iter = aReflowState.path->FirstChild();
00354            nsReflowPath::iterator end = aReflowState.path->EndChildren();
00355 
00356            for ( ; iter != end; ++iter) {
00357                if (*iter == mLegendFrame)
00358                    reflowLegend = PR_TRUE;
00359                else if (*iter == mContentFrame)
00360                    reflowContent = PR_TRUE;
00361            }
00362       }
00363   }
00364 
00365   if (aDesiredSize.mFlags & NS_REFLOW_CALC_MAX_WIDTH) {
00366     reflowLegend = PR_TRUE;
00367     reflowContent = PR_TRUE;
00368   } else if (reason == eReflowReason_Dirty) {
00369     // if dirty then check dirty flags
00370     if (GetStateBits() & NS_FRAME_IS_DIRTY) {
00371       reflowLegend = PR_TRUE;
00372       reflowContent = PR_TRUE;
00373     } else {
00374       if (reflowContent) {
00375         reflowContent = mContentFrame ?
00376           (mContentFrame->GetStateBits()
00377            & (NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN)) != 0 : PR_FALSE;
00378       }
00379 
00380       if (reflowLegend) {
00381         reflowLegend = mLegendFrame ?
00382           (mLegendFrame->GetStateBits()
00383            & (NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN)) != 0 : PR_FALSE;
00384       }
00385     }
00386   }
00387 
00388   // availSize could have unconstrained values, don't perform any addition on them
00389   nsSize availSize(aReflowState.mComputedWidth, aReflowState.availableHeight);
00390 
00391   // get our border and padding
00392   const nsMargin &borderPadding = aReflowState.mComputedBorderPadding;
00393   const nsMargin &padding       = aReflowState.mComputedPadding;
00394   nsMargin border = borderPadding - padding;
00395   if (aDesiredSize.mComputeMEW) {
00396     aDesiredSize.mMaxElementWidth = borderPadding.left + borderPadding.right;
00397   }
00398   
00399 
00400   // Figure out how big the legend is if there is one. 
00401   // get the legend's margin
00402   nsMargin legendMargin(0,0,0,0);
00403   // reflow the legend only if needed
00404   if (mLegendFrame) {
00405     const nsStyleMargin* marginStyle = mLegendFrame->GetStyleMargin();
00406     marginStyle->GetMargin(legendMargin);
00407 
00408     if (reflowLegend) {
00409       nsHTMLReflowState legendReflowState(aPresContext, aReflowState,
00410                                           mLegendFrame, nsSize(NS_INTRINSICSIZE,NS_INTRINSICSIZE),
00411                                           reason);
00412 
00413       // always give the legend as much size as it needs
00414       legendReflowState.mComputedWidth = NS_INTRINSICSIZE;
00415       legendReflowState.mComputedHeight = NS_INTRINSICSIZE;
00416 
00417       nsHTMLReflowMetrics legendDesiredSize(0,0);
00418 
00419       ReflowChild(mLegendFrame, aPresContext, legendDesiredSize, legendReflowState,
00420                   0, 0, NS_FRAME_NO_MOVE_FRAME, aStatus);
00421 #ifdef NOISY_REFLOW
00422       printf("  returned (%d, %d)\n", legendDesiredSize.width, legendDesiredSize.height);
00423       if (legendDesiredSize.mComputeMEW)
00424         printf("  and maxEW %d\n", 
00425                legendDesiredSize.mMaxElementWidth);
00426 #endif
00427       // figure out the legend's rectangle
00428       mLegendRect.width  = legendDesiredSize.width + legendMargin.left + legendMargin.right;
00429       mLegendRect.height = legendDesiredSize.height + legendMargin.top + legendMargin.bottom;
00430       mLegendRect.x = borderPadding.left;
00431       mLegendRect.y = 0;
00432 
00433       nscoord oldSpace = mLegendSpace;
00434       mLegendSpace = 0;
00435       if (mLegendRect.height > border.top) {
00436         // center the border on the legend
00437         mLegendSpace = mLegendRect.height - border.top;
00438       } else {
00439         mLegendRect.y = (border.top - mLegendRect.height)/2;
00440       }
00441 
00442       // if the legend space changes then we need to reflow the 
00443       // content area as well.
00444       if (mLegendSpace != oldSpace) {
00445         if (reflowContent == PR_FALSE || reason == eReflowReason_Dirty) {
00446           reflowContent = PR_TRUE;
00447           reason = eReflowReason_Resize;
00448         }
00449       }
00450 
00451       // if we are contrained then remove the legend from our available height.
00452       if (NS_INTRINSICSIZE != availSize.height) {
00453         if (availSize.height >= mLegendSpace)
00454           availSize.height -= mLegendSpace;
00455       }
00456   
00457       // don't get any smaller than the legend
00458       if (NS_INTRINSICSIZE != availSize.width) {
00459         if (availSize.width < mLegendRect.width)
00460           availSize.width = mLegendRect.width;
00461       }
00462 
00463       FinishReflowChild(mLegendFrame, aPresContext, &legendReflowState, 
00464                         legendDesiredSize, 0, 0, NS_FRAME_NO_MOVE_FRAME);    
00465 
00466     }
00467   } else {
00468     mLegendRect.Empty();
00469     mLegendSpace = 0;
00470   }
00471 
00472   nsRect contentRect;
00473 
00474   // reflow the content frame only if needed
00475   if (mContentFrame) {
00476     if (reflowContent) {
00477       availSize.width = aReflowState.mComputedWidth;
00478 
00479       nsHTMLReflowState kidReflowState(aPresContext, aReflowState, mContentFrame,
00480                                        availSize, reason);
00481 
00482       nsHTMLReflowMetrics kidDesiredSize(aDesiredSize.mComputeMEW, aDesiredSize.mFlags);
00483       // Reflow the frame
00484       ReflowChild(mContentFrame, aPresContext, kidDesiredSize, kidReflowState,
00485                   borderPadding.left + kidReflowState.mComputedMargin.left,
00486                   borderPadding.top + mLegendSpace + kidReflowState.mComputedMargin.top,
00487                   0, aStatus);
00488 
00489       // set the rect. make sure we add the margin back in.
00490       contentRect.SetRect(borderPadding.left,borderPadding.top + mLegendSpace,kidDesiredSize.width ,kidDesiredSize.height);
00491       if (aReflowState.mComputedHeight != NS_INTRINSICSIZE &&
00492           borderPadding.top + mLegendSpace+kidDesiredSize.height > aReflowState.mComputedHeight) {
00493         kidDesiredSize.height = aReflowState.mComputedHeight-(borderPadding.top + mLegendSpace);
00494       }
00495 
00496       FinishReflowChild(mContentFrame, aPresContext, &kidReflowState, 
00497                         kidDesiredSize, contentRect.x, contentRect.y, 0);
00498       if (aDesiredSize.mComputeMEW) {
00499         aDesiredSize.mMaxElementWidth = kidDesiredSize.mMaxElementWidth;
00500         if (eStyleUnit_Coord == aReflowState.mStylePosition->mWidth.GetUnit() &&
00501             NS_INTRINSICSIZE != aReflowState.mComputedWidth)
00502           aDesiredSize.mMaxElementWidth = aReflowState.mComputedWidth;
00503         if (eStyleUnit_Percent == aReflowState.mStylePosition->mWidth.GetUnit())
00504           aDesiredSize.mMaxElementWidth = 0;
00505         aDesiredSize.mMaxElementWidth += borderPadding.left + borderPadding.right;
00506       }
00507       if (aDesiredSize.mFlags & NS_REFLOW_CALC_MAX_WIDTH) {
00508         aDesiredSize.mMaximumWidth = kidDesiredSize.mMaximumWidth +
00509                                      borderPadding.left + borderPadding.right;
00510       }
00511       NS_FRAME_TRACE_REFLOW_OUT("FieldSet::Reflow", aStatus);
00512 
00513     } else {
00514       // if we don't need to reflow just get the old size
00515       contentRect = mContentFrame->GetRect();
00516       const nsStyleMargin* marginStyle = mContentFrame->GetStyleMargin();
00517 
00518       nsMargin m(0,0,0,0);
00519       marginStyle->GetMargin(m);
00520       contentRect.Inflate(m);
00521     }
00522   }
00523 
00524   // use the computed width if the inner content does not fill it
00525   if (aReflowState.mComputedWidth != NS_INTRINSICSIZE &&
00526       aReflowState.mComputedWidth > contentRect.width) {
00527     contentRect.width = aReflowState.mComputedWidth;
00528   }
00529 
00530   if (mLegendFrame) {
00531     // if the content rect is larger then the  legend we can align the legend
00532     if (contentRect.width > mLegendRect.width) {
00533       PRInt32 align = ((nsLegendFrame*)mLegendFrame)->GetAlign();
00534 
00535       switch(align) {
00536         case NS_STYLE_TEXT_ALIGN_RIGHT:
00537           mLegendRect.x = contentRect.width - mLegendRect.width + borderPadding.left;
00538           break;
00539         case NS_STYLE_TEXT_ALIGN_CENTER:
00540           float p2t;
00541           p2t = aPresContext->PixelsToTwips();
00542           mLegendRect.x = NSIntPixelsToTwips((nscoord) NSToIntRound((float)(contentRect.width/2 - mLegendRect.width/2 + borderPadding.left) / p2t),p2t);
00543           break;
00544       }
00545   
00546     } else {
00547       //otherwise make place for the legend
00548       contentRect.width = mLegendRect.width;
00549     }
00550     // place the legend
00551     nsRect actualLegendRect(mLegendRect);
00552     actualLegendRect.Deflate(legendMargin);
00553 
00554     nsPoint curOrigin = mLegendFrame->GetPosition();
00555 
00556     // only if the origin changed
00557     if ((curOrigin.x != mLegendRect.x) || (curOrigin.y != mLegendRect.y)) {
00558       mLegendFrame->SetPosition(nsPoint(actualLegendRect.x , actualLegendRect.y));
00559       nsContainerFrame::PositionFrameView(mLegendFrame);
00560 
00561       // We need to recursively process the legend frame's
00562       // children since we're moving the frame after Reflow.
00563       nsContainerFrame::PositionChildViews(mLegendFrame);
00564     }
00565   }
00566 
00567   // Return our size and our result
00568   if (aReflowState.mComputedHeight == NS_INTRINSICSIZE) {
00569     aDesiredSize.height = mLegendSpace + 
00570                           borderPadding.top +
00571                           contentRect.height +
00572                           borderPadding.bottom;
00573   } else {
00574     nscoord min = borderPadding.top + borderPadding.bottom + mLegendRect.height;
00575     aDesiredSize.height = aReflowState.mComputedHeight + borderPadding.top + borderPadding.bottom;
00576     if (aDesiredSize.height < min)
00577       aDesiredSize.height = min;
00578   }
00579   aDesiredSize.width = contentRect.width + borderPadding.left + borderPadding.right;
00580   aDesiredSize.ascent  = aDesiredSize.height;
00581   aDesiredSize.descent = 0;
00582   if (aDesiredSize.mComputeMEW) {
00583     // if the legend is wider use it
00584     if (aDesiredSize.mMaxElementWidth < mLegendRect.width + borderPadding.left + borderPadding.right)
00585       aDesiredSize.mMaxElementWidth = mLegendRect.width + borderPadding.left + borderPadding.right;
00586   }
00587   aDesiredSize.mOverflowArea = nsRect(0, 0, aDesiredSize.width, aDesiredSize.height);
00588   // make the mMaximumWidth large enough if the legendframe determines the size
00589   if ((aDesiredSize.mFlags & NS_REFLOW_CALC_MAX_WIDTH) && mLegendFrame) {
00590     aDesiredSize.mMaximumWidth = PR_MAX(aDesiredSize.mMaximumWidth, mLegendRect.width +
00591                                         borderPadding.left + borderPadding.right);
00592   }
00593   if (mLegendFrame)
00594     ConsiderChildOverflow(aDesiredSize.mOverflowArea, mLegendFrame);
00595   if (mContentFrame)
00596     ConsiderChildOverflow(aDesiredSize.mOverflowArea, mContentFrame);
00597   FinishAndStoreOverflow(&aDesiredSize);
00598 
00599   Invalidate(aDesiredSize.mOverflowArea);
00600 
00601   NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
00602   return NS_OK;
00603 }
00604 
00605 PRIntn
00606 nsFieldSetFrame::GetSkipSides() const
00607 {
00608   return 0;
00609 }
00610 
00611 NS_IMETHODIMP
00612 nsFieldSetFrame::AppendFrames(nsIAtom*       aListName,
00613                               nsIFrame*      aFrameList)
00614 {
00615   aFrameList = MaybeSetLegend(aFrameList, aListName);
00616   if (aFrameList) {
00617     ReParentFrameList(aFrameList);
00618     return mContentFrame->AppendFrames(aListName, aFrameList);
00619   }
00620   return NS_OK;
00621 }
00622 
00623 NS_IMETHODIMP
00624 nsFieldSetFrame::InsertFrames(nsIAtom*       aListName,
00625                               nsIFrame*      aPrevFrame,
00626                               nsIFrame*      aFrameList)
00627 {
00628   aFrameList = MaybeSetLegend(aFrameList, aListName);
00629   if (aFrameList) {
00630     ReParentFrameList(aFrameList);
00631     if (NS_UNLIKELY(aPrevFrame == mLegendFrame)) {
00632       aPrevFrame = nsnull;
00633     }
00634     return mContentFrame->InsertFrames(aListName, aPrevFrame, aFrameList);
00635   }
00636   return NS_OK;
00637 }
00638 
00639 NS_IMETHODIMP
00640 nsFieldSetFrame::RemoveFrame(nsIAtom*       aListName,
00641                              nsIFrame*      aOldFrame)
00642 {
00643   // For reference, see bug 70648, bug 276104 and bug 236071.
00644   if (aOldFrame == mLegendFrame) {
00645     NS_ASSERTION(!aListName, "Unexpected frame list when removing legend frame");
00646     NS_ASSERTION(mLegendFrame->GetParent() == this, "Legend Parent has wrong parent");
00647     NS_ASSERTION(mLegendFrame->GetNextSibling() == mContentFrame, "mContentFrame is not next sibling");
00648     nsPresContext* presContext = GetPresContext();
00649     mFrames.DestroyFrame(presContext, mLegendFrame);
00650     mLegendFrame = nsnull;
00651     AddStateBits(NS_FRAME_IS_DIRTY);
00652     if (GetParent()) {
00653       GetParent()->ReflowDirtyChild(presContext->GetPresShell(), this);
00654     }
00655     return NS_OK;
00656   }
00657   return mContentFrame->RemoveFrame(aListName, aOldFrame);
00658 }
00659 
00660 NS_IMETHODIMP
00661 nsFieldSetFrame::ReplaceFrame(nsIAtom*       aListName,
00662                               nsIFrame*      aOldFrame,
00663                               nsIFrame*      aNewFrame)
00664 {
00665   if (aOldFrame == mLegendFrame) {
00666     mLegendFrame = aNewFrame;
00667     return nsContainerFrame::ReplaceFrame(aListName, aOldFrame, aNewFrame);
00668   }
00669   return mContentFrame->ReplaceFrame(aListName, aOldFrame, aNewFrame);
00670 }
00671 
00672 NS_IMETHODIMP
00673 nsFieldSetFrame::GetFrameForPoint(const nsPoint&    aPoint, 
00674                                   nsFramePaintLayer aWhichLayer,
00675                                   nsIFrame**        aFrame)
00676 {
00677   // this should act like a block, so we need to override
00678   return GetFrameForPointUsing(aPoint, nsnull, aWhichLayer, (aWhichLayer == NS_FRAME_PAINT_LAYER_BACKGROUND), aFrame);
00679 }
00680 
00681 #ifdef ACCESSIBILITY
00682 NS_IMETHODIMP nsFieldSetFrame::GetAccessible(nsIAccessible** aAccessible)
00683 {
00684   nsCOMPtr<nsIAccessibilityService> accService = do_GetService("@mozilla.org/accessibilityService;1");
00685 
00686   if (accService) {
00687     return accService->CreateHTMLGroupboxAccessible(NS_STATIC_CAST(nsIFrame*, this), aAccessible);
00688   }
00689 
00690   return NS_ERROR_FAILURE;
00691 }
00692 #endif
00693 
00694 nsIFrame*
00695 nsFieldSetFrame::MaybeSetLegend(nsIFrame* aFrameList, nsIAtom* aListName)
00696 {
00697   if (!mLegendFrame && aFrameList->GetType() == nsLayoutAtoms::legendFrame) {
00698     NS_ASSERTION(!aListName, "Unexpected frame list when adding legend frame");
00699     mLegendFrame = aFrameList;
00700     aFrameList = mLegendFrame->GetNextSibling();
00701     mLegendFrame->SetNextSibling(mContentFrame);
00702     mFrames.SetFrames(mLegendFrame);
00703     AddStateBits(NS_FRAME_IS_DIRTY);
00704     if (GetParent()) {
00705       GetParent()->ReflowDirtyChild(mLegendFrame->GetPresContext()->GetPresShell(), this);
00706     }
00707   }
00708   return aFrameList;
00709 }
00710 
00711 void
00712 nsFieldSetFrame::ReParentFrameList(nsIFrame* aFrameList)
00713 {
00714   nsFrameManager* frameManager = GetPresContext()->FrameManager();
00715   for (nsIFrame* frame = aFrameList; frame; frame = frame->GetNextSibling()) {
00716     frame->SetParent(mContentFrame);
00717     frameManager->ReParentStyleContext(frame);
00718   }
00719   mContentFrame->AddStateBits(GetStateBits() & NS_FRAME_HAS_CHILD_WITH_VIEW);
00720 }