Back to index

lightning-sunbird  0.9+nobinonly
nsMathMLmtableFrame.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 MathML Project.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * The University Of Queensland.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Roger B. Sidje <rbs@maths.uq.edu.au>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nsFrame.h"
00041 #include "nsAreaFrame.h"
00042 #include "nsPresContext.h"
00043 #include "nsUnitConversion.h"
00044 #include "nsStyleContext.h"
00045 #include "nsStyleConsts.h"
00046 #include "nsINameSpaceManager.h"
00047 #include "nsIRenderingContext.h"
00048 #include "nsIFontMetrics.h"
00049 
00050 #include "nsVoidArray.h"
00051 #include "nsFrameManager.h"
00052 #include "nsStyleChangeList.h"
00053 #include "nsTableOuterFrame.h"
00054 #include "nsTableFrame.h"
00055 #include "nsTableCellFrame.h"
00056 #include "celldata.h"
00057 
00058 #include "nsMathMLmtableFrame.h"
00059 
00060 //
00061 // <mtable> -- table or matrix - implementation
00062 //
00063 
00064 // helper function to perform an in-place split of a space-delimited string,
00065 // and return an array of pointers for the beginning of each segment, i.e.,
00066 // aOffset[0] is the first string, aOffset[1] is the second string, etc.
00067 // Used to parse attributes like columnalign='left right', rowalign='top bottom'
00068 static void
00069 SplitString(nsString&    aString, // [IN/OUT]
00070             nsVoidArray& aOffset) // [OUT]
00071 {
00072   static const PRUnichar kNullCh = PRUnichar('\0');
00073 
00074   aString.Append(kNullCh);  // put an extra null at the end
00075 
00076   PRUnichar* start = aString.BeginWriting();
00077   PRUnichar* end   = start;
00078 
00079   while (kNullCh != *start) {
00080     while ((kNullCh != *start) && nsCRT::IsAsciiSpace(*start)) {  // skip leading space
00081       start++;
00082     }
00083     end = start;
00084 
00085     while ((kNullCh != *end) && (PR_FALSE == nsCRT::IsAsciiSpace(*end))) { // look for space or end
00086       end++;
00087     }
00088     *end = kNullCh; // end string here
00089 
00090     if (start < end) {
00091       aOffset.AppendElement(start); // record the beginning of this segment
00092     }
00093 
00094     start = ++end;
00095   }
00096 }
00097 
00098 struct nsValueList
00099 {
00100   nsString    mData;
00101   nsVoidArray mArray;
00102 
00103   nsValueList(nsString& aData) {
00104     mData.Assign(aData);
00105     SplitString(mData, mArray);
00106   }
00107 };
00108 
00109 // Each rowalign='top bottom' or columnalign='left right center' (from
00110 // <mtable> or <mtr>) is split once (lazily) into a nsValueList which is
00111 // stored in the frame manager. Cell frames query the frame manager
00112 // to see what values apply to them.
00113 
00114 // XXX See bug 69409 - MathML attributes are not mapped to style.
00115 // This code is not suitable for dynamic updates, for example when the
00116 // rowalign and columalign attributes are changed with JavaScript.
00117 // The code doesn't include hooks for AttributeChanged() notifications.
00118 
00119 static void
00120 DestroyValueListFunc(void*           aFrame,
00121                      nsIAtom*        aPropertyName,
00122                      void*           aPropertyValue,
00123                      void*           aDtorData)
00124 {
00125   delete NS_STATIC_CAST(nsValueList*, aPropertyValue);
00126 }
00127 
00128 static PRUnichar*
00129 GetValueAt(nsPresContext*  aPresContext,
00130            nsIFrame*       aTableOrRowFrame,
00131            nsIAtom*        aAttributeAtom,
00132            PRInt32         aRowOrColIndex)
00133 {
00134   PRUnichar* result = nsnull;
00135   nsPropertyTable *propTable = aPresContext->PropertyTable();
00136   nsValueList* valueList;
00137 
00138   valueList = NS_STATIC_CAST(nsValueList*,
00139                              propTable->GetProperty(aTableOrRowFrame,
00140                                                     aAttributeAtom));
00141 
00142   if (!valueList) {
00143     // The property isn't there yet, so set it
00144     nsAutoString values;
00145     if (NS_CONTENT_ATTR_HAS_VALUE ==
00146         aTableOrRowFrame->GetContent()->GetAttr(kNameSpaceID_None, aAttributeAtom, values)) {
00147       valueList = new nsValueList(values);
00148       if (valueList) {
00149         propTable->SetProperty(aTableOrRowFrame, aAttributeAtom,
00150                                valueList, DestroyValueListFunc, nsnull);
00151       }
00152     }
00153   }
00154 
00155   if (valueList) {
00156     PRInt32 count = valueList->mArray.Count();
00157     result = (aRowOrColIndex < count)
00158            ? (PRUnichar*)(valueList->mArray[aRowOrColIndex])
00159            : (PRUnichar*)(valueList->mArray[count-1]);
00160   }
00161   return result;
00162 }
00163 
00164 #ifdef NS_DEBUG
00165 #define DEBUG_VERIFY_THAT_FRAME_IS(_frame, _expected) \
00166   NS_ASSERTION(NS_STYLE_DISPLAY_##_expected == _frame->GetStyleDisplay()->mDisplay, "internal error");
00167 #else
00168 #define DEBUG_VERIFY_THAT_FRAME_IS(_frame, _expected)
00169 #endif
00170 
00171 static void
00172 MapAttributesInto(nsPresContext* aPresContext,
00173                   nsIContent*     aCellContent,
00174                   nsIFrame*       aCellFrame,
00175                   nsIFrame*       aCellInnerFrame)
00176 {
00177   nsTableCellFrame* cellFrame = NS_STATIC_CAST(nsTableCellFrame*, aCellFrame);
00178   nsTableCellFrame* sibling;
00179 
00180   PRInt32 rowIndex, colIndex;
00181   nsresult rv = cellFrame->GetCellIndexes(rowIndex, colIndex);
00182   NS_ASSERTION(NS_SUCCEEDED(rv), "cannot find the position of the cell frame");
00183   if (NS_FAILED(rv)) return;
00184 
00185   nsIFrame* rowFrame = cellFrame->GetParent();
00186   nsIFrame* rowgroupFrame = rowFrame->GetParent();
00187   nsIFrame* tableFrame = rowgroupFrame->GetParent();
00188   DEBUG_VERIFY_THAT_FRAME_IS(rowFrame, TABLE_ROW);
00189   DEBUG_VERIFY_THAT_FRAME_IS(rowgroupFrame, TABLE_ROW_GROUP);
00190   DEBUG_VERIFY_THAT_FRAME_IS(tableFrame, TABLE);
00191 #ifdef NS_DEBUG
00192   PRBool originates;
00193   ((nsTableFrame*)tableFrame)->GetCellInfoAt(rowIndex, colIndex, &originates);
00194   NS_ASSERTION(originates, "internal error");
00195 #endif
00196 
00197   nsIAtom* atom;
00198   PRUnichar* attr;
00199   nsAutoString value;
00200   PRBool hasChanged = PR_FALSE;
00201   NS_NAMED_LITERAL_STRING(trueStr, "true");
00202 
00204   // process attributes that depend on the index of the row:
00205   // rowalign, rowlines
00206 
00207   // see if the rowalign attribute is not already set
00208   atom = nsMathMLAtoms::rowalign_;
00209   rv = aCellContent->GetAttr(kNameSpaceID_None, atom, value);
00210   if (NS_CONTENT_ATTR_NOT_THERE == rv) {
00211     // see if the rowalign attribute was specified on the row
00212     attr = GetValueAt(aPresContext, rowFrame, atom, rowIndex);
00213     if (!attr) {
00214        // see if the rowalign attribute was specified on the table
00215       attr = GetValueAt(aPresContext, tableFrame, atom, rowIndex);
00216     }
00217     // set the attribute without notifying that we want a reflow
00218     if (attr) {
00219       hasChanged = PR_TRUE;
00220       aCellContent->SetAttr(kNameSpaceID_None, atom, nsDependentString(attr), PR_FALSE);
00221     }
00222   }
00223   // if we are not on the first row, see if |rowlines| was specified on the table.
00224   // Note that we pass 'rowIndex-1' because the CSS rule in mathml.css is associated
00225   // to 'border-top', and it is as if we draw the line on behalf of the previous cell.
00226   // This way of doing so allows us to handle selective lines, [row]\hline[row][row]',
00227   // and cases of spanning cells without further complications.
00228   if (rowIndex > 0) {
00229     attr = GetValueAt(aPresContext, tableFrame, nsMathMLAtoms::rowlines_, rowIndex-1);
00230     // set the special -moz-math-rowline without notifying that we want a reflow
00231     if (attr) {
00232       hasChanged = PR_TRUE;
00233       aCellContent->SetAttr(kNameSpaceID_None, nsMathMLAtoms::MOZrowline, nsDependentString(attr), PR_FALSE);
00234     }
00235   }
00236   else {
00237     // set the special -moz-math-firstrow to annotate that we are on the first row
00238     hasChanged = PR_TRUE;
00239     aCellContent->SetAttr(kNameSpaceID_None, nsMathMLAtoms::MOZfirstrow, trueStr, PR_FALSE);
00240   }
00241   // if we are on the last row, set the special -moz-math-lastrow
00242   PRInt32 rowSpan = ((nsTableFrame*)tableFrame)->GetEffectiveRowSpan(*cellFrame);
00243   sibling = ((nsTableFrame*)tableFrame)->GetCellFrameAt(rowIndex+rowSpan, colIndex);
00244   if (!sibling) {
00245     hasChanged = PR_TRUE;
00246     aCellContent->SetAttr(kNameSpaceID_None, nsMathMLAtoms::MOZlastrow, trueStr, PR_FALSE);
00247   }
00248 
00250   // process attributes that depend on the index of the column:
00251   // columnalign, columnlines, XXX need columnwidth too
00252 
00253   // see if the columnalign attribute is not already set
00254   atom = nsMathMLAtoms::columnalign_;
00255   rv = aCellContent->GetAttr(kNameSpaceID_None, atom, value);
00256   if (NS_CONTENT_ATTR_NOT_THERE == rv) {
00257     // see if the columnalign attribute was specified on the row
00258     attr = GetValueAt(aPresContext, rowFrame, atom, colIndex);
00259     if (!attr) {
00260        // see if the columnalign attribute was specified on the table
00261       attr = GetValueAt(aPresContext, tableFrame, atom, colIndex);
00262     }
00263     if (attr) {
00264       hasChanged = PR_TRUE;
00265       aCellContent->SetAttr(kNameSpaceID_None, atom, nsDependentString(attr), PR_FALSE);
00266     }
00267   }
00268   // if we are not on the first column, see if |columnlines| was specified on
00269   // the table. Note that we pass 'colIndex-1' because the CSS rule in mathml.css
00270   // is associated to 'border-left', and it is as if we draw the line on behalf
00271   // of the previous cell. This way of doing so allows us to handle selective lines,
00272   // e.g., 'r|cl', and cases of spanning cells without further complications.
00273   if (colIndex > 0) {
00274     attr = GetValueAt(aPresContext, tableFrame, nsMathMLAtoms::columnlines_, colIndex-1);
00275     // set the special -moz-math-columnline without notifying that we want a reflow
00276     if (attr) {
00277       hasChanged = PR_TRUE;
00278       aCellContent->SetAttr(kNameSpaceID_None, nsMathMLAtoms::MOZcolumnline, nsDependentString(attr), PR_FALSE);
00279     }
00280   }
00281   else {
00282     // set the special -moz-math-firstcolumn to annotate that we are on the first column
00283     hasChanged = PR_TRUE;
00284     aCellContent->SetAttr(kNameSpaceID_None, nsMathMLAtoms::MOZfirstcolumn, trueStr, PR_FALSE);
00285   }
00286   // if we are on the last column, set the special -moz-math-lastcolumn
00287   PRInt32 colSpan = ((nsTableFrame*)tableFrame)->GetEffectiveColSpan(*cellFrame);
00288   sibling = ((nsTableFrame*)tableFrame)->GetCellFrameAt(rowIndex, colIndex+colSpan);
00289   if (!sibling) {
00290     hasChanged = PR_TRUE;
00291     aCellContent->SetAttr(kNameSpaceID_None, nsMathMLAtoms::MOZlastcolumn, trueStr, PR_FALSE);
00292   }
00293 
00294   // now, re-resolve the style contexts in our subtree to pick up any changes
00295   if (hasChanged) {
00296     nsFrameManager *fm = aPresContext->FrameManager();
00297     nsStyleChangeList changeList;
00298     nsChangeHint maxChange = fm->ComputeStyleChangeFor(aCellFrame, &changeList,
00299                                                        NS_STYLE_HINT_NONE);
00300 #ifdef DEBUG
00301     // Use the parent frame to make sure we catch in-flows and such
00302     nsIFrame* parentFrame = aCellFrame->GetParent();
00303     fm->DebugVerifyStyleTree(parentFrame ? parentFrame : aCellFrame);
00304 #endif
00305   }
00306 }
00307 
00308 // the align attribute of mtable can have a row number which indicates
00309 // from where to anchor the table, e.g., top5 means anchor the table at
00310 // the top of the 5th row, axis-1 means anchor the table on the axis of
00311 // the last row (could have been nicer if the REC used the '#' separator,
00312 // e.g., top#5, or axis#-1)
00313 
00314 enum eAlign {
00315   eAlign_top,
00316   eAlign_bottom,
00317   eAlign_center,
00318   eAlign_baseline,
00319   eAlign_axis
00320 };
00321 
00322 static void
00323 ParseAlignAttribute(nsString& aValue, eAlign& aAlign, PRInt32& aRowIndex)
00324 {
00325   // by default, the table is centered about the axis
00326   aRowIndex = 0;
00327   aAlign = eAlign_axis;
00328   PRInt32 len = 0;
00329   if (0 == aValue.Find("top")) {
00330     len = 3; // 3 is the length of 'top'
00331     aAlign = eAlign_top;
00332   }
00333   else if (0 == aValue.Find("bottom")) {
00334     len = 6; // 6 is the length of 'bottom'
00335     aAlign = eAlign_bottom;
00336   }
00337   else if (0 == aValue.Find("center")) {
00338     len = 6; // 6 is the length of 'center'
00339     aAlign = eAlign_center;
00340   }
00341   else if (0 == aValue.Find("baseline")) {
00342     len = 8; // 8 is the length of 'baseline'
00343     aAlign = eAlign_baseline;
00344   }
00345   else if (0 == aValue.Find("axis")) {
00346     len = 4; // 4 is the length of 'axis'
00347     aAlign = eAlign_axis;
00348   }
00349   if (len) {
00350     PRInt32 error;
00351     aValue.Cut(0, len); // aValue is not a const here
00352     aRowIndex = aValue.ToInteger(&error);
00353     if (error)
00354       aRowIndex = 0;
00355   }
00356 }
00357 
00358 // --------
00359 // implementation of nsMathMLmtableOuterFrame
00360 
00361 NS_IMPL_ADDREF_INHERITED(nsMathMLmtableOuterFrame, nsMathMLFrame)
00362 NS_IMPL_RELEASE_INHERITED(nsMathMLmtableOuterFrame, nsMathMLFrame)
00363 NS_IMPL_QUERY_INTERFACE_INHERITED1(nsMathMLmtableOuterFrame, nsTableOuterFrame, nsMathMLFrame)
00364 
00365 nsresult
00366 NS_NewMathMLmtableOuterFrame (nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00367 {
00368   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00369   if (nsnull == aNewFrame) {
00370     return NS_ERROR_NULL_POINTER;
00371   }
00372   nsMathMLmtableOuterFrame* it = new (aPresShell) nsMathMLmtableOuterFrame;
00373   if (!it)
00374     return NS_ERROR_OUT_OF_MEMORY;
00375 
00376   *aNewFrame = it;
00377   return NS_OK;
00378 }
00379 
00380 nsMathMLmtableOuterFrame::nsMathMLmtableOuterFrame()
00381   :nsTableOuterFrame()
00382 {
00383 }
00384 
00385 nsMathMLmtableOuterFrame::~nsMathMLmtableOuterFrame()
00386 {
00387 }
00388 
00389 NS_IMETHODIMP
00390 nsMathMLmtableOuterFrame::InheritAutomaticData(nsIFrame* aParent)
00391 {
00392   // XXX the REC says that by default, displaystyle=false in <mtable>
00393 
00394   // let the base class inherit the scriptlevel and displaystyle from our parent
00395   nsMathMLFrame::InheritAutomaticData(aParent);
00396 
00397   // see if the displaystyle attribute is there and let it override what we inherited
00398   nsAutoString value;
00399   if (NS_CONTENT_ATTR_HAS_VALUE ==
00400       GetAttribute(mContent, nsnull, nsMathMLAtoms::displaystyle_, value)) {
00401     if (value.EqualsLiteral("true")) {
00402       mPresentationData.flags |= NS_MATHML_DISPLAYSTYLE;
00403     }
00404     else if (value.EqualsLiteral("false")) {
00405       mPresentationData.flags &= ~NS_MATHML_DISPLAYSTYLE;
00406     }
00407   }
00408 
00409   return NS_OK;
00410 }
00411 
00412 NS_IMETHODIMP
00413 nsMathMLmtableOuterFrame::Init(nsPresContext*  aPresContext,
00414                                nsIContent*      aContent,
00415                                nsIFrame*        aParent,
00416                                nsStyleContext*  aContext,
00417                                nsIFrame*        aPrevInFlow)
00418 {
00419   MapAttributesIntoCSS(aPresContext, aContent);
00420 
00421   return nsTableOuterFrame::Init(aPresContext, aContent, aParent, aContext, aPrevInFlow);
00422 }
00423 
00424 nsIFrame*
00425 nsMathMLmtableOuterFrame::GetRowFrameAt(nsPresContext* aPresContext,
00426                                         PRInt32         aRowIndex)
00427 {
00428   // To find the row at the given index, we will iterate downwards or
00429   // upwards depending on the sign of the index
00430   nsTableIteration dir = eTableLTR;
00431   if (aRowIndex < 0) {
00432     aRowIndex = -aRowIndex;
00433     dir = eTableRTL;
00434   }
00435   // if our inner table says that the index is valid, find the row now
00436   PRInt32 rowCount, colCount;
00437   GetTableSize(rowCount, colCount);
00438   if (aRowIndex <= rowCount) {
00439     nsIFrame* innerTableFrame = mFrames.FirstChild();
00440     nsTableIterator rowgroupIter(*innerTableFrame, dir);
00441     nsIFrame* rowgroupFrame = rowgroupIter.First();
00442     while (rowgroupFrame) {
00443       nsTableIterator rowIter(*rowgroupFrame, dir);
00444       nsIFrame* rowFrame = rowIter.First();
00445       while (rowFrame) {
00446         if (--aRowIndex == 0) {
00447           DEBUG_VERIFY_THAT_FRAME_IS(rowFrame, TABLE_ROW);
00448           return rowFrame;
00449         }
00450         rowFrame = rowIter.Next();
00451       }
00452       rowgroupFrame = rowgroupIter.Next();
00453     }
00454   }
00455   return nsnull;
00456 }
00457 
00458 NS_IMETHODIMP
00459 nsMathMLmtableOuterFrame::Reflow(nsPresContext*          aPresContext,
00460                                  nsHTMLReflowMetrics&     aDesiredSize,
00461                                  const nsHTMLReflowState& aReflowState,
00462                                  nsReflowStatus&          aStatus)
00463 {
00464   nsresult rv;
00465   nsAutoString value;
00466   // we want to return a table that is anchored according to the align attribute
00467 
00468   nsHTMLReflowState reflowState(aReflowState);
00469   if ((NS_FRAME_FIRST_REFLOW & mState) &&
00470       (NS_UNCONSTRAINEDSIZE == reflowState.availableWidth)) {
00471     // We are going to reflow twice because the table frame code is
00472     // skipping the Pass 2 reflow when, at the Pass 1 reflow, the available
00473     // size is unconstrained. Skipping the Pass2 messes the MathML vertical
00474     // alignments that are resolved during the reflow of cell frames.
00475 
00476     nscoord oldComputedWidth = reflowState.mComputedWidth;
00477     reflowState.mComputedWidth = NS_UNCONSTRAINEDSIZE;
00478     reflowState.reason = eReflowReason_Initial;
00479 
00480     rv = nsTableOuterFrame::Reflow(aPresContext, aDesiredSize, reflowState, aStatus);
00481 
00482     // The second reflow will just be a reflow with a constrained width
00483     reflowState.availableWidth = aDesiredSize.width;
00484     reflowState.mComputedWidth = oldComputedWidth;
00485     reflowState.reason = eReflowReason_StyleChange;
00486 
00487     mState &= ~NS_FRAME_FIRST_REFLOW;
00488   }
00489   else if (mRect.width) {
00490     reflowState.availableWidth = mRect.width;
00491   }
00492 
00493   rv = nsTableOuterFrame::Reflow(aPresContext, aDesiredSize, reflowState, aStatus);
00494   NS_ASSERTION(aDesiredSize.height >= 0, "illegal height for mtable");
00495   NS_ASSERTION(aDesiredSize.width >= 0, "illegal width for mtable");
00496 
00497   // see if the user has set the align attribute on the <mtable>
00498   // XXX should we also check <mstyle> ?
00499   PRInt32 rowIndex = 0;
00500   eAlign tableAlign = eAlign_axis;
00501   if (NS_CONTENT_ATTR_HAS_VALUE ==
00502       GetAttribute(mContent, nsnull, nsMathMLAtoms::align_, value)) {
00503     ParseAlignAttribute(value, tableAlign, rowIndex);
00504   }
00505 
00506   // adjustments if there is a specified row from where to anchor the table
00507   // (conceptually: when there is no row of reference, picture the table as if
00508   // it is wrapped in a single big fictional row at dy = 0, this way of
00509   // doing so allows us to have a single code path for all cases).
00510   nscoord dy = 0;
00511   nscoord height = aDesiredSize.height;
00512   nsIFrame* rowFrame = nsnull;
00513   if (rowIndex) {
00514     rowFrame = GetRowFrameAt(aPresContext, rowIndex);
00515     if (rowFrame) {
00516       // translate the coordinates to be relative to us
00517       nsIFrame* frame = rowFrame;
00518       height = frame->GetSize().height;
00519       do {
00520         dy += frame->GetPosition().y;
00521         frame = frame->GetParent();
00522       } while (frame != this);
00523     }
00524   }
00525   switch (tableAlign) {
00526     case eAlign_top:
00527       aDesiredSize.ascent = dy;
00528       break;
00529     case eAlign_bottom:
00530       aDesiredSize.ascent = dy + height;
00531       break;
00532     case eAlign_center:
00533       aDesiredSize.ascent = dy + height/2;
00534       break;
00535     case eAlign_baseline:
00536       if (rowFrame) {
00537         // anchor the table on the baseline of the row of reference
00538         nscoord rowAscent = ((nsTableRowFrame*)rowFrame)->GetMaxCellAscent();
00539         if (rowAscent) { // the row has at least one cell with 'vertical-align: baseline'
00540           aDesiredSize.ascent = dy + rowAscent;
00541           break;
00542         }
00543       }
00544       // in other situations, fallback to center
00545       aDesiredSize.ascent = dy + height/2;
00546       break;
00547     case eAlign_axis:
00548     default: {
00549       // XXX should instead use style data from the row of reference here ?
00550       aReflowState.rendContext->SetFont(GetStyleFont()->mFont, nsnull);
00551       nsCOMPtr<nsIFontMetrics> fm;
00552       aReflowState.rendContext->GetFontMetrics(*getter_AddRefs(fm));
00553       nscoord axisHeight;
00554       GetAxisHeight(*aReflowState.rendContext, fm, axisHeight);
00555       if (rowFrame) {
00556         // anchor the table on the axis of the row of reference
00557         // XXX fallback to baseline because it is a hard problem
00558         // XXX need to fetch the axis of the row; would need rowalign=axis to work better
00559         nscoord rowAscent = ((nsTableRowFrame*)rowFrame)->GetMaxCellAscent();
00560         if (rowAscent) { // the row has at least one cell with 'vertical-align: baseline'
00561           aDesiredSize.ascent = dy + rowAscent;
00562           break;
00563         }
00564       }
00565       // in other situations, fallback to using half of the height
00566       aDesiredSize.ascent = dy + height/2 + axisHeight;
00567     }
00568   }
00569   aDesiredSize.descent = aDesiredSize.height - aDesiredSize.ascent;
00570 
00571   mReference.x = 0;
00572   mReference.y = aDesiredSize.ascent;
00573 
00574   // just make-up a bounding metrics
00575   mBoundingMetrics.Clear();
00576   mBoundingMetrics.ascent = aDesiredSize.ascent;
00577   mBoundingMetrics.descent = aDesiredSize.descent;
00578   mBoundingMetrics.width = aDesiredSize.width;
00579   mBoundingMetrics.leftBearing = 0;
00580   mBoundingMetrics.rightBearing = aDesiredSize.width;
00581 
00582   aDesiredSize.mBoundingMetrics = mBoundingMetrics;
00583   NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
00584 
00585   return rv;
00586 }
00587 
00588 PRBool
00589 nsMathMLmtableOuterFrame::IsFrameOfType(PRUint32 aFlags) const
00590 {
00591   return !(aFlags & ~nsIFrame::eMathML);
00592 }
00593 
00594 // --------
00595 // implementation of nsMathMLmtdFrame
00596 
00597 NS_IMPL_ADDREF_INHERITED(nsMathMLmtdFrame, nsTableCellFrame)
00598 NS_IMPL_RELEASE_INHERITED(nsMathMLmtdFrame, nsTableCellFrame)
00599 NS_IMPL_QUERY_INTERFACE_INHERITED0(nsMathMLmtdFrame, nsTableCellFrame)
00600 
00601 nsresult
00602 NS_NewMathMLmtdFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00603 {
00604   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00605   if (nsnull == aNewFrame) {
00606     return NS_ERROR_NULL_POINTER;
00607   }
00608   nsMathMLmtdFrame* it = new (aPresShell) nsMathMLmtdFrame;
00609   if (nsnull == it) {
00610     return NS_ERROR_OUT_OF_MEMORY;
00611   }
00612   *aNewFrame = it;
00613   return NS_OK;
00614 }
00615 
00616 nsMathMLmtdFrame::nsMathMLmtdFrame()
00617 {
00618 }
00619 
00620 nsMathMLmtdFrame::~nsMathMLmtdFrame()
00621 {
00622 }
00623 
00624 PRInt32
00625 nsMathMLmtdFrame::GetRowSpan()
00626 {
00627   PRInt32 rowspan = 1;
00628   nsAutoString value;
00629   if (NS_CONTENT_ATTR_HAS_VALUE == mContent->GetAttr(kNameSpaceID_None,
00630                    nsMathMLAtoms::rowspan_, value)) {
00631     PRInt32 error;
00632     rowspan = value.ToInteger(&error);
00633     if (error || rowspan < 0)
00634       rowspan = 1;
00635     rowspan = PR_MIN(rowspan, MAX_ROWSPAN);
00636   }
00637   return rowspan;
00638 }
00639 
00640 PRInt32
00641 nsMathMLmtdFrame::GetColSpan()
00642 {
00643   PRInt32 colspan = 1;
00644   nsAutoString value;
00645   if (NS_CONTENT_ATTR_HAS_VALUE == mContent->GetAttr(kNameSpaceID_None,
00646                    nsMathMLAtoms::columnspan_, value)) {
00647     PRInt32 error;
00648     colspan = value.ToInteger(&error);
00649     if (error || colspan < 0 || colspan > MAX_COLSPAN)
00650       colspan = 1;
00651   }
00652   return colspan;
00653 }
00654 
00655 PRBool
00656 nsMathMLmtdFrame::IsFrameOfType(PRUint32 aFlags) const
00657 {
00658   return !(aFlags & ~nsIFrame::eMathML);
00659 }
00660 
00661 // --------
00662 // implementation of nsMathMLmtdInnerFrame
00663 
00664 NS_IMPL_ADDREF_INHERITED(nsMathMLmtdInnerFrame, nsMathMLFrame)
00665 NS_IMPL_RELEASE_INHERITED(nsMathMLmtdInnerFrame, nsMathMLFrame)
00666 NS_IMPL_QUERY_INTERFACE_INHERITED1(nsMathMLmtdInnerFrame, nsBlockFrame, nsMathMLFrame)
00667 
00668 nsresult
00669 NS_NewMathMLmtdInnerFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00670 {
00671   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00672   if (nsnull == aNewFrame) {
00673     return NS_ERROR_NULL_POINTER;
00674   }
00675   nsMathMLmtdInnerFrame* it = new (aPresShell) nsMathMLmtdInnerFrame;
00676   if (nsnull == it) {
00677     return NS_ERROR_OUT_OF_MEMORY;
00678   }
00679   *aNewFrame = it;
00680   return NS_OK;
00681 }
00682 
00683 nsMathMLmtdInnerFrame::nsMathMLmtdInnerFrame()
00684 {
00685   // Set the right bits -- see what NS_NewTableCellInnerFrame does
00686   AddStateBits(NS_BLOCK_SPACE_MGR | NS_BLOCK_MARGIN_ROOT);
00687 }
00688 
00689 nsMathMLmtdInnerFrame::~nsMathMLmtdInnerFrame()
00690 {
00691 }
00692 
00693 NS_IMETHODIMP
00694 nsMathMLmtdInnerFrame::Init(nsPresContext*  aPresContext,
00695                             nsIContent*      aContent,
00696                             nsIFrame*        aParent,
00697                             nsStyleContext*  aContext,
00698                             nsIFrame*        aPrevInFlow)
00699 {
00700   nsresult rv = nsBlockFrame::Init(aPresContext, aContent, aParent, aContext, aPrevInFlow);
00701 
00702   // record that children that are ignorable whitespace should be excluded
00703   mState |= NS_FRAME_EXCLUDE_IGNORABLE_WHITESPACE;
00704 
00705   return rv;
00706 }
00707 
00708 NS_IMETHODIMP
00709 nsMathMLmtdInnerFrame::Reflow(nsPresContext*          aPresContext,
00710                               nsHTMLReflowMetrics&     aDesiredSize,
00711                               const nsHTMLReflowState& aReflowState,
00712                               nsReflowStatus&          aStatus)
00713 {
00714   // Map attributes to style (hopefully, bug 69409 will eventually help here).
00715   if (NS_FRAME_FIRST_REFLOW & mState) {
00716     MapAttributesInto(aPresContext, mContent, mParent, this);
00717   }
00718 
00719   // Let the base class do the reflow
00720   nsresult rv = nsBlockFrame::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
00721 
00722   // more about <maligngroup/> and <malignmark/> later
00723   // ...
00724   return rv;
00725 }
00726 
00727 PRBool
00728 nsMathMLmtdInnerFrame::IsFrameOfType(PRUint32 aFlags) const
00729 {
00730   return !(aFlags & ~nsIFrame::eMathML);
00731 }
00732