PRBool needAnotherLegendReflow = PR_FALSE;
if (NS_INTRINSICSIZE != availSize.height) availSize.height -= (contentMargin.top + contentMargin.bottom);
nsFieldSetFrame::Reflow(nsIPresContext&          aPresContext,                        nsHTMLReflowMetrics&     aDesiredSize,                        const nsHTMLReflowState& aReflowState,                        nsReflowStatus&          aStatus){  // XXX remove the following when the reflow state is fixed#ifndef bug4534_not_fixed  FieldSetHack((nsHTMLReflowState&)aReflowState, "fieldset", PR_TRUE);#endif  // availSize could have unconstrained values, don't perform any addition on them  nsSize availSize(aReflowState.computedWidth, aReflowState.computedHeight);    float p2t;  aPresContext.GetScaledPixelsToTwips(&p2t);  const PRInt32 desiredLegendOffset = NSIntPixelsToTwips(DESIRED_LEGEND_OFFSET, p2t);  const nsStyleSpacing* spacing =    (const nsStyleSpacing*)mStyleContext->GetStyleData(eStyleStruct_Spacing);  //nsMargin margin;  //spacing->CalcMarginFor(this, margin);   nsMargin border;  nsMargin padding;  spacing->CalcBorderFor(this, border);  spacing->CalcPaddingFor(this, padding);  nsMargin borderPadding = border + padding;  nsMargin legendMargin(0,0,0,0);  if (mLegendFrame) {    nsIStyleContext* legendSC = nsnull;    mLegendFrame->GetStyleContext(&legendSC);    if (legendSC) {      const nsStyleSpacing* legendSpacing =        (const nsStyleSpacing*)legendSC->GetStyleData(eStyleStruct_Spacing);      legendSpacing->CalcMarginFor(mLegendFrame, legendMargin);      NS_RELEASE(legendSC);    }  }  // reduce available space by border, padding, etc. if we're in a constrained situation  nscoord horTaken = borderPadding.left + borderPadding.right + legendMargin.left + legendMargin.right;  nscoord verTaken = borderPadding.top + borderPadding.bottom + legendMargin.top + legendMargin.bottom;  if (NS_INTRINSICSIZE != availSize.width) {    availSize.width -= horTaken;    availSize.width = PR_MAX(availSize.width,0);  }  if (NS_AUTOHEIGHT != availSize.height) {    // XXX this assumes that the legend is taller than the top border width    availSize.height -= verTaken;    availSize.height = PR_MAX(availSize.height,0);  }  nsSize maxElementSize(0,0);  // Try to reflow the legend into the available space. It might not fit  nsSize legendSize(0,0);  if (mLegendFrame) {    nsHTMLReflowState legendReflowState(aPresContext, aReflowState,                                        mLegendFrame, availSize);    // XXX remove when reflow state is fixed#ifndef bug4534_not_fixed    FieldSetHack((nsHTMLReflowState&)legendReflowState, "fieldset's legend", PR_FALSE);#endif    ReflowChild(mLegendFrame, aPresContext, aDesiredSize, legendReflowState, aStatus);    legendSize.width  = aDesiredSize.width;    legendSize.height = aDesiredSize.height;    // The legend didn't fit    if ((NS_INTRINSICSIZE != availSize.width) &&         (availSize.width < aDesiredSize.width + legendMargin.left + legendMargin.right)) {      availSize.width = aDesiredSize.width + horTaken;    }     if (NS_AUTOHEIGHT != availSize.height) {      if (availSize.height < aDesiredSize.height) {        availSize.height = 0;      } else {        availSize.height -= aDesiredSize.height;      }    }    if (aDesiredSize.maxElementSize) {      maxElementSize.width  = aDesiredSize.maxElementSize->width;      maxElementSize.height = aDesiredSize.maxElementSize->height;    }  }  PRBool needAnotherLegendReflow = PR_FALSE;  // Try to reflow the area frame into the available space. It might not fit  nsHTMLReflowState contentReflowState(aPresContext, aReflowState,                                       mContentFrame, availSize);  // XXX remove when reflow state is fixed#ifndef bug4534_not_fixed  FieldSetHack(contentReflowState, "fieldset's area", PR_FALSE);#endif  nscoord contentTopOffset = (legendSize.height > border.top)                                 ? legendSize.height + padding.top                                : border.top + padding.top;  ReflowChild(mContentFrame, aPresContext, aDesiredSize, contentReflowState, aStatus);  nsSize contentSize(aDesiredSize.width, aDesiredSize.height);  // The content didn't fit  if ((NS_UNCONSTRAINEDSIZE != availSize.width) && (availSize.width < aDesiredSize.width)) {    needAnotherLegendReflow = PR_TRUE;    availSize.width = contentSize.width + borderPadding.left + borderPadding.right;  }  if ((NS_UNCONSTRAINEDSIZE != availSize.height) && (availSize.height < aDesiredSize.height)) {    needAnotherLegendReflow = PR_TRUE;  }  if (aDesiredSize.maxElementSize) {    aDesiredSize.maxElementSize->width = PR_MAX(aDesiredSize.maxElementSize->width,maxElementSize.width);    maxElementSize.height += aDesiredSize.maxElementSize->height;  }  // need to reflow the legend a 2nd time  if (needAnotherLegendReflow && mLegendFrame) {    nsHTMLReflowState legendReflowState(aPresContext, aReflowState,                                        mLegendFrame, availSize);    // XXX remove when reflow state is fixed#ifndef bug4534_not_fixed    FieldSetHack(legendReflowState, "fieldset's legend frame", PR_FALSE);#endif    ReflowChild(mLegendFrame, aPresContext, aDesiredSize, legendReflowState, aStatus);    legendSize.width  = aDesiredSize.width;    legendSize.height = aDesiredSize.height;  }  nscoord legendWidth = legendSize.width + border.left + border.right;  nscoord contentWidth = contentSize.width + borderPadding.left + borderPadding.right;  PRBool legendOffsetFits = PR_TRUE; // can the legend be offset by the 10 pixels  aDesiredSize.width = (legendWidth > contentWidth) ? legendWidth : contentWidth;  // if we are constrained and the child is smaller, use the constrained values  if ((NS_INTRINSICSIZE != aReflowState.computedWidth) &&      (aReflowState.computedWidth > aDesiredSize.width)) {    aDesiredSize.width = aReflowState.computedWidth;  }    if ((NS_UNCONSTRAINEDSIZE != aReflowState.availableWidth) &&       ( (legendWidth + (2 * desiredLegendOffset)) > aDesiredSize.width) ) {    legendOffsetFits = PR_FALSE;  }  // Place the legend   nsRect legendRect(0, 0, 0, 0);  contentTopOffset = border.top + padding.top;  if (mLegendFrame) {    nscoord legendTopOffset;    if (legendSize.height > border.top) {      legendTopOffset = 0;    } else {      legendTopOffset = (border.top - legendSize.height) / 2;    }    contentTopOffset = legendTopOffset + legendSize.height + padding.top;    nscoord legendLeftOffset = 0;    PRInt32 align = ((nsLegendFrame*)mLegendFrame)->GetAlign();    // if there isn't room for the 10 pixel left/right offset, align center    if (!legendOffsetFits) {      align = NS_STYLE_TEXT_ALIGN_CENTER;    }    switch(align) {    case NS_STYLE_TEXT_ALIGN_LEFT:      legendLeftOffset = border.left + desiredLegendOffset;      break;    case NS_STYLE_TEXT_ALIGN_RIGHT:      legendLeftOffset = aDesiredSize.width - border.right - desiredLegendOffset - legendSize.width;      break;    // XXX The spec specifies center and top but we are following IE4's lead and making left    // be the upper left and right be the upper right. IE4 introduced center which is not part     // of the spec.    case NS_STYLE_TEXT_ALIGN_CENTER:    case NS_STYLE_VERTICAL_ALIGN_BOTTOM:    case NS_STYLE_VERTICAL_ALIGN_TOP:    default:      legendLeftOffset = (aDesiredSize.width - legendSize.width) / 2;      break;    }    // reduce by legend margins    legendRect = nsRect(legendLeftOffset + legendMargin.left, legendTopOffset + legendMargin.top,                         legendSize.width, legendSize.height);    mLegendFrame->SetRect(legendRect);    // cache values so the border will be painted around the vertical center of the legend    mTopBorderOffset = legendSize.height / 2;    mTopBorderGap = nsRect(legendLeftOffset, legendTopOffset, legendSize.width + legendMargin.left +                           legendMargin.right, legendSize.height + legendMargin.top +                           legendMargin.bottom);  }  // Place the content area frame  nsRect contentRect(borderPadding.left, contentTopOffset, contentSize.width, contentSize.height);  mContentFrame->SetRect(contentRect);      // Return our size and our result  aDesiredSize.height  = contentTopOffset + legendSize.height + contentSize.height + borderPadding.bottom;  // if we are constrained and the child is smaller, use the constrained values  if ((NS_AUTOHEIGHT != aReflowState.computedHeight) && (aReflowState.computedHeight > aDesiredSize.height)) {    aDesiredSize.height = aReflowState.computedHeight;  }  aDesiredSize.ascent  = aDesiredSize.height;  aDesiredSize.descent = 0;  if (nsnull != aDesiredSize.maxElementSize) {    aDesiredSize.maxElementSize->width  = maxElementSize.width;    aDesiredSize.maxElementSize->height = maxElementSize.height;    aDesiredSize.AddBorderPaddingToMaxElementSize(borderPadding);  }  aStatus = NS_FRAME_COMPLETE;  return NS_OK;}