2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1997 Martin Jones (mjones@kde.org)
5 * (C) 1997 Torben Weis (weis@kde.org)
6 * (C) 1998 Waldo Bastian (bastian@kde.org)
7 * (C) 1999 Lars Knoll (knoll@kde.org)
8 * (C) 1999 Antti Koivisto (koivisto@kde.org)
9 * Copyright (C) 2003 Apple Computer, Inc.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public License
22 * along with this library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
29 //#define DEBUG_LAYOUT
31 #include "rendering/render_table.h"
32 #include "rendering/table_layout.h"
33 #include "html/html_tableimpl.h"
34 #include "misc/htmltags.h"
35 #include "misc/htmlattrs.h"
36 #include "xml/dom_docimpl.h"
40 #include <qapplication.h>
46 #include "khtmlview.h"
48 using namespace khtml;
51 RenderTable::RenderTable(DOM::NodeImpl* node)
56 head = foot = firstBody = 0;
62 has_col_elems = false;
66 needSectionRecalc = false;
69 columnPos.resize( 2 );
72 columns.fill( ColumnStruct() );
77 RenderTable::~RenderTable()
82 void RenderTable::setStyle(RenderStyle *_style)
84 ETableLayout oldTableLayout = style() ? style()->tableLayout() : TAUTO;
85 RenderBlock::setStyle(_style);
87 // In the collapsed border model, there is no cell spacing.
88 hspacing = collapseBorders() ? 0 : style()->horizontalBorderSpacing();
89 vspacing = collapseBorders() ? 0 : style()->verticalBorderSpacing();
90 columnPos[0] = hspacing;
92 if ( !tableLayout || style()->tableLayout() != oldTableLayout ) {
95 // According to the CSS2 spec, you only use fixed table layout if an
96 // explicit width is specified on the table. Auto width implies auto table layout.
97 if (style()->tableLayout() == TFIXED && !style()->width().isVariable()) {
98 tableLayout = new FixedTableLayout(this);
100 kdDebug( 6040 ) << "using fixed table layout" << endl;
103 tableLayout = new AutoTableLayout(this);
107 void RenderTable::addChild(RenderObject *child, RenderObject *beforeChild)
110 kdDebug( 6040 ) << renderName() << "(Table)::addChild( " << child->renderName() << ", " <<
111 (beforeChild ? beforeChild->renderName() : "0") << " )" << endl;
113 RenderObject *o = child;
115 if (child->element() && child->element()->id() == ID_FORM) {
116 RenderContainer::addChild(child,beforeChild);
120 switch(child->style()->display())
123 tCaption = static_cast<RenderBlock *>(child);
126 case TABLE_COLUMN_GROUP:
127 has_col_elems = true;
129 case TABLE_HEADER_GROUP:
131 head = static_cast<RenderTableSection *>(child);
132 else if ( !firstBody )
133 firstBody = static_cast<RenderTableSection *>(child);
135 case TABLE_FOOTER_GROUP:
137 foot = static_cast<RenderTableSection *>(child);
141 case TABLE_ROW_GROUP:
143 firstBody = static_cast<RenderTableSection *>(child);
146 if ( !beforeChild && lastChild() &&
147 lastChild()->isTableSection() && lastChild()->isAnonymous() ) {
150 RenderObject *lastBox = beforeChild;
151 while ( lastBox && lastBox->parent()->isAnonymous() &&
152 !lastBox->isTableSection() && lastBox->style()->display() != TABLE_CAPTION )
153 lastBox = lastBox->parent();
154 if ( lastBox && lastBox->isAnonymous() ) {
155 lastBox->addChild( child, beforeChild );
158 if ( beforeChild && !beforeChild->isTableSection() )
160 //kdDebug( 6040 ) << this <<" creating anonymous table section beforeChild="<< beforeChild << endl;
161 o = new (renderArena()) RenderTableSection(document() /* anonymous */);
162 RenderStyle *newStyle = new (renderArena()) RenderStyle();
163 newStyle->inheritFrom(style());
164 newStyle->setDisplay(TABLE_ROW_GROUP);
165 o->setStyle(newStyle);
166 addChild(o, beforeChild);
170 child->setNeedsLayoutAndMinMaxRecalc();
173 RenderContainer::addChild(child,beforeChild);
178 void RenderTable::calcWidth()
180 if ( isPositioned() ) {
181 calcAbsoluteHorizontal();
184 RenderBlock *cb = containingBlock();
185 int availableWidth = cb->contentWidth();
187 LengthType widthType = style()->width().type;
188 if (widthType > Relative && style()->width().value > 0) {
189 // Percent or fixed table
190 m_width = style()->width().minWidth( availableWidth );
191 if(m_minWidth > m_width) m_width = m_minWidth;
194 // An auto width table should shrink to fit within the line width if necessary in order to
195 // avoid overlapping floats.
196 availableWidth = cb->lineWidth( m_y );
198 // Subtract out any fixed margins from our available width for auto width tables.
200 if (style()->marginLeft().type != Variable)
201 marginTotal += style()->marginLeft().width(availableWidth);
202 if (style()->marginRight().type != Variable)
203 marginTotal += style()->marginRight().width(availableWidth);
205 // Subtract out our margins to get the available content width.
206 int availContentWidth = kMax(0, availableWidth - marginTotal);
208 // Ensure we aren't bigger than our max width or smaller than our min width.
209 m_width = kMin(availContentWidth, m_maxWidth);
212 m_width = kMax(m_width, m_minWidth);
214 // Finally, with our true width determined, compute our margins for real.
217 calcHorizontalMargins(style()->marginLeft(),style()->marginRight(),availableWidth);
220 void RenderTable::layout()
222 KHTMLAssert( needsLayout() );
223 KHTMLAssert( minMaxKnown() );
224 KHTMLAssert( !needSectionRecalc );
226 if (posChildNeedsLayout() && !normalChildNeedsLayout() && !selfNeedsLayout()) {
227 // All we have to is lay out our positioned objects.
228 layoutPositionedObjects(true);
229 setNeedsLayout(false);
233 QRect oldBounds, oldFullBounds;
234 bool checkForRepaint = checkForRepaintDuringLayout();
236 getAbsoluteRepaintRectIncludingFloats(oldBounds, oldFullBounds);
238 //kdDebug( 6040 ) << renderName() << "(Table)"<< this << " ::layout0() width=" << width() << ", needsLayout=" << needsLayout() << endl;
240 m_height = m_overflowHeight = 0;
241 initMaxMarginValues();
243 //int oldWidth = m_width;
245 m_overflowWidth = m_width;
247 // the optimisation below doesn't work since the internal table
248 // layout could have changed. we need to add a flag to the table
249 // layout that tells us if something has changed in the min max
250 // calculations to do it correctly.
251 // if ( oldWidth != m_width || columns.size() + 1 != columnPos.size() )
252 tableLayout->layout();
255 kdDebug( 6040 ) << renderName() << "(Table)::layout1() width=" << width() << ", marginLeft=" << marginLeft() << " marginRight=" << marginRight() << endl;
260 // layout child objects
261 int calculatedHeight = 0;
263 RenderObject *child = firstChild();
265 if ( child->needsLayout() && !(child->element() && child->element()->id() == ID_FORM))
267 if ( child->isTableSection() ) {
268 static_cast<RenderTableSection *>(child)->calcRowHeight();
269 calculatedHeight += static_cast<RenderTableSection *>(child)->layoutRows( 0 );
271 child = child->nextSibling();
274 // ### collapse caption margin
275 if(tCaption && tCaption->style()->captionSide() != CAPBOTTOM) {
276 tCaption->setPos(tCaption->marginLeft(), m_height);
277 m_height += tCaption->height() + tCaption->marginTop() + tCaption->marginBottom();
280 int bpTop = borderTop() + (collapseBorders() ? 0 : paddingTop());
281 int bpBottom = borderBottom() + (collapseBorders() ? 0 : paddingBottom());
285 int oldHeight = m_height;
287 int newHeight = m_height;
288 m_height = oldHeight;
290 Length h = style()->height();
293 th = newHeight; // FIXME: Leave this alone for now but investigate later.
294 else if (h.isFixed())
295 th = h.value - (bpTop + bpBottom); // Tables size as though CSS height includes border/padding.
296 else if (h.isPercent())
297 th = calcPercentageHeight(h);
301 if ( th > calculatedHeight ) {
302 // we have to redistribute that height to get the constraint correctly
303 // just force the first body to the height needed
304 // ### FIXME This should take height constraints on all table sections into account and distribute
305 // accordingly. For now this should be good enough
307 firstBody->calcRowHeight();
308 firstBody->layoutRows( th - calculatedHeight );
310 else if (!style()->htmlHacks()) {
311 // Completely empty tables (with no sections or anything) should at least honor specified height
317 int bl = borderLeft();
318 if (!collapseBorders())
321 // position the table sections
323 head->setPos(bl, m_height);
324 m_height += head->height();
326 RenderObject *body = firstBody;
328 if ( body != head && body != foot && body->isTableSection() ) {
329 body->setPos(bl, m_height);
330 m_height += body->height();
332 body = body->nextSibling();
335 foot->setPos(bl, m_height);
336 m_height += foot->height();
339 m_height += bpBottom;
341 if(tCaption && tCaption->style()->captionSide()==CAPBOTTOM) {
342 tCaption->setPos(tCaption->marginLeft(), m_height);
343 m_height += tCaption->height() + tCaption->marginTop() + tCaption->marginBottom();
346 //kdDebug(0) << "table height: " << m_height << endl;
348 // table can be containing block of positioned elements.
349 // ### only pass true if width or height changed.
350 layoutPositionedObjects( true );
352 // Repaint with our new bounds if they are different from our old bounds.
354 repaintAfterLayoutIfNeeded(oldBounds, oldFullBounds);
356 m_overflowHeight = kMax(m_overflowHeight, m_height);
357 m_overflowWidth = kMax(m_overflowWidth, m_width);
359 setNeedsLayout(false);
362 void RenderTable::setCellWidths()
365 kdDebug( 6040 ) << renderName() << "(Table, this=0x" << this << ")::setCellWidths()" << endl;
368 RenderObject *child = firstChild();
370 if ( child->isTableSection() )
371 static_cast<RenderTableSection *>(child)->setCellWidths();
372 child = child->nextSibling();
376 void RenderTable::paint(PaintInfo& i, int _tx, int _ty)
381 PaintAction paintAction = i.phase;
384 kdDebug( 6040 ) << "RenderTable::paint() w/h = (" << width() << "/" << height() << ")" << endl;
387 int os = 2*maximalOutlineSize(paintAction);
388 if ((_ty >= i.r.y() + i.r.height() + os) || (_ty + height() <= i.r.y() - os)) return;
389 if ((_tx >= i.r.x() + i.r.width() + os) || (_tx + width() <= i.r.x() - os)) return;
392 kdDebug( 6040 ) << "RenderTable::paint(2) " << _tx << "/" << _ty << " (" << _y << "/" << _h << ")" << endl;
395 if ((paintAction == PaintActionBlockBackground || paintAction == PaintActionChildBlockBackground)
396 && shouldPaintBackgroundOrBorder() && style()->visibility() == VISIBLE)
397 paintBoxDecorations(i, _tx, _ty);
399 // We're done. We don't bother painting any children.
400 if (paintAction == PaintActionBlockBackground)
402 // We don't paint our own background, but we do let the kids paint their backgrounds.
403 if (paintAction == PaintActionChildBlockBackgrounds)
404 paintAction = PaintActionChildBlockBackground;
405 PaintInfo paintInfo(i.p, i.r, paintAction, paintingRootForChildren(i));
407 for (RenderObject *child = firstChild(); child; child = child->nextSibling())
408 if (child->isTableSection() || child == tCaption)
409 child->paint(paintInfo, _tx, _ty);
411 if (collapseBorders() && paintAction == PaintActionChildBlockBackground && style()->visibility() == VISIBLE) {
412 // Collect all the unique border styles that we want to paint in a sorted list. Once we
413 // have all the styles sorted, we then do individual passes, painting each style of border
414 // from lowest precedence to highest precedence.
415 paintInfo.phase = PaintActionCollapsedTableBorders;
416 QValueList<CollapsedBorderValue> borderStyles;
417 collectBorders(borderStyles);
418 QValueListIterator<CollapsedBorderValue> it = borderStyles.begin();
419 QValueListIterator<CollapsedBorderValue> end = borderStyles.end();
420 for (; it != end; ++it) {
421 m_currentBorder = &(*it);
422 for (RenderObject* child = firstChild(); child; child = child->nextSibling())
423 if (child->isTableSection())
424 child->paint(paintInfo, _tx, _ty);
429 outlineBox(i.p, _tx, _ty, "blue");
433 void RenderTable::paintBoxDecorations(PaintInfo& i, int _tx, int _ty)
438 // Account for the caption.
440 int captionHeight = (tCaption->height() + tCaption->marginBottom() + tCaption->marginTop());
442 if (tCaption->style()->captionSide() != CAPBOTTOM)
443 _ty += captionHeight;
446 int my = kMax(_ty, i.r.y());
449 mh= kMax(0, h - (i.r.y() - _ty));
451 mh = kMin(i.r.height(), h);
453 paintBackground(i.p, style()->backgroundColor(), style()->backgroundLayers(), my, mh, _tx, _ty, w, h);
455 if (style()->hasBorder() && !collapseBorders())
456 paintBorder(i.p, _tx, _ty, w, h, style());
459 void RenderTable::calcMinMaxWidth()
461 KHTMLAssert( !minMaxKnown() );
463 if ( needSectionRecalc )
467 kdDebug( 6040 ) << renderName() << "(Table " << this << ")::calcMinMaxWidth()" << endl;
470 tableLayout->calcMinMaxWidth();
472 if (tCaption && tCaption->minWidth() > m_minWidth)
473 m_minWidth = tCaption->minWidth();
477 kdDebug( 6040 ) << renderName() << " END: (Table " << this << ")::calcMinMaxWidth() min = " << m_minWidth << " max = " << m_maxWidth << endl;
481 void RenderTable::splitColumn( int pos, int firstSpan )
483 // we need to add a new columnStruct
484 int oldSize = columns.size();
485 columns.resize( oldSize + 1 );
486 int oldSpan = columns[pos].span;
487 // qDebug("splitColumn( %d,%d ), oldSize=%d, oldSpan=%d", pos, firstSpan, oldSize, oldSpan );
488 KHTMLAssert( oldSpan > firstSpan );
489 columns[pos].span = firstSpan;
490 memmove( columns.data()+pos+1, columns.data()+pos, (oldSize-pos)*sizeof(ColumnStruct) );
491 columns[pos+1].span = oldSpan - firstSpan;
493 // change width of all rows.
494 RenderObject *child = firstChild();
496 if ( child->isTableSection() ) {
497 RenderTableSection *section = static_cast<RenderTableSection *>(child);
498 int size = section->numRows();
500 if ( section->cCol > pos )
502 while ( row < size ) {
503 section->grid[row].row->resize( oldSize+1 );
504 RenderTableSection::Row &r = *section->grid[row].row;
505 memmove( r.data()+pos+1, r.data()+pos, (oldSize-pos)*sizeof( RenderTableCell * ) );
506 // qDebug("moving from %d to %d, num=%d", pos, pos+1, (oldSize-pos-1) );
507 r[pos+1] = r[pos] ? (RenderTableCell *)-1 : 0;
511 child = child->nextSibling();
513 columnPos.resize( numEffCols()+1 );
514 setNeedsLayoutAndMinMaxRecalc();
517 void RenderTable::appendColumn( int span )
520 int pos = columns.size();
521 // qDebug("appendColumn( %d ), size=%d", span, pos );
522 int newSize = pos + 1;
523 columns.resize( newSize );
524 columns[pos].span = span;
525 //qDebug("appending column at %d, span %d", pos, span );
527 // change width of all rows.
528 RenderObject *child = firstChild();
530 if ( child->isTableSection() ) {
531 RenderTableSection *section = static_cast<RenderTableSection *>(child);
532 int size = section->numRows();
534 while ( row < size ) {
535 section->grid[row].row->resize( newSize );
536 section->cellAt( row, pos ) = 0;
541 child = child->nextSibling();
543 columnPos.resize( numEffCols()+1 );
544 setNeedsLayoutAndMinMaxRecalc();
547 RenderTableCol *RenderTable::colElement( int col ) {
548 if ( !has_col_elems )
550 RenderObject *child = firstChild();
553 if ( child->isTableCol() ) {
554 RenderTableCol *colElem = static_cast<RenderTableCol *>(child);
555 int span = colElem->span();
556 if ( !colElem->firstChild() ) {
562 RenderObject *next = child->firstChild();
564 next = child->nextSibling();
565 if ( !next && child->parent()->isTableCol() )
566 next = child->parent()->nextSibling();
574 void RenderTable::recalcSections()
577 head = foot = firstBody = 0;
578 has_col_elems = false;
580 RenderObject *child = firstChild();
581 // We need to get valid pointers to caption, head, foot and firstbody again
583 switch (child->style()->display()) {
586 tCaption = static_cast<RenderBlock*>(child);
587 tCaption->setNeedsLayout(true);
591 case TABLE_COLUMN_GROUP:
592 has_col_elems = true;
594 case TABLE_HEADER_GROUP: {
595 RenderTableSection *section = static_cast<RenderTableSection *>(child);
598 else if ( !firstBody )
600 if ( section->needCellRecalc )
601 section->recalcCells();
604 case TABLE_FOOTER_GROUP: {
605 RenderTableSection *section = static_cast<RenderTableSection *>(child);
608 else if ( !firstBody )
610 if ( section->needCellRecalc )
611 section->recalcCells();
614 case TABLE_ROW_GROUP: {
615 RenderTableSection *section = static_cast<RenderTableSection *>(child);
618 if ( section->needCellRecalc )
619 section->recalcCells();
624 child = child->nextSibling();
626 needSectionRecalc = false;
627 setNeedsLayout(true);
630 RenderObject* RenderTable::removeChildNode(RenderObject* child)
632 setNeedSectionRecalc();
633 return RenderContainer::removeChildNode( child );
636 int RenderTable::borderLeft() const
638 if (collapseBorders()) {
639 // FIXME: For strict mode, returning 0 is correct, since the table border half spills into the margin,
640 // but I'm working to get this changed. For now, follow the spec.
643 return RenderBlock::borderLeft();
646 int RenderTable::borderRight() const
648 if (collapseBorders()) {
649 // FIXME: For strict mode, returning 0 is correct, since the table border half spills into the margin,
650 // but I'm working to get this changed. For now, follow the spec.
653 return RenderBlock::borderRight();
656 int RenderTable::borderTop() const
658 if (collapseBorders()) {
659 // FIXME: For strict mode, returning 0 is correct, since the table border half spills into the margin,
660 // but I'm working to get this changed. For now, follow the spec.
663 return RenderBlock::borderTop();
666 int RenderTable::borderBottom() const
668 if (collapseBorders()) {
669 // FIXME: For strict mode, returning 0 is correct, since the table border half spills into the margin,
670 // but I'm working to get this changed. For now, follow the spec.
673 return RenderBlock::borderBottom();
676 RenderTableCell* RenderTable::cellAbove(const RenderTableCell* cell) const
678 // Find the section and row to look in
680 RenderTableSection* section = 0;
683 // cell is not in the first row, so use the above row in its own section
684 section = cell->section();
687 // cell is at top of a section, use last row in previous section
688 for (RenderObject *prevSection = cell->section()->previousSibling();
689 prevSection && rAbove < 0;
690 prevSection = prevSection->previousSibling()) {
691 if (prevSection->isTableSection()) {
692 section = static_cast<RenderTableSection *>(prevSection);
693 if (section->numRows() > 0)
694 rAbove = section->numRows()-1;
699 // Look up the cell in the section's grid, which requires effective col index
700 if (section && rAbove >= 0) {
701 int effCol = colToEffCol(cell->col());
702 RenderTableCell* aboveCell;
703 // If we hit a span back up to a real cell.
705 aboveCell = section->cellAt(rAbove, effCol);
707 } while (aboveCell == (RenderTableCell *)-1 && effCol >=0);
708 return (aboveCell == (RenderTableCell *)-1) ? 0 : aboveCell;
714 RenderTableCell* RenderTable::cellBelow(const RenderTableCell* cell) const
716 // Find the section and row to look in
717 int r = cell->row() + cell->rowSpan() - 1;
718 RenderTableSection* section = 0;
720 if (r < cell->section()->numRows()-1) {
721 // The cell is not in the last row, so use the next row in the section.
722 section = cell->section();
725 // The cell is at the bottom of a section. Use the first row in the next section.
726 for (RenderObject* nextSection = cell->section()->nextSibling();
727 nextSection && rBelow < 0;
728 nextSection = nextSection->nextSibling())
730 if (nextSection->isTableSection()) {
731 section = static_cast<RenderTableSection *>(nextSection);
732 if (section->numRows() > 0)
738 // Look up the cell in the section's grid, which requires effective col index
739 if (section && rBelow >= 0) {
740 int effCol = colToEffCol(cell->col());
741 RenderTableCell* belowCell;
742 // If we hit a colspan back up to a real cell.
744 belowCell = section->cellAt(rBelow, effCol);
746 } while (belowCell == (RenderTableCell *)-1 && effCol >=0);
747 return (belowCell == (RenderTableCell *)-1) ? 0 : belowCell;
753 RenderTableCell* RenderTable::cellLeft(const RenderTableCell* cell) const
755 RenderTableSection* section = cell->section();
756 int effCol = colToEffCol(cell->col());
760 // If we hit a colspan back up to a real cell.
761 RenderTableCell* prevCell;
763 prevCell = section->cellAt(cell->row(), effCol-1);
765 } while (prevCell == (RenderTableCell *)-1 && effCol >=0);
766 return (prevCell == (RenderTableCell *)-1) ? 0 : prevCell;
769 RenderTableCell* RenderTable::cellRight(const RenderTableCell* cell) const
771 int effCol = colToEffCol(cell->col()+cell->colSpan());
772 if (effCol >= numEffCols())
774 RenderTableCell* result = cell->section()->cellAt(cell->row(), effCol);
775 return (result == (RenderTableCell*)-1) ? 0 : result;
778 RenderBlock* RenderTable::firstLineBlock() const
783 void RenderTable::updateFirstLetter()
787 void RenderTable::dump(QTextStream *stream, QString ind) const
790 *stream << " tCaption";
796 *stream << endl << ind << "cspans:";
797 for ( unsigned int i = 0; i < columns.size(); i++ )
798 *stream << " " << columns[i].span;
799 *stream << endl << ind;
801 RenderBlock::dump(stream,ind);
805 // --------------------------------------------------------------------------
807 RenderTableSection::RenderTableSection(DOM::NodeImpl* node)
808 : RenderContainer(node)
810 // init RenderObject attributes
811 setInline(false); // our object is not Inline
815 needCellRecalc = false;
818 RenderTableSection::~RenderTableSection()
823 void RenderTableSection::detach()
825 // recalc cell info because RenderTable has unguarded pointers
826 // stored that point to this RenderTableSection.
828 table()->setNeedSectionRecalc();
830 RenderContainer::detach();
833 void RenderTableSection::setStyle(RenderStyle* _style)
835 // we don't allow changing this one
837 _style->setDisplay(style()->display());
838 else if (_style->display() != TABLE_FOOTER_GROUP && _style->display() != TABLE_HEADER_GROUP)
839 _style->setDisplay(TABLE_ROW_GROUP);
841 RenderContainer::setStyle(_style);
844 void RenderTableSection::addChild(RenderObject *child, RenderObject *beforeChild)
847 kdDebug( 6040 ) << renderName() << "(TableSection)::addChild( " << child->renderName() << ", beforeChild=" <<
848 (beforeChild ? beforeChild->renderName() : "0") << " )" << endl;
850 RenderObject *row = child;
852 if (child->element() && child->element()->id() == ID_FORM) {
853 RenderContainer::addChild(child,beforeChild);
857 if ( !child->isTableRow() ) {
860 beforeChild = lastChild();
862 if( beforeChild && beforeChild->isAnonymous() )
865 RenderObject *lastBox = beforeChild;
866 while ( lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableRow() )
867 lastBox = lastBox->parent();
868 if ( lastBox && lastBox->isAnonymous() ) {
869 lastBox->addChild( child, beforeChild );
872 //kdDebug( 6040 ) << "creating anonymous table row" << endl;
873 row = new (renderArena()) RenderTableRow(document() /* anonymous table */);
874 RenderStyle *newStyle = new (renderArena()) RenderStyle();
875 newStyle->inheritFrom(style());
876 newStyle->setDisplay( TABLE_ROW );
877 row->setStyle(newStyle);
878 addChild(row, beforeChild);
881 row->addChild(child);
882 child->setNeedsLayoutAndMinMaxRecalc();
892 ensureRows( cRow+1 );
895 grid[cRow].height = child->style()->height();
896 if ( grid[cRow].height.type == Relative )
897 grid[cRow].height = Length();
901 RenderContainer::addChild(child,beforeChild);
904 void RenderTableSection::ensureRows(int numRows)
906 int nRows = gridRows;
907 if (numRows > nRows) {
908 if (numRows > static_cast<int>(grid.size()))
909 grid.resize(numRows*2+1);
912 int nCols = table()->numEffCols();
913 for (int r = nRows; r < numRows; r++ ) {
914 grid[r].row = new Row(nCols);
915 grid[r].row->fill(0);
916 grid[r].baseLine = 0;
917 grid[r].height = Length();
923 void RenderTableSection::addCell( RenderTableCell *cell )
925 int rSpan = cell->rowSpan();
926 int cSpan = cell->colSpan();
927 QMemArray<RenderTable::ColumnStruct> &columns = table()->columns;
928 int nCols = columns.size();
930 // ### mozilla still seems to do the old HTML way, even for strict DTD
931 // (see the annotation on table cell layouting in the CSS specs and the testcase below:
933 // <TR><TD>1 <TD rowspan="2">2 <TD>3 <TD>4
934 // <TR><TD colspan="2">5
937 // find empty space for the cell
941 while ( cCol < nCols && cellAt( cRow, cCol ) )
945 while ( pos < nCols && span < cSpan ) {
946 if ( cellAt( cRow, pos ) ) {
951 span += columns[pos].span;
956 while ( cCol < nCols && cellAt( cRow, cCol ) )
960 // qDebug("adding cell at %d/%d span=(%d/%d)", cRow, cCol, rSpan, cSpan );
963 // we ignore height settings on rowspan cells
964 Length height = cell->style()->height();
965 if ( height.value > 0 || (height.type == Relative && height.value >= 0) ) {
966 Length cRowHeight = grid[cRow].height;
967 switch( height.type ) {
969 if ( !(cRowHeight.type == Percent) ||
970 ( cRowHeight.type == Percent && cRowHeight.value < height.value ) )
971 grid[cRow].height = height;
974 if ( cRowHeight.type < Percent ||
975 ( cRowHeight.type == Fixed && cRowHeight.value < height.value ) )
976 grid[cRow].height = height;
980 // we treat this as variable. This is correct according to HTML4, as it only specifies length for the height.
981 if ( cRowHeight.type == Variable ||
982 ( cRowHeight.type == Relative && cRowHeight.value < height.value ) )
983 grid[cRow].height = height;
992 // make sure we have enough rows
993 ensureRows( cRow + rSpan );
996 // tell the cell where it is
997 RenderTableCell *set = cell;
1000 if ( cCol >= nCols ) {
1001 table()->appendColumn( cSpan );
1002 currentSpan = cSpan;
1004 if ( cSpan < columns[cCol].span )
1005 table()->splitColumn( cCol, cSpan );
1006 currentSpan = columns[cCol].span;
1009 while ( r < rSpan ) {
1010 if ( !cellAt( cRow + r, cCol ) ) {
1011 // qDebug(" adding cell at %d, %d", cRow + r, cCol );
1012 cellAt( cRow + r, cCol ) = set;
1017 cSpan -= currentSpan;
1018 set = (RenderTableCell *)-1;
1021 cell->setRow( cRow );
1022 cell->setCol( table()->effColToCol( col ) );
1028 void RenderTableSection::setCellWidths()
1031 kdDebug( 6040 ) << renderName() << "(Table, this=0x" << this << ")::setCellWidths()" << endl;
1033 QMemArray<int> &columnPos = table()->columnPos;
1035 int rows = gridRows;
1036 for ( int i = 0; i < rows; i++ ) {
1037 Row &row = *grid[i].row;
1038 int cols = row.size();
1039 for ( int j = 0; j < cols; j++ ) {
1040 RenderTableCell *cell = row[j];
1041 // qDebug("cell[%d,%d] = %p", i, j, cell );
1042 if ( !cell || cell == (RenderTableCell *)-1 )
1045 int cspan = cell->colSpan();
1046 while ( cspan && endCol < cols ) {
1047 cspan -= table()->columns[endCol].span;
1050 int w = columnPos[endCol] - columnPos[j] - table()->hBorderSpacing();
1052 kdDebug( 6040 ) << "setting width of cell " << cell << " " << cell->row() << "/" << cell->col() << " to " << w << " colspan=" << cell->colSpan() << " start=" << j << " end=" << endCol << endl;
1054 int oldWidth = cell->width();
1055 if ( w != oldWidth ) {
1056 cell->setNeedsLayout(true);
1057 cell->setWidth( w );
1064 void RenderTableSection::calcRowHeight()
1067 RenderTableCell *cell;
1069 int totalRows = gridRows;
1070 int spacing = table()->vBorderSpacing();
1072 rowPos.resize( totalRows + 1 );
1073 rowPos[0] = spacing;
1075 for ( int r = 0; r < totalRows; r++ ) {
1080 // qDebug("height of row %d is %d/%d", r, grid[r].height.value, grid[r].height.type );
1081 int ch = grid[r].height.minWidth( 0 );
1082 int pos = rowPos[ r+1 ] + ch + spacing;
1084 if ( pos > rowPos[r+1] )
1087 Row *row = grid[r].row;
1088 int totalCols = row->size();
1089 int totalRows = gridRows;
1091 for ( int c = 0; c < totalCols; c++ ) {
1092 cell = cellAt(r, c);
1093 if ( !cell || cell == (RenderTableCell *)-1 )
1095 if ( r < totalRows - 1 && cellAt(r+1, c) == cell )
1098 if ( ( indx = r - cell->rowSpan() + 1 ) < 0 )
1101 if (cell->overrideSize() != -1) {
1102 cell->setOverrideSize(-1);
1103 cell->setChildNeedsLayout(true, false);
1104 cell->layoutIfNeeded();
1107 // Explicit heights use the border box in quirks mode. In strict mode do the right
1108 // thing and actually add in the border and padding.
1109 ch = cell->style()->height().width(0) +
1110 (cell->style()->htmlHacks() ? 0 : (cell->paddingTop() + cell->paddingBottom() +
1111 cell->borderTop() + cell->borderBottom()));
1112 if (cell->height() > ch)
1113 ch = cell->height();
1115 pos = rowPos[ indx ] + ch + spacing;
1117 if ( pos > rowPos[r+1] )
1120 // find out the baseline
1121 EVerticalAlign va = cell->style()->verticalAlign();
1122 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP
1123 || va == SUPER || va == SUB)
1125 int b=cell->baselinePosition();
1130 int td = rowPos[ indx ] + ch - b;
1136 //do we have baseline aligned elements?
1138 // increase rowheight if baseline requires
1139 int bRowPos = baseline + bdesc + spacing ; // + 2*padding
1140 if (rowPos[r+1]<bRowPos)
1141 rowPos[r+1]=bRowPos;
1143 grid[r].baseLine = baseline;
1146 if ( rowPos[r+1] < rowPos[r] )
1147 rowPos[r+1] = rowPos[r];
1148 // qDebug("rowpos(%d)=%d", r, rowPos[r] );
1152 int RenderTableSection::layoutRows( int toAdd )
1156 int totalRows = gridRows;
1157 int hspacing = table()->hBorderSpacing();
1158 int vspacing = table()->vBorderSpacing();
1160 if (toAdd && totalRows && (rowPos[totalRows] || !nextSibling())) {
1162 int totalHeight = rowPos[totalRows] + toAdd;
1163 // qDebug("layoutRows: totalHeight = %d", totalHeight );
1166 int totalPercent = 0;
1167 int numVariable = 0;
1168 for ( int r = 0; r < totalRows; r++ ) {
1169 if ( grid[r].height.type == Variable )
1171 else if ( grid[r].height.type == Percent )
1172 totalPercent += grid[r].height.value;
1174 if ( totalPercent ) {
1175 // qDebug("distributing %d over percent rows totalPercent=%d", dh, totalPercent );
1176 // try to satisfy percent
1178 if ( totalPercent > 100 )
1180 int rh = rowPos[1]-rowPos[0];
1181 for ( int r = 0; r < totalRows; r++ ) {
1182 if ( totalPercent > 0 && grid[r].height.type == Percent ) {
1183 int toAdd = kMin(dh, (totalHeight * grid[r].height.value / 100)-rh);
1184 // If toAdd is negative, then we don't want to shrink the row (this bug
1185 // affected Outlook Web Access).
1186 toAdd = QMAX(0, toAdd);
1189 totalPercent -= grid[r].height.value;
1190 // qDebug( "adding %d to row %d", toAdd, r );
1192 if ( r < totalRows-1 )
1193 rh = rowPos[r+2] - rowPos[r+1];
1197 if ( numVariable ) {
1198 // distribute over variable cols
1199 // qDebug("distributing %d over variable rows numVariable=%d", dh, numVariable );
1201 for ( int r = 0; r < totalRows; r++ ) {
1202 if ( numVariable > 0 && grid[r].height.type == Variable ) {
1203 int toAdd = dh/numVariable;
1211 if (dh>0 && rowPos[totalRows]) {
1212 // if some left overs, distribute equally.
1213 int tot=rowPos[totalRows];
1216 for ( int r = 0; r < totalRows; r++ ) {
1217 //weight with the original height
1218 add+=dh*(rowPos[r+1]-prev)/tot;
1225 int leftOffset = hspacing;
1227 int nEffCols = table()->numEffCols();
1228 for ( int r = 0; r < totalRows; r++ )
1230 Row *row = grid[r].row;
1231 int totalCols = row->size();
1232 for ( int c = 0; c < nEffCols; c++ )
1234 RenderTableCell *cell = cellAt(r, c);
1235 if (!cell || cell == (RenderTableCell *)-1 )
1237 if ( r < totalRows - 1 && cell == cellAt(r+1, c) )
1240 if ( ( rindx = r-cell->rowSpan()+1 ) < 0 )
1243 rHeight = rowPos[r+1] - rowPos[rindx] - vspacing;
1245 // Force percent height children to lay themselves out again.
1246 // This will cause these children to grow to fill the cell.
1247 // FIXME: There is still more work to do here to fully match WinIE (should
1248 // it become necessary to do so). In quirks mode, WinIE behaves like we
1249 // do, but it will clip the cells that spill out of the table section. In
1250 // strict mode, Mozilla and WinIE both regrow the table to accommodate the
1251 // new height of the cell (thus letting the percentages cause growth one
1252 // time only). We may also not be handling row-spanning cells correctly.
1254 // Note also the oddity where replaced elements always flex, and yet blocks/tables do
1255 // not necessarily flex. WinIE is crazy and inconsistent, and we can't hope to
1256 // match the behavior perfectly, but we'll continue to refine it as we discover new
1258 bool cellChildrenFlex = false;
1259 bool flexAllChildren = cell->style()->height().isFixed() ||
1260 (!table()->style()->height().isVariable() && rHeight != cell->height());
1261 RenderObject* o = cell->firstChild();
1263 if (!o->isText() && o->style()->height().isPercent() && (o->isReplaced() || o->scrollsOverflow() || flexAllChildren)) {
1264 // Tables with no sections do not flex.
1265 if (!o->isTable() || static_cast<RenderTable*>(o)->hasSections()) {
1266 o->setNeedsLayout(true, false);
1267 cell->setChildNeedsLayout(true, false);
1268 cellChildrenFlex = true;
1271 o = o->nextSibling();
1273 if (cellChildrenFlex) {
1274 cell->setOverrideSize(kMax(0,
1275 rHeight - cell->borderTop() - cell->paddingTop() -
1276 cell->borderBottom() - cell->paddingBottom()));
1277 cell->layoutIfNeeded();
1279 // Alignment within a cell is based off the calculated
1280 // height, which becomes irrelevant once the cell has
1281 // been resized based off its percentage. -dwh
1282 cell->setCellTopExtra(0);
1283 cell->setCellBottomExtra(0);
1287 kdDebug( 6040 ) << "setting position " << r << "/" << c << ": "
1288 << table()->columnPos[c] /*+ padding */ << "/" << rowPos[rindx] << " height=" << rHeight<< endl;
1291 EVerticalAlign va = cell->style()->verticalAlign();
1300 te = getBaseline(r) - cell->baselinePosition() ;
1306 te = (rHeight - cell->height())/2;
1309 te = rHeight - cell->height();
1315 // kdDebug( 6040 ) << "CELL " << cell << " te=" << te << ", be=" << rHeight - cell->height() - te << ", rHeight=" << rHeight << ", valign=" << va << endl;
1317 cell->setCellTopExtra( te );
1318 cell->setCellBottomExtra( rHeight - cell->height() - te);
1321 int oldCellX = cell->xPos();
1322 int oldCellY = cell->yPos();
1324 if (style()->direction()==RTL) {
1326 table()->columnPos[(int)totalCols] -
1327 table()->columnPos[table()->colToEffCol(cell->col()+cell->colSpan())] +
1331 cell->setPos( table()->columnPos[c] + leftOffset, rowPos[rindx] );
1334 // If the cell moved, we have to repaint it as well as any floating/positioned
1335 // descendants. An exception is if we need a layout. In this case, we know we're going to
1336 // repaint ourselves (and the cell) anyway.
1337 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
1338 cell->repaintDuringLayoutIfMoved(oldCellX, oldCellY);
1342 m_height = rowPos[totalRows];
1347 void RenderTableSection::paint(PaintInfo& i, int tx, int ty)
1349 unsigned int totalRows = gridRows;
1350 unsigned int totalCols = table()->columns.size();
1355 // check which rows and cols are visible and only paint these
1356 // ### fixme: could use a binary search here
1357 PaintAction paintAction = i.phase;
1358 int x = i.r.x(); int y = i.r.y(); int w = i.r.width(); int h = i.r.height();
1360 int os = 2*maximalOutlineSize(paintAction);
1361 unsigned int startrow = 0;
1362 unsigned int endrow = totalRows;
1363 for ( ; startrow < totalRows; startrow++ ) {
1364 if ( ty + rowPos[startrow+1] >= y - os)
1367 for ( ; endrow > 0; endrow-- ) {
1368 if ( ty + rowPos[endrow-1] <= y + h + os)
1371 unsigned int startcol = 0;
1372 unsigned int endcol = totalCols;
1373 if ( style()->direction() == LTR ) {
1374 for ( ; startcol < totalCols; startcol++ ) {
1375 if ( tx + table()->columnPos[startcol+1] >= x - os)
1378 for ( ; endcol > 0; endcol-- ) {
1379 if ( tx + table()->columnPos[endcol-1] <= x + w + os)
1384 if ( startcol < endcol ) {
1386 for ( unsigned int r = startrow; r < endrow; r++ ) {
1387 unsigned int c = startcol;
1388 // since a cell can be -1 (indicating a colspan) we might have to search backwards to include it
1389 while ( c && cellAt( r, c ) == (RenderTableCell *)-1 )
1391 for ( ; c < endcol; c++ ) {
1392 RenderTableCell *cell = cellAt(r, c);
1393 if (!cell || cell == (RenderTableCell *)-1 )
1396 // Cells must always paint in the order in which they appear taking into account
1397 // their upper left originating row/column. For cells with rowspans, avoid repainting
1398 // if we've already seen the cell.
1399 if (r > startrow && (cellAt(r-1, c) == cell))
1403 kdDebug( 6040 ) << "painting cell " << r << "/" << c << endl;
1405 cell->paint(i, tx, ty);
1411 void RenderTableSection::recalcCells()
1418 RenderObject *row = firstChild();
1422 ensureRows( cRow+1 );
1423 RenderObject *cell = row->firstChild();
1425 if ( cell->isTableCell() )
1426 addCell( static_cast<RenderTableCell *>(cell) );
1427 cell = cell->nextSibling();
1429 row = row->nextSibling();
1431 needCellRecalc = false;
1432 setNeedsLayout(true);
1435 void RenderTableSection::clearGrid()
1437 int rows = gridRows;
1439 delete grid[rows].row;
1443 RenderObject* RenderTableSection::removeChildNode(RenderObject* child)
1445 setNeedCellRecalc();
1446 return RenderContainer::removeChildNode( child );
1450 void RenderTableSection::dump(QTextStream *stream, QString ind) const
1452 *stream << endl << ind << "grid=(" << grid.size() << "," << table()->numEffCols() << ")" << endl << ind;
1453 for ( unsigned int r = 0; r < grid.size(); r++ ) {
1454 for ( int c = 0; c < table()->numEffCols(); c++ ) {
1455 if ( cellAt( r, c ) && cellAt( r, c ) != (RenderTableCell *)-1 )
1456 *stream << "(" << cellAt( r, c )->row() << "," << cellAt( r, c )->col() << ","
1457 << cellAt(r, c)->rowSpan() << "," << cellAt(r, c)->colSpan() << ") ";
1459 *stream << cellAt( r, c ) << "null cell ";
1461 *stream << endl << ind;
1463 RenderContainer::dump(stream,ind);
1467 // -------------------------------------------------------------------------
1469 RenderTableRow::RenderTableRow(DOM::NodeImpl* node)
1470 : RenderContainer(node)
1472 // init RenderObject attributes
1473 setInline(false); // our object is not Inline
1476 void RenderTableRow::detach()
1478 RenderTableSection *s = section();
1480 s->setNeedCellRecalc();
1482 RenderContainer::detach();
1485 void RenderTableRow::setStyle(RenderStyle* style)
1487 style->setDisplay(TABLE_ROW);
1488 RenderContainer::setStyle(style);
1491 void RenderTableRow::addChild(RenderObject *child, RenderObject *beforeChild)
1494 kdDebug( 6040 ) << renderName() << "(TableRow)::addChild( " << child->renderName() << " )" << ", " <<
1495 (beforeChild ? beforeChild->renderName() : "0") << " )" << endl;
1497 if (child->element() && child->element()->id() == ID_FORM) {
1498 RenderContainer::addChild(child,beforeChild);
1502 RenderTableCell *cell;
1504 if ( !child->isTableCell() ) {
1505 RenderObject *last = beforeChild;
1508 RenderTableCell *cell = 0;
1509 if( last && last->isAnonymous() && last->isTableCell() )
1510 cell = static_cast<RenderTableCell *>(last);
1512 cell = new (renderArena()) RenderTableCell(document() /* anonymous object */);
1513 RenderStyle *newStyle = new (renderArena()) RenderStyle();
1514 newStyle->inheritFrom(style());
1515 newStyle->setDisplay( TABLE_CELL );
1516 cell->setStyle(newStyle);
1517 addChild(cell, beforeChild);
1519 cell->addChild(child);
1520 child->setNeedsLayoutAndMinMaxRecalc();
1523 cell = static_cast<RenderTableCell *>(child);
1525 static_cast<RenderTableSection *>(parent())->addCell( cell );
1527 RenderContainer::addChild(cell,beforeChild);
1529 if ( ( beforeChild || nextSibling()) && section() )
1530 section()->setNeedCellRecalc();
1533 RenderObject* RenderTableRow::removeChildNode(RenderObject* child)
1535 // RenderTableCell detach should do it
1537 // section()->setNeedCellRecalc();
1538 return RenderContainer::removeChildNode( child );
1542 void RenderTableRow::dump(QTextStream *stream, QString ind) const
1544 RenderContainer::dump(stream,ind);
1548 void RenderTableRow::layout()
1550 KHTMLAssert( needsLayout() );
1551 KHTMLAssert( minMaxKnown() );
1553 RenderObject *child = firstChild();
1555 if (child->isTableCell()) {
1556 RenderTableCell *cell = static_cast<RenderTableCell *>(child);
1557 if (child->needsLayout()) {
1558 cell->calcVerticalMargins();
1560 cell->setCellTopExtra(0);
1561 cell->setCellBottomExtra(0);
1564 child = child->nextSibling();
1566 setNeedsLayout(false);
1569 QRect RenderTableRow::getAbsoluteRepaintRect()
1571 // For now, just repaint the whole table.
1572 // FIXME: Find a better way to do this.
1573 RenderTable* parentTable = table();
1575 return parentTable->getAbsoluteRepaintRect();
1580 // -------------------------------------------------------------------------
1582 RenderTableCell::RenderTableCell(DOM::NodeImpl* _node)
1583 : RenderBlock(_node)
1588 updateFromElement();
1589 setShouldPaintBackgroundOrBorder(true);
1592 m_percentageHeight = 0;
1595 void RenderTableCell::detach()
1597 if (parent() && section())
1598 section()->setNeedCellRecalc();
1600 RenderBlock::detach();
1603 void RenderTableCell::updateFromElement()
1605 int oldRSpan = rSpan;
1606 int oldCSpan = cSpan;
1607 DOM::NodeImpl* node = element();
1608 if (node && (node->id() == ID_TD || node->id() == ID_TH)) {
1609 DOM::HTMLTableCellElementImpl *tc = static_cast<DOM::HTMLTableCellElementImpl *>(node);
1610 cSpan = tc->colSpan();
1611 rSpan = tc->rowSpan();
1613 if ((oldRSpan != rSpan || oldCSpan != cSpan) && style() && parent())
1614 setNeedsLayoutAndMinMaxRecalc();
1617 void RenderTableCell::calcMinMaxWidth()
1619 RenderBlock::calcMinMaxWidth();
1620 if (element() && style()->whiteSpace() == NORMAL) {
1621 // See if nowrap was set.
1622 DOMString nowrap = static_cast<ElementImpl*>(element())->getAttribute(ATTR_NOWRAP);
1623 if (!nowrap.isNull() && style()->width().isFixed())
1624 // Nowrap is set, but we didn't actually use it because of the
1625 // fixed width set on the cell. Even so, it is a WinIE/Moz trait
1626 // to make the minwidth of the cell into the fixed width. They do this
1627 // even in strict mode, so do not make this a quirk. Affected the top
1629 if (m_minWidth < style()->width().value)
1630 m_minWidth = style()->width().value;
1634 void RenderTableCell::calcWidth()
1638 void RenderTableCell::setWidth( int width )
1640 if ( width != m_width ) {
1642 m_widthChanged = true;
1646 void RenderTableCell::layout()
1648 layoutBlock(m_widthChanged);
1649 m_widthChanged = false;
1652 void RenderTableCell::computeAbsoluteRepaintRect(QRect& r, bool f)
1654 r.setY(r.y() + _topExtra);
1655 RenderBlock::computeAbsoluteRepaintRect(r, f);
1658 bool RenderTableCell::absolutePosition(int &xPos, int &yPos, bool f)
1660 bool ret = RenderBlock::absolutePosition(xPos, yPos, f);
1666 short RenderTableCell::baselinePosition( bool ) const
1668 RenderObject *o = firstChild();
1669 int offset = paddingTop() + borderTop();
1670 if ( !o ) return offset;
1671 while ( o->firstChild() ) {
1672 if ( !o->isInline() )
1673 offset += o->paddingTop() + o->borderTop();
1674 o = o->firstChild();
1676 offset += o->baselinePosition( true );
1681 void RenderTableCell::setStyle( RenderStyle *style )
1683 style->setDisplay(TABLE_CELL);
1685 if (style->whiteSpace() == KHTML_NOWRAP) {
1686 // Figure out if we are really nowrapping or if we should just
1687 // use normal instead. If the width of the cell is fixed, then
1688 // we don't actually use NOWRAP.
1689 if (style->width().isFixed())
1690 style->setWhiteSpace(NORMAL);
1692 style->setWhiteSpace(NOWRAP);
1695 RenderBlock::setStyle( style );
1696 setShouldPaintBackgroundOrBorder(true);
1699 bool RenderTableCell::requiresLayer() {
1700 // FIXME: This is only here until we figure out how to position
1701 // table cells properly when they have layers.
1705 // The following rules apply for resolving conflicts and figuring out which border
1707 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
1708 // borders. Any border with this value suppresses all borders at this location.
1709 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
1710 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
1711 // the default value for the border style.)
1712 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
1713 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
1714 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
1715 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
1716 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
1717 // is used when two elements of the same type disagree.
1718 static CollapsedBorderValue compareBorders(const CollapsedBorderValue& border1,
1719 const CollapsedBorderValue& border2)
1721 // Sanity check the values passed in. If either is null, return the other.
1722 if (!border2.exists()) return border1;
1723 if (!border1.exists()) return border2;
1726 if (border1.style() == BHIDDEN || border2.style() == BHIDDEN)
1727 return CollapsedBorderValue(); // No border should exist at this location.
1729 // Rule #2 above. A style of 'none' has lowest priority and always loses to any other border.
1730 if (border2.style() == BNONE) return border1;
1731 if (border1.style() == BNONE) return border2;
1733 // The first part of rule #3 above. Wider borders win.
1734 if (border1.width() != border2.width())
1735 return border1.width() > border2.width() ? border1 : border2;
1737 // The borders have equal width. Sort by border style.
1738 if (border1.style() != border2.style())
1739 return border1.style() > border2.style() ? border1 : border2;
1741 // The border have the same width and style. Rely on precedence (cell over row over row group, etc.)
1742 return border1.precedence >= border2.precedence ? border1 : border2;
1745 CollapsedBorderValue RenderTableCell::collapsedLeftBorder() const
1747 // For border left, we need to check, in order of precedence:
1748 // (1) Our left border.
1749 CollapsedBorderValue result(&style()->borderLeft(), BCELL);
1751 // (2) The previous cell's right border.
1752 RenderTableCell* prevCell = table()->cellLeft(this);
1754 result = compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL));
1755 if (!result.exists()) return result;
1757 else if (col() == 0) {
1758 // (3) Our row's left border.
1759 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderLeft(), BROW));
1760 if (!result.exists()) return result;
1762 // (4) Our row group's left border.
1763 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderLeft(), BROWGROUP));
1764 if (!result.exists()) return result;
1767 // (5) Our column's left border.
1768 RenderTableCol* colElt = table()->colElement(col());
1770 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
1771 if (!result.exists()) return result;
1774 // (6) The previous column's right border.
1776 colElt = table()->colElement(col()-1);
1778 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
1779 if (!result.exists()) return result;
1784 // (7) The table's left border.
1785 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderLeft(), BTABLE));
1786 if (!result.exists()) return result;
1792 CollapsedBorderValue RenderTableCell::collapsedRightBorder() const
1794 RenderTable* tableElt = table();
1795 bool inLastColumn = false;
1796 int effCol = tableElt->colToEffCol(col()+colSpan()-1);
1797 if (effCol == tableElt->numEffCols()-1)
1798 inLastColumn = true;
1800 // For border right, we need to check, in order of precedence:
1801 // (1) Our right border.
1802 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderRight(), BCELL);
1804 // (2) The next cell's left border.
1805 if (!inLastColumn) {
1806 RenderTableCell* nextCell = tableElt->cellRight(this);
1807 if (nextCell && nextCell->style()) {
1808 result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL));
1809 if (!result.exists()) return result;
1813 // (3) Our row's right border.
1814 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderRight(), BROW));
1815 if (!result.exists()) return result;
1817 // (4) Our row group's right border.
1818 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderRight(), BROWGROUP));
1819 if (!result.exists()) return result;
1822 // (5) Our column's right border.
1823 RenderTableCol* colElt = table()->colElement(col()+colSpan()-1);
1825 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
1826 if (!result.exists()) return result;
1829 // (6) The next column's left border.
1830 if (!inLastColumn) {
1831 colElt = tableElt->colElement(col()+colSpan());
1833 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
1834 if (!result.exists()) return result;
1838 // (7) The table's right border.
1839 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderRight(), BTABLE));
1840 if (!result.exists()) return result;
1846 CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
1848 // For border top, we need to check, in order of precedence:
1849 // (1) Our top border.
1850 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderTop(), BCELL);
1852 RenderTableCell* prevCell = table()->cellAbove(this);
1854 // (2) A previous cell's bottom border.
1855 result = compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderBottom(), BCELL));
1856 if (!result.exists()) return result;
1859 // (3) Our row's top border.
1860 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderTop(), BROW));
1861 if (!result.exists()) return result;
1863 // (4) The previous row's bottom border.
1865 RenderObject* prevRow = 0;
1866 if (prevCell->section() == section())
1867 prevRow = parent()->previousSibling();
1869 prevRow = prevCell->section()->lastChild();
1872 result = compareBorders(result, CollapsedBorderValue(&prevRow->style()->borderBottom(), BROW));
1873 if (!result.exists()) return result;
1877 // Now check row groups.
1878 RenderObject* currSection = parent()->parent();
1880 // (5) Our row group's top border.
1881 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
1882 if (!result.exists()) return result;
1884 // (6) Previous row group's bottom border.
1885 for (currSection = currSection->previousSibling(); currSection;
1886 currSection = currSection->previousSibling()) {
1887 if (currSection->isTableSection()) {
1888 RenderTableSection* section = static_cast<RenderTableSection*>(currSection);
1889 result = compareBorders(result, CollapsedBorderValue(§ion->style()->borderBottom(), BROWGROUP));
1890 if (!result.exists()) return result;
1896 // (8) Our column's top border.
1897 RenderTableCol* colElt = table()->colElement(col());
1899 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderTop(), BCOL));
1900 if (!result.exists()) return result;
1903 // (9) The table's top border.
1904 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderTop(), BTABLE));
1905 if (!result.exists()) return result;
1911 CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
1913 // For border top, we need to check, in order of precedence:
1914 // (1) Our bottom border.
1915 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBottom(), BCELL);
1917 RenderTableCell* nextCell = table()->cellBelow(this);
1919 // (2) A following cell's top border.
1920 result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderTop(), BCELL));
1921 if (!result.exists()) return result;
1924 // (3) Our row's bottom border. (FIXME: Deal with rowspan!)
1925 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderBottom(), BROW));
1926 if (!result.exists()) return result;
1928 // (4) The next row's top border.
1930 result = compareBorders(result, CollapsedBorderValue(&nextCell->parent()->style()->borderTop(), BROW));
1931 if (!result.exists()) return result;
1934 // Now check row groups.
1935 RenderObject* currSection = parent()->parent();
1936 if (row() + rowSpan() >= static_cast<RenderTableSection*>(currSection)->numRows()) {
1937 // (5) Our row group's bottom border.
1938 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP));
1939 if (!result.exists()) return result;
1941 // (6) Following row group's top border.
1942 for (currSection = currSection->nextSibling(); currSection;
1943 currSection = currSection->nextSibling()) {
1944 if (currSection->isTableSection()) {
1945 RenderTableSection* section = static_cast<RenderTableSection*>(currSection);
1946 result = compareBorders(result, CollapsedBorderValue(§ion->style()->borderTop(), BROWGROUP));
1947 if (!result.exists()) return result;
1953 // (8) Our column's bottom border.
1954 RenderTableCol* colElt = table()->colElement(col());
1956 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderBottom(), BCOL));
1957 if (!result.exists()) return result;
1960 // (9) The table's bottom border.
1961 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderBottom(), BTABLE));
1962 if (!result.exists()) return result;
1968 int RenderTableCell::borderLeft() const
1970 if (table()->collapseBorders()) {
1971 CollapsedBorderValue border = collapsedLeftBorder();
1972 if (border.exists())
1973 return int(border.width()/2.0+0.5); // Give the extra pixel to top and left.
1976 return RenderBlock::borderLeft();
1979 int RenderTableCell::borderRight() const
1981 if (table()->collapseBorders()) {
1982 CollapsedBorderValue border = collapsedRightBorder();
1983 if (border.exists())
1984 return border.width()/2;
1987 return RenderBlock::borderRight();
1990 int RenderTableCell::borderTop() const
1992 if (table()->collapseBorders()) {
1993 CollapsedBorderValue border = collapsedTopBorder();
1994 if (border.exists())
1995 return int(border.width()/2.0+0.5); // Give the extra pixel to top and left.
1998 return RenderBlock::borderTop();
2001 int RenderTableCell::borderBottom() const
2003 if (table()->collapseBorders()) {
2004 CollapsedBorderValue border = collapsedBottomBorder();
2005 if (border.exists())
2006 return border.width()/2;
2009 return RenderBlock::borderBottom();
2013 #include <qpainter.h>
2015 static void outlineBox(QPainter *p, int _tx, int _ty, int w, int h)
2017 p->setPen(QPen(QColor("yellow"), 3, Qt::DotLine));
2018 p->setBrush( Qt::NoBrush );
2019 p->drawRect(_tx, _ty, w, h );
2023 void RenderTableCell::paint(PaintInfo& i, int _tx, int _ty)
2027 kdDebug( 6040 ) << renderName() << "(RenderTableCell)::paint() w/h = (" << width() << "/" << height() << ")" << " _y/_h=" << _y << "/" << _h << endl;
2033 // check if we need to do anything at all...
2034 int os = 2*maximalOutlineSize(i.phase);
2035 if ((_ty >= i.r.y() + i.r.height() + os) || (_ty + _topExtra + m_height + _bottomExtra <= i.r.y() - os))
2038 if (i.phase == PaintActionCollapsedTableBorders && style()->visibility() == VISIBLE) {
2040 int h = height() + borderTopExtra() + borderBottomExtra();
2041 paintCollapsedBorder(i.p, _tx, _ty, w, h);
2044 RenderBlock::paintObject(i, _tx, _ty + _topExtra);
2047 ::outlineBox( i.p, _tx, _ty, width(), height() + borderTopExtra() + borderBottomExtra());
2051 static EBorderStyle collapsedBorderStyle(EBorderStyle style)
2053 if (style == OUTSET)
2055 else if (style == INSET)
2060 struct CollapsedBorder {
2063 CollapsedBorderValue border;
2064 RenderObject::BorderSide side;
2073 class CollapsedBorders
2076 CollapsedBorders(int i) :count(0) {}
2078 void addBorder(const CollapsedBorderValue& b, RenderObject::BorderSide s, bool paint,
2079 int _x1, int _y1, int _x2, int _y2,
2080 EBorderStyle _style)
2082 if (b.exists() && paint) {
2083 borders[count].border = b;
2084 borders[count].side = s;
2085 borders[count].shouldPaint = paint;
2086 borders[count].x1 = _x1;
2087 borders[count].x2 = _x2;
2088 borders[count].y1 = _y1;
2089 borders[count].y2 = _y2;
2090 borders[count].style = _style;
2095 CollapsedBorder* nextBorder() {
2096 for (int i = 0; i < count; i++) {
2097 if (borders[i].border.exists() && borders[i].shouldPaint) {
2098 borders[i].shouldPaint = false;
2106 CollapsedBorder borders[4];
2110 static void addBorderStyle(QValueList<CollapsedBorderValue>& borderStyles, CollapsedBorderValue borderValue)
2112 if (!borderValue.exists() || borderStyles.contains(borderValue))
2115 QValueListIterator<CollapsedBorderValue> it = borderStyles.begin();
2116 QValueListIterator<CollapsedBorderValue> end = borderStyles.end();
2117 for (; it != end; ++it) {
2118 CollapsedBorderValue result = compareBorders(*it, borderValue);
2119 if (result == *it) {
2120 borderStyles.insert(it, borderValue);
2125 borderStyles.append(borderValue);
2128 void RenderTableCell::collectBorders(QValueList<CollapsedBorderValue>& borderStyles)
2130 addBorderStyle(borderStyles, collapsedLeftBorder());
2131 addBorderStyle(borderStyles, collapsedRightBorder());
2132 addBorderStyle(borderStyles, collapsedTopBorder());
2133 addBorderStyle(borderStyles, collapsedBottomBorder());
2136 void RenderTableCell::paintCollapsedBorder(QPainter* p, int _tx, int _ty, int w, int h)
2138 if (!table()->currentBorderStyle())
2141 CollapsedBorderValue leftVal = collapsedLeftBorder();
2142 CollapsedBorderValue rightVal = collapsedRightBorder();
2143 CollapsedBorderValue topVal = collapsedTopBorder();
2144 CollapsedBorderValue bottomVal = collapsedBottomBorder();
2146 // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
2147 int topWidth = topVal.width();
2148 int bottomWidth = bottomVal.width();
2149 int leftWidth = leftVal.width();
2150 int rightWidth = rightVal.width();
2154 w += leftWidth/2 + int(rightWidth/2.0+0.5);
2155 h += topWidth/2 + int(bottomWidth/2.0+0.5);
2157 bool tt = topVal.isTransparent();
2158 bool bt = bottomVal.isTransparent();
2159 bool rt = rightVal.isTransparent();
2160 bool lt = leftVal.isTransparent();
2162 EBorderStyle ts = collapsedBorderStyle(topVal.style());
2163 EBorderStyle bs = collapsedBorderStyle(bottomVal.style());
2164 EBorderStyle ls = collapsedBorderStyle(leftVal.style());
2165 EBorderStyle rs = collapsedBorderStyle(rightVal.style());
2167 bool render_t = ts > BHIDDEN && !tt;
2168 bool render_l = ls > BHIDDEN && !lt;
2169 bool render_r = rs > BHIDDEN && !rt;
2170 bool render_b = bs > BHIDDEN && !bt;
2172 // We never paint diagonals at the joins. We simply let the border with the highest
2173 // precedence paint on top of borders with lower precedence.
2174 CollapsedBorders borders(4);
2175 borders.addBorder(topVal, BSTop, render_t, _tx, _ty, _tx + w, _ty + topWidth, ts);
2176 borders.addBorder(bottomVal, BSBottom, render_b, _tx, _ty + h - bottomWidth, _tx + w, _ty + h, bs);
2177 borders.addBorder(leftVal, BSLeft, render_l, _tx, _ty, _tx + leftWidth, _ty + h, ls);
2178 borders.addBorder(rightVal, BSRight, render_r, _tx + w - rightWidth, _ty, _tx + w, _ty + h, rs);
2180 for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
2181 if (border->border == *table()->currentBorderStyle())
2182 drawBorder(p, border->x1, border->y1, border->x2, border->y2, border->side,
2183 border->border.color(), style()->color(), border->style, 0, 0);
2187 QRect RenderTableCell::getAbsoluteRepaintRect()
2189 int ow = style() ? style()->outlineSize() : 0;
2190 QRect r(-ow, -ow - borderTopExtra(),
2191 overflowWidth(false)+ow*2, overflowHeight(false)+borderTopExtra()+borderBottomExtra()+ow*2);
2192 computeAbsoluteRepaintRect(r);
2196 void RenderTableCell::paintBoxDecorations(PaintInfo& i, int _tx, int _ty)
2198 RenderTable* tableElt = table();
2199 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
2203 int h = height() + borderTopExtra() + borderBottomExtra();
2204 _ty -= borderTopExtra();
2206 QColor c = style()->backgroundColor();
2207 if ( !c.isValid() && parent() ) // take from row
2208 c = parent()->style()->backgroundColor();
2209 if ( !c.isValid() && parent() && parent()->parent() ) // take from rowgroup
2210 c = parent()->parent()->style()->backgroundColor();
2211 if ( !c.isValid() ) {
2212 // see if we have a col or colgroup for this
2213 RenderTableCol *col = table()->colElement( _col );
2215 c = col->style()->backgroundColor();
2216 if ( !c.isValid() ) {
2218 RenderStyle *style = col->parent()->style();
2219 if ( style->display() == TABLE_COLUMN_GROUP )
2220 c = style->backgroundColor();
2225 // FIXME: This code is just plain wrong. Rows and columns should paint their backgrounds
2226 // independent from the cell.
2227 // ### get offsets right in case the bgimage is inherited.
2228 const BackgroundLayer* bgLayer = style()->backgroundLayers();
2229 if (!bgLayer->hasImage() && parent())
2230 bgLayer = parent()->style()->backgroundLayers();
2231 if (!bgLayer->hasImage() && parent() && parent()->parent())
2232 bgLayer = parent()->parent()->style()->backgroundLayers();
2233 if (!bgLayer->hasImage()) {
2234 // see if we have a col or colgroup for this
2235 RenderTableCol* col = table()->colElement(_col);
2237 bgLayer = col->style()->backgroundLayers();
2238 if (!bgLayer->hasImage()) {
2240 RenderStyle *style = col->parent()->style();
2241 if (style->display() == TABLE_COLUMN_GROUP)
2242 bgLayer = style->backgroundLayers();
2247 int my = kMax(_ty, i.r.y());
2248 int end = kMin(i.r.y() + i.r.height(), _ty + h);
2251 if (bgLayer->hasImage() || c.isValid())
2252 paintBackground(i.p, c, bgLayer, my, mh, _tx, _ty, w, h);
2254 if (style()->hasBorder() && !tableElt->collapseBorders())
2255 paintBorder(i.p, _tx, _ty, w, h, style());
2260 void RenderTableCell::dump(QTextStream *stream, QString ind) const
2262 *stream << " row=" << _row;
2263 *stream << " col=" << _col;
2264 *stream << " rSpan=" << rSpan;
2265 *stream << " cSpan=" << cSpan;
2266 // *stream << " nWrap=" << nWrap;
2268 RenderBlock::dump(stream,ind);
2272 // -------------------------------------------------------------------------
2274 RenderTableCol::RenderTableCol(DOM::NodeImpl* node)
2275 : RenderContainer(node)
2277 // init RenderObject attributes
2278 setInline(true); // our object is not Inline
2281 updateFromElement();
2284 void RenderTableCol::updateFromElement()
2286 int oldSpan = _span;
2287 DOM::NodeImpl *node = element();
2288 if (node && (node->id() == ID_COL || node->id() == ID_COLGROUP)) {
2289 DOM::HTMLTableColElementImpl *tc = static_cast<DOM::HTMLTableColElementImpl *>(node);
2293 _span = !(style() && style()->display() == TABLE_COLUMN_GROUP);
2294 if (_span != oldSpan && style() && parent())
2295 setNeedsLayoutAndMinMaxRecalc();
2298 bool RenderTableCol::canHaveChildren() const
2300 // cols cannot have children. This is actually necessary to fix a bug
2301 // with libraries.uc.edu, which makes a <p> be a table-column.
2302 return style()->display() == TABLE_COLUMN_GROUP;
2305 void RenderTableCol::addChild(RenderObject *child, RenderObject *beforeChild)
2308 //kdDebug( 6040 ) << renderName() << "(Table)::addChild( " << child->renderName() << " )" << ", " <<
2309 // (beforeChild ? beforeChild->renderName() : 0) << " )" << endl;
2312 KHTMLAssert(child->style()->display() == TABLE_COLUMN);
2314 // these have to come before the table definition!
2315 RenderContainer::addChild(child,beforeChild);
2319 void RenderTableCol::dump(QTextStream *stream, QString ind) const
2321 *stream << " _span=" << _span;
2322 RenderContainer::dump(stream,ind);