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, 2004, 2005, 2006 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.
28 #include "RenderTableCell.h"
30 #include "GraphicsContext.h"
31 #include "RenderTableCol.h"
32 #include "html_tableimpl.h"
33 #include "htmlnames.h"
34 #include <qtextstream.h>
38 using namespace HTMLNames;
40 RenderTableCell::RenderTableCell(NodeImpl* _node)
47 setShouldPaintBackgroundOrBorder(true);
50 m_percentageHeight = 0;
53 void RenderTableCell::destroy()
55 if (parent() && section())
56 section()->setNeedCellRecalc();
58 RenderBlock::destroy();
61 void RenderTableCell::updateFromElement()
65 NodeImpl* node = element();
66 if (node && (node->hasTagName(tdTag) || node->hasTagName(thTag))) {
67 HTMLTableCellElementImpl *tc = static_cast<HTMLTableCellElementImpl *>(node);
68 cSpan = tc->colSpan();
69 rSpan = tc->rowSpan();
71 if ((oldRSpan != rSpan || oldCSpan != cSpan) && style() && parent())
72 setNeedsLayoutAndMinMaxRecalc();
75 Length RenderTableCell::styleOrColWidth()
77 Length w = style()->width();
78 if (colSpan() > 1 || !w.isAuto())
80 RenderTableCol* col = table()->colElement(_col);
82 w = col->style()->width();
86 void RenderTableCell::calcMinMaxWidth()
88 RenderBlock::calcMinMaxWidth();
89 if (element() && style()->autoWrap()) {
90 // See if nowrap was set.
91 Length w = styleOrColWidth();
92 DOMString nowrap = static_cast<ElementImpl*>(element())->getAttribute(nowrapAttr);
93 if (!nowrap.isNull() && w.isFixed())
94 // Nowrap is set, but we didn't actually use it because of the
95 // fixed width set on the cell. Even so, it is a WinIE/Moz trait
96 // to make the minwidth of the cell into the fixed width. They do this
97 // even in strict mode, so do not make this a quirk. Affected the top
99 if (m_minWidth < w.value())
100 m_minWidth = w.value();
104 void RenderTableCell::calcWidth()
108 void RenderTableCell::setWidth(int width)
110 if (width != m_width) {
112 m_widthChanged = true;
116 void RenderTableCell::layout()
118 layoutBlock(m_widthChanged);
119 m_widthChanged = false;
122 void RenderTableCell::computeAbsoluteRepaintRect(IntRect& r, bool f)
124 r.setY(r.y() + _topExtra);
125 r.move(-parent()->xPos(), -parent()->yPos()); // Rows are in the same coordinate space, so don't add their offset in.
126 RenderBlock::computeAbsoluteRepaintRect(r, f);
129 bool RenderTableCell::absolutePosition(int &xPos, int &yPos, bool f)
131 bool result = RenderBlock::absolutePosition(xPos, yPos, f);
132 xPos -= parent()->xPos(); // Rows are in the same coordinate space, so don't add their offset in.
133 yPos -= parent()->yPos();
137 short RenderTableCell::baselinePosition(bool) const
139 RenderObject* o = firstChild();
140 int offset = paddingTop() + borderTop();
143 return offset + contentHeight();
144 while (o->firstChild()) {
146 offset += o->paddingTop() + o->borderTop();
151 return paddingTop() + borderTop() + contentHeight();
153 offset += o->baselinePosition(true);
157 void RenderTableCell::setStyle(RenderStyle *style)
159 style->setDisplay(TABLE_CELL);
161 if (style->whiteSpace() == KHTML_NOWRAP) {
162 // Figure out if we are really nowrapping or if we should just
163 // use normal instead. If the width of the cell is fixed, then
164 // we don't actually use NOWRAP.
165 if (style->width().isFixed())
166 style->setWhiteSpace(NORMAL);
168 style->setWhiteSpace(NOWRAP);
171 RenderBlock::setStyle(style);
172 setShouldPaintBackgroundOrBorder(true);
175 bool RenderTableCell::requiresLayer()
177 return isPositioned() || isTransparent() || hasOverflowClip();
180 // The following rules apply for resolving conflicts and figuring out which border
182 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
183 // borders. Any border with this value suppresses all borders at this location.
184 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
185 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
186 // the default value for the border style.)
187 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
188 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
189 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
190 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
191 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
192 // is used when two elements of the same type disagree.
193 static CollapsedBorderValue compareBorders(const CollapsedBorderValue& border1,
194 const CollapsedBorderValue& border2)
196 // Sanity check the values passed in. If either is null, return the other.
197 if (!border2.exists()) return border1;
198 if (!border1.exists()) return border2;
201 if (border1.style() == BHIDDEN || border2.style() == BHIDDEN)
202 return CollapsedBorderValue(); // No border should exist at this location.
204 // Rule #2 above. A style of 'none' has lowest priority and always loses to any other border.
205 if (border2.style() == BNONE) return border1;
206 if (border1.style() == BNONE) return border2;
208 // The first part of rule #3 above. Wider borders win.
209 if (border1.width() != border2.width())
210 return border1.width() > border2.width() ? border1 : border2;
212 // The borders have equal width. Sort by border style.
213 if (border1.style() != border2.style())
214 return border1.style() > border2.style() ? border1 : border2;
216 // The border have the same width and style. Rely on precedence (cell over row over row group, etc.)
217 return border1.precedence >= border2.precedence ? border1 : border2;
220 CollapsedBorderValue RenderTableCell::collapsedLeftBorder(bool rtl) const
222 RenderTable* tableElt = table();
225 leftmostColumn = col() == 0;
227 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
228 leftmostColumn = effCol == tableElt->numEffCols() - 1;
231 // For border left, we need to check, in order of precedence:
232 // (1) Our left border.
233 CollapsedBorderValue result(&style()->borderLeft(), BCELL);
235 // (2) The right border of the cell to the left.
236 RenderTableCell* prevCell = rtl ? tableElt->cellAfter(this) : tableElt->cellBefore(this);
238 result = compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL));
239 if (!result.exists())
241 } else if (leftmostColumn) {
242 // (3) Our row's left border.
243 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderLeft(), BROW));
244 if (!result.exists())
247 // (4) Our row group's left border.
248 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderLeft(), BROWGROUP));
249 if (!result.exists())
253 // (5) Our column's left border.
254 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? colSpan() - 1 : 0));
256 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
257 if (!result.exists())
261 // (6) The right border of the column to the left.
262 if (!leftmostColumn) {
263 colElt = tableElt->colElement(col() + (rtl ? colSpan() : -1));
265 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
266 if (!result.exists())
270 // (7) The table's left border.
271 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderLeft(), BTABLE));
272 if (!result.exists())
279 CollapsedBorderValue RenderTableCell::collapsedRightBorder(bool rtl) const
281 RenderTable* tableElt = table();
282 bool rightmostColumn;
284 rightmostColumn = col() == 0;
286 int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
287 rightmostColumn = effCol == tableElt->numEffCols() - 1;
290 // For border right, we need to check, in order of precedence:
291 // (1) Our right border.
292 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderRight(), BCELL);
294 // (2) The left border of the cell to the right.
295 if (!rightmostColumn) {
296 RenderTableCell* nextCell = rtl ? tableElt->cellBefore(this) : tableElt->cellAfter(this);
297 if (nextCell && nextCell->style()) {
298 result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL));
299 if (!result.exists())
303 // (3) Our row's right border.
304 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderRight(), BROW));
305 if (!result.exists())
308 // (4) Our row group's right border.
309 result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderRight(), BROWGROUP));
310 if (!result.exists())
314 // (5) Our column's right border.
315 RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? 0 : colSpan() - 1));
317 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
318 if (!result.exists())
322 // (6) The left border of the column to the right.
323 if (!rightmostColumn) {
324 colElt = tableElt->colElement(col() + (rtl ? -1 : colSpan()));
326 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
327 if (!result.exists())
331 // (7) The table's right border.
332 result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderRight(), BTABLE));
333 if (!result.exists())
340 CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
342 // For border top, we need to check, in order of precedence:
343 // (1) Our top border.
344 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderTop(), BCELL);
346 RenderTableCell* prevCell = table()->cellAbove(this);
348 // (2) A previous cell's bottom border.
349 result = compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderBottom(), BCELL));
350 if (!result.exists())
354 // (3) Our row's top border.
355 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderTop(), BROW));
356 if (!result.exists())
359 // (4) The previous row's bottom border.
361 RenderObject* prevRow = 0;
362 if (prevCell->section() == section())
363 prevRow = parent()->previousSibling();
365 prevRow = prevCell->section()->lastChild();
368 result = compareBorders(result, CollapsedBorderValue(&prevRow->style()->borderBottom(), BROW));
369 if (!result.exists())
374 // Now check row groups.
375 RenderObject* currSection = parent()->parent();
377 // (5) Our row group's top border.
378 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
379 if (!result.exists())
382 // (6) Previous row group's bottom border.
383 for (currSection = currSection->previousSibling(); currSection;
384 currSection = currSection->previousSibling()) {
385 if (currSection->isTableSection()) {
386 RenderTableSection* section = static_cast<RenderTableSection*>(currSection);
387 result = compareBorders(result, CollapsedBorderValue(§ion->style()->borderBottom(), BROWGROUP));
388 if (!result.exists())
395 // (8) Our column's top border.
396 RenderTableCol* colElt = table()->colElement(col());
398 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderTop(), BCOL));
399 if (!result.exists())
403 // (9) The table's top border.
404 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderTop(), BTABLE));
405 if (!result.exists())
412 CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
414 // For border top, we need to check, in order of precedence:
415 // (1) Our bottom border.
416 CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBottom(), BCELL);
418 RenderTableCell* nextCell = table()->cellBelow(this);
420 // (2) A following cell's top border.
421 result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderTop(), BCELL));
422 if (!result.exists())
426 // (3) Our row's bottom border. (FIXME: Deal with rowspan!)
427 result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderBottom(), BROW));
428 if (!result.exists())
431 // (4) The next row's top border.
433 result = compareBorders(result, CollapsedBorderValue(&nextCell->parent()->style()->borderTop(), BROW));
434 if (!result.exists())
438 // Now check row groups.
439 RenderObject* currSection = parent()->parent();
440 if (row() + rowSpan() >= static_cast<RenderTableSection*>(currSection)->numRows()) {
441 // (5) Our row group's bottom border.
442 result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP));
443 if (!result.exists())
446 // (6) Following row group's top border.
447 for (currSection = currSection->nextSibling(); currSection;
448 currSection = currSection->nextSibling()) {
449 if (currSection->isTableSection()) {
450 RenderTableSection* section = static_cast<RenderTableSection*>(currSection);
451 result = compareBorders(result, CollapsedBorderValue(§ion->style()->borderTop(), BROWGROUP));
452 if (!result.exists())
459 // (8) Our column's bottom border.
460 RenderTableCol* colElt = table()->colElement(col());
462 result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderBottom(), BCOL));
463 if (!result.exists()) return result;
466 // (9) The table's bottom border.
467 result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderBottom(), BTABLE));
468 if (!result.exists())
475 int RenderTableCell::borderLeft() const
477 if (table()->collapseBorders()) {
478 CollapsedBorderValue border = collapsedLeftBorder(table()->style()->direction() == RTL);
480 return int(border.width() / 2.0 + 0.5); // Give the extra pixel to top and left.
483 return RenderBlock::borderLeft();
486 int RenderTableCell::borderRight() const
488 if (table()->collapseBorders()) {
489 CollapsedBorderValue border = collapsedRightBorder(table()->style()->direction() == RTL);
491 return border.width() / 2;
494 return RenderBlock::borderRight();
497 int RenderTableCell::borderTop() const
499 if (table()->collapseBorders()) {
500 CollapsedBorderValue border = collapsedTopBorder();
502 return int(border.width() / 2.0 + 0.5); // Give the extra pixel to top and left.
505 return RenderBlock::borderTop();
508 int RenderTableCell::borderBottom() const
510 if (table()->collapseBorders()) {
511 CollapsedBorderValue border = collapsedBottomBorder();
513 return border.width() / 2;
516 return RenderBlock::borderBottom();
520 static void outlineBox(GraphicsContext* p, int _tx, int _ty, int w, int h)
522 p->setPen(Pen(Color("yellow"), 3, Qt::DotLine));
523 p->setBrush(Qt::NoBrush);
524 p->drawRect(_tx, _ty, w, h);
528 void RenderTableCell::paint(PaintInfo& i, int _tx, int _ty)
533 // check if we need to do anything at all...
534 int os = 2*maximalOutlineSize(i.phase);
535 if (_ty >= i.r.bottom() + os || _ty + _topExtra + m_height + _bottomExtra <= i.r.y() - os)
538 if (i.phase == PaintActionCollapsedTableBorders && style()->visibility() == VISIBLE) {
540 int h = height() + borderTopExtra() + borderBottomExtra();
541 paintCollapsedBorder(i.p, _tx, _ty, w, h);
543 RenderBlock::paintObject(i, _tx, _ty + _topExtra);
546 ::outlineBox( i.p, _tx, _ty, width(), height() + borderTopExtra() + borderBottomExtra());
550 static EBorderStyle collapsedBorderStyle(EBorderStyle style)
554 else if (style == INSET)
559 struct CollapsedBorder {
562 CollapsedBorderValue border;
563 RenderObject::BorderSide side;
572 class CollapsedBorders
575 CollapsedBorders(int i) :count(0) {}
577 void addBorder(const CollapsedBorderValue& b, RenderObject::BorderSide s, bool paint,
578 int _x1, int _y1, int _x2, int _y2,
581 if (b.exists() && paint) {
582 borders[count].border = b;
583 borders[count].side = s;
584 borders[count].shouldPaint = paint;
585 borders[count].x1 = _x1;
586 borders[count].x2 = _x2;
587 borders[count].y1 = _y1;
588 borders[count].y2 = _y2;
589 borders[count].style = _style;
594 CollapsedBorder* nextBorder() {
595 for (int i = 0; i < count; i++) {
596 if (borders[i].border.exists() && borders[i].shouldPaint) {
597 borders[i].shouldPaint = false;
605 CollapsedBorder borders[4];
609 static void addBorderStyle(QValueList<CollapsedBorderValue>& borderStyles, CollapsedBorderValue borderValue)
611 if (!borderValue.exists() || borderStyles.contains(borderValue))
614 QValueListIterator<CollapsedBorderValue> it = borderStyles.begin();
615 QValueListIterator<CollapsedBorderValue> end = borderStyles.end();
616 for (; it != end; ++it) {
617 CollapsedBorderValue result = compareBorders(*it, borderValue);
619 borderStyles.insert(it, borderValue);
624 borderStyles.append(borderValue);
627 void RenderTableCell::collectBorders(QValueList<CollapsedBorderValue>& borderStyles)
629 bool rtl = table()->style()->direction() == RTL;
630 addBorderStyle(borderStyles, collapsedLeftBorder(rtl));
631 addBorderStyle(borderStyles, collapsedRightBorder(rtl));
632 addBorderStyle(borderStyles, collapsedTopBorder());
633 addBorderStyle(borderStyles, collapsedBottomBorder());
636 void RenderTableCell::paintCollapsedBorder(GraphicsContext* p, int _tx, int _ty, int w, int h)
638 if (!table()->currentBorderStyle())
641 bool rtl = table()->style()->direction() == RTL;
642 CollapsedBorderValue leftVal = collapsedLeftBorder(rtl);
643 CollapsedBorderValue rightVal = collapsedRightBorder(rtl);
644 CollapsedBorderValue topVal = collapsedTopBorder();
645 CollapsedBorderValue bottomVal = collapsedBottomBorder();
647 // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
648 int topWidth = topVal.width();
649 int bottomWidth = bottomVal.width();
650 int leftWidth = leftVal.width();
651 int rightWidth = rightVal.width();
653 _tx -= leftWidth / 2;
655 w += leftWidth / 2 + int(rightWidth / 2.0 + 0.5);
656 h += topWidth / 2 + int(bottomWidth / 2.0 + 0.5);
658 bool tt = topVal.isTransparent();
659 bool bt = bottomVal.isTransparent();
660 bool rt = rightVal.isTransparent();
661 bool lt = leftVal.isTransparent();
663 EBorderStyle ts = collapsedBorderStyle(topVal.style());
664 EBorderStyle bs = collapsedBorderStyle(bottomVal.style());
665 EBorderStyle ls = collapsedBorderStyle(leftVal.style());
666 EBorderStyle rs = collapsedBorderStyle(rightVal.style());
668 bool render_t = ts > BHIDDEN && !tt;
669 bool render_l = ls > BHIDDEN && !lt;
670 bool render_r = rs > BHIDDEN && !rt;
671 bool render_b = bs > BHIDDEN && !bt;
673 // We never paint diagonals at the joins. We simply let the border with the highest
674 // precedence paint on top of borders with lower precedence.
675 CollapsedBorders borders(4);
676 borders.addBorder(topVal, BSTop, render_t, _tx, _ty, _tx + w, _ty + topWidth, ts);
677 borders.addBorder(bottomVal, BSBottom, render_b, _tx, _ty + h - bottomWidth, _tx + w, _ty + h, bs);
678 borders.addBorder(leftVal, BSLeft, render_l, _tx, _ty, _tx + leftWidth, _ty + h, ls);
679 borders.addBorder(rightVal, BSRight, render_r, _tx + w - rightWidth, _ty, _tx + w, _ty + h, rs);
681 for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
682 if (border->border == *table()->currentBorderStyle())
683 drawBorder(p, border->x1, border->y1, border->x2, border->y2, border->side,
684 border->border.color(), style()->color(), border->style, 0, 0);
688 IntRect RenderTableCell::getAbsoluteRepaintRect()
690 int ow = style() ? style()->outlineSize() : 0;
691 IntRect r(-ow, -ow - borderTopExtra(),
692 overflowWidth(false) + ow * 2, overflowHeight(false) + borderTopExtra() + borderBottomExtra() + ow * 2);
693 computeAbsoluteRepaintRect(r);
697 void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& i, int _tx, int _ty, RenderObject* backgroundObject)
699 if (!backgroundObject)
702 RenderTable* tableElt = table();
703 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
706 if (backgroundObject != this) {
708 _ty += m_y + _topExtra;
712 int h = height() + borderTopExtra() + borderBottomExtra();
713 _ty -= borderTopExtra();
715 int my = kMax(_ty, i.r.y());
716 int end = kMin(i.r.bottom(), _ty + h);
719 Color c = backgroundObject->style()->backgroundColor();
720 const BackgroundLayer* bgLayer = backgroundObject->style()->backgroundLayers();
722 if (bgLayer->hasImage() || c.isValid()) {
723 // We have to clip here because the background would paint
724 // on top of the borders otherwise. This only matters for cells and rows.
725 bool hasLayer = backgroundObject->layer() && (backgroundObject == this || backgroundObject == parent());
726 if (hasLayer && tableElt->collapseBorders()) {
727 IntRect clipRect(_tx + borderLeft(), _ty + borderTop(),
728 w - borderLeft() - borderRight(), h - borderTop() - borderBottom());
730 i.p->addClip(clipRect);
732 paintBackground(i.p, c, bgLayer, my, mh, _tx, _ty, w, h);
733 if (hasLayer && tableElt->collapseBorders())
738 void RenderTableCell::paintBoxDecorations(PaintInfo& i, int _tx, int _ty)
740 RenderTable* tableElt = table();
741 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
744 // Paint our cell background.
745 paintBackgroundsBehindCell(i, _tx, _ty, this);
748 int h = height() + borderTopExtra() + borderBottomExtra();
749 _ty -= borderTopExtra();
751 if (style()->hasBorder() && !tableElt->collapseBorders())
752 paintBorder(i.p, _tx, _ty, w, h, style());
756 void RenderTableCell::dump(QTextStream *stream, QString ind) const
758 *stream << " row=" << _row;
759 *stream << " col=" << _col;
760 *stream << " rSpan=" << rSpan;
761 *stream << " cSpan=" << cSpan;
762 // *stream << " nWrap=" << nWrap;
764 RenderBlock::dump(stream,ind);