1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
43 #include "nsTPtrArray.h"
46 #include "nsAlgorithm.h"
47 #include "nsAutoPtr.h"
49 #undef DEBUG_TABLE_CELLMAP
51 class nsTableColFrame
;
52 class nsTableCellFrame
;
53 class nsTableRowFrame
;
54 class nsTableRowGroupFrame
;
58 class nsCellMapColumnIterator
;
62 PRInt32 mNumCellsOrig
; // number of cells originating in the col
63 PRInt32 mNumCellsSpan
; // number of cells spanning into the col via colspans (not rowspans)
66 nsColInfo(PRInt32 aNumCellsOrig
,
67 PRInt32 aNumCellsSpan
);
80 nsTArray
<BCData
> mRightBorders
;
81 nsTArray
<BCData
> mBottomBorders
;
82 BCData mLowerRightCorner
;
88 nsTableCellMap(nsTableFrame
& aTableFrame
,
89 PRBool aBorderCollapse
);
92 * NOT VIRTUAL BECAUSE THIS CLASS SHOULD **NEVER** BE SUBCLASSED
96 void RemoveGroupCellMap(nsTableRowGroupFrame
* aRowGroup
);
98 void InsertGroupCellMap(nsTableRowGroupFrame
* aNewRowGroup
,
99 nsTableRowGroupFrame
*& aPrevRowGroup
);
102 * Get the nsCellMap for the given row group. If aStartHint is non-null,
103 * will start looking with that cellmap and only fall back to starting at the
104 * beginning of the list if that doesn't find us the right nsCellMap.
105 * Otherwise, just start at the beginning.
107 * aRowGroup must not be null.
109 nsCellMap
* GetMapFor(const nsTableRowGroupFrame
* aRowGroup
,
110 nsCellMap
* aStartHint
) const;
112 /** synchronize the cellmaps with the rowgroups again **/
113 void Synchronize(nsTableFrame
* aTableFrame
);
115 nsTableCellFrame
* GetCellFrame(PRInt32 aRowIndex
,
118 PRBool aUseRowIfOverlap
) const;
120 /** return the CellData for the cell at (aRowIndex, aColIndex) */
121 CellData
* GetDataAt(PRInt32 aRowIndex
,
122 PRInt32 aColIndex
) const;
124 // this function creates a col if needed
125 nsColInfo
* GetColInfoAt(PRInt32 aColIndex
);
127 /** append the cellFrame at the end of the row at aRowIndex and return the col index
129 CellData
* AppendCell(nsTableCellFrame
& aCellFrame
,
131 PRBool aRebuildIfNecessary
,
132 nsRect
& aDamageArea
);
134 void InsertCells(nsTArray
<nsTableCellFrame
*>& aCellFrames
,
136 PRInt32 aColIndexBefore
,
137 nsRect
& aDamageArea
);
139 void RemoveCell(nsTableCellFrame
* aCellFrame
,
141 nsRect
& aDamageArea
);
142 /** Remove the previously gathered column information */
144 void InsertRows(nsTableRowGroupFrame
* aRowGroup
,
145 nsTArray
<nsTableRowFrame
*>& aRows
,
146 PRInt32 aFirstRowIndex
,
147 PRBool aConsiderSpans
,
148 nsRect
& aDamageArea
);
150 void RemoveRows(PRInt32 aFirstRowIndex
,
151 PRInt32 aNumRowsToRemove
,
152 PRBool aConsiderSpans
,
153 nsRect
& aDamageArea
);
155 PRInt32
GetNumCellsOriginatingInRow(PRInt32 aRowIndex
) const;
156 PRInt32
GetNumCellsOriginatingInCol(PRInt32 aColIndex
) const;
158 /** indicate whether the row has more than one cell that either originates
159 * or is spanned from the rows above
161 PRBool
HasMoreThanOneCell(PRInt32 aRowIndex
) const;
163 PRInt32
GetEffectiveRowSpan(PRInt32 aRowIndex
,
164 PRInt32 aColIndex
) const;
165 PRInt32
GetEffectiveColSpan(PRInt32 aRowIndex
,
166 PRInt32 aColIndex
) const;
168 /** return the total number of columns in the table represented by this CellMap */
169 PRInt32
GetColCount() const;
171 /** return the actual number of rows in the table represented by this CellMap */
172 PRInt32
GetRowCount() const;
174 nsTableCellFrame
* GetCellInfoAt(PRInt32 aRowX
,
176 PRBool
* aOriginates
= nsnull
,
177 PRInt32
* aColSpan
= nsnull
) const;
180 * Returns the index at the given row and column coordinates.
182 * @see nsITableLayout::GetIndexByRowAndColumn()
184 * @param aRow [in] the row coordinate
185 * @param aColumn [in] the column coordinate
186 * @returns the index for the cell
188 PRInt32
GetIndexByRowAndColumn(PRInt32 aRow
, PRInt32 aColumn
) const;
191 * Retrieves the row and column coordinates for the given index.
193 * @see nsITableLayout::GetRowAndColumnByIndex()
195 * @param aIndex [in] the index for which coordinates are to be retrieved
196 * @param aRow [out] the row coordinate to be returned
197 * @param aColumn [out] the column coordinate to be returned
199 void GetRowAndColumnByIndex(PRInt32 aIndex
,
200 PRInt32
*aRow
, PRInt32
*aColumn
) const;
202 void AddColsAtEnd(PRUint32 aNumCols
);
203 void RemoveColsAtEnd();
205 PRBool
RowIsSpannedInto(PRInt32 aRowIndex
, PRInt32 aNumEffCols
) const;
206 PRBool
RowHasSpanningCells(PRInt32 aRowIndex
, PRInt32 aNumEffCols
) const;
207 void RebuildConsideringCells(nsCellMap
* aCellMap
,
208 nsTArray
<nsTableCellFrame
*>* aCellFrames
,
212 nsRect
& aDamageArea
);
216 * Rebuild due to rows being inserted or deleted with cells spanning
217 * into or out of the rows. This function can only handle insertion
218 * or deletion but NOT both. So either aRowsToInsert must be null
219 * or aNumRowsToRemove must be 0.
221 * // XXXbz are both allowed to happen? That'd be a no-op...
223 void RebuildConsideringRows(nsCellMap
* aCellMap
,
224 PRInt32 aStartRowIndex
,
225 nsTArray
<nsTableRowFrame
*>* aRowsToInsert
,
226 PRInt32 aNumRowsToRemove
,
227 nsRect
& aDamageArea
);
230 void ExpandZeroColSpans();
232 void SetNotTopStart(PRUint8 aSide
,
236 PRBool aIsLowerRight
= PR_FALSE
);
238 void SetBCBorderEdge(mozilla::css::Side aEdge
,
240 PRUint32 aCellMapStart
,
244 BCBorderOwner aOwner
,
248 void SetBCBorderCorner(Corner aCorner
,
250 PRUint32 aCellMapStart
,
253 mozilla::css::Side aOwner
,
256 PRBool aIsBottomRight
= PR_FALSE
);
258 /** dump a representation of the cell map to stdout for debugging */
260 void Dump(char* aString
= nsnull
) const;
264 BCData
* GetRightMostBorder(PRInt32 aRowIndex
);
265 BCData
* GetBottomMostBorder(PRInt32 aColIndex
);
267 friend class nsCellMap
;
268 friend class BCMapCellIterator
;
269 friend class BCPaintBorderIterator
;
270 friend class nsCellMapColumnIterator
;
272 /** Insert a row group cellmap after aPrevMap, if aPrefMap is null insert it
273 * at the beginning, the ordering of the cellmap corresponds to the ordering of
274 * rowgroups once OrderRowGroups has been called
276 void InsertGroupCellMap(nsCellMap
* aPrevMap
,
278 void DeleteRightBottomBorders();
280 nsTableFrame
& mTableFrame
;
281 nsAutoTArray
<nsColInfo
, 8> mCols
;
282 nsCellMap
* mFirstMap
;
283 // border collapsing info
287 /** nsCellMap is a support class for nsTablePart.
288 * It maintains an Rows x Columns grid onto which the cells of the table are mapped.
289 * This makes processing of rowspan and colspan attributes much easier.
290 * Each cell is represented by a CellData object.
293 * @see nsTableFrame::AddCellToMap
294 * @see nsTableFrame::GrowCellMap
295 * @see nsTableFrame::BuildCellIntoMap
297 * mRows is an array of rows. Each row is an array of cells. a cell
304 * @param aRowGroupFrame the row group frame this is a cellmap for
305 * @param aIsBC whether the table is doing border-collapse
307 nsCellMap(nsTableRowGroupFrame
* aRowGroupFrame
, PRBool aIsBC
);
310 * NOT VIRTUAL BECAUSE THIS CLASS SHOULD **NEVER** BE SUBCLASSED
314 static nsresult
Init();
315 static void Shutdown();
317 nsCellMap
* GetNextSibling() const;
318 void SetNextSibling(nsCellMap
* aSibling
);
320 nsTableRowGroupFrame
* GetRowGroup() const;
322 nsTableCellFrame
* GetCellFrame(PRInt32 aRowIndex
,
325 PRBool aUseRowSpanIfOverlap
) const;
328 * Returns highest cell index within the cell map.
330 * @param aColCount [in] the number of columns in the table
332 PRInt32
GetHighestIndex(PRInt32 aColCount
);
335 * Returns the index of the given row and column coordinates.
337 * @see nsITableLayout::GetIndexByRowAndColumn()
339 * @param aColCount [in] the number of columns in the table
340 * @param aRow [in] the row coordinate
341 * @param aColumn [in] the column coordinate
343 PRInt32
GetIndexByRowAndColumn(PRInt32 aColCount
,
344 PRInt32 aRow
, PRInt32 aColumn
) const;
347 * Get the row and column coordinates at the given index.
349 * @see nsITableLayout::GetRowAndColumnByIndex()
351 * @param aColCount [in] the number of columns in the table
352 * @param aIndex [in] the index for which coordinates are to be retrieved
353 * @param aRow [out] the row coordinate to be returned
354 * @param aColumn [out] the column coordinate to be returned
356 void GetRowAndColumnByIndex(PRInt32 aColCount
, PRInt32 aIndex
,
357 PRInt32
*aRow
, PRInt32
*aColumn
) const;
359 /** append the cellFrame at an empty or dead cell or finally at the end of
360 * the row at aRowIndex and return a pointer to the celldata entry in the
363 * @param aMap - reference to the table cell map
364 * @param aCellFrame - a pointer to the cellframe which will be appended
366 * @param aRowIndex - to this row the celldata entry will be added
367 * @param aRebuildIfNecessay - if a cell spans into a row below it might be
368 * necesserary to rebuild the cellmap as this rowspan
369 * might overlap another cell.
370 * @param aDamageArea - area in cellmap coordinates which have been updated.
371 * @param aColToBeginSearch - if not null contains the column number where
372 * the search for a empty or dead cell in the
374 * @return - a pointer to the celldata entry inserted into
377 CellData
* AppendCell(nsTableCellMap
& aMap
,
378 nsTableCellFrame
* aCellFrame
,
380 PRBool aRebuildIfNecessary
,
382 PRInt32
* aBeginSearchAtCol
= nsnull
);
384 /** Function to be called when a cell is added at a location which is spanned
385 * to by a zero colspan. We handle this situation by collapsing the zero
386 * colspan, since there is really no good way to deal with it (trying to
387 * increase the number of columns to hold the new cell would just mean the
388 * zero colspan needs to expand).
390 * @param aMap - reference to the table cell map
391 * @param aOrigData - zero colspanned cell that will be collapsed
392 * @param aRowIndex - row where the first collision appears
393 * @param aColIndex - column where the first collision appears
395 void CollapseZeroColSpan(nsTableCellMap
& aMap
,
400 void InsertCells(nsTableCellMap
& aMap
,
401 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
403 PRInt32 aColIndexBefore
,
404 nsRect
& aDamageArea
);
406 void RemoveCell(nsTableCellMap
& aMap
,
407 nsTableCellFrame
* aCellFrame
,
409 nsRect
& aDamageArea
);
411 void InsertRows(nsTableCellMap
& aMap
,
412 nsTArray
<nsTableRowFrame
*>& aRows
,
413 PRInt32 aFirstRowIndex
,
414 PRBool aConsiderSpans
,
415 nsRect
& aDamageArea
);
417 void RemoveRows(nsTableCellMap
& aMap
,
418 PRInt32 aFirstRowIndex
,
419 PRInt32 aNumRowsToRemove
,
420 PRBool aConsiderSpans
,
421 nsRect
& aDamageArea
);
423 PRInt32
GetNumCellsOriginatingInRow(PRInt32 aRowIndex
) const;
424 PRInt32
GetNumCellsOriginatingInCol(PRInt32 aColIndex
) const;
426 /** return the number of rows in the table represented by this CellMap */
427 PRInt32
GetRowCount(PRBool aConsiderDeadRowSpanRows
= PR_FALSE
) const;
429 nsTableCellFrame
* GetCellInfoAt(const nsTableCellMap
& aMap
,
432 PRBool
* aOriginates
= nsnull
,
433 PRInt32
* aColSpan
= nsnull
) const;
435 PRBool
RowIsSpannedInto(PRInt32 aRowIndex
,
436 PRInt32 aNumEffCols
) const;
438 PRBool
RowHasSpanningCells(PRInt32 aRowIndex
,
439 PRInt32 aNumEffCols
) const;
441 void ExpandZeroColSpans(nsTableCellMap
& aMap
);
443 /** indicate whether the row has more than one cell that either originates
444 * or is spanned from the rows above
446 PRBool
HasMoreThanOneCell(PRInt32 aRowIndex
) const;
448 /* Get the rowspan for a cell starting at aRowIndex and aColIndex.
449 * If aGetEffective is true the size will not exceed the last content based
450 * row. Cells can have a specified rowspan that extends below the last
451 * content based row. This is legitimate considering incr. reflow where the
452 * content rows will arive later.
454 PRInt32
GetRowSpan(PRInt32 aRowIndex
,
456 PRBool aGetEffective
) const;
458 PRInt32
GetEffectiveColSpan(const nsTableCellMap
& aMap
,
461 PRBool
& aIsZeroColSpan
) const;
463 typedef nsTPtrArray
<CellData
> CellDataArray
;
465 /** dump a representation of the cell map to stdout for debugging */
467 void Dump(PRBool aIsBorderCollapse
) const;
471 friend class nsTableCellMap
;
472 friend class BCMapCellIterator
;
473 friend class BCPaintBorderIterator
;
474 friend class nsTableFrame
;
475 friend class nsCellMapColumnIterator
;
478 * Increase the number of rows in this cellmap by aNumRows. Put the
479 * new rows at aRowIndex. If aRowIndex is -1, put them at the end.
481 PRBool
Grow(nsTableCellMap
& aMap
,
483 PRInt32 aRowIndex
= -1);
485 void GrowRow(CellDataArray
& aRow
,
488 /** assign aCellData to the cell at (aRow,aColumn) */
489 void SetDataAt(nsTableCellMap
& aMap
,
491 PRInt32 aMapRowIndex
,
494 CellData
* GetDataAt(PRInt32 aMapRowIndex
,
495 PRInt32 aColIndex
) const;
497 PRInt32
GetNumCellsIn(PRInt32 aColIndex
) const;
499 void ExpandWithRows(nsTableCellMap
& aMap
,
500 nsTArray
<nsTableRowFrame
*>& aRowFrames
,
501 PRInt32 aStartRowIndex
,
502 nsRect
& aDamageArea
);
504 void ExpandWithCells(nsTableCellMap
& aMap
,
505 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
509 PRBool aRowSpanIsZero
,
510 nsRect
& aDamageArea
);
512 void ShrinkWithoutRows(nsTableCellMap
& aMap
,
513 PRInt32 aFirstRowIndex
,
514 PRInt32 aNumRowsToRemove
,
515 nsRect
& aDamageArea
);
517 void ShrinkWithoutCell(nsTableCellMap
& aMap
,
518 nsTableCellFrame
& aCellFrame
,
521 nsRect
& aDamageArea
);
524 * Rebuild due to rows being inserted or deleted with cells spanning
525 * into or out of the rows. This function can only handle insertion
526 * or deletion but NOT both. So either aRowsToInsert must be null
527 * or aNumRowsToRemove must be 0.
529 * // XXXbz are both allowed to happen? That'd be a no-op...
531 void RebuildConsideringRows(nsTableCellMap
& aMap
,
532 PRInt32 aStartRowIndex
,
533 nsTArray
<nsTableRowFrame
*>* aRowsToInsert
,
534 PRInt32 aNumRowsToRemove
,
535 nsRect
& aDamageArea
);
537 void RebuildConsideringCells(nsTableCellMap
& aMap
,
538 PRInt32 aNumOrigCols
,
539 nsTArray
<nsTableCellFrame
*>* aCellFrames
,
543 nsRect
& aDamageArea
);
545 PRBool
CellsSpanOut(nsTArray
<nsTableRowFrame
*>& aNewRows
) const;
547 /** If a cell spans out of the area defined by aStartRowIndex, aEndRowIndex
548 * and aStartColIndex, aEndColIndex the cellmap changes are more severe so
549 * the corresponding routines needs to be called. This is also necessary if
550 * cells outside spans into this region.
551 * @aStartRowIndex - y start index
552 * @aEndRowIndex - y end index
553 * @param aStartColIndex - x start index
554 * @param aEndColIndex - x end index
555 * @return - true if a cell span crosses the border of the
558 PRBool
CellsSpanInOrOut(PRInt32 aStartRowIndex
,
559 PRInt32 aEndRowIndex
,
560 PRInt32 aStartColIndex
,
561 PRInt32 aEndColIndex
) const;
563 void ExpandForZeroSpan(nsTableCellFrame
* aCellFrame
,
564 PRInt32 aNumColsInTable
);
566 PRBool
CreateEmptyRow(PRInt32 aRowIndex
,
569 PRInt32
GetRowSpanForNewCell(nsTableCellFrame
* aCellFrameToAdd
,
571 PRBool
& aIsZeroRowSpan
) const;
573 PRInt32
GetColSpanForNewCell(nsTableCellFrame
& aCellFrameToAdd
,
574 PRBool
& aIsZeroColSpan
) const;
576 // Destroy a CellData struct. This will handle the case of aData
577 // actually being a BCCellData properly.
578 void DestroyCellData(CellData
* aData
);
579 // Allocate a CellData struct. This will handle needing to create a
580 // BCCellData properly.
581 // @param aOrigCell the originating cell to pass to the celldata constructor
582 CellData
* AllocCellData(nsTableCellFrame
* aOrigCell
);
584 /** an array containing, for each row, the CellDatas for the cells
585 * in that row. It can be larger than mContentRowCount due to row spans
586 * extending beyond the table */
587 // XXXbz once we have auto TArrays, we should probably use them here.
588 nsTArray
<CellDataArray
> mRows
;
590 /** the number of rows in the table (content) which is not indentical to the
591 * number of rows in the cell map due to row spans extending beyond the end
592 * of thetable (dead rows) or empty tr tags
594 PRInt32 mContentRowCount
;
596 // the row group that corresponds to this map
597 nsTableRowGroupFrame
* mRowGroupFrame
;
599 // the next row group cell map
600 nsCellMap
* mNextSibling
;
602 // Whether this is a BC cellmap or not
605 // Prescontext to deallocate and allocate celldata
606 nsRefPtr
<nsPresContext
> mPresContext
;
610 * A class for iterating the cells in a given column. Must be given a
611 * non-null nsTableCellMap and a column number valid for that cellmap.
613 class nsCellMapColumnIterator
616 nsCellMapColumnIterator(const nsTableCellMap
* aMap
, PRInt32 aCol
) :
617 mMap(aMap
), mCurMap(aMap
->mFirstMap
), mCurMapStart(0),
618 mCurMapRow(0), mCol(aCol
), mFoundCells(0)
620 NS_PRECONDITION(aMap
, "Must have map");
621 NS_PRECONDITION(mCol
< aMap
->GetColCount(), "Invalid column");
622 mOrigCells
= aMap
->GetNumCellsOriginatingInCol(mCol
);
624 mCurMapContentRowCount
= mCurMap
->GetRowCount();
625 PRUint32 rowArrayLength
= mCurMap
->mRows
.Length();
626 mCurMapRelevantRowCount
= NS_MIN(mCurMapContentRowCount
, rowArrayLength
);
627 if (mCurMapRelevantRowCount
== 0 && mOrigCells
> 0) {
628 // This row group is useless; advance!
634 NS_ASSERTION(mOrigCells
== 0, "Why no rowgroups?");
639 nsTableCellFrame
* GetNextFrame(PRInt32
* aRow
, PRInt32
* aColSpan
);
642 void AdvanceRowGroup();
644 // Advance the row; aIncrement is considered to be a cell's rowspan,
645 // so if 0 is passed in we'll advance to the next rowgroup.
646 void IncrementRow(PRInt32 aIncrement
);
648 const nsTableCellMap
* mMap
;
649 const nsCellMap
* mCurMap
;
651 // mCurMapStart is the row in the entire nsTableCellMap where
652 // mCurMap starts. This is used to compute row indices to pass to
653 // nsTableCellMap::GetDataAt, so must be a _content_ row index.
654 PRUint32 mCurMapStart
;
656 // In steady-state mCurMapRow is the row in our current nsCellMap
657 // that we'll use the next time GetNextFrame() is called. Due to
658 // the way we skip over rowspans, the entry in mCurMapRow and mCol
659 // is either null, dead, originating, or a colspan. In particular,
660 // it cannot be a rowspan or overlap entry.
664 PRUint32 mFoundCells
;
666 // The number of content rows in mCurMap. This may be bigger than the number
667 // of "relevant" rows, or it might be smaller.
668 PRUint32 mCurMapContentRowCount
;
670 // The number of "relevant" rows in mCurMap. That is, the number of rows
671 // which might have an originating cell in them. Once mCurMapRow reaches
672 // mCurMapRelevantRowCount, we should move to the next map.
673 PRUint32 mCurMapRelevantRowCount
;
677 /* ----- inline methods ----- */
678 inline PRInt32
nsTableCellMap::GetColCount() const
680 return mCols
.Length();
683 inline nsCellMap
* nsCellMap::GetNextSibling() const
688 inline void nsCellMap::SetNextSibling(nsCellMap
* aSibling
)
690 mNextSibling
= aSibling
;
693 inline nsTableRowGroupFrame
* nsCellMap::GetRowGroup() const
695 return mRowGroupFrame
;
698 inline PRInt32
nsCellMap::GetRowCount(PRBool aConsiderDeadRowSpanRows
) const
700 PRInt32 rowCount
= (aConsiderDeadRowSpanRows
) ? mRows
.Length() : mContentRowCount
;
706 inline nsColInfo::nsColInfo()
707 :mNumCellsOrig(0), mNumCellsSpan(0)
710 inline nsColInfo::nsColInfo(PRInt32 aNumCellsOrig
,
711 PRInt32 aNumCellsSpan
)
712 :mNumCellsOrig(aNumCellsOrig
), mNumCellsSpan(aNumCellsSpan
)