Back to index

lightning-sunbird  0.9+nobinonly
nsCellMap.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 #include "nsLayoutAtoms.h"
00039 #include "nsVoidArray.h"
00040 #include "nsCellMap.h"
00041 #include "nsTableFrame.h"
00042 #include "nsTableCellFrame.h"
00043 #include "nsTableRowGroupFrame.h"
00044 
00045 
00046 // colspan=0 gets a minimum number of cols initially to make computations easier
00047 #define MIN_NUM_COLS_FOR_ZERO_COLSPAN 2
00048 
00049 // CellData 
00050 
00051 MOZ_DECL_CTOR_COUNTER(CellData)
00052 
00053 CellData::CellData(nsTableCellFrame* aOrigCell)
00054 {
00055   MOZ_COUNT_CTOR(CellData);
00056   mOrigCell = aOrigCell;
00057 }
00058 
00059 CellData::~CellData()
00060 {
00061   MOZ_COUNT_DTOR(CellData);
00062 }
00063 
00064 BCCellData::BCCellData(nsTableCellFrame* aOrigCell)
00065 :CellData(aOrigCell)
00066 {
00067   MOZ_COUNT_CTOR(BCCellData);
00068 }
00069 
00070 BCCellData::~BCCellData()
00071 {
00072   MOZ_COUNT_DTOR(BCCellData);
00073 }
00074 
00075 MOZ_DECL_CTOR_COUNTER(nsCellMap)
00076 
00077 // nsTableCellMap
00078 
00079 nsTableCellMap::nsTableCellMap(nsTableFrame&   aTableFrame,
00080                                PRBool          aBorderCollapse)
00081 :mTableFrame(aTableFrame), mFirstMap(nsnull), mBCInfo(nsnull)
00082 {
00083   MOZ_COUNT_CTOR(nsTableCellMap);
00084 
00085   nsAutoVoidArray orderedRowGroups;
00086   PRUint32 numRowGroups;
00087   aTableFrame.OrderRowGroups(orderedRowGroups, numRowGroups);
00088 
00089   for (PRUint32 rgX = 0; rgX < numRowGroups; rgX++) {
00090     nsTableRowGroupFrame* rgFrame =
00091       nsTableFrame::GetRowGroupFrame((nsIFrame*)orderedRowGroups.ElementAt(rgX));
00092     if (rgFrame) {
00093       nsTableRowGroupFrame* prior = (0 == rgX)
00094         ? nsnull : nsTableFrame::GetRowGroupFrame((nsIFrame*)orderedRowGroups.ElementAt(rgX - 1));
00095       InsertGroupCellMap(*rgFrame, prior);
00096     }
00097   }
00098   if (aBorderCollapse) {
00099     mBCInfo = new BCInfo();
00100   }
00101 }
00102 
00103 nsTableCellMap::~nsTableCellMap()
00104 {
00105   MOZ_COUNT_DTOR(nsTableCellMap);
00106 
00107   nsCellMap* cellMap = mFirstMap;
00108   while (cellMap) {
00109     nsCellMap* next = cellMap->GetNextSibling();
00110     delete cellMap;
00111     cellMap = next;
00112   }
00113 
00114   PRInt32 colCount = mCols.Count();
00115   for (PRInt32 colX = 0; colX < colCount; colX++) {
00116     nsColInfo* colInfo = (nsColInfo *)mCols.ElementAt(colX);
00117     if (colInfo) {
00118       delete colInfo;
00119     }
00120   }
00121   if (mBCInfo) {
00122     DeleteRightBottomBorders();
00123     delete mBCInfo;
00124   }
00125 }
00126 
00127 // Get the bcData holding the border segments of the right edge of the table
00128 BCData*
00129 nsTableCellMap::GetRightMostBorder(PRInt32 aRowIndex)
00130 {
00131   if (!mBCInfo) ABORT1(nsnull);
00132 
00133   PRInt32 numRows = mBCInfo->mRightBorders.Count();
00134   if (aRowIndex < numRows) {
00135     return (BCData*)mBCInfo->mRightBorders.ElementAt(aRowIndex);
00136   }
00137 
00138   BCData* bcData;
00139   PRInt32 rowX = numRows;
00140 
00141   do {
00142     bcData = new BCData();
00143     if (!bcData) ABORT1(nsnull);
00144     mBCInfo->mRightBorders.AppendElement(bcData);
00145   } while (++rowX <= aRowIndex);
00146 
00147   return bcData;
00148 }
00149 
00150 // Get the bcData holding the border segments of the bottom edge of the table
00151 BCData*
00152 nsTableCellMap::GetBottomMostBorder(PRInt32 aColIndex)
00153 {
00154   if (!mBCInfo) ABORT1(nsnull);
00155 
00156   PRInt32 numCols = mBCInfo->mBottomBorders.Count();
00157   if (aColIndex < numCols) {
00158     return (BCData*)mBCInfo->mBottomBorders.ElementAt(aColIndex);
00159   }
00160 
00161   BCData* bcData;
00162   PRInt32 colX = numCols;
00163 
00164   do {
00165     bcData = new BCData();
00166     if (!bcData) ABORT1(nsnull);
00167     mBCInfo->mBottomBorders.AppendElement(bcData);
00168   } while (++colX <= aColIndex);
00169 
00170   return bcData;
00171 }
00172 
00173 // delete the borders corresponding to the right and bottom edges of the table
00174 void 
00175 nsTableCellMap::DeleteRightBottomBorders()
00176 {
00177   if (mBCInfo) {
00178     PRInt32 numCols = mBCInfo->mBottomBorders.Count();
00179     if (numCols > 0) {
00180       for (PRInt32 colX = numCols - 1; colX >= 0; colX--) {
00181         BCData* bcData = (BCData*)mBCInfo->mBottomBorders.ElementAt(colX);
00182         if (bcData) {
00183           delete bcData;
00184         }
00185         mBCInfo->mBottomBorders.RemoveElementAt(colX);
00186       }
00187     }
00188     PRUint32 numRows = mBCInfo->mRightBorders.Count();
00189     if (numRows > 0) {
00190       for (PRInt32 rowX = numRows - 1; rowX >= 0; rowX--) {
00191         BCData* bcData = (BCData*)mBCInfo->mRightBorders.ElementAt(rowX);
00192         if (bcData) {
00193           delete bcData;
00194         }
00195         mBCInfo->mRightBorders.RemoveElementAt(rowX);
00196       }
00197     }
00198   }
00199 }
00200       
00201 void 
00202 nsTableCellMap::InsertGroupCellMap(nsCellMap* aPrevMap,
00203                                    nsCellMap& aNewMap)
00204 {
00205   nsCellMap* next;
00206   if (aPrevMap) {
00207     next = aPrevMap->GetNextSibling();
00208     aPrevMap->SetNextSibling(&aNewMap);
00209   }
00210   else {
00211     next = mFirstMap;
00212     mFirstMap = &aNewMap;
00213   }
00214   aNewMap.SetNextSibling(next);
00215 }
00216 
00217 void nsTableCellMap::InsertGroupCellMap(nsTableRowGroupFrame&  aNewGroup,
00218                                         nsTableRowGroupFrame*& aPrevGroup)
00219 {
00220   nsCellMap* newMap = new nsCellMap(aNewGroup);
00221   if (newMap) {
00222     nsCellMap* prevMap = nsnull;
00223     nsCellMap* lastMap = mFirstMap;
00224     if (aPrevGroup) {
00225       nsCellMap* map = mFirstMap;
00226       while (map) {
00227         lastMap = map;
00228         if (map->GetRowGroup() == aPrevGroup) {
00229           prevMap = map;
00230           break;
00231         }
00232         map = map->GetNextSibling();
00233       }
00234     }
00235     if (!prevMap) {
00236       if (aPrevGroup) {
00237         prevMap = lastMap;
00238         aPrevGroup = (prevMap) ? prevMap->GetRowGroup() : nsnull;
00239       }
00240       else {
00241         aPrevGroup = nsnull;
00242       }
00243     }
00244     InsertGroupCellMap(prevMap, *newMap);
00245   }
00246 }
00247 
00248 void nsTableCellMap::RemoveGroupCellMap(nsTableRowGroupFrame* aGroup)
00249 {
00250   nsCellMap* map = mFirstMap;
00251   nsCellMap* prior = nsnull;
00252   while (map) {
00253     if (map->GetRowGroup() == aGroup) {
00254       nsCellMap* next = map->GetNextSibling();
00255       if (mFirstMap == map) {
00256         mFirstMap = next;
00257       }
00258       else {
00259         prior->SetNextSibling(next);
00260       }
00261       delete map;
00262       break;
00263     }
00264     prior = map;
00265     map = map->GetNextSibling();
00266   }
00267 }
00268 
00269 nsCellMap* 
00270 nsTableCellMap::GetMapFor(nsTableRowGroupFrame& aRowGroup)
00271 {
00272   NS_ASSERTION(!aRowGroup.GetPrevInFlow(), "GetMapFor called with continuation");
00273   for (nsCellMap* map = mFirstMap; map; map = map->GetNextSibling()) {
00274     if (&aRowGroup == map->GetRowGroup()) {
00275       return map;
00276     }
00277   }
00278   // if aRowGroup is a repeated header or footer find the header or footer it was repeated from
00279   if (aRowGroup.IsRepeatable()) {
00280     nsTableFrame* fifTable = NS_STATIC_CAST(nsTableFrame*, mTableFrame.GetFirstInFlow());
00281 
00282     nsAutoVoidArray rowGroups;
00283     PRUint32 numRowGroups;
00284     nsTableRowGroupFrame *thead, *tfoot;
00285     nsIFrame *ignore;
00286     // find the original header/footer 
00287     fifTable->OrderRowGroups(rowGroups, numRowGroups, &ignore, &thead, &tfoot);
00288 
00289     const nsStyleDisplay* display = aRowGroup.GetStyleDisplay();
00290     nsTableRowGroupFrame* rgOrig = 
00291       (NS_STYLE_DISPLAY_TABLE_HEADER_GROUP == display->mDisplay) ? thead : tfoot; 
00292     // find the row group cell map using the original header/footer
00293     if (rgOrig) {
00294       for (nsCellMap* map = mFirstMap; map; map = map->GetNextSibling()) {
00295         if (rgOrig == map->GetRowGroup()) {
00296           return map;
00297         }
00298       }
00299     }
00300   }
00301 
00302   return nsnull;
00303 }
00304 
00305 void
00306 nsTableCellMap::Synchronize(nsTableFrame* aTableFrame)
00307 {
00308   nsAutoVoidArray orderedRowGroups;
00309   nsAutoVoidArray maps;
00310   PRUint32 numRowGroups;
00311   PRInt32 mapIndex;
00312 
00313   maps.Clear();
00314   aTableFrame->OrderRowGroups(orderedRowGroups, numRowGroups);
00315   if (!numRowGroups) {
00316     return;
00317   }
00318 
00319   for (PRUint32 rgX = 0; rgX < numRowGroups; rgX++) {
00320     nsTableRowGroupFrame* rgFrame =
00321       nsTableFrame::GetRowGroupFrame((nsIFrame*)orderedRowGroups.ElementAt(rgX));
00322     if (rgFrame) {
00323       nsCellMap* map = GetMapFor(*(nsTableRowGroupFrame*)(rgFrame->GetFirstInFlow()));
00324       if (map) {
00325         if (!maps.AppendElement(map)) {
00326           delete map;
00327           NS_WARNING("Could not AppendElement");
00328         }
00329       }
00330     }
00331   }
00332   mapIndex = maps.Count() - 1;
00333   nsCellMap* nextMap = (nsCellMap*) maps.ElementAt(mapIndex);
00334   nextMap->SetNextSibling(nsnull);
00335   for (mapIndex-- ; mapIndex >= 0; mapIndex--) {
00336     nsCellMap* map = (nsCellMap*) maps.ElementAt(mapIndex);
00337     map->SetNextSibling(nextMap);
00338     nextMap = map;
00339   }
00340   mFirstMap = nextMap;
00341 }
00342 
00343 PRBool
00344 nsTableCellMap::HasMoreThanOneCell(PRInt32 aRowIndex)
00345 {
00346   PRInt32 rowIndex = aRowIndex;
00347   nsCellMap* map = mFirstMap;
00348   while (map) {
00349     if (map->GetRowCount() > rowIndex) {
00350       return map->HasMoreThanOneCell(*this, rowIndex);
00351     }
00352     rowIndex -= map->GetRowCount();
00353     map = map->GetNextSibling();
00354   }
00355   return PR_FALSE;
00356 }
00357 
00358 PRInt32 
00359 nsTableCellMap::GetEffectiveRowSpan(PRInt32 aRowIndex,
00360                                     PRInt32 aColIndex)
00361 {
00362   PRInt32 rowIndex = aRowIndex;
00363   nsCellMap* map = mFirstMap;
00364   while (map) {
00365     if (map->GetRowCount() > rowIndex) {
00366       PRBool zeroRowSpan;
00367       return map->GetRowSpan(*this, rowIndex, aColIndex, PR_TRUE, zeroRowSpan);
00368     }
00369     rowIndex -= map->GetRowCount();
00370     map = map->GetNextSibling();
00371   }
00372   return nsnull;
00373 }
00374 
00375 PRInt32 
00376 nsTableCellMap::GetEffectiveColSpan(PRInt32 aRowIndex,
00377                                     PRInt32 aColIndex)
00378 {
00379   PRInt32 rowIndex = aRowIndex;
00380   nsCellMap* map = mFirstMap;
00381   while (map) {
00382     if (map->GetRowCount() > rowIndex) {
00383       PRBool zeroColSpan;
00384       return map->GetEffectiveColSpan(*this, rowIndex, aColIndex, zeroColSpan);
00385     }
00386     rowIndex -= map->GetRowCount();
00387     map = map->GetNextSibling();
00388   }
00389   return nsnull;
00390 }
00391 
00392 nsTableCellFrame* 
00393 nsTableCellMap::GetCellFrame(PRInt32   aRowIndex,
00394                              PRInt32   aColIndex,
00395                              CellData& aData,
00396                              PRBool    aUseRowIfOverlap) const
00397 {
00398   PRInt32 rowIndex = aRowIndex;
00399   nsCellMap* map = mFirstMap;
00400   while (map) {
00401     if (map->GetRowCount() > rowIndex) {
00402       return map->GetCellFrame(rowIndex, aColIndex, aData, aUseRowIfOverlap);
00403     }
00404     rowIndex -= map->GetRowCount();
00405     map = map->GetNextSibling();
00406   }
00407   return nsnull;
00408 }
00409 
00410 nsColInfo* 
00411 nsTableCellMap::GetColInfoAt(PRInt32 aColIndex)
00412 {
00413   PRInt32 numColsToAdd = aColIndex + 1 - mCols.Count();
00414   if (numColsToAdd > 0) {
00415     AddColsAtEnd(numColsToAdd);  // XXX this could fail to add cols in theory
00416   }
00417   return (nsColInfo*)mCols.ElementAt(aColIndex);
00418 }
00419 
00420 PRInt32 
00421 nsTableCellMap::GetRowCount() const
00422 {
00423   PRInt32 numRows = 0;
00424   nsCellMap* map = mFirstMap;
00425   while (map) {
00426     numRows += map->GetRowCount();
00427     map = map->GetNextSibling();
00428   }
00429   return numRows;
00430 }
00431 
00432 CellData* 
00433 nsTableCellMap::GetDataAt(PRInt32 aRowIndex, 
00434                           PRInt32 aColIndex,
00435                           PRBool  aUpdateZeroSpan)
00436 {
00437   PRInt32 rowIndex = aRowIndex;
00438   nsCellMap* map = mFirstMap;
00439   while (map) {
00440     if (map->GetRowCount() > rowIndex) {
00441       return map->GetDataAt(*this, rowIndex, aColIndex, aUpdateZeroSpan);
00442     }
00443     rowIndex -= map->GetRowCount();
00444     map = map->GetNextSibling();
00445   }
00446   return nsnull;
00447 }
00448 
00449 void 
00450 nsTableCellMap::AddColsAtEnd(PRUint32 aNumCols)
00451 {
00452   PRBool added;
00453   // XXX We really should have a way to say "make this voidarray at least
00454   // N entries long" to avoid reallocating N times.  On the other hand, the
00455   // number of likely allocations here isn't TOO gigantic, and we may not
00456   // know about many of them at a time.
00457   for (PRUint32 numX = 1; numX <= aNumCols; numX++) {
00458     nsColInfo* colInfo = new nsColInfo();
00459     if (colInfo) {
00460       added = mCols.AppendElement(colInfo);
00461       if (!added) {
00462         delete colInfo;
00463         NS_WARNING("Could not AppendElement");
00464       }
00465     }
00466     if (mBCInfo) {
00467       BCData* bcData = new BCData();
00468       if (bcData) {
00469         added = mBCInfo->mBottomBorders.AppendElement(bcData);
00470         if (!added) {
00471           delete bcData;
00472           NS_WARNING("Could not AppendElement");
00473         }
00474       }
00475     }
00476   }
00477 }
00478 
00479 void 
00480 nsTableCellMap::RemoveColsAtEnd()
00481 {
00482   // Remove the cols at the end which don't have originating cells or cells spanning 
00483   // into them. Only do this if the col was created as eColAnonymousCell  
00484   PRInt32 numCols = GetColCount();
00485   PRInt32 lastGoodColIndex = mTableFrame.GetIndexOfLastRealCol();
00486   for (PRInt32 colX = numCols - 1; (colX >= 0) && (colX > lastGoodColIndex); colX--) {
00487     nsColInfo* colInfo = (nsColInfo*)mCols.ElementAt(colX);
00488     if (colInfo) {
00489       if ((colInfo->mNumCellsOrig <= 0) && (colInfo->mNumCellsSpan <= 0))  {
00490 
00491         delete colInfo;
00492         mCols.RemoveElementAt(colX);
00493 
00494         if (mBCInfo) { 
00495           PRInt32 count = mBCInfo->mBottomBorders.Count();
00496           if (colX < count) {
00497             BCData* bcData = (BCData*)mBCInfo->mBottomBorders.ElementAt(colX);
00498             if (bcData) {
00499               delete bcData;
00500             }
00501             mBCInfo->mBottomBorders.RemoveElementAt(colX);
00502           }
00503         }
00504       }
00505       else break; // only remove until we encounter the 1st valid one
00506     }
00507     else {
00508       NS_ASSERTION(0, "null entry in column info array");
00509       mCols.RemoveElementAt(colX);
00510     }
00511   }
00512 }
00513 
00514 void 
00515 nsTableCellMap::ClearCols()
00516 {
00517   PRInt32 numCols = GetColCount();
00518   for (PRInt32 colX = numCols - 1; (colX >= 0);colX--) {
00519     nsColInfo* colInfo = (nsColInfo*)mCols.ElementAt(colX);  
00520     delete colInfo;
00521     mCols.RemoveElementAt(colX);
00522     if (mBCInfo) { 
00523       PRInt32 count = mBCInfo->mBottomBorders.Count();
00524       if (colX < count) {
00525         BCData* bcData = (BCData*)mBCInfo->mBottomBorders.ElementAt(colX);
00526         if (bcData) {
00527               delete bcData;
00528         }
00529         mBCInfo->mBottomBorders.RemoveElementAt(colX);
00530       }
00531     }
00532   }
00533 }
00534 void
00535 nsTableCellMap::InsertRows(nsTableRowGroupFrame& aParent,
00536                            nsVoidArray&          aRows,
00537                            PRInt32               aFirstRowIndex,
00538                            PRBool                aConsiderSpans,
00539                            nsRect&               aDamageArea)
00540 {
00541   PRInt32 numNewRows = aRows.Count();
00542   if ((numNewRows <= 0) || (aFirstRowIndex < 0)) ABORT0(); 
00543 
00544   PRInt32 rowIndex = aFirstRowIndex;
00545   nsCellMap* cellMap = mFirstMap;
00546   while (cellMap) {
00547     nsTableRowGroupFrame* rg = cellMap->GetRowGroup();
00548     if (rg == &aParent) {
00549       cellMap->InsertRows(*this, aRows, rowIndex, aConsiderSpans, aDamageArea);
00550       aDamageArea.y = aFirstRowIndex;
00551       aDamageArea.height = PR_MAX(0, GetRowCount() - aFirstRowIndex);
00552 #ifdef DEBUG_TABLE_CELLMAP 
00553       Dump("after InsertRows");
00554 #endif
00555       if (mBCInfo) {
00556         BCData* bcData;
00557         PRInt32 count = mBCInfo->mRightBorders.Count();
00558         if (aFirstRowIndex < count) {
00559           for (PRInt32 rowX = aFirstRowIndex; rowX < aFirstRowIndex + numNewRows; rowX++) {
00560             bcData = new BCData(); if (!bcData) ABORT0();
00561             mBCInfo->mRightBorders.InsertElementAt(bcData, rowX);
00562           }
00563         }
00564         else {
00565           GetRightMostBorder(aFirstRowIndex); // this will create missing entries
00566           for (PRInt32 rowX = aFirstRowIndex + 1; rowX < aFirstRowIndex + numNewRows; rowX++) {
00567             bcData = new BCData(); if (!bcData) ABORT0();
00568             mBCInfo->mRightBorders.AppendElement(bcData);
00569           }
00570         }
00571       }
00572       return;
00573     }
00574     rowIndex -= cellMap->GetRowCount();
00575     cellMap = cellMap->GetNextSibling();
00576   }
00577 
00578   NS_ASSERTION(PR_FALSE, "Attempt to insert row into wrong map.");
00579 }
00580 
00581 void
00582 nsTableCellMap::RemoveRows(PRInt32         aFirstRowIndex,
00583                            PRInt32         aNumRowsToRemove,
00584                            PRBool          aConsiderSpans,
00585                            nsRect&         aDamageArea)
00586 {
00587   PRInt32 rowIndex = aFirstRowIndex;
00588   nsCellMap* cellMap = mFirstMap;
00589   while (cellMap) {
00590     if (cellMap->GetRowCount() > rowIndex) {
00591       cellMap->RemoveRows(*this, rowIndex, aNumRowsToRemove, aConsiderSpans, aDamageArea);
00592       nsTableRowGroupFrame* rg = cellMap->GetRowGroup();
00593       aDamageArea.y += (rg) ? rg->GetStartRowIndex() : 0;
00594       aDamageArea.height = PR_MAX(0, GetRowCount() - aFirstRowIndex);
00595       if (mBCInfo) {
00596         BCData* bcData;
00597         for (PRInt32 rowX = aFirstRowIndex + aNumRowsToRemove - 1; rowX >= aFirstRowIndex; rowX--) {
00598           if (rowX < mBCInfo->mRightBorders.Count()) {
00599             bcData = (BCData*)mBCInfo->mRightBorders.ElementAt(rowX);
00600             if (bcData) {
00601               delete bcData;
00602             }
00603             mBCInfo->mRightBorders.RemoveElementAt(rowX);
00604           }
00605         }
00606       }
00607       break;
00608     }
00609     rowIndex -= cellMap->GetRowCount();
00610     cellMap = cellMap->GetNextSibling();
00611   }
00612 #ifdef DEBUG_TABLE_CELLMAP
00613   Dump("after RemoveRows");
00614 #endif
00615 }
00616 
00617 PRInt32
00618 nsTableCellMap::GetNumCellsOriginatingInRow(PRInt32 aRowIndex)
00619 {
00620   PRInt32 originCount = 0;
00621 
00622   CellData* cellData;
00623   PRInt32   colIndex = 0;
00624 
00625   do {
00626          cellData = GetDataAt(aRowIndex, colIndex);
00627          if (cellData && cellData->GetCellFrame())
00628                 originCount++;
00629     colIndex++;
00630   } while(cellData);
00631 
00632   return originCount;
00633 }
00634 
00635 CellData* 
00636 nsTableCellMap::AppendCell(nsTableCellFrame& aCellFrame,
00637                            PRInt32           aRowIndex,
00638                            PRBool            aRebuildIfNecessary,
00639                            nsRect&           aDamageArea)
00640 {
00641   NS_ASSERTION(&aCellFrame == aCellFrame.GetFirstInFlow(), "invalid call on continuing frame");
00642   nsIFrame* rgFrame = aCellFrame.GetParent(); // get the row
00643   if (!rgFrame) return 0;
00644   rgFrame = rgFrame->GetParent();   // get the row group
00645   if (!rgFrame) return 0;
00646 
00647   CellData* result = nsnull;
00648   PRInt32 rowIndex = aRowIndex;
00649   nsCellMap* cellMap = mFirstMap;
00650   while (cellMap) {
00651     if (cellMap->GetRowGroup() == rgFrame) {
00652       result = cellMap->AppendCell(*this, &aCellFrame, rowIndex, aRebuildIfNecessary, aDamageArea);
00653       nsTableRowGroupFrame* rg = cellMap->GetRowGroup();
00654       aDamageArea.y += (rg) ? rg->GetStartRowIndex() : 0;
00655       break;
00656     }
00657     rowIndex -= cellMap->GetRowCount();
00658     cellMap = cellMap->GetNextSibling();
00659   }
00660 #ifdef DEBUG_TABLE_CELLMAP
00661   Dump("after AppendCell");
00662 #endif
00663   return result;
00664 }
00665 
00666 
00667 void 
00668 nsTableCellMap::InsertCells(nsVoidArray&          aCellFrames,
00669                             PRInt32               aRowIndex,
00670                             PRInt32               aColIndexBefore,
00671                             nsRect&               aDamageArea)
00672 {
00673   PRInt32 rowIndex = aRowIndex;
00674   nsCellMap* cellMap = mFirstMap;
00675   while (cellMap) {
00676     if (cellMap->GetRowCount() > rowIndex) {
00677       cellMap->InsertCells(*this, aCellFrames, rowIndex, aColIndexBefore, aDamageArea);
00678       nsTableRowGroupFrame* rg = cellMap->GetRowGroup();
00679       aDamageArea.y += (rg) ? rg->GetStartRowIndex() : 0;
00680       aDamageArea.width = PR_MAX(0, GetColCount() - aColIndexBefore - 1);
00681       break;
00682     }
00683     rowIndex -= cellMap->GetRowCount();
00684     cellMap = cellMap->GetNextSibling();
00685   }
00686 #ifdef DEBUG_TABLE_CELLMAP
00687   Dump("after InsertCells");
00688 #endif
00689 }
00690 
00691 
00692 void 
00693 nsTableCellMap::RemoveCell(nsTableCellFrame* aCellFrame,
00694                            PRInt32           aRowIndex,
00695                            nsRect&           aDamageArea)
00696 {
00697   if (!aCellFrame) ABORT0();
00698   NS_ASSERTION(aCellFrame == (nsTableCellFrame *)aCellFrame->GetFirstInFlow(),
00699                "invalid call on continuing frame");
00700   PRInt32 rowIndex = aRowIndex;
00701   nsCellMap* cellMap = mFirstMap;
00702   while (cellMap) {
00703     if (cellMap->GetRowCount() > rowIndex) {
00704       cellMap->RemoveCell(*this, aCellFrame, rowIndex, aDamageArea);
00705       nsTableRowGroupFrame* rg = cellMap->GetRowGroup();
00706       aDamageArea.y += (rg) ? rg->GetStartRowIndex() : 0;
00707       PRInt32 colIndex;
00708       aCellFrame->GetColIndex(colIndex);
00709       aDamageArea.width = PR_MAX(0, GetColCount() - colIndex - 1);
00710 #ifdef DEBUG_TABLE_CELLMAP
00711       Dump("after RemoveCell");
00712 #endif
00713       return;
00714     }
00715     rowIndex -= cellMap->GetRowCount();
00716     cellMap = cellMap->GetNextSibling();
00717   }
00718   // if we reach this point - the cell did not get removed, the caller of this routine 
00719   // will delete the cell and the cellmap will probably hold a reference to 
00720   // the deleted cell which will cause a subsequent crash when this cell is 
00721   // referenced later
00722   NS_ERROR("nsTableCellMap::RemoveCell - could not remove cell");
00723 }
00724 
00725 void
00726 SetDamageArea(PRInt32 aXOrigin,
00727               PRInt32 aYOrigin,
00728               PRInt32 aWidth,
00729               PRInt32 aHeight,
00730               nsRect& aDamageArea)
00731 {
00732   aDamageArea.x      = aXOrigin;
00733   aDamageArea.y      = aYOrigin;
00734   aDamageArea.width  = PR_MAX(1, aWidth);
00735   aDamageArea.height = PR_MAX(1, aHeight);
00736 }
00737 
00738 void 
00739 nsTableCellMap::RebuildConsideringCells(nsCellMap*      aCellMap,
00740                                         nsVoidArray*    aCellFrames,
00741                                         PRInt32         aRowIndex,
00742                                         PRInt32         aColIndex,
00743                                         PRBool          aInsert,
00744                                         nsRect&         aDamageArea)
00745 {
00746   PRInt32 numOrigCols = GetColCount();
00747   ClearCols();
00748   nsCellMap* cellMap = mFirstMap;
00749   PRInt32 rowCount = 0;
00750   while (cellMap) {
00751     if (cellMap == aCellMap) {
00752       cellMap->RebuildConsideringCells(*this, numOrigCols, aCellFrames, aRowIndex, aColIndex, aInsert, aDamageArea);
00753       
00754     }
00755     else {
00756       cellMap->RebuildConsideringCells(*this, numOrigCols, nsnull, -1, 0, PR_FALSE, aDamageArea);
00757     }
00758     rowCount += cellMap->GetRowCount();
00759     cellMap = cellMap->GetNextSibling();
00760   }
00761   SetDamageArea(0, 0, GetColCount(), rowCount, aDamageArea);
00762 }
00763 
00764 void 
00765 nsTableCellMap::RebuildConsideringRows(nsCellMap*      aCellMap,
00766                                        PRInt32         aStartRowIndex,
00767                                        nsVoidArray*    aRowsToInsert,
00768                                        PRBool          aNumRowsToRemove,
00769                                        nsRect&         aDamageArea)
00770 {
00771   PRInt32 numOrigCols = GetColCount();
00772   ClearCols();
00773   nsCellMap* cellMap = mFirstMap;
00774   PRInt32 rowCount = 0;
00775   while (cellMap) {
00776     if (cellMap == aCellMap) {
00777       cellMap->RebuildConsideringRows(*this, aStartRowIndex, aRowsToInsert, aNumRowsToRemove, aDamageArea);
00778     }
00779     else {
00780       cellMap->RebuildConsideringCells(*this, numOrigCols, nsnull, -1, 0, PR_FALSE, aDamageArea);
00781     }
00782     rowCount += cellMap->GetRowCount();
00783     cellMap = cellMap->GetNextSibling();
00784   }
00785   SetDamageArea(0, 0, GetColCount(), rowCount, aDamageArea);
00786 }
00787 
00788 PRInt32 
00789 nsTableCellMap::GetNumCellsOriginatingInCol(PRInt32 aColIndex) const
00790 {
00791   PRInt32 colCount = mCols.Count();
00792   if ((aColIndex >= 0) && (aColIndex < colCount)) {
00793     return ((nsColInfo *)mCols.ElementAt(aColIndex))->mNumCellsOrig;
00794   }
00795   else {
00796     NS_ASSERTION(PR_FALSE, "nsCellMap::GetNumCellsOriginatingInCol - bad col index");
00797     return 0;
00798   }
00799 }
00800 
00801 #ifdef NS_DEBUG
00802 void 
00803 nsTableCellMap::Dump(char* aString) const
00804 {
00805   if (aString) 
00806     printf("%s \n", aString);
00807   printf("***** START TABLE CELL MAP DUMP ***** %p\n", this);
00808   // output col info
00809   PRInt32 colCount = mCols.Count();
00810   printf ("cols array orig/span-> %p", this);
00811   for (PRInt32 colX = 0; colX < colCount; colX++) {
00812     nsColInfo* colInfo = (nsColInfo *)mCols.ElementAt(colX);
00813     printf ("%d=%d/%d ", colX, colInfo->mNumCellsOrig, colInfo->mNumCellsSpan);
00814   }
00815   printf(" cols in cache %d\n", mTableFrame.GetColCache().Count());
00816   nsCellMap* cellMap = mFirstMap;
00817   while (cellMap) {
00818     cellMap->Dump(nsnull != mBCInfo);
00819     cellMap = cellMap->GetNextSibling();
00820   }
00821   if (nsnull != mBCInfo) {
00822     printf("***** bottom borders *****\n");
00823     nscoord       size;
00824     BCBorderOwner owner;
00825     PRUint8       side;
00826     PRBool        segStart;
00827     PRPackedBool  bevel;  
00828     PRInt32       colIndex;    
00829     PRInt32 numCols = mBCInfo->mBottomBorders.Count();
00830     for (PRInt32 i = 0; i <= 2; i++) {
00831      
00832       printf("\n          ");
00833       for (colIndex = 0; colIndex < numCols; colIndex++) {
00834         BCData* cd = (BCData*)mBCInfo->mBottomBorders.ElementAt(colIndex);;
00835         if (cd) {
00836           if (0 == i) {
00837             size = cd->GetTopEdge(owner, segStart);
00838             printf("t=%d%X%d ", size, owner, segStart);
00839           }
00840           else if (1 == i) {
00841             size = cd->GetLeftEdge(owner, segStart);
00842             printf("l=%d%X%d ", size, owner, segStart);
00843           }
00844           else {
00845             size = cd->GetCorner(side, bevel);
00846             printf("c=%d%X%d ", size, side, bevel);
00847           }
00848         }
00849       }
00850       BCData* cd = &mBCInfo->mLowerRightCorner;
00851       if (cd) {
00852         if (0 == i) {
00853            size = cd->GetTopEdge(owner, segStart);
00854            printf("t=%d%X%d ", size, owner, segStart);
00855         }
00856         else if (1 == i) {
00857           size = cd->GetLeftEdge(owner, segStart);
00858           printf("l=%d%X%d ", size, owner, segStart);
00859         }
00860         else {
00861           size = cd->GetCorner(side, bevel);
00862           printf("c=%d%X%d ", size, side, bevel);
00863         }
00864       }
00865     }
00866     printf("\n");
00867   }
00868   printf("***** END TABLE CELL MAP DUMP *****\n");
00869 }
00870 #endif
00871 
00872 nsTableCellFrame* 
00873 nsTableCellMap::GetCellInfoAt(PRInt32  aRowIndex, 
00874                               PRInt32  aColIndex, 
00875                               PRBool*  aOriginates, 
00876                               PRInt32* aColSpan)
00877 {
00878   PRInt32 rowIndex = aRowIndex;
00879   nsCellMap* cellMap = mFirstMap;
00880   while (cellMap) {
00881     if (cellMap->GetRowCount() > rowIndex) {
00882       return cellMap->GetCellInfoAt(*this, rowIndex, aColIndex, aOriginates, aColSpan);
00883     }
00884     rowIndex -= cellMap->GetRowCount();
00885     cellMap = cellMap->GetNextSibling();
00886   }
00887   return nsnull;
00888 }
00889   
00890 
00891 PRBool nsTableCellMap::RowIsSpannedInto(PRInt32 aRowIndex)
00892 {
00893   PRInt32 rowIndex = aRowIndex;
00894   nsCellMap* cellMap = mFirstMap;
00895   while (cellMap) {
00896     if (cellMap->GetRowCount() > rowIndex) {
00897       return cellMap->RowIsSpannedInto(*this, rowIndex);
00898     }
00899     rowIndex -= cellMap->GetRowCount();
00900     cellMap = cellMap->GetNextSibling();
00901   }
00902   return PR_FALSE;
00903 }
00904 
00905 PRBool nsTableCellMap::RowHasSpanningCells(PRInt32 aRowIndex)
00906 {
00907   PRInt32 rowIndex = aRowIndex;
00908   nsCellMap* cellMap = mFirstMap;
00909   while (cellMap) {
00910     if (cellMap->GetRowCount() > rowIndex) {
00911       return cellMap->RowHasSpanningCells(*this, rowIndex);
00912     }
00913     rowIndex -= cellMap->GetRowCount();
00914     cellMap = cellMap->GetNextSibling();
00915   }
00916   return PR_FALSE;
00917 }
00918 
00919 PRBool nsTableCellMap::ColIsSpannedInto(PRInt32 aColIndex)
00920 {
00921   PRBool result = PR_FALSE;
00922 
00923   PRInt32 colCount = mCols.Count();
00924   if ((aColIndex >= 0) && (aColIndex < colCount)) {
00925     result = (PRBool) ((nsColInfo *)mCols.ElementAt(aColIndex))->mNumCellsSpan;
00926   }
00927   return result;
00928 }
00929 
00930 PRBool nsTableCellMap::ColHasSpanningCells(PRInt32 aColIndex)
00931 {
00932   nsCellMap* cellMap = mFirstMap;
00933   while (cellMap) {
00934     if (cellMap->ColHasSpanningCells(*this, aColIndex)) {
00935       return PR_TRUE;
00936     }
00937     cellMap = cellMap->GetNextSibling();
00938   }
00939   return PR_FALSE;
00940 }
00941 
00942 BCData* 
00943 nsTableCellMap::GetBCData(PRUint8     aSide, 
00944                           nsCellMap&  aCellMap,
00945                           PRUint32    aRowIndex, 
00946                           PRUint32    aColIndex,
00947                           PRBool      aIsLowerRight)
00948 {
00949   if (!mBCInfo || aIsLowerRight) ABORT1(nsnull);
00950 
00951   BCCellData* cellData;
00952   BCData* bcData = nsnull;
00953 
00954   switch(aSide) {
00955   case NS_SIDE_BOTTOM:
00956     aRowIndex++;
00957   case NS_SIDE_TOP:
00958     cellData = (BCCellData*)aCellMap.GetDataAt(*this, aRowIndex, aColIndex, PR_FALSE);
00959     if (cellData) {
00960       bcData = &cellData->mData;
00961     }
00962     else {
00963       NS_ASSERTION(aSide == NS_SIDE_BOTTOM, "program error");
00964       // try the next row group
00965       nsCellMap* cellMap = aCellMap.GetNextSibling();
00966       if (cellMap) {
00967         cellData = (BCCellData*)cellMap->GetDataAt(*this, 0, aColIndex, PR_FALSE);
00968         if (cellData) {
00969           bcData = &cellData->mData;
00970         }
00971         else {
00972           bcData = GetBottomMostBorder(aColIndex);
00973         }
00974       }
00975     }
00976     break;
00977   case NS_SIDE_RIGHT:
00978     aColIndex++;
00979   case NS_SIDE_LEFT:
00980     cellData = (BCCellData*)aCellMap.GetDataAt(*this, aRowIndex, aColIndex, PR_FALSE);
00981     if (cellData) {
00982       bcData = &cellData->mData;
00983     }
00984     else {
00985       NS_ASSERTION(aSide == NS_SIDE_RIGHT, "program error");
00986       bcData = GetRightMostBorder(aRowIndex);
00987     }
00988     break;
00989   }
00990   return bcData;
00991 }
00992 
00993 // store the aSide border segment at coord = (aRowIndex, aColIndex). For top/left, store
00994 // the info at coord. For bottom/left store it at the adjacent location so that it is 
00995 // top/left at that location. If the new location is at the right or bottom edge of the 
00996 // table, then store it one of the special arrays (right most borders, bottom most borders).  
00997 void 
00998 nsTableCellMap::SetBCBorderEdge(PRUint8       aSide, 
00999                                 nsCellMap&    aCellMap,
01000                                 PRUint32      aCellMapStart,
01001                                 PRUint32      aRowIndex, 
01002                                 PRUint32      aColIndex,
01003                                 PRUint32      aLength,
01004                                 BCBorderOwner aOwner,
01005                                 nscoord       aSize,
01006                                 PRBool        aChanged)
01007 {
01008   if (!mBCInfo) ABORT0();
01009   //if (aRowIndex >= 80) {
01010   //  NS_ASSERTION(PR_FALSE, "hello");
01011   //}
01012 
01013   BCCellData* cellData;
01014   PRInt32 lastIndex, xIndex, yIndex;
01015   PRInt32 xPos = aColIndex;
01016   PRInt32 yPos = aRowIndex;
01017   PRInt32 rgYPos = aRowIndex - aCellMapStart;
01018   PRBool changed;
01019 
01020   switch(aSide) {
01021   case NS_SIDE_BOTTOM:
01022     rgYPos++;
01023     yPos++;
01024   case NS_SIDE_TOP:
01025     lastIndex = xPos + aLength - 1;
01026     for (xIndex = xPos; xIndex <= lastIndex; xIndex++) {
01027       changed = aChanged && (xIndex == xPos);
01028       BCData* bcData = nsnull;
01029       cellData = (BCCellData*)aCellMap.GetDataAt(*this, rgYPos, xIndex, PR_FALSE);
01030       if (!cellData) {
01031         PRInt32 numRgRows = aCellMap.GetRowCount();
01032         if (yPos < numRgRows) { // add a dead cell data
01033           nsRect damageArea;
01034           cellData = (BCCellData*)aCellMap.AppendCell(*this, nsnull, rgYPos, PR_FALSE, damageArea); if (!cellData) ABORT0();
01035         }
01036         else {
01037           NS_ASSERTION(aSide == NS_SIDE_BOTTOM, "program error");
01038           // try the next non empty row group
01039           nsCellMap* cellMap = aCellMap.GetNextSibling();
01040           while (cellMap && (0 == cellMap->GetRowCount())) {
01041             cellMap = cellMap->GetNextSibling();
01042           }
01043           if (cellMap) {
01044             cellData = (BCCellData*)cellMap->GetDataAt(*this, 0, xIndex, PR_FALSE);
01045             if (!cellData) { // add a dead cell
01046               nsRect damageArea;
01047               cellData = (BCCellData*)cellMap->AppendCell(*this, nsnull, 0, PR_FALSE, damageArea); 
01048             }
01049           }
01050           else { // must be at the end of the table
01051             bcData = GetBottomMostBorder(xIndex);
01052           }
01053         }
01054       }
01055       if (!bcData && cellData) {
01056         bcData = &cellData->mData;
01057       }
01058       if (bcData) {
01059         bcData->SetTopEdge(aOwner, aSize, changed);
01060       }
01061       else NS_ASSERTION(PR_FALSE, "program error");
01062     }
01063     break;
01064   case NS_SIDE_RIGHT:
01065     xPos++;
01066   case NS_SIDE_LEFT:
01067     // since top, bottom borders were set, there should already be a cellData entry
01068     lastIndex = rgYPos + aLength - 1;
01069     for (yIndex = rgYPos; yIndex <= lastIndex; yIndex++) {
01070       changed = aChanged && (yIndex == rgYPos);
01071       cellData = (BCCellData*)aCellMap.GetDataAt(*this, yIndex, xPos, PR_FALSE);
01072       if (cellData) {
01073         cellData->mData.SetLeftEdge(aOwner, aSize, changed);
01074       }
01075       else {
01076         NS_ASSERTION(aSide == NS_SIDE_RIGHT, "program error");
01077         BCData* bcData = GetRightMostBorder(yIndex + aCellMapStart);
01078         if (bcData) {
01079           bcData->SetLeftEdge(aOwner, aSize, changed);
01080         }
01081         else NS_ASSERTION(PR_FALSE, "program error");
01082       }
01083     }
01084     break;
01085   }
01086 }
01087 
01088 // store corner info (aOwner, aSubSize, aBevel). For aCorner = eTopLeft, store the info at 
01089 // (aRowIndex, aColIndex). For eTopRight, store it in the entry to the right where
01090 // it would be top left. For eBottomRight, store it in the entry to the bottom. etc. 
01091 void 
01092 nsTableCellMap::SetBCBorderCorner(Corner      aCorner,
01093                                   nsCellMap&  aCellMap,
01094                                   PRUint32    aCellMapStart,
01095                                   PRUint32    aRowIndex, 
01096                                   PRUint32    aColIndex,
01097                                   PRUint8     aOwner,
01098                                   nscoord     aSubSize,
01099                                   PRBool      aBevel,
01100                                   PRBool      aIsBottomRight)
01101 {
01102   if (!mBCInfo) ABORT0();
01103 
01104   if (aIsBottomRight) { 
01105     mBCInfo->mLowerRightCorner.SetCorner(aSubSize, aOwner, aBevel);
01106     return;
01107   }
01108 
01109   PRInt32 xPos = aColIndex;
01110   PRInt32 yPos = aRowIndex;
01111   PRInt32 rgYPos = aRowIndex - aCellMapStart;
01112 
01113   if (eTopRight == aCorner) {
01114     xPos++;
01115   }
01116   else if (eBottomRight == aCorner) {
01117     xPos++;
01118     rgYPos++;
01119     yPos++;
01120   }
01121   else if (eBottomLeft == aCorner) {
01122     rgYPos++;
01123     yPos++;
01124   }
01125 
01126   BCCellData* cellData = nsnull;
01127   BCData*     bcData   = nsnull;
01128   if (GetColCount() <= xPos) {
01129     NS_ASSERTION(xPos == GetColCount(), "program error");
01130     // at the right edge of the table as we checked the corner before
01131     bcData = GetRightMostBorder(yPos);
01132   }
01133   else {
01134     cellData = (BCCellData*)aCellMap.GetDataAt(*this, rgYPos, xPos, PR_FALSE);
01135     if (!cellData) {
01136       PRInt32 numRgRows = aCellMap.GetRowCount();
01137       if (yPos < numRgRows) { // add a dead cell data
01138         nsRect damageArea;
01139         cellData = (BCCellData*)aCellMap.AppendCell(*this, nsnull, rgYPos, PR_FALSE, damageArea); 
01140       }
01141       else {
01142         // try the next non empty row group
01143         nsCellMap* cellMap = aCellMap.GetNextSibling();
01144         while (cellMap && (0 == cellMap->GetRowCount())) {
01145           cellMap = cellMap->GetNextSibling();
01146         }
01147         if (cellMap) {
01148           cellData = (BCCellData*)cellMap->GetDataAt(*this, 0, xPos, PR_FALSE);
01149           if (!cellData) { // add a dead cell
01150             nsRect damageArea;
01151             cellData = (BCCellData*)cellMap->AppendCell(*this, nsnull, 0, PR_FALSE, damageArea); 
01152           }
01153         }
01154         else { // must be a the bottom of the table
01155           bcData = GetBottomMostBorder(xPos);
01156         }
01157       }
01158     }
01159   }
01160   if (!bcData && cellData) {
01161     bcData = &cellData->mData;
01162   }
01163   if (bcData) {
01164     bcData->SetCorner(aSubSize, aOwner, aBevel);
01165   }
01166   else NS_ASSERTION(PR_FALSE, "program error");
01167 }
01168 
01169 nsCellMap::nsCellMap(nsTableRowGroupFrame& aRowGroup)
01170   : mRowCount(0), mRowGroupFrame(&aRowGroup), mNextSibling(nsnull)
01171 {
01172   MOZ_COUNT_CTOR(nsCellMap);
01173 }
01174 
01175 nsCellMap::~nsCellMap()
01176 {
01177   MOZ_COUNT_DTOR(nsCellMap);
01178   
01179   PRInt32 mapRowCount = mRows.Count();
01180   for (PRInt32 rowX = 0; rowX < mapRowCount; rowX++) {
01181     nsVoidArray* row = (nsVoidArray *)(mRows.ElementAt(rowX));
01182     PRInt32 colCount = row->Count();
01183     for (PRInt32 colX = 0; colX < colCount; colX++) {
01184       CellData* data = (CellData *)(row->ElementAt(colX));
01185       if (data) {
01186         delete data;
01187       } 
01188     }
01189     delete row;
01190   }
01191 }
01192 
01193 nsTableCellFrame* 
01194 nsCellMap::GetCellFrame(PRInt32   aRowIndexIn,
01195                         PRInt32   aColIndexIn,
01196                         CellData& aData,
01197                         PRBool    aUseRowIfOverlap) const
01198 {
01199   PRInt32 rowIndex = aRowIndexIn - aData.GetRowSpanOffset();
01200   PRInt32 colIndex = aColIndexIn - aData.GetColSpanOffset();
01201   if (aData.IsOverlap()) {
01202     if (aUseRowIfOverlap) {
01203       colIndex = aColIndexIn;
01204     }
01205     else {
01206       rowIndex = aRowIndexIn;
01207     }
01208   }
01209 
01210   nsVoidArray* row = (nsVoidArray*) mRows.SafeElementAt(rowIndex);
01211   if (row) {
01212     CellData* data = (CellData*)row->SafeElementAt(colIndex);
01213     if (data) {
01214       return data->GetCellFrame();
01215     }
01216   }
01217   return nsnull;
01218 }
01219 
01220 PRBool nsCellMap::Grow(nsTableCellMap& aMap,
01221                        PRInt32         aNumRows,
01222                        PRInt32         aRowIndex)
01223 {
01224   PRInt32 numCols = aMap.GetColCount();
01225   PRInt32 startRowIndex = (aRowIndex >= 0) ? aRowIndex : mRows.Count();
01226   PRInt32 endRowIndex = startRowIndex + aNumRows - 1;
01227   // XXX We really should have a way to say "make this voidarray at least
01228   // N entries long" to avoid reallocating N times.  On the other hand, the
01229   // number of likely allocations here isn't TOO gigantic, and we may not
01230   // know about many of them at a time.
01231   for (PRInt32 rowX = startRowIndex; rowX <= endRowIndex; rowX++) {
01232     nsVoidArray* row;
01233     row = (0 == numCols) ? new nsVoidArray(4) : new nsVoidArray(numCols);
01234     if (row) {
01235       mRows.InsertElementAt(row, rowX);
01236     }
01237     else return PR_FALSE;
01238   }
01239   return PR_TRUE;
01240 }
01241 
01242 void nsCellMap::GrowRow(nsVoidArray& aRow,
01243                         PRInt32      aNumCols)
01244                      
01245 {
01246   for (PRInt32 colX = 0; colX < aNumCols; colX++) {
01247     aRow.AppendElement(nsnull);
01248   }
01249 }
01250 
01251 void
01252 nsCellMap::InsertRows(nsTableCellMap& aMap,
01253                       nsVoidArray&    aRows,
01254                       PRInt32         aFirstRowIndex,
01255                       PRBool          aConsiderSpans,
01256                       nsRect&         aDamageArea)
01257 {
01258   PRInt32 numCols = aMap.GetColCount();
01259   NS_ASSERTION(aFirstRowIndex >= 0, "nsCellMap::InsertRows called with negative rowIndex");
01260   if (aFirstRowIndex > mRowCount) {
01261     // create (aFirstRowIndex - mRowCount) empty rows up to aFirstRowIndex
01262     PRInt32 numEmptyRows = aFirstRowIndex - mRowCount;
01263     if (!Grow(aMap, numEmptyRows, mRowCount)) {
01264       return;
01265     }
01266     // update mRowCount, since non-empty rows will be added
01267     mRowCount += numEmptyRows;
01268   }
01269 
01270   if (!aConsiderSpans) {
01271     ExpandWithRows(aMap, aRows, aFirstRowIndex, aDamageArea);
01272     return;
01273   }
01274 
01275   // if any cells span into or out of the row being inserted, then rebuild
01276   PRBool spansCauseRebuild = CellsSpanInOrOut(aMap, aFirstRowIndex, 
01277                                               aFirstRowIndex, 0, numCols - 1);
01278 
01279   // if any of the new cells span out of the new rows being added, then rebuild
01280   // XXX it would be better to only rebuild the portion of the map that follows the new rows
01281   if (!spansCauseRebuild && (aFirstRowIndex < mRows.Count())) {
01282     spansCauseRebuild = CellsSpanOut(aRows);
01283   }
01284 
01285   if (spansCauseRebuild) {
01286     aMap.RebuildConsideringRows(this, aFirstRowIndex, &aRows, 0, aDamageArea);
01287   }
01288   else {
01289     ExpandWithRows(aMap, aRows, aFirstRowIndex, aDamageArea);
01290   }
01291 }
01292 
01293 void
01294 nsCellMap::RemoveRows(nsTableCellMap& aMap,
01295                       PRInt32         aFirstRowIndex,
01296                       PRInt32         aNumRowsToRemove,
01297                       PRBool          aConsiderSpans,
01298                       nsRect&         aDamageArea)
01299 {
01300   PRInt32 numRows = mRows.Count();
01301   PRInt32 numCols = aMap.GetColCount();
01302 
01303   if (aFirstRowIndex >= numRows) {
01304     // reduce the content based row count based on the function arguments
01305     // as they are known to be real rows even if the cell map did not create
01306     // rows for them before.
01307     mRowCount -= aNumRowsToRemove;
01308     return;
01309   }
01310   if (!aConsiderSpans) {
01311     ShrinkWithoutRows(aMap, aFirstRowIndex, aNumRowsToRemove, aDamageArea);
01312     return;
01313   }
01314   PRInt32 endRowIndex = aFirstRowIndex + aNumRowsToRemove - 1;
01315   if (endRowIndex >= numRows) {
01316     NS_ASSERTION(PR_FALSE, "nsCellMap::RemoveRows tried to remove too many rows");
01317     endRowIndex = numRows - 1;
01318   }
01319   PRBool spansCauseRebuild = CellsSpanInOrOut(aMap, aFirstRowIndex, endRowIndex,
01320                                               0, numCols - 1);
01321 
01322   if (spansCauseRebuild) {
01323     aMap.RebuildConsideringRows(this, aFirstRowIndex, nsnull, aNumRowsToRemove, aDamageArea);
01324   }
01325   else {
01326     ShrinkWithoutRows(aMap, aFirstRowIndex, aNumRowsToRemove, aDamageArea);
01327   }
01328 }
01329 
01330 
01331 
01332 
01333 CellData* 
01334 nsCellMap::AppendCell(nsTableCellMap&   aMap,
01335                       nsTableCellFrame* aCellFrame, 
01336                       PRInt32           aRowIndex,
01337                       PRBool            aRebuildIfNecessary,
01338                       nsRect&           aDamageArea,
01339                       PRInt32*          aColToBeginSearch)
01340 {
01341   PRInt32 origNumMapRows = mRows.Count();
01342   PRInt32 origNumCols = aMap.GetColCount();
01343   PRBool  zeroRowSpan;
01344   PRInt32 rowSpan = (aCellFrame) ? GetRowSpanForNewCell(*aCellFrame, aRowIndex, zeroRowSpan) : 1;
01345   // add new rows if necessary
01346   PRInt32 endRowIndex = aRowIndex + rowSpan - 1;
01347   if (endRowIndex >= origNumMapRows) {
01348     Grow(aMap, 1 + endRowIndex - origNumMapRows);
01349   }
01350 
01351   // get the first null or dead CellData in the desired row. It will equal origNumCols if there are none
01352   CellData* origData = nsnull;
01353   PRInt32 startColIndex = 0;
01354   if (aColToBeginSearch)
01355     startColIndex = *aColToBeginSearch;
01356   for (; startColIndex < origNumCols; startColIndex++) {
01357     CellData* data = GetDataAt(aMap, aRowIndex, startColIndex, PR_TRUE);
01358     if (!data) 
01359       break;
01360     if (data->IsDead()) {
01361       origData = data;
01362       break; 
01363     }
01364   }
01365   // We found the place to append the cell, when the next cell is appended 
01366   // the next search does not need to duplicate the search but can start 
01367   // just at the next cell.
01368   if (aColToBeginSearch)
01369     *aColToBeginSearch =  startColIndex + 1; 
01370   
01371   PRBool  zeroColSpan;
01372   PRInt32 colSpan = (aCellFrame) ? GetColSpanForNewCell(*aCellFrame, startColIndex, origNumCols, zeroColSpan) : 1;
01373 
01374   // if the new cell could potentially span into other rows and collide with 
01375   // originating cells there, we will play it safe and just rebuild the map
01376   if (aRebuildIfNecessary && (aRowIndex < mRowCount - 1) && (rowSpan > 1)) {
01377     nsAutoVoidArray newCellArray;
01378     newCellArray.AppendElement(aCellFrame);
01379     aMap.RebuildConsideringCells(this, &newCellArray, aRowIndex, startColIndex, PR_TRUE, aDamageArea);
01380     return origData;
01381   }
01382   mRowCount = PR_MAX(mRowCount, aRowIndex + 1);
01383 
01384   // add new cols to the table map if necessary
01385   PRInt32 endColIndex = startColIndex + colSpan - 1;
01386   if (endColIndex >= origNumCols) {
01387     NS_ASSERTION(aCellFrame, "dead cells should not require new columns");
01388     aMap.AddColsAtEnd(1 + endColIndex - origNumCols);
01389   }
01390 
01391   // Setup CellData for this cell
01392   if (origData) {
01393     NS_ASSERTION(origData->IsDead(), "replacing a non dead cell is a memory leak");
01394     if (aCellFrame) { // do nothing to replace a dead cell with a dead cell
01395       origData->Init(aCellFrame);
01396       // we are replacing a dead cell, increase the number of cells 
01397       // originating at this column
01398       nsColInfo* colInfo = aMap.GetColInfoAt(startColIndex);
01399       NS_ASSERTION(colInfo, "access to a non existing column");
01400       if (colInfo) { 
01401         colInfo->mNumCellsOrig++;
01402       }
01403     }
01404   }
01405   else {
01406     origData = (aMap.mBCInfo) ? new BCCellData(aCellFrame) : new CellData(aCellFrame); if (!origData) ABORT1(origData);
01407     SetDataAt(aMap, *origData, aRowIndex, startColIndex, PR_TRUE);
01408   }
01409 
01410   SetDamageArea(startColIndex, aRowIndex, 1 + endColIndex - startColIndex, 1 + endRowIndex - aRowIndex, aDamageArea); 
01411 
01412   if (!aCellFrame) {
01413     return origData;
01414   }
01415 
01416   // initialize the cell frame
01417   aCellFrame->SetColIndex(startColIndex);
01418 
01419   // Create CellData objects for the rows that this cell spans. Set
01420   // their mOrigCell to nsnull and their mSpanData to point to data.
01421   for (PRInt32 rowX = aRowIndex; rowX <= endRowIndex; rowX++) {
01422     for (PRInt32 colX = startColIndex; colX <= endColIndex; colX++) {
01423       if ((rowX != aRowIndex) || (colX != startColIndex)) { // skip orig cell data done above
01424         CellData* cellData = GetDataAt(aMap, rowX, colX, PR_FALSE);
01425         if (cellData) {
01426           if (cellData->IsOrig()) {
01427             NS_ASSERTION(PR_FALSE, "cannot overlap originating cell");
01428             continue;
01429           }
01430           if (rowX > aRowIndex) { // row spanning into cell
01431             if (cellData->IsRowSpan()) {
01432               NS_ASSERTION(PR_FALSE, "invalid overlap");
01433             }
01434             else {
01435               cellData->SetRowSpanOffset(rowX - aRowIndex);
01436               if (zeroRowSpan) {
01437                 cellData->SetZeroRowSpan(PR_TRUE);
01438               }
01439             }
01440           }
01441           if (colX > startColIndex) { // col spanning into cell
01442             if (!cellData->IsColSpan()) { 
01443               if (cellData->IsRowSpan()) {
01444                 cellData->SetOverlap(PR_TRUE);
01445               }
01446               cellData->SetColSpanOffset(colX - startColIndex);
01447               if (zeroColSpan) {
01448                 cellData->SetZeroColSpan(PR_TRUE);
01449               }
01450               // only count the 1st spanned col of a zero col span
01451               if (!zeroColSpan || (colX == startColIndex + 1)) {
01452                 nsColInfo* colInfo = aMap.GetColInfoAt(colX);
01453                 colInfo->mNumCellsSpan++;
01454               }
01455             }
01456           }
01457         }
01458         else { 
01459           cellData = (aMap.mBCInfo) ? new BCCellData(nsnull) : new CellData(nsnull);
01460           if (!cellData) return origData;
01461           if (rowX > aRowIndex) {
01462             cellData->SetRowSpanOffset(rowX - aRowIndex);
01463           }
01464           if (zeroRowSpan) {
01465             cellData->SetZeroRowSpan(PR_TRUE);
01466           }
01467           if (colX > startColIndex) {
01468             cellData->SetColSpanOffset(colX - startColIndex);
01469           }
01470           if (zeroColSpan) {
01471             cellData->SetZeroColSpan(PR_TRUE);
01472           }
01473           // only count the 1st spanned col of a zero col span
01474           SetDataAt(aMap, *cellData, rowX, colX, (colX == startColIndex + 1));
01475         }
01476       }
01477     }
01478   }
01479 #ifdef DEBUG_TABLE_CELLMAP
01480   printf("appended cell=%p row=%d \n", aCellFrame, aRowIndex);
01481   aMap.Dump();
01482 #endif
01483   return origData;
01484 }
01485 
01486 PRBool nsCellMap::CellsSpanOut(nsVoidArray&    aRows)
01487 { 
01488   PRInt32 numNewRows = aRows.Count();
01489   for (PRInt32 rowX = 0; rowX < numNewRows; rowX++) {
01490     nsIFrame* rowFrame = (nsIFrame *) aRows.ElementAt(rowX);
01491     nsIFrame* cellFrame = rowFrame->GetFirstChild(nsnull);
01492     while (cellFrame) {
01493       if (IS_TABLE_CELL(cellFrame->GetType())) {
01494         PRBool zeroSpan;
01495         PRInt32 rowSpan = GetRowSpanForNewCell((nsTableCellFrame &)*cellFrame, rowX, zeroSpan);
01496         if (rowX + rowSpan > numNewRows) {
01497           return PR_TRUE;
01498         }
01499       }
01500       cellFrame = cellFrame->GetNextSibling();
01501     }
01502   }
01503   return PR_FALSE;
01504 }
01505     
01506 // return PR_TRUE if any cells have rows spans into or out of the region 
01507 // defined by the row and col indices or any cells have colspans into the region
01508 PRBool nsCellMap::CellsSpanInOrOut(nsTableCellMap& aMap,
01509                                    PRInt32         aStartRowIndex, 
01510                                    PRInt32         aEndRowIndex,
01511                                    PRInt32         aStartColIndex, 
01512                                    PRInt32         aEndColIndex)
01513 {
01514   /*
01515    * this routine will watch the cells adjacent to the region or at the edge
01516    * they are marked with *. The routine will verify whether they span in or
01517    * are spanned out.
01518    *
01519    *                           startCol          endCol
01520    *             r1c1   r1c2   r1c3      r1c4    r1c5    r1rc6  r1c7
01521    *  startrow   r2c1   r2c2  *r2c3     *r2c4   *r2c5   *r2rc6  r2c7
01522    *  endrow     r3c1   r3c2  *r3c3      r3c4    r3c5   *r3rc6  r3c7
01523    *             r4c1   r4c2  *r4c3     *r4c4   *r4c5    r4rc6  r4c7
01524    *             r5c1   r5c2   r5c3      r5c4    r5c5    r5rc6  r5c7  
01525    */
01526 
01527   PRInt32 numRows = mRows.Count(); // use the cellmap rows to determine the 
01528                                    // current cellmap extent.
01529   for (PRInt32 colX = aStartColIndex; colX <= aEndColIndex; colX++) {
01530     CellData* cellData;
01531     if (aStartRowIndex > 0) {
01532       cellData = GetDataAt(aMap, aStartRowIndex, colX, PR_TRUE);
01533       if (cellData && (cellData->IsRowSpan())) {
01534         return PR_TRUE; // there is a row span into the region
01535       }
01536     }
01537     if (aEndRowIndex < numRows - 1) { // is there anything below aEndRowIndex
01538       cellData = GetDataAt(aMap, aEndRowIndex + 1, colX, PR_TRUE);
01539       if ((cellData) && (cellData->IsRowSpan())) {
01540         return PR_TRUE; // there is a row span out of the region
01541       }
01542     }
01543     else {
01544       cellData = GetDataAt(aMap, aEndRowIndex, colX, PR_TRUE);
01545       if ((cellData) && (cellData->IsRowSpan()) && (mRowCount < numRows)) {
01546         return PR_TRUE; // this cell might be the cause of a dead row
01547       }
01548     }
01549   }
01550   if (aStartColIndex > 0) {
01551     for (PRInt32 rowX = aStartRowIndex; rowX <= aEndRowIndex; rowX++) {
01552       CellData* cellData = GetDataAt(aMap, rowX, aStartColIndex, PR_TRUE);
01553       if (cellData && (cellData->IsColSpan())) {
01554         return PR_TRUE; // there is a col span into the region
01555       }
01556       nsVoidArray* row = (nsVoidArray *)(mRows.SafeElementAt(rowX));
01557       if (row) {
01558         cellData = (CellData *)(row->SafeElementAt(aEndColIndex + 1));
01559         if (cellData && (cellData->IsColSpan())) {
01560           return PR_TRUE; // there is a col span out of the region 
01561         }
01562       }
01563     }
01564   }
01565   return PR_FALSE;
01566 }
01567 
01568 void nsCellMap::InsertCells(nsTableCellMap& aMap,
01569                             nsVoidArray&    aCellFrames, 
01570                             PRInt32         aRowIndex,
01571                             PRInt32         aColIndexBefore,
01572                             nsRect&         aDamageArea)
01573 {
01574   if (aCellFrames.Count() == 0) return;
01575   PRInt32 numCols = aMap.GetColCount();
01576   if (aColIndexBefore >= numCols) {
01577     NS_ERROR("Inserting instead of appending cells indicates a serious cellmap error");
01578     aColIndexBefore = numCols - 1;
01579   }
01580 
01581   // get the starting col index of the 1st new cells 
01582   PRInt32 startColIndex;
01583   for (startColIndex = aColIndexBefore + 1; startColIndex < numCols; startColIndex++) {
01584     CellData* data = GetDataAt(aMap, aRowIndex, startColIndex, PR_TRUE);
01585     if (!data || data->IsOrig()) { // stop unless it is a span
01586       break; 
01587     }
01588   }
01589 
01590   // record whether inserted cells are going to cause complications due 
01591   // to existing row spans, col spans or table sizing. 
01592   PRBool spansCauseRebuild = PR_FALSE;
01593 
01594   // check that all cells have the same row span
01595   PRInt32 numNewCells = aCellFrames.Count();
01596   PRBool zeroRowSpan = PR_FALSE;
01597   PRInt32 rowSpan = 0;
01598   for (PRInt32 cellX = 0; cellX < numNewCells; cellX++) {
01599     nsTableCellFrame* cell = (nsTableCellFrame*) aCellFrames.ElementAt(cellX);
01600     PRInt32 rowSpan2 = GetRowSpanForNewCell(*cell, aRowIndex, zeroRowSpan);
01601     if (rowSpan == 0) {
01602       rowSpan = rowSpan2;
01603     }
01604     else if (rowSpan != rowSpan2) {
01605       spansCauseRebuild = PR_TRUE;
01606       break;
01607     }
01608   }
01609 
01610   // check if the new cells will cause the table to add more rows
01611   if (!spansCauseRebuild) {
01612     if (mRows.Count() < aRowIndex + rowSpan) {
01613       spansCauseRebuild = PR_TRUE;
01614     }
01615   }
01616 
01617   if (!spansCauseRebuild) {
01618     spansCauseRebuild = CellsSpanInOrOut(aMap, aRowIndex, aRowIndex + rowSpan - 1, 
01619                                          startColIndex, numCols - 1);
01620   }
01621 
01622   if (spansCauseRebuild) {
01623     aMap.RebuildConsideringCells(this, &aCellFrames, aRowIndex, startColIndex, PR_TRUE, aDamageArea);
01624   }
01625   else {
01626     ExpandWithCells(aMap, aCellFrames, aRowIndex, startColIndex, rowSpan, zeroRowSpan, aDamageArea);
01627   }
01628 }
01629  
01630 void
01631 nsCellMap::ExpandWithRows(nsTableCellMap& aMap,
01632                           nsVoidArray&    aRowFrames,
01633                           PRInt32         aStartRowIndexIn,
01634                           nsRect&         aDamageArea)
01635 {
01636   PRInt32 startRowIndex = (aStartRowIndexIn >= 0) ? aStartRowIndexIn : 0;
01637   PRInt32 numNewRows  = aRowFrames.Count();
01638   PRInt32 endRowIndex = startRowIndex + numNewRows - 1;
01639 
01640   // create the new rows first
01641   if (!Grow(aMap, numNewRows, startRowIndex)) {
01642     return;
01643   }
01644   mRowCount += numNewRows;
01645 
01646   PRInt32 newRowIndex = 0;
01647   for (PRInt32 rowX = startRowIndex; rowX <= endRowIndex; rowX++) {
01648     nsTableRowFrame* rFrame = (nsTableRowFrame *)aRowFrames.ElementAt(newRowIndex);
01649     // append cells 
01650     nsIFrame* cFrame = rFrame->GetFirstChild(nsnull);
01651     PRInt32 colIndex = 0;
01652     while (cFrame) {
01653       if (IS_TABLE_CELL(cFrame->GetType())) {
01654         AppendCell(aMap, (nsTableCellFrame *)cFrame, rowX, PR_FALSE, aDamageArea, &colIndex);
01655       }
01656       cFrame = cFrame->GetNextSibling();
01657     }
01658     newRowIndex++;
01659   }
01660 
01661   SetDamageArea(0, startRowIndex, aMap.GetColCount(), 1 + endRowIndex - startRowIndex, aDamageArea); 
01662 }
01663 
01664 void nsCellMap::ExpandWithCells(nsTableCellMap& aMap,
01665                                 nsVoidArray&    aCellFrames,
01666                                 PRInt32         aRowIndex,
01667                                 PRInt32         aColIndex,
01668                                 PRInt32         aRowSpan, // same for all cells
01669                                 PRBool          aRowSpanIsZero,
01670                                 nsRect&         aDamageArea)
01671 {
01672   PRInt32 endRowIndex = aRowIndex + aRowSpan - 1;
01673   PRInt32 startColIndex = aColIndex;
01674   PRInt32 endColIndex = aColIndex;
01675   PRInt32 numCells = aCellFrames.Count();
01676   PRInt32 totalColSpan = 0;
01677 
01678   // add cellData entries for the space taken up by the new cells
01679   for (PRInt32 cellX = 0; cellX < numCells; cellX++) {
01680     nsTableCellFrame* cellFrame = (nsTableCellFrame*) aCellFrames.ElementAt(cellX);
01681     CellData* origData = (aMap.mBCInfo) ? new BCCellData(cellFrame) : new CellData(cellFrame); // the originating cell
01682     if (!origData) return;
01683 
01684     // set the starting and ending col index for the new cell
01685     PRBool zeroColSpan = PR_FALSE;
01686     PRInt32 colSpan = GetColSpanForNewCell(*cellFrame, aColIndex, aMap.GetColCount(), zeroColSpan);
01687     totalColSpan += colSpan;
01688     if (cellX == 0) {
01689       endColIndex = aColIndex + colSpan - 1;
01690     }
01691     else {
01692       startColIndex = endColIndex + 1;
01693       endColIndex   = startColIndex + colSpan - 1;
01694     }
01695  
01696     // add the originating cell data and any cell data corresponding to row/col spans
01697     for (PRInt32 rowX = aRowIndex; rowX <= endRowIndex; rowX++) {
01698       nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rowX);
01699       for (PRInt32 colX = aColIndex; colX <= endColIndex; colX++) {
01700         row->InsertElementAt(nsnull, colX);
01701         CellData* data = origData;
01702         if ((rowX != aRowIndex) || (colX != startColIndex)) {
01703           data = (aMap.mBCInfo) ? new BCCellData(nsnull) : new CellData(nsnull);
01704           if (!data) return;
01705           if (rowX > aRowIndex) {
01706             data->SetRowSpanOffset(rowX - aRowIndex);
01707             if (aRowSpanIsZero) {
01708               data->SetZeroRowSpan(PR_TRUE);
01709             }
01710           }
01711           if (colX > startColIndex) {
01712             data->SetColSpanOffset(colX - startColIndex);
01713             if (zeroColSpan) {
01714               data->SetZeroColSpan(PR_TRUE);
01715             }
01716           }
01717         }
01718         // only count 1st spanned col of colspan=0
01719         SetDataAt(aMap, *data, rowX, colX, (colX == aColIndex + 1)); 
01720       }
01721     }
01722     cellFrame->SetColIndex(startColIndex);
01723   }
01724   PRInt32 damageHeight = (aRowSpanIsZero) ? aMap.GetColCount() - aRowIndex : aRowSpan;
01725   SetDamageArea(aColIndex, aRowIndex, 1 + endColIndex - aColIndex, damageHeight, aDamageArea); 
01726 
01727   PRInt32 rowX;
01728 
01729   // update the row and col info due to shifting
01730   for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) {
01731     nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rowX);
01732     PRInt32 numCols = row->Count();
01733     PRInt32 colX;
01734     for (colX = aColIndex + totalColSpan; colX < numCols; colX++) {
01735       CellData* data = (CellData*) row->ElementAt(colX);
01736       if (data) {
01737         // increase the origin and span counts beyond the spanned cols
01738         if (data->IsOrig()) {
01739           // a cell that gets moved needs adjustment as well as it new orignating col
01740           data->GetCellFrame()->SetColIndex(colX);
01741           nsColInfo* colInfo = aMap.GetColInfoAt(colX);
01742           colInfo->mNumCellsOrig++;
01743         }
01744         // if the colspan is 0 only count the 1st spanned col
01745         PRBool countAsSpan = PR_FALSE;
01746         if (data->IsColSpan()) {
01747           if ( (!data->IsZeroColSpan()) ||
01748                ((data->IsZeroColSpan()) && (colX > aColIndex + totalColSpan) &&
01749                 (!IsZeroColSpan(rowX, colX - 1)))) {
01750             nsColInfo* colInfo = aMap.GetColInfoAt(colX);
01751             colInfo->mNumCellsSpan++;
01752             countAsSpan = PR_TRUE;
01753           }
01754         }
01755         
01756         // decrease the origin and span counts within the spanned cols
01757         PRInt32 colX2 = colX - totalColSpan;
01758         nsColInfo* colInfo2 = aMap.GetColInfoAt(colX2);
01759         if (data->IsOrig()) {
01760           // the old originating col of a moved cell needs adjustment
01761           colInfo2->mNumCellsOrig--;
01762         }
01763         else if (countAsSpan) {
01764           colInfo2->mNumCellsSpan--;
01765         }
01766       }
01767     }
01768   }
01769 }
01770 
01771 void nsCellMap::ShrinkWithoutRows(nsTableCellMap& aMap,
01772                                   PRInt32         aStartRowIndex,
01773                                   PRInt32         aNumRowsToRemove,
01774                                   nsRect&         aDamageArea)
01775 {
01776   PRInt32 endRowIndex = aStartRowIndex + aNumRowsToRemove - 1;
01777   PRInt32 colCount = aMap.GetColCount();
01778   for (PRInt32 rowX = endRowIndex; rowX >= aStartRowIndex; --rowX) {
01779     nsVoidArray* row = (nsVoidArray *)(mRows.ElementAt(rowX));
01780     PRInt32 colX;
01781     for (colX = 0; colX < colCount; colX++) {
01782       CellData* data = (CellData *) row->SafeElementAt(colX);
01783       if (data) {
01784         // Adjust the column counts.
01785         if (data->IsOrig()) {
01786           // Decrement the column count.
01787           nsColInfo* colInfo = aMap.GetColInfoAt(colX);
01788           colInfo->mNumCellsOrig--;
01789         }
01790         // colspan=0 is only counted as a spanned cell in the 1st col it spans
01791         else if (data->IsColSpan()) {
01792           if ( (!data->IsZeroColSpan()) ||
01793                ((data->IsZeroColSpan()) && (rowX == aStartRowIndex) && (!IsZeroColSpan(rowX, colX - 1)))) {
01794             nsColInfo* colInfo = aMap.GetColInfoAt(colX);
01795             colInfo->mNumCellsSpan--;
01796           }
01797         }
01798       }
01799     }
01800 
01801     PRInt32 rowLength = row->Count();
01802     // Delete our row information.
01803     for (colX = 0; colX < rowLength; colX++) {
01804       CellData* data = (CellData *)(row->ElementAt(colX));
01805       if (data) {
01806         delete data;
01807       }
01808     }
01809 
01810     mRows.RemoveElementAt(rowX);
01811     delete row;
01812 
01813     // Decrement our row and next available index counts.
01814     mRowCount--;
01815   }
01816   aMap.RemoveColsAtEnd();
01817 
01818   SetDamageArea(0, aStartRowIndex, aMap.GetColCount(), 0, aDamageArea); 
01819 }
01820 
01821 PRInt32 nsCellMap::GetColSpanForNewCell(nsTableCellFrame& aCellFrameToAdd, 
01822                                         PRInt32           aColIndex,
01823                                         PRInt32           aNumColsInTable,
01824                                         PRBool&           aIsZeroColSpan)
01825 {
01826   aIsZeroColSpan = PR_FALSE;
01827   PRInt32 colSpan = aCellFrameToAdd.GetColSpan();
01828   if (0 == colSpan) {
01829     // use a min value for a zero colspan to make computations easier elsewhere 
01830     colSpan = PR_MAX(MIN_NUM_COLS_FOR_ZERO_COLSPAN, aNumColsInTable - aColIndex);
01831     aIsZeroColSpan = PR_TRUE;
01832   }
01833   return colSpan;
01834 }
01835 
01836 PRInt32 nsCellMap::GetEffectiveColSpan(nsTableCellMap& aMap,
01837                                        PRInt32         aRowIndex,
01838                                        PRInt32         aColIndex,
01839                                        PRBool&         aZeroColSpan)
01840 {
01841   PRInt32 numColsInTable = aMap.GetColCount();
01842   aZeroColSpan = PR_FALSE;
01843   PRInt32 colSpan = 1;
01844   nsVoidArray* row = (nsVoidArray *)(mRows.SafeElementAt(aRowIndex));
01845   if (row) {
01846     PRInt32 colX;
01847     CellData* data;
01848     PRInt32 maxCols = numColsInTable;
01849     PRBool hitOverlap = PR_FALSE; // XXX this is not ever being set to PR_TRUE
01850     for (colX = aColIndex + 1; colX < maxCols; colX++) {
01851       data = GetDataAt(aMap, aRowIndex, colX, PR_TRUE);
01852       if (data) {
01853         // for an overlapping situation get the colspan from the originating cell and
01854         // use that as the max number of cols to iterate. Since this is rare, only 
01855         // pay the price of looking up the cell's colspan here.
01856         if (!hitOverlap && data->IsOverlap()) {
01857           CellData* origData = GetDataAt(aMap, aRowIndex, aColIndex, PR_TRUE);
01858           if (origData && origData->IsOrig()) {
01859             nsTableCellFrame* cellFrame = origData->GetCellFrame();
01860             if (cellFrame) {
01861               // possible change the number of colums to iterate
01862               maxCols = PR_MIN(aColIndex + cellFrame->GetColSpan(), maxCols);
01863               if (colX >= maxCols) 
01864                 break;
01865             }
01866           }
01867         }
01868         if (data->IsColSpan()) {
01869           colSpan++;
01870           if (data->IsZeroColSpan()) {
01871             aZeroColSpan = PR_TRUE;
01872           }
01873         }
01874         else {
01875           break;
01876         }
01877       }
01878       else break;
01879     }
01880   }
01881   return colSpan;
01882 }
01883 
01884 PRInt32 
01885 nsCellMap::GetRowSpanForNewCell(nsTableCellFrame& aCellFrameToAdd, 
01886                                 PRInt32           aRowIndex,
01887                                 PRBool&           aIsZeroRowSpan)
01888 {
01889   aIsZeroRowSpan = PR_FALSE;
01890   PRInt32 rowSpan = aCellFrameToAdd.GetRowSpan();
01891   if (0 == rowSpan) {
01892     // use a min value of 2 for a zero rowspan to make computations easier elsewhere 
01893     rowSpan = PR_MAX(2, mRows.Count() - aRowIndex);
01894     aIsZeroRowSpan = PR_TRUE;
01895   }
01896   return rowSpan;
01897 }
01898 
01899 PRBool nsCellMap::HasMoreThanOneCell(nsTableCellMap& aMap,
01900                                      PRInt32       aRowIndex)
01901 {
01902   nsVoidArray* row = (nsVoidArray *)(mRows.SafeElementAt(aRowIndex));
01903   if (row) {
01904     PRInt32 maxColIndex = row->Count();
01905     PRInt32 count = 0;
01906     PRInt32 colIndex;
01907     for (colIndex = 0; colIndex < maxColIndex; colIndex++) {
01908       CellData* cellData = GetDataAt(aMap, aRowIndex, colIndex, PR_FALSE);
01909       if (cellData && (cellData->GetCellFrame() || cellData->IsRowSpan()))
01910         count++;
01911       if (count > 1)
01912         return PR_TRUE;
01913     }
01914   }
01915   return PR_FALSE;
01916 }
01917 
01918 PRInt32 nsCellMap::GetRowSpan(nsTableCellMap& aMap,
01919                               PRInt32         aRowIndex,
01920                               PRInt32         aColIndex,
01921                               PRBool          aGetEffective,
01922                               PRBool&         aZeroRowSpan)
01923 {
01924   aZeroRowSpan = PR_FALSE;
01925   PRInt32 rowSpan = 1;
01926   PRInt32 rowCount = (aGetEffective) ? mRowCount : mRows.Count();
01927   PRInt32 rowX;
01928   for (rowX = aRowIndex + 1; rowX < rowCount; rowX++) {
01929     CellData* data = GetDataAt(aMap, rowX, aColIndex, PR_TRUE);
01930     if (data) {
01931       if (data->IsRowSpan()) {
01932         rowSpan++;
01933         if (data->IsZeroRowSpan()) {
01934           aZeroRowSpan = PR_TRUE;
01935         }
01936       }
01937       else {
01938         break;
01939       }
01940     } 
01941     else break;
01942   }
01943   if (aZeroRowSpan && (rowX < rowCount)) {
01944     rowSpan += rowCount - rowX;
01945   }
01946   return rowSpan;
01947 }
01948 
01949 void nsCellMap::ShrinkWithoutCell(nsTableCellMap&   aMap,
01950                                   nsTableCellFrame& aCellFrame,
01951                                   PRInt32           aRowIndex,
01952                                   PRInt32           aColIndex,
01953                                   nsRect&           aDamageArea)
01954 {
01955   PRInt32 colX, rowX;
01956 
01957   // get the rowspan and colspan from the cell map since the content may have changed
01958   PRBool  zeroRowSpan, zeroColSpan;
01959   PRInt32 numCols = aMap.GetColCount();
01960   PRInt32 rowSpan = GetRowSpan(aMap, aRowIndex, aColIndex, PR_FALSE, zeroRowSpan);
01961   PRInt32 colSpan = GetEffectiveColSpan(aMap, aRowIndex, aColIndex, zeroColSpan);
01962   PRInt32 endRowIndex = aRowIndex + rowSpan - 1;
01963   PRInt32 endColIndex = aColIndex + colSpan - 1;
01964 
01965   SetDamageArea(aColIndex, aRowIndex, 1 + endColIndex - aColIndex, 1 + endRowIndex - aRowIndex, aDamageArea); 
01966 
01967   // adjust the col counts due to the deleted cell before removing it
01968   for (colX = aColIndex; colX <= endColIndex; colX++) {
01969     nsColInfo* colInfo = aMap.GetColInfoAt(colX);
01970     if (colX == aColIndex) {
01971       colInfo->mNumCellsOrig--;
01972     }
01973     // a colspan=0 cell is only counted as a spanner in the 1st col it spans
01974     else if (!zeroColSpan || (zeroColSpan && (colX == aColIndex + 1))) {
01975       colInfo->mNumCellsSpan--;
01976     }
01977   }
01978 
01979   // remove the deleted cell and cellData entries for it
01980   for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) {
01981     nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rowX);
01982     for (colX = endColIndex; colX >= aColIndex; colX--) {
01983       CellData* doomedData = (CellData*) row->ElementAt(colX);
01984       delete doomedData;
01985       row->RemoveElementAt(colX);
01986     }
01987   }
01988 
01989   numCols = aMap.GetColCount();
01990 
01991   // update the row and col info due to shifting
01992   for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) {
01993     nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rowX);
01994     PRInt32 rowCount = row->Count();
01995     for (colX = aColIndex; colX < numCols - colSpan; colX++) {
01996       CellData* data = (colX < rowCount) ? (CellData*)row->ElementAt(colX) : nsnull;
01997       if (data) {
01998         if (data->IsOrig()) {
01999           // a cell that gets moved to the left needs adjustment in its new location 
02000           data->GetCellFrame()->SetColIndex(colX);
02001           nsColInfo* colInfo = aMap.GetColInfoAt(colX);
02002           colInfo->mNumCellsOrig++;
02003           // a cell that gets moved to the left needs adjustment in its old location 
02004           colInfo = aMap.GetColInfoAt(colX + colSpan);
02005           if (colInfo) {
02006             colInfo->mNumCellsOrig--;
02007           }
02008         }
02009         // a colspan=0 cell is only counted as a spanner in the 1st col it spans
02010         else if (data->IsColSpan()) {
02011           if ( (!data->IsZeroColSpan()) ||
02012                ((data->IsZeroColSpan()) && (rowX == aRowIndex) && (!IsZeroColSpan(rowX, colX - 1)))) {
02013             // a cell that gets moved to the left needs adjustment in its new location 
02014             nsColInfo* colInfo = aMap.GetColInfoAt(colX);
02015             colInfo->mNumCellsSpan++;
02016             // a cell that gets moved to the left needs adjustment in its old location 
02017             colInfo = aMap.GetColInfoAt(colX + colSpan);
02018             if (colInfo) {
02019               colInfo->mNumCellsSpan--;
02020             }
02021           }
02022         }
02023       }
02024     }
02025   }
02026   aMap.RemoveColsAtEnd();
02027 }
02028 
02029 void
02030 nsCellMap::RebuildConsideringRows(nsTableCellMap& aMap,
02031                                   PRInt32         aStartRowIndex,
02032                                   nsVoidArray*    aRowsToInsert,
02033                                   PRBool          aNumRowsToRemove,
02034                                   nsRect&         aDamageArea)
02035 {
02036   // copy the old cell map into a new array
02037   PRInt32 numOrigRows = mRows.Count();
02038   void** origRows = new void*[numOrigRows];
02039   if (!origRows) return;
02040   PRInt32 rowX, colX;
02041   // copy the orig rows
02042   for (rowX = 0; rowX < numOrigRows; rowX++) {
02043     nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rowX);
02044     origRows[rowX] = row;
02045   }
02046 
02047   mRows.Clear();
02048   // adjust mRowCount based on the function arguments as they are known to
02049   // be real rows.
02050   mRowCount -= aNumRowsToRemove;
02051   if (aRowsToInsert) { 
02052     mRowCount += aRowsToInsert->Count();
02053     Grow(aMap, numOrigRows); 
02054   }
02055 
02056   // aStartRowIndex might be after all existing rows so we should limit the
02057   // copy to the amount of exisiting rows
02058   PRInt32 copyEndRowIndex = PR_MIN(numOrigRows, aStartRowIndex);
02059 
02060   // put back the rows before the affected ones just as before
02061   for (rowX = 0; rowX < copyEndRowIndex; rowX++) {
02062     nsVoidArray* row = (nsVoidArray *)origRows[rowX];
02063     PRInt32 numCols = row->Count();
02064     for (colX = 0; colX < numCols; colX++) {
02065       // put in the original cell from the cell map
02066       CellData* data = (CellData*) row->ElementAt(colX);
02067       if (data && data->IsOrig()) {
02068         AppendCell(aMap, data->GetCellFrame(), rowX, PR_FALSE, aDamageArea);
02069       }
02070     }
02071   }
02072   PRInt32 copyStartRowIndex;
02073   if (aRowsToInsert) {
02074     // add in the new cells and create rows if necessary
02075     PRInt32 numNewRows = aRowsToInsert->Count();
02076     rowX = aStartRowIndex;
02077     for (PRInt32 newRowX = 0; newRowX < numNewRows; newRowX++) {
02078       nsTableRowFrame* rFrame = (nsTableRowFrame *)aRowsToInsert->ElementAt(newRowX);
02079       nsIFrame* cFrame = rFrame->GetFirstChild(nsnull);
02080       while (cFrame) {
02081         if (IS_TABLE_CELL(cFrame->GetType())) {
02082           AppendCell(aMap, (nsTableCellFrame *)cFrame, rowX, PR_FALSE, aDamageArea);
02083         }
02084         cFrame = cFrame->GetNextSibling();
02085       }
02086       rowX++;
02087     }
02088     copyStartRowIndex = aStartRowIndex;
02089   }
02090   else {
02091     rowX = aStartRowIndex;
02092     copyStartRowIndex = aStartRowIndex + aNumRowsToRemove;
02093   }
02094   // put back the rows after the affected ones just as before
02095   for (PRInt32 copyRowX = copyStartRowIndex; copyRowX < numOrigRows; copyRowX++) {
02096     nsVoidArray* row = (nsVoidArray *)origRows[copyRowX];
02097     PRInt32 numCols = row->Count();
02098     for (colX = 0; colX < numCols; colX++) {
02099       // put in the original cell from the cell map
02100       CellData* data = (CellData*) row->ElementAt(colX);
02101       if (data && data->IsOrig()) {
02102         AppendCell(aMap, data->GetCellFrame(), rowX, PR_FALSE, aDamageArea);
02103       }
02104     }
02105     rowX++;
02106   }
02107 
02108   // delete the old cell map
02109   for (rowX = 0; rowX < numOrigRows; rowX++) {
02110     nsVoidArray* row = (nsVoidArray *)origRows[rowX];
02111     PRInt32 len = row->Count();
02112     for (colX = 0; colX < len; colX++) {
02113       CellData* data = (CellData*) row->ElementAt(colX);
02114       delete data;
02115     }
02116     delete row;
02117   }
02118   delete [] origRows;
02119 
02120   SetDamageArea(0, 0, aMap.GetColCount(), GetRowCount(), aDamageArea); 
02121 }
02122 
02123 void nsCellMap::RebuildConsideringCells(nsTableCellMap& aMap,
02124                                         PRInt32         aNumOrigCols,
02125                                         nsVoidArray*    aCellFrames,
02126                                         PRInt32         aRowIndex,
02127                                         PRInt32         aColIndex,
02128                                         PRBool          aInsert,
02129                                         nsRect&         aDamageArea)
02130 {
02131   // copy the old cell map into a new array
02132   PRInt32 mRowCountOrig = mRowCount;
02133   PRInt32 numOrigRows   = mRows.Count();
02134   void** origRows = new void*[numOrigRows];
02135   if (!origRows) return;
02136   PRInt32 rowX;
02137   for (rowX = 0; rowX < numOrigRows; rowX++) {
02138     nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rowX);
02139     origRows[rowX] = row;
02140   }
02141 
02142   // reinitialize data members
02143   mRows.Clear();
02144   mRowCount = 0;
02145 
02146   PRInt32 numNewCells = (aCellFrames) ? aCellFrames->Count() : 0;
02147   
02148   // the new cells might extend the previous column number
02149   NS_ASSERTION(aNumOrigCols >= aColIndex, "Appending cells far beyond cellmap data?!");
02150   PRInt32 numCols = aInsert ? PR_MAX(aNumOrigCols, aColIndex + 1) : aNumOrigCols;  
02151   
02152   // build the new cell map 
02153   for (rowX = 0; rowX < numOrigRows; rowX++) {
02154     nsVoidArray* row = (nsVoidArray *)origRows[rowX];
02155     for (PRInt32 colX = 0; colX < numCols; colX++) {
02156       if ((rowX == aRowIndex) && (colX == aColIndex)) { 
02157         if (aInsert) { // put in the new cells
02158           for (PRInt32 cellX = 0; cellX < numNewCells; cellX++) {
02159             nsTableCellFrame* cell = (nsTableCellFrame*)aCellFrames->ElementAt(cellX);
02160             if (cell) {
02161               AppendCell(aMap, cell, rowX, PR_FALSE, aDamageArea);
02162             }
02163           }
02164         }
02165         else {
02166           continue; // do not put the deleted cell back
02167         }
02168       }
02169       // put in the original cell from the cell map
02170       CellData* data = (CellData*) row->SafeElementAt(colX);
02171       if (data && data->IsOrig()) {
02172         AppendCell(aMap, data->GetCellFrame(), rowX, PR_FALSE, aDamageArea);
02173       }
02174     }
02175   }
02176 
02177   if (aInsert && numOrigRows <= aRowIndex) { // append the new cells 
02178                                              // below the last original row
02179     NS_ASSERTION (numOrigRows == aRowIndex, 
02180                   "Appending cells far beyond the last row");
02181     for (PRInt32 cellX = 0; cellX < numNewCells; cellX++) {
02182       nsTableCellFrame* cell = (nsTableCellFrame*)aCellFrames->ElementAt(cellX);
02183       if (cell) {
02184         AppendCell(aMap, cell, aRowIndex, PR_FALSE, aDamageArea);
02185       }
02186     }
02187   }
02188   // For for cell deletion, since the row is not being deleted, 
02189   // keep mRowCount the same as before. 
02190   mRowCount = PR_MAX(mRowCount, mRowCountOrig);
02191 
02192   // delete the old cell map
02193   for (rowX = 0; rowX < numOrigRows; rowX++) {
02194     nsVoidArray* row = (nsVoidArray *)origRows[rowX];
02195     PRInt32 len = row->Count();
02196     for (PRInt32 colX = 0; colX < len; colX++) {
02197       CellData* data = (CellData*) row->SafeElementAt(colX);
02198       if(data)
02199         delete data;
02200     }
02201     delete row;
02202   }
02203   delete [] origRows; 
02204 }
02205 
02206 void nsCellMap::RemoveCell(nsTableCellMap&   aMap,
02207                            nsTableCellFrame* aCellFrame,
02208                            PRInt32           aRowIndex,
02209                            nsRect&           aDamageArea)
02210 {
02211   PRInt32 numRows = mRows.Count();
02212   if ((aRowIndex < 0) || (aRowIndex >= numRows)) {
02213     NS_ASSERTION(PR_FALSE, "bad arg in nsCellMap::RemoveCell");
02214     return;
02215   }
02216   PRInt32 numCols = aMap.GetColCount();
02217 
02218   // get the starting col index of the cell to remove
02219   PRInt32 startColIndex;
02220   for (startColIndex = 0; startColIndex < numCols; startColIndex++) {
02221     CellData* data = GetDataAt(aMap, aRowIndex, startColIndex, PR_FALSE);
02222     if (data && (data->IsOrig()) && (aCellFrame == data->GetCellFrame())) {
02223       break; // we found the col index
02224     }
02225   }
02226 
02227   PRBool isZeroRowSpan;
02228   PRInt32 rowSpan = GetRowSpan(aMap, aRowIndex, startColIndex, PR_FALSE, isZeroRowSpan);
02229   // record whether removing the cells is going to cause complications due 
02230   // to existing row spans, col spans or table sizing. 
02231   PRBool spansCauseRebuild = CellsSpanInOrOut(aMap, aRowIndex, aRowIndex + rowSpan - 1, 
02232                                               startColIndex, numCols - 1);
02233   // XXX if the cell has a col span to the end of the map, and the end has no originating 
02234   // cells, we need to assume that this the only such cell, and rebuild so that there are 
02235   // no extraneous cols at the end. The same is true for removing rows.
02236   if (!aCellFrame->GetRowSpan() || !aCellFrame->GetColSpan())
02237     spansCauseRebuild = PR_TRUE;
02238 
02239   if (spansCauseRebuild) {
02240     aMap.RebuildConsideringCells(this, nsnull, aRowIndex, startColIndex, PR_FALSE, aDamageArea);
02241   }
02242   else {
02243     ShrinkWithoutCell(aMap, *aCellFrame, aRowIndex, startColIndex, aDamageArea);
02244   }
02245 }
02246 
02247 #ifdef NS_DEBUG
02248 void nsCellMap::Dump(PRBool aIsBorderCollapse) const
02249 {
02250   printf("\n  ***** START GROUP CELL MAP DUMP ***** %p\n", this);
02251   nsTableRowGroupFrame* rg = GetRowGroup();
02252   const nsStyleDisplay* display = rg->GetStyleDisplay();
02253   switch (display->mDisplay) {
02254   case NS_STYLE_DISPLAY_TABLE_HEADER_GROUP:
02255     printf("  thead ");
02256     break;
02257   case NS_STYLE_DISPLAY_TABLE_FOOTER_GROUP:
02258     printf("  tfoot ");
02259     break;
02260   case NS_STYLE_DISPLAY_TABLE_ROW_GROUP:
02261     printf("  tbody ");
02262     break;
02263   default:
02264     printf("HUH? wrong display type on rowgroup");
02265   }
02266   PRInt32 mapRowCount = mRows.Count();
02267   printf("mapRowCount=%d tableRowCount=%d\n", mapRowCount, mRowCount);
02268   
02269 
02270   PRInt32 rowIndex, colIndex;
02271   for (rowIndex = 0; rowIndex < mapRowCount; rowIndex++) {
02272     nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rowIndex);
02273     printf("  row %d : ", rowIndex);
02274     PRInt32 colCount = row->Count();
02275     for (colIndex = 0; colIndex < colCount; colIndex++) {
02276       CellData* cd = (CellData *)row->ElementAt(colIndex);
02277       if (cd) {
02278         if (cd->IsOrig()) {
02279           printf("C%d,%d  ", rowIndex, colIndex);
02280         } else {
02281           nsTableCellFrame* cell = nsnull;
02282           if (cd->IsRowSpan()) {
02283             cell = GetCellFrame(rowIndex, colIndex, *cd, PR_TRUE);
02284             printf("R ");
02285           }
02286           if (cd->IsColSpan()) {
02287             cell = GetCellFrame(rowIndex, colIndex, *cd, PR_FALSE);
02288             printf("C ");
02289           }
02290           if (!(cd->IsRowSpan() && cd->IsColSpan())) {
02291             printf("  ");
02292           } 
02293           printf("  ");  
02294         }
02295       } else {
02296         printf("----  ");
02297       }
02298     }
02299     if (aIsBorderCollapse) {
02300       nscoord       size;
02301       BCBorderOwner owner;
02302       PRUint8       side;
02303       PRBool        segStart;
02304       PRPackedBool  bevel;      
02305       for (PRInt32 i = 0; i <= 2; i++) {
02306         printf("\n          ");
02307         for (colIndex = 0; colIndex < colCount; colIndex++) {
02308           BCCellData* cd = (BCCellData *)row->ElementAt(colIndex);
02309           if (cd) {
02310             if (0 == i) {
02311               size = cd->mData.GetTopEdge(owner, segStart);
02312               printf("t=%d%d%d ", size, owner, segStart);
02313             }
02314             else if (1 == i) {
02315               size = cd->mData.GetLeftEdge(owner, segStart);
02316               printf("l=%d%d%d ", size, owner, segStart);
02317             }
02318             else {
02319               size = cd->mData.GetCorner(side, bevel);
02320               printf("c=%d%d%d ", size, side, bevel);
02321             }
02322           }
02323         }
02324       }
02325     }
02326     printf("\n");
02327   }
02328 
02329   // output info mapping Ci,j to cell address
02330   PRInt32 cellCount = 0;
02331   for (PRInt32 rIndex = 0; rIndex < mapRowCount; rIndex++) {
02332     nsVoidArray* row = (nsVoidArray *)mRows.ElementAt(rIndex);
02333     PRInt32 colCount = row->Count();
02334     printf("  ");
02335     for (colIndex = 0; colIndex < colCount; colIndex++) {
02336       CellData* cd = (CellData *)row->ElementAt(colIndex);
02337       if (cd) {
02338         if (cd->IsOrig()) {
02339           nsTableCellFrame* cellFrame = cd->GetCellFrame();
02340           PRInt32 cellFrameColIndex;
02341           cellFrame->GetColIndex(cellFrameColIndex);
02342           printf("C%d,%d=%p(%d)  ", rIndex, colIndex, cellFrame, cellFrameColIndex);
02343           cellCount++;
02344         }
02345       }
02346     }
02347     printf("\n");
02348   }
02349 
02350   printf("  ***** END GROUP CELL MAP DUMP *****\n");
02351 }
02352 #endif
02353 
02354 PRBool 
02355 nsCellMap::IsZeroColSpan(PRInt32 aRowIndex,
02356                          PRInt32 aColIndex) const
02357 {
02358   nsVoidArray* row = (nsVoidArray*)mRows.SafeElementAt(aRowIndex);
02359   if (row) {
02360     CellData* data = (CellData*)row->SafeElementAt(aColIndex);
02361     if (data && data->IsZeroColSpan()) {
02362       return PR_TRUE;
02363     }
02364   }
02365   return PR_FALSE;
02366 }
02367 
02368 void 
02369 nsCellMap::AdjustForZeroSpan(nsTableCellMap& aMap,
02370                              PRInt32         aRowIndex,
02371                              PRInt32         aColIndex)
02372 {
02373   PRInt32 numColsInTable = aMap.GetColCount();
02374   CellData* data = GetDataAt(aMap, aRowIndex, aColIndex, PR_FALSE);
02375   if (!data) return;
02376 
02377   nsTableCellFrame* cell = (data->IsOrig()) ? data->GetCellFrame() : nsnull;
02378   if (!cell) return;
02379 
02380   PRInt32 cellRowSpan = cell->GetRowSpan();
02381   PRInt32 cellColSpan = cell->GetColSpan();
02382 
02383   PRInt32 endRowIndex = (0 == cell->GetRowSpan()) ? mRows.Count() - 1 : aRowIndex + cellRowSpan - 1;
02384   PRInt32 endColIndex = (0 == cell->GetColSpan()) ? numColsInTable - 1 : aColIndex + cellColSpan - 1;
02385   // if there is both a rowspan=0 and colspan=0 then only expand the cols to a minimum
02386   if ((0 == cellRowSpan) && (0 == cellColSpan)) {
02387     endColIndex = aColIndex + MIN_NUM_COLS_FOR_ZERO_COLSPAN - 1;
02388   }
02389 
02390   // Create span CellData objects filling out the rows to the end of the
02391   // map if the rowspan is 0, and/or filling out the cols to the end of 
02392   // table if the colspan is 0. If there is both a rowspan=0 and colspan=0
02393   // then only fill out the cols to a minimum value.
02394   for (PRInt32 colX = aColIndex; colX <= endColIndex; colX++) {
02395     PRInt32 rowX;
02396     // check to see if there is any cells originating after the cols 
02397     PRBool cellsOrig = PR_FALSE;
02398     if (colX >= aColIndex + MIN_NUM_COLS_FOR_ZERO_COLSPAN - 1) {
02399       for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) {
02400         CellData* cellData = GetDataAt(aMap, rowX, colX, PR_FALSE);
02401         if (cellData && cellData->IsOrig()) {
02402           cellsOrig = PR_TRUE;
02403           break; // there are cells in this col, so don't consider it
02404         }
02405       }
02406     }
02407     if (cellsOrig) break;
02408     for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) {
02409       if ((colX > aColIndex) || (rowX > aRowIndex)) {
02410         CellData* oldData = GetDataAt(aMap, rowX, colX, PR_FALSE); 
02411         if (!oldData) {
02412           CellData* newData = (aMap.mBCInfo) ? new BCCellData(nsnull) : new CellData(nsnull);
02413           if (!newData) return;
02414           if (colX > aColIndex) {
02415             newData->SetColSpanOffset(colX - aColIndex);
02416             newData->SetZeroColSpan(PR_TRUE);
02417           }
02418           if (rowX > aRowIndex) {
02419             newData->SetRowSpanOffset(rowX - aRowIndex);
02420             newData->SetZeroRowSpan(PR_TRUE);
02421           }
02422           // colspan=0 is only counted as spanning the 1st col to the right of its origin
02423           SetDataAt(aMap, *newData, rowX, colX, (colX == aColIndex + 1));
02424         }
02425       }
02426     }
02427   }
02428 }
02429 
02430 CellData* 
02431 nsCellMap::GetDataAt(nsTableCellMap& aMap,
02432                      PRInt32         aMapRowIndex, 
02433                      PRInt32         aColIndex,
02434                      PRBool          aUpdateZeroSpan)
02435 {
02436   PRInt32 numColsInTable = aMap.GetColCount();
02437   if ((aMapRowIndex < 0) || (aMapRowIndex >= mRows.Count())) {
02438     return nsnull;
02439   }
02440 
02441   nsVoidArray* row = (nsVoidArray *)(mRows.ElementAt(aMapRowIndex));
02442   if (!row) return nsnull;
02443 
02444   CellData* data = (CellData *)(row->SafeElementAt(aColIndex));
02445   if (!data && aUpdateZeroSpan) {
02446     PRBool didZeroExpand = PR_FALSE;
02447     // check for special zero row span
02448     PRInt32 prevRowX = aMapRowIndex - 1;
02449     // find the last non null data in the same col
02450     for ( ; prevRowX > 0; prevRowX--) {
02451       nsVoidArray* prevRow = (nsVoidArray *)(mRows.ElementAt(prevRowX));
02452       CellData* prevData = (CellData *)(prevRow->SafeElementAt(aColIndex));
02453       if (prevData) {
02454         if (prevData->IsZeroRowSpan()) {
02455           PRInt32 rowIndex = prevRowX - prevData->GetRowSpanOffset();
02456           PRInt32 colIndex = 0;
02457           // if there is a colspan and no overlap then the rowspan offset 
02458           // and colspan offset point to the same cell
02459           if ((prevData->IsColSpan()) && (!prevData->IsOverlap())) {
02460             colIndex = prevData->GetColSpanOffset();
02461           }
02462           AdjustForZeroSpan(aMap, rowIndex, colIndex);
02463           didZeroExpand = PR_TRUE;
02464         }
02465         break;
02466       }
02467     }
02468 
02469     // check for special zero col span
02470     if (!didZeroExpand && (aColIndex > 0) && (aColIndex < numColsInTable)) { 
02471       PRInt32 prevColX = aColIndex - 1;
02472       // find the last non null data in the same row
02473       for ( ; prevColX > 0; prevColX--) {
02474         CellData* prevData = GetDataAt(aMap, aMapRowIndex, prevColX, PR_FALSE);
02475         if (prevData) {
02476           if (prevData->IsZeroColSpan()) {
02477             PRInt32 colIndex = prevColX - prevData->GetColSpanOffset();
02478             // if there were also a rowspan, it would have been handled above
02479             AdjustForZeroSpan(aMap, aMapRowIndex, colIndex);
02480             didZeroExpand = PR_TRUE;
02481           }
02482           break;
02483         }
02484       }
02485     }
02486 
02487     // if zero span adjustments were made the data may be available now
02488     if (!data && didZeroExpand) {
02489       data = GetDataAt(aMap, aMapRowIndex, aColIndex, PR_FALSE);
02490     }
02491   }
02492   return data;
02493 }
02494 
02495 // only called if the cell at aMapRowIndex, aColIndex is null
02496 void nsCellMap::SetDataAt(nsTableCellMap& aMap,
02497                           CellData&       aNewCell, 
02498                           PRInt32         aMapRowIndex, 
02499                           PRInt32         aColIndex,
02500                           PRBool          aCountZeroSpanAsSpan)
02501 {
02502   nsVoidArray* row = (nsVoidArray *)(mRows.SafeElementAt(aMapRowIndex));
02503   if (row) {
02504     // the table map may need cols added
02505     PRInt32 numColsToAdd = aColIndex + 1 - aMap.GetColCount();
02506     if (numColsToAdd > 0) {
02507       aMap.AddColsAtEnd(numColsToAdd);
02508     }
02509     // the row may need cols added
02510     numColsToAdd = aColIndex + 1 - row->Count();
02511     if (numColsToAdd > 0) {
02512       GrowRow(*row, numColsToAdd);
02513     }
02514 
02515     CellData* doomedData = (CellData*)row->ElementAt(aColIndex);
02516     delete doomedData;
02517 
02518     row->ReplaceElementAt(&aNewCell, aColIndex);
02519     // update the originating cell counts if cell originates in this row, col
02520     nsColInfo* colInfo = aMap.GetColInfoAt(aColIndex);
02521     if (colInfo) {
02522       if (aNewCell.IsOrig()) { 
02523         colInfo->mNumCellsOrig++;
02524       }
02525       else if ((aNewCell.IsColSpan()) && 
02526                (!aNewCell.IsZeroColSpan() || aCountZeroSpanAsSpan)) {
02527         colInfo->mNumCellsSpan++;
02528       }
02529     }
02530     else NS_ASSERTION(PR_FALSE, "SetDataAt called with col index > table map num cols");
02531   }
02532   else NS_ASSERTION(PR_FALSE, "SetDataAt called with row index > num rows");
02533 }
02534 
02535 nsTableCellFrame* 
02536 nsCellMap::GetCellInfoAt(nsTableCellMap& aMap,
02537                          PRInt32         aRowX, 
02538                          PRInt32         aColX, 
02539                          PRBool*         aOriginates, 
02540                          PRInt32*        aColSpan)
02541 {
02542   if (aOriginates) {
02543     *aOriginates = PR_FALSE;
02544   }
02545   CellData* data = GetDataAt(aMap, aRowX, aColX, PR_TRUE);
02546   nsTableCellFrame* cellFrame = nsnull;  
02547   if (data) {
02548     if (data->IsOrig()) {
02549       cellFrame = data->GetCellFrame();
02550       if (aOriginates)
02551         *aOriginates = PR_TRUE;
02552       if (cellFrame && aColSpan) {
02553         PRInt32 initialColIndex;
02554         cellFrame->GetColIndex(initialColIndex);
02555         PRBool zeroSpan;
02556         *aColSpan = GetEffectiveColSpan(aMap, aRowX, initialColIndex, zeroSpan);
02557       }
02558     }
02559     else {
02560       cellFrame = GetCellFrame(aRowX, aColX, *data, PR_TRUE);
02561       if (aColSpan)
02562         *aColSpan = 0;
02563     }
02564   }
02565   return cellFrame;
02566 }
02567   
02568 
02569 PRBool nsCellMap::RowIsSpannedInto(nsTableCellMap& aMap,
02570                                    PRInt32         aRowIndex)
02571 {
02572   PRInt32 numColsInTable = aMap.GetColCount();
02573   if ((0 > aRowIndex) || (aRowIndex >= mRowCount)) {
02574     return PR_FALSE;
02575   }
02576   for (PRInt32 colIndex = 0; colIndex < numColsInTable; colIndex++) {
02577     CellData* cd = GetDataAt(aMap, aRowIndex, colIndex, PR_TRUE);
02578     if (cd) { // there's really a cell at (aRowIndex, colIndex)
02579       if (cd->IsSpan()) { // the cell at (aRowIndex, colIndex) is the result of a span
02580         if (cd->IsRowSpan() && GetCellFrame(aRowIndex, colIndex, *cd, PR_TRUE)) { // XXX why the last check
02581           return PR_TRUE;
02582         }
02583       }
02584     }
02585   }
02586   return PR_FALSE;
02587 }
02588 
02589 PRBool nsCellMap::RowHasSpanningCells(nsTableCellMap& aMap,
02590                                       PRInt32         aRowIndex)
02591 {
02592   PRInt32 numColsInTable = aMap.GetColCount();
02593   if ((0 > aRowIndex) || (aRowIndex >= mRowCount)) {
02594     return PR_FALSE;
02595   }
02596   if (aRowIndex != mRowCount - 1) {
02597     // aRowIndex is not the last row, so we check the next row after aRowIndex for spanners
02598     for (PRInt32 colIndex = 0; colIndex < numColsInTable; colIndex++) {
02599       CellData* cd = GetDataAt(aMap, aRowIndex, colIndex, PR_TRUE);
02600       if (cd && (cd->IsOrig())) { // cell originates 
02601         CellData* cd2 = GetDataAt(aMap, aRowIndex + 1, colIndex, PR_TRUE);
02602         if (cd2 && cd2->IsRowSpan()) { // cd2 is spanned by a row
02603           if (cd->GetCellFrame() == GetCellFrame(aRowIndex + 1, colIndex, *cd2, PR_TRUE)) {
02604             return PR_TRUE;
02605           }
02606         }
02607       }
02608     }
02609   }
02610   return PR_FALSE;
02611 }
02612 
02613 PRBool nsCellMap::ColHasSpanningCells(nsTableCellMap& aMap,
02614                                       PRInt32         aColIndex)
02615 {
02616   PRInt32 numColsInTable = aMap.GetColCount();
02617   NS_PRECONDITION (aColIndex < numColsInTable, "bad col index arg");
02618   if ((0 > aColIndex) || (aColIndex >= numColsInTable - 1)) 
02619     return PR_FALSE;
02620  
02621   for (PRInt32 rowIndex = 0; rowIndex < mRowCount; rowIndex++) {
02622     CellData* cd = GetDataAt(aMap, rowIndex, aColIndex, PR_TRUE);
02623     if (cd && (cd->IsOrig())) { // cell originates 
02624       CellData* cd2 = GetDataAt(aMap, rowIndex, aColIndex +1, PR_TRUE);
02625       if (cd2 && cd2->IsColSpan()) { // cd2 is spanned by a col
02626         if (cd->GetCellFrame() == GetCellFrame(rowIndex , aColIndex + 1, *cd2, PR_FALSE)) {
02627           return PR_TRUE;
02628         }
02629       }
02630     }
02631   }
02632   return PR_FALSE;
02633 }