RenderTableCell.cpp [plain text]
#include "config.h"
#include "RenderTableCell.h"
#include "GraphicsContext.h"
#include "RenderTableCol.h"
#include "HTMLNames.h"
#include "HTMLTableCellElement.h"
#include "TextStream.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderTableCell::RenderTableCell(Node* _node)
: RenderBlock(_node)
{
_col = -1;
_row = -1;
cSpan = rSpan = 1;
updateFromElement();
setShouldPaintBackgroundOrBorder(true);
_topExtra = 0;
_bottomExtra = 0;
m_percentageHeight = 0;
}
void RenderTableCell::destroy()
{
if (parent() && section())
section()->setNeedCellRecalc();
RenderBlock::destroy();
}
void RenderTableCell::updateFromElement()
{
int oldRSpan = rSpan;
int oldCSpan = cSpan;
Node* node = element();
if (node && (node->hasTagName(tdTag) || node->hasTagName(thTag))) {
HTMLTableCellElement* tc = static_cast<HTMLTableCellElement*>(node);
cSpan = tc->colSpan();
rSpan = tc->rowSpan();
}
if ((oldRSpan != rSpan || oldCSpan != cSpan) && style() && parent())
setNeedsLayoutAndMinMaxRecalc();
}
Length RenderTableCell::styleOrColWidth()
{
Length w = style()->width();
if (colSpan() > 1 || !w.isAuto())
return w;
RenderTableCol* col = table()->colElement(_col);
if (col)
w = col->style()->width();
return w;
}
void RenderTableCell::calcMinMaxWidth()
{
table()->recalcSectionsIfNeeded();
RenderBlock::calcMinMaxWidth();
if (element() && style()->autoWrap()) {
Length w = styleOrColWidth();
String nowrap = static_cast<Element*>(element())->getAttribute(nowrapAttr);
if (!nowrap.isNull() && w.isFixed())
if (m_minWidth < w.value())
m_minWidth = w.value();
}
}
void RenderTableCell::calcWidth()
{
}
void RenderTableCell::setWidth(int width)
{
if (width != m_width) {
m_width = width;
m_widthChanged = true;
}
}
void RenderTableCell::layout()
{
layoutBlock(m_widthChanged);
m_widthChanged = false;
}
void RenderTableCell::computeAbsoluteRepaintRect(IntRect& r, bool f)
{
r.setY(r.y() + _topExtra);
r.move(-parent()->xPos(), -parent()->yPos()); RenderBlock::computeAbsoluteRepaintRect(r, f);
}
bool RenderTableCell::absolutePosition(int &xPos, int &yPos, bool f)
{
bool result = RenderBlock::absolutePosition(xPos, yPos, f);
xPos -= parent()->xPos(); yPos -= parent()->yPos();
return result;
}
short RenderTableCell::baselinePosition(bool) const
{
RenderObject* o = firstChild();
int offset = paddingTop() + borderTop();
if (!o)
return offset + contentHeight();
while (o->firstChild() && !o->isReplaced()) {
if (!o->isInline())
offset += o->paddingTop() + o->borderTop();
o = o->firstChild();
}
if (!o->isInline())
return paddingTop() + borderTop() + contentHeight();
offset += o->baselinePosition(true);
return offset;
}
void RenderTableCell::setStyle(RenderStyle *newStyle)
{
if (parent() && section() && style() && style()->height() != newStyle->height())
section()->setNeedCellRecalc();
newStyle->setDisplay(TABLE_CELL);
if (newStyle->whiteSpace() == KHTML_NOWRAP) {
if (newStyle->width().isFixed())
newStyle->setWhiteSpace(NORMAL);
else
newStyle->setWhiteSpace(NOWRAP);
}
RenderBlock::setStyle(newStyle);
setShouldPaintBackgroundOrBorder(true);
}
bool RenderTableCell::requiresLayer()
{
return isPositioned() || isTransparent() || hasOverflowClip();
}
static CollapsedBorderValue compareBorders(const CollapsedBorderValue& border1,
const CollapsedBorderValue& border2)
{
if (!border2.exists()) return border1;
if (!border1.exists()) return border2;
if (border1.style() == BHIDDEN || border2.style() == BHIDDEN)
return CollapsedBorderValue();
if (border2.style() == BNONE) return border1;
if (border1.style() == BNONE) return border2;
if (border1.width() != border2.width())
return border1.width() > border2.width() ? border1 : border2;
if (border1.style() != border2.style())
return border1.style() > border2.style() ? border1 : border2;
return border1.precedence >= border2.precedence ? border1 : border2;
}
CollapsedBorderValue RenderTableCell::collapsedLeftBorder(bool rtl) const
{
RenderTable* tableElt = table();
bool leftmostColumn;
if (!rtl)
leftmostColumn = col() == 0;
else {
int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
leftmostColumn = effCol == tableElt->numEffCols() - 1;
}
CollapsedBorderValue result(&style()->borderLeft(), BCELL);
RenderTableCell* prevCell = rtl ? tableElt->cellAfter(this) : tableElt->cellBefore(this);
if (prevCell) {
result = compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL));
if (!result.exists())
return result;
} else if (leftmostColumn) {
result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderLeft(), BROW));
if (!result.exists())
return result;
result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderLeft(), BROWGROUP));
if (!result.exists())
return result;
}
RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? colSpan() - 1 : 0));
if (colElt) {
result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
if (!result.exists())
return result;
}
if (!leftmostColumn) {
colElt = tableElt->colElement(col() + (rtl ? colSpan() : -1));
if (colElt) {
result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
if (!result.exists())
return result;
}
} else {
result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderLeft(), BTABLE));
if (!result.exists())
return result;
}
return result;
}
CollapsedBorderValue RenderTableCell::collapsedRightBorder(bool rtl) const
{
RenderTable* tableElt = table();
bool rightmostColumn;
if (rtl)
rightmostColumn = col() == 0;
else {
int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
rightmostColumn = effCol == tableElt->numEffCols() - 1;
}
CollapsedBorderValue result = CollapsedBorderValue(&style()->borderRight(), BCELL);
if (!rightmostColumn) {
RenderTableCell* nextCell = rtl ? tableElt->cellBefore(this) : tableElt->cellAfter(this);
if (nextCell && nextCell->style()) {
result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL));
if (!result.exists())
return result;
}
} else {
result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderRight(), BROW));
if (!result.exists())
return result;
result = compareBorders(result, CollapsedBorderValue(§ion()->style()->borderRight(), BROWGROUP));
if (!result.exists())
return result;
}
RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? 0 : colSpan() - 1));
if (colElt) {
result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
if (!result.exists())
return result;
}
if (!rightmostColumn) {
colElt = tableElt->colElement(col() + (rtl ? -1 : colSpan()));
if (colElt) {
result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
if (!result.exists())
return result;
}
} else {
result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderRight(), BTABLE));
if (!result.exists())
return result;
}
return result;
}
CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
{
CollapsedBorderValue result = CollapsedBorderValue(&style()->borderTop(), BCELL);
RenderTableCell* prevCell = table()->cellAbove(this);
if (prevCell) {
result = compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderBottom(), BCELL));
if (!result.exists())
return result;
}
result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderTop(), BROW));
if (!result.exists())
return result;
if (prevCell) {
RenderObject* prevRow = 0;
if (prevCell->section() == section())
prevRow = parent()->previousSibling();
else
prevRow = prevCell->section()->lastChild();
if (prevRow) {
result = compareBorders(result, CollapsedBorderValue(&prevRow->style()->borderBottom(), BROW));
if (!result.exists())
return result;
}
}
RenderTableSection* currSection = section();
if (row() == 0) {
result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
if (!result.exists())
return result;
currSection = table()->sectionAbove(currSection);
if (currSection) {
result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP));
if (!result.exists())
return result;
}
}
if (!currSection) {
RenderTableCol* colElt = table()->colElement(col());
if (colElt) {
result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderTop(), BCOL));
if (!result.exists())
return result;
}
result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderTop(), BTABLE));
if (!result.exists())
return result;
}
return result;
}
CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
{
CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBottom(), BCELL);
RenderTableCell* nextCell = table()->cellBelow(this);
if (nextCell) {
result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderTop(), BCELL));
if (!result.exists())
return result;
}
result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderBottom(), BROW));
if (!result.exists())
return result;
if (nextCell) {
result = compareBorders(result, CollapsedBorderValue(&nextCell->parent()->style()->borderTop(), BROW));
if (!result.exists())
return result;
}
RenderTableSection* currSection = section();
if (row() + rowSpan() >= static_cast<RenderTableSection*>(currSection)->numRows()) {
result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP));
if (!result.exists())
return result;
currSection = table()->sectionBelow(currSection);
if (currSection) {
result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
if (!result.exists())
return result;
}
}
if (!currSection) {
RenderTableCol* colElt = table()->colElement(col());
if (colElt) {
result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderBottom(), BCOL));
if (!result.exists()) return result;
}
result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderBottom(), BTABLE));
if (!result.exists())
return result;
}
return result;
}
int RenderTableCell::borderLeft() const
{
if (table()->collapseBorders()) {
CollapsedBorderValue border = collapsedLeftBorder(table()->style()->direction() == RTL);
if (border.exists())
return (border.width() + 1) / 2; return 0;
}
return RenderBlock::borderLeft();
}
int RenderTableCell::borderRight() const
{
if (table()->collapseBorders()) {
CollapsedBorderValue border = collapsedRightBorder(table()->style()->direction() == RTL);
if (border.exists())
return border.width() / 2;
return 0;
}
return RenderBlock::borderRight();
}
int RenderTableCell::borderTop() const
{
if (table()->collapseBorders()) {
CollapsedBorderValue border = collapsedTopBorder();
if (border.exists())
return (border.width() + 1) / 2; return 0;
}
return RenderBlock::borderTop();
}
int RenderTableCell::borderBottom() const
{
if (table()->collapseBorders()) {
CollapsedBorderValue border = collapsedBottomBorder();
if (border.exists())
return border.width() / 2;
return 0;
}
return RenderBlock::borderBottom();
}
#ifdef BOX_DEBUG
static void outlineBox(GraphicsContext* p, int _tx, int _ty, int w, int h)
{
p->setPen(Pen(Color("yellow"), 3, Qt::DotLine));
p->setBrush(Qt::NoBrush);
p->drawRect(_tx, _ty, w, h);
}
#endif
void RenderTableCell::paint(PaintInfo& i, int _tx, int _ty)
{
_tx += m_x;
_ty += m_y;
int os = 2*maximalOutlineSize(i.phase);
if (i.phase == PaintPhaseCollapsedTableBorders && style()->visibility() == VISIBLE) {
if (_ty - table()->outerBorderTop() >= i.r.bottom() + os || _ty + _topExtra + m_height + _bottomExtra + table()->outerBorderBottom() <= i.r.y() - os)
return;
int w = width();
int h = height() + borderTopExtra() + borderBottomExtra();
paintCollapsedBorder(i.p, _tx, _ty, w, h);
} else {
if (_ty >= i.r.bottom() + os || _ty + _topExtra + m_height + _bottomExtra <= i.r.y() - os)
return;
RenderBlock::paintObject(i, _tx, _ty + _topExtra);
}
#ifdef BOX_DEBUG
::outlineBox( i.p, _tx, _ty, width(), height() + borderTopExtra() + borderBottomExtra());
#endif
}
static EBorderStyle collapsedBorderStyle(EBorderStyle style)
{
if (style == OUTSET)
style = GROOVE;
else if (style == INSET)
style = RIDGE;
return style;
}
struct CollapsedBorder {
CollapsedBorder(){}
CollapsedBorderValue border;
RenderObject::BorderSide side;
bool shouldPaint;
int x1;
int y1;
int x2;
int y2;
EBorderStyle style;
};
class CollapsedBorders
{
public:
CollapsedBorders(int i) :count(0) {}
void addBorder(const CollapsedBorderValue& b, RenderObject::BorderSide s, bool paint,
int _x1, int _y1, int _x2, int _y2,
EBorderStyle _style)
{
if (b.exists() && paint) {
borders[count].border = b;
borders[count].side = s;
borders[count].shouldPaint = paint;
borders[count].x1 = _x1;
borders[count].x2 = _x2;
borders[count].y1 = _y1;
borders[count].y2 = _y2;
borders[count].style = _style;
count++;
}
}
CollapsedBorder* nextBorder() {
for (int i = 0; i < count; i++) {
if (borders[i].border.exists() && borders[i].shouldPaint) {
borders[i].shouldPaint = false;
return &borders[i];
}
}
return 0;
}
CollapsedBorder borders[4];
int count;
};
static void addBorderStyle(DeprecatedValueList<CollapsedBorderValue>& borderStyles, CollapsedBorderValue borderValue)
{
if (!borderValue.exists() || borderStyles.contains(borderValue))
return;
DeprecatedValueListIterator<CollapsedBorderValue> it = borderStyles.begin();
DeprecatedValueListIterator<CollapsedBorderValue> end = borderStyles.end();
for (; it != end; ++it) {
CollapsedBorderValue result = compareBorders(*it, borderValue);
if (result == *it) {
borderStyles.insert(it, borderValue);
return;
}
}
borderStyles.append(borderValue);
}
void RenderTableCell::collectBorders(DeprecatedValueList<CollapsedBorderValue>& borderStyles)
{
bool rtl = table()->style()->direction() == RTL;
addBorderStyle(borderStyles, collapsedLeftBorder(rtl));
addBorderStyle(borderStyles, collapsedRightBorder(rtl));
addBorderStyle(borderStyles, collapsedTopBorder());
addBorderStyle(borderStyles, collapsedBottomBorder());
}
void RenderTableCell::paintCollapsedBorder(GraphicsContext* p, int _tx, int _ty, int w, int h)
{
if (!table()->currentBorderStyle())
return;
bool rtl = table()->style()->direction() == RTL;
CollapsedBorderValue leftVal = collapsedLeftBorder(rtl);
CollapsedBorderValue rightVal = collapsedRightBorder(rtl);
CollapsedBorderValue topVal = collapsedTopBorder();
CollapsedBorderValue bottomVal = collapsedBottomBorder();
int topWidth = topVal.width();
int bottomWidth = bottomVal.width();
int leftWidth = leftVal.width();
int rightWidth = rightVal.width();
_tx -= leftWidth / 2;
_ty -= topWidth / 2;
w += leftWidth / 2 + (rightWidth + 1) / 2;
h += topWidth / 2 + (bottomWidth + 1) / 2;
bool tt = topVal.isTransparent();
bool bt = bottomVal.isTransparent();
bool rt = rightVal.isTransparent();
bool lt = leftVal.isTransparent();
EBorderStyle ts = collapsedBorderStyle(topVal.style());
EBorderStyle bs = collapsedBorderStyle(bottomVal.style());
EBorderStyle ls = collapsedBorderStyle(leftVal.style());
EBorderStyle rs = collapsedBorderStyle(rightVal.style());
bool render_t = ts > BHIDDEN && !tt;
bool render_l = ls > BHIDDEN && !lt;
bool render_r = rs > BHIDDEN && !rt;
bool render_b = bs > BHIDDEN && !bt;
CollapsedBorders borders(4);
borders.addBorder(topVal, BSTop, render_t, _tx, _ty, _tx + w, _ty + topWidth, ts);
borders.addBorder(bottomVal, BSBottom, render_b, _tx, _ty + h - bottomWidth, _tx + w, _ty + h, bs);
borders.addBorder(leftVal, BSLeft, render_l, _tx, _ty, _tx + leftWidth, _ty + h, ls);
borders.addBorder(rightVal, BSRight, render_r, _tx + w - rightWidth, _ty, _tx + w, _ty + h, rs);
for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
if (border->border == *table()->currentBorderStyle())
drawBorder(p, border->x1, border->y1, border->x2, border->y2, border->side,
border->border.color(), style()->color(), border->style, 0, 0);
}
}
void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& i, int _tx, int _ty, RenderObject* backgroundObject)
{
if (!backgroundObject)
return;
RenderTable* tableElt = table();
if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
return;
if (backgroundObject != this) {
_tx += m_x;
_ty += m_y + _topExtra;
}
int w = width();
int h = height() + borderTopExtra() + borderBottomExtra();
_ty -= borderTopExtra();
int my = max(_ty, i.r.y());
int end = min(i.r.bottom(), _ty + h);
int mh = end - my;
Color c = backgroundObject->style()->backgroundColor();
const BackgroundLayer* bgLayer = backgroundObject->style()->backgroundLayers();
if (bgLayer->hasImage() || c.isValid()) {
bool hasLayer = backgroundObject->layer() && (backgroundObject == this || backgroundObject == parent());
if (hasLayer && tableElt->collapseBorders()) {
IntRect clipRect(_tx + borderLeft(), _ty + borderTop(),
w - borderLeft() - borderRight(), h - borderTop() - borderBottom());
i.p->save();
i.p->addClip(clipRect);
}
paintBackground(i.p, c, bgLayer, my, mh, _tx, _ty, w, h);
if (hasLayer && tableElt->collapseBorders())
i.p->restore();
}
}
void RenderTableCell::paintBoxDecorations(PaintInfo& i, int _tx, int _ty)
{
RenderTable* tableElt = table();
if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
return;
paintBackgroundsBehindCell(i, _tx, _ty, this);
int w = width();
int h = height() + borderTopExtra() + borderBottomExtra();
_ty -= borderTopExtra();
if (style()->hasBorder() && !tableElt->collapseBorders())
paintBorder(i.p, _tx, _ty, w, h, style());
}
#ifndef NDEBUG
void RenderTableCell::dump(TextStream *stream, DeprecatedString ind) const
{
*stream << " row=" << _row;
*stream << " col=" << _col;
*stream << " rSpan=" << rSpan;
*stream << " cSpan=" << cSpan;
RenderBlock::dump(stream,ind);
}
#endif
}