1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
|
/*
* Copyright (C) 1997 Martin Jones (mjones@kde.org)
* (C) 1997 Torben Weis (weis@kde.org)
* (C) 1998 Waldo Bastian (bastian@kde.org)
* (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "RenderTable.h"
#include "AutoTableLayout.h"
#include "DeleteButtonController.h"
#include "Document.h"
#include "FixedTableLayout.h"
#include "FrameView.h"
#include "HTMLNames.h"
#include "RenderLayer.h"
#include "RenderTableCell.h"
#include "RenderTableCol.h"
#include "RenderTableSection.h"
#include "RenderView.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderTable::RenderTable(Node* node)
: RenderBlock(node)
, m_caption(0)
, m_head(0)
, m_foot(0)
, m_firstBody(0)
, m_tableLayout(0)
, m_currentBorder(0)
, m_frame(Void)
, m_rules(None)
, m_hasColElements(false)
, m_needsSectionRecalc(0)
, m_hSpacing(0)
, m_vSpacing(0)
, m_borderLeft(0)
, m_borderRight(0)
{
m_columnPos.fill(0, 2);
m_columns.fill(ColumnStruct(), 1);
}
RenderTable::~RenderTable()
{
delete m_tableLayout;
}
void RenderTable::setStyle(RenderStyle* newStyle)
{
ETableLayout oldTableLayout = style() ? style()->tableLayout() : TAUTO;
RenderBlock::setStyle(newStyle);
// In the collapsed border model, there is no cell spacing.
m_hSpacing = collapseBorders() ? 0 : style()->horizontalBorderSpacing();
m_vSpacing = collapseBorders() ? 0 : style()->verticalBorderSpacing();
m_columnPos[0] = m_hSpacing;
if (!m_tableLayout || style()->tableLayout() != oldTableLayout) {
delete m_tableLayout;
// According to the CSS2 spec, you only use fixed table layout if an
// explicit width is specified on the table. Auto width implies auto table layout.
if (style()->tableLayout() == TFIXED && !style()->width().isAuto())
m_tableLayout = new FixedTableLayout(this);
else
m_tableLayout = new AutoTableLayout(this);
}
}
static inline void resetSectionPointerIfNotBefore(RenderTableSection*& ptr, RenderObject* before)
{
if (!before || !ptr)
return;
RenderObject* o = before->previousSibling();
while (o && o != ptr)
o = o->previousSibling();
if (!o)
ptr = 0;
}
void RenderTable::addChild(RenderObject* child, RenderObject* beforeChild)
{
// Make sure we don't append things after :after-generated content if we have it.
if (!beforeChild && isAfterContent(lastChild()))
beforeChild = lastChild();
bool wrapInAnonymousSection = true;
bool isTableElement = element() && element()->hasTagName(tableTag);
if (child->isRenderBlock() && child->style()->display() == TABLE_CAPTION) {
// First caption wins.
if (beforeChild && m_caption) {
RenderObject* o = beforeChild->previousSibling();
while (o && o != m_caption)
o = o->previousSibling();
if (!o)
m_caption = 0;
}
if (!m_caption)
m_caption = static_cast<RenderBlock*>(child);
wrapInAnonymousSection = false;
} else if (child->isTableCol()) {
m_hasColElements = true;
wrapInAnonymousSection = false;
} else if (child->isTableSection()) {
switch (child->style()->display()) {
case TABLE_HEADER_GROUP:
if (child->isTableSection()) {
resetSectionPointerIfNotBefore(m_head, beforeChild);
if (!m_head) {
m_head = static_cast<RenderTableSection*>(child);
} else {
resetSectionPointerIfNotBefore(m_firstBody, beforeChild);
if (!m_firstBody)
m_firstBody = static_cast<RenderTableSection*>(child);
}
}
wrapInAnonymousSection = false;
break;
case TABLE_FOOTER_GROUP:
if (child->isTableSection()) {
resetSectionPointerIfNotBefore(m_foot, beforeChild);
if (!m_foot) {
m_foot = static_cast<RenderTableSection*>(child);
wrapInAnonymousSection = false;
break;
}
}
// Fall through.
case TABLE_ROW_GROUP:
if (child->isTableSection()) {
resetSectionPointerIfNotBefore(m_firstBody, beforeChild);
if (!m_firstBody)
m_firstBody = static_cast<RenderTableSection*>(child);
}
wrapInAnonymousSection = false;
break;
default:
ASSERT_NOT_REACHED();
}
} else if (child->isTableCell() || child->isTableRow()) {
wrapInAnonymousSection = true;
} else {
// Allow a form to just sit at the top level.
wrapInAnonymousSection = !isTableElement || !child->element() || !(child->element()->hasTagName(formTag) && document()->isHTMLDocument());
// FIXME: Allow the delete button container element to sit at the top level. This is needed until http://bugs.webkit.org/show_bug.cgi?id=11363 is fixed.
if (wrapInAnonymousSection && child->element() && child->element()->isHTMLElement() && static_cast<HTMLElement*>(child->element())->id() == DeleteButtonController::containerElementIdentifier)
wrapInAnonymousSection = false;
}
if (!wrapInAnonymousSection) {
// If the next renderer is actually wrapped in an anonymous table section, we need to go up and find that.
while (beforeChild && !beforeChild->isTableSection() && !beforeChild->isTableCol() && beforeChild->style()->display() != TABLE_CAPTION)
beforeChild = beforeChild->parent();
RenderContainer::addChild(child, beforeChild);
return;
}
if (!beforeChild && lastChild() && lastChild()->isTableSection() && lastChild()->isAnonymous()) {
lastChild()->addChild(child);
return;
}
RenderObject* lastBox = beforeChild;
while (lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableSection() && lastBox->style()->display() != TABLE_CAPTION)
lastBox = lastBox->parent();
if (lastBox && lastBox->isAnonymous() && !isAfterContent(lastBox)) {
lastBox->addChild(child, beforeChild);
return;
}
if (beforeChild && !beforeChild->isTableSection() && beforeChild->style()->display() != TABLE_CAPTION)
beforeChild = 0;
RenderTableSection* section = new (renderArena()) RenderTableSection(document() /* anonymous */);
RenderStyle* newStyle = new (renderArena()) RenderStyle();
newStyle->inheritFrom(style());
newStyle->setDisplay(TABLE_ROW_GROUP);
section->setStyle(newStyle);
addChild(section, beforeChild);
section->addChild(child);
}
void RenderTable::calcWidth()
{
if (isPositioned())
calcAbsoluteHorizontal();
RenderBlock* cb = containingBlock();
int availableWidth = cb->availableWidth();
LengthType widthType = style()->width().type();
if (widthType > Relative && style()->width().isPositive()) {
// Percent or fixed table
m_width = style()->width().calcMinValue(availableWidth);
m_width = max(minPrefWidth(), m_width);
} else {
// An auto width table should shrink to fit within the line width if necessary in order to
// avoid overlapping floats.
availableWidth = cb->lineWidth(m_y);
// Subtract out any fixed margins from our available width for auto width tables.
int marginTotal = 0;
if (!style()->marginLeft().isAuto())
marginTotal += style()->marginLeft().calcValue(availableWidth);
if (!style()->marginRight().isAuto())
marginTotal += style()->marginRight().calcValue(availableWidth);
// Subtract out our margins to get the available content width.
int availContentWidth = max(0, availableWidth - marginTotal);
// Ensure we aren't bigger than our max width or smaller than our min width.
m_width = min(availContentWidth, maxPrefWidth());
}
m_width = max(m_width, minPrefWidth());
// Finally, with our true width determined, compute our margins for real.
m_marginRight = 0;
m_marginLeft = 0;
calcHorizontalMargins(style()->marginLeft(), style()->marginRight(), availableWidth);
}
void RenderTable::layout()
{
ASSERT(needsLayout());
if (layoutOnlyPositionedObjects())
return;
recalcSectionsIfNeeded();
IntRect oldBounds;
IntRect oldOutlineBox;
bool checkForRepaint = checkForRepaintDuringLayout();
if (checkForRepaint) {
oldBounds = absoluteClippedOverflowRect();
oldOutlineBox = absoluteOutlineBox();
}
view()->pushLayoutState(this, IntSize(m_x, m_y));
m_height = 0;
m_overflowHeight = 0;
m_overflowTop = 0;
initMaxMarginValues();
int oldWidth = m_width;
calcWidth();
if (m_caption && m_width != oldWidth)
m_caption->setNeedsLayout(true, false);
// FIXME: The optimisation below doesn't work since the internal table
// layout could have changed. we need to add a flag to the table
// layout that tells us if something has changed in the min max
// calculations to do it correctly.
// if ( oldWidth != m_width || columns.size() + 1 != columnPos.size() )
m_tableLayout->layout();
setCellWidths();
// layout child objects
int calculatedHeight = 0;
int oldTableTop = m_caption ? m_caption->height() + m_caption->marginTop() + m_caption->marginBottom() : 0;
bool collapsing = collapseBorders();
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
// FIXME: What about a form that has a display value that makes it a table section?
if (child->needsLayout() && !(child->element() && child->element()->hasTagName(formTag)))
child->layout();
if (child->isTableSection()) {
RenderTableSection* section = static_cast<RenderTableSection*>(child);
calculatedHeight += section->calcRowHeight();
if (collapsing)
section->recalcOuterBorder();
}
}
m_overflowWidth = m_width + (collapsing ? outerBorderRight() - borderRight() : 0);
m_overflowLeft = collapsing ? borderLeft() - outerBorderLeft() : 0;
// If any table section moved vertically, we will just repaint everything from that
// section down (it is quite unlikely that any of the following sections
// did not shift).
bool sectionMoved = false;
int movedSectionTop = 0;
// FIXME: Collapse caption margin.
if (m_caption && m_caption->style()->captionSide() != CAPBOTTOM) {
IntRect captionRect(m_caption->xPos(), m_caption->yPos(), m_caption->width(), m_caption->height());
m_caption->setPos(m_caption->marginLeft(), m_height);
if (!selfNeedsLayout() && m_caption->checkForRepaintDuringLayout())
m_caption->repaintDuringLayoutIfMoved(captionRect);
m_height += m_caption->height() + m_caption->marginTop() + m_caption->marginBottom();
m_overflowLeft = min(m_overflowLeft, m_caption->xPos() + m_caption->overflowLeft(false));
m_overflowWidth = max(m_overflowWidth, m_caption->xPos() + m_caption->overflowWidth(false));
m_overflowTop = min(m_overflowTop, m_caption->yPos() + m_caption->overflowTop(false));
m_overflowHeight = max(m_overflowHeight, m_caption->yPos() + m_caption->overflowHeight(false));
if (m_height != oldTableTop) {
sectionMoved = true;
movedSectionTop = min(m_height, oldTableTop);
}
}
int bpTop = borderTop() + (collapsing ? 0 : paddingTop());
int bpBottom = borderBottom() + (collapsing ? 0 : paddingBottom());
m_height += bpTop;
if (!isPositioned())
calcHeight();
Length h = style()->height();
int th = 0;
if (h.isFixed())
// Tables size as though CSS height includes border/padding.
th = h.value() - (bpTop + bpBottom);
else if (h.isPercent())
th = calcPercentageHeight(h);
th = max(0, th);
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (!child->isTableSection())
continue;
// FIXME: Distribute extra height between all table body sections instead of giving it all to the first one.
static_cast<RenderTableSection*>(child)->layoutRows(child == m_firstBody ? max(0, th - calculatedHeight) : 0);
}
if (!m_firstBody && th > calculatedHeight && !style()->htmlHacks()) {
// Completely empty tables (with no sections or anything) should at least honor specified height
// in strict mode.
m_height += th;
}
int bl = borderLeft();
if (!collapsing)
bl += paddingLeft();
// position the table sections
RenderTableSection* section = m_head ? m_head : (m_firstBody ? m_firstBody : m_foot);
while (section) {
if (!sectionMoved && section->yPos() != m_height) {
sectionMoved = true;
movedSectionTop = min(m_height, section->yPos()) + section->overflowTop(false);
}
section->setPos(bl, m_height);
m_height += section->height();
m_overflowLeft = min(m_overflowLeft, section->xPos() + section->overflowLeft(false));
m_overflowWidth = max(m_overflowWidth, section->xPos() + section->overflowWidth(false));
m_overflowTop = min(m_overflowTop, section->yPos() + section->overflowTop(false));
m_overflowHeight = max(m_overflowHeight, section->yPos() + section->overflowHeight(false));
section = sectionBelow(section);
}
m_height += bpBottom;
if (m_caption && m_caption->style()->captionSide() == CAPBOTTOM) {
IntRect captionRect(m_caption->xPos(), m_caption->yPos(), m_caption->width(), m_caption->height());
m_caption->setPos(m_caption->marginLeft(), m_height);
if (!selfNeedsLayout() && m_caption->checkForRepaintDuringLayout())
m_caption->repaintDuringLayoutIfMoved(captionRect);
m_height += m_caption->height() + m_caption->marginTop() + m_caption->marginBottom();
m_overflowLeft = min(m_overflowLeft, m_caption->xPos() + m_caption->overflowLeft(false));
m_overflowWidth = max(m_overflowWidth, m_caption->xPos() + m_caption->overflowWidth(false));
}
if (isPositioned())
calcHeight();
m_overflowHeight = max(m_overflowHeight, m_height);
// table can be containing block of positioned elements.
// FIXME: Only pass true if width or height changed.
layoutPositionedObjects(true);
if (!hasOverflowClip()) {
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
m_overflowLeft = min(m_overflowLeft, boxShadow->x - boxShadow->blur);
m_overflowWidth = max(m_overflowWidth, m_width + boxShadow->x + boxShadow->blur);
m_overflowTop = min(m_overflowTop, boxShadow->y - boxShadow->blur);
m_overflowHeight = max(m_overflowHeight, m_height + boxShadow->y + boxShadow->blur);
}
if (hasReflection()) {
IntRect reflection(reflectionBox());
m_overflowTop = min(m_overflowTop, reflection.y());
m_overflowHeight = max(m_overflowHeight, reflection.bottom());
m_overflowLeft = min(m_overflowLeft, reflection.x());
m_overflowHeight = max(m_overflowWidth, reflection.right());
}
}
view()->popLayoutState();
bool didFullRepaint = true;
// Repaint with our new bounds if they are different from our old bounds.
if (checkForRepaint)
didFullRepaint = repaintAfterLayoutIfNeeded(oldBounds, oldOutlineBox);
if (!didFullRepaint && sectionMoved)
repaintRectangle(IntRect(m_overflowLeft, movedSectionTop, m_overflowWidth - m_overflowLeft, m_overflowHeight - movedSectionTop));
setNeedsLayout(false);
}
void RenderTable::setCellWidths()
{
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isTableSection())
static_cast<RenderTableSection*>(child)->setCellWidths();
}
}
void RenderTable::paint(PaintInfo& paintInfo, int tx, int ty)
{
tx += xPos();
ty += yPos();
PaintPhase paintPhase = paintInfo.phase;
int os = 2 * maximalOutlineSize(paintPhase);
if (ty + overflowTop(false) >= paintInfo.rect.bottom() + os || ty + overflowHeight(false) <= paintInfo.rect.y() - os)
return;
if (tx + overflowLeft(false) >= paintInfo.rect.right() + os || tx + overflowWidth(false) <= paintInfo.rect.x() - os)
return;
if ((paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) && hasBoxDecorations() && style()->visibility() == VISIBLE)
paintBoxDecorations(paintInfo, tx, ty);
if (paintPhase == PaintPhaseMask) {
paintMask(paintInfo, tx, ty);
return;
}
// We're done. We don't bother painting any children.
if (paintPhase == PaintPhaseBlockBackground)
return;
// We don't paint our own background, but we do let the kids paint their backgrounds.
if (paintPhase == PaintPhaseChildBlockBackgrounds)
paintPhase = PaintPhaseChildBlockBackground;
PaintInfo info(paintInfo);
info.phase = paintPhase;
info.paintingRoot = paintingRootForChildren(paintInfo);
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (!child->hasLayer() && (child->isTableSection() || child == m_caption))
child->paint(info, tx, ty);
}
if (collapseBorders() && paintPhase == PaintPhaseChildBlockBackground && style()->visibility() == VISIBLE) {
// Collect all the unique border styles that we want to paint in a sorted list. Once we
// have all the styles sorted, we then do individual passes, painting each style of border
// from lowest precedence to highest precedence.
info.phase = PaintPhaseCollapsedTableBorders;
RenderTableCell::CollapsedBorderStyles borderStyles;
RenderObject* stop = nextInPreOrderAfterChildren();
for (RenderObject* o = firstChild(); o && o != stop; o = o->nextInPreOrder())
if (o->isTableCell())
static_cast<RenderTableCell*>(o)->collectBorderStyles(borderStyles);
RenderTableCell::sortBorderStyles(borderStyles);
size_t count = borderStyles.size();
for (size_t i = 0; i < count; ++i) {
m_currentBorder = &borderStyles[i];
for (RenderObject* child = firstChild(); child; child = child->nextSibling())
if (child->isTableSection())
child->paint(info, tx, ty);
}
m_currentBorder = 0;
}
}
void RenderTable::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
{
int w = width();
int h = height();
// Account for the caption.
if (m_caption) {
int captionHeight = (m_caption->height() + m_caption->marginBottom() + m_caption->marginTop());
h -= captionHeight;
if (m_caption->style()->captionSide() != CAPBOTTOM)
ty += captionHeight;
}
int my = max(ty, paintInfo.rect.y());
int mh;
if (ty < paintInfo.rect.y())
mh = max(0, h - (paintInfo.rect.y() - ty));
else
mh = min(paintInfo.rect.height(), h);
paintBoxShadow(paintInfo.context, tx, ty, w, h, style());
paintFillLayers(paintInfo, style()->backgroundColor(), style()->backgroundLayers(), my, mh, tx, ty, w, h);
if (style()->hasBorder() && !collapseBorders())
paintBorder(paintInfo.context, tx, ty, w, h, style());
}
void RenderTable::paintMask(PaintInfo& paintInfo, int tx, int ty)
{
if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
return;
int w = width();
int h = height();
// Account for the caption.
if (m_caption) {
int captionHeight = (m_caption->height() + m_caption->marginBottom() + m_caption->marginTop());
h -= captionHeight;
if (m_caption->style()->captionSide() != CAPBOTTOM)
ty += captionHeight;
}
int my = max(ty, paintInfo.rect.y());
int mh;
if (ty < paintInfo.rect.y())
mh = max(0, h - (paintInfo.rect.y() - ty));
else
mh = min(paintInfo.rect.height(), h);
paintMaskImages(paintInfo, my, mh, tx, ty, w, h);
}
void RenderTable::calcPrefWidths()
{
ASSERT(prefWidthsDirty());
recalcSectionsIfNeeded();
recalcHorizontalBorders();
m_tableLayout->calcPrefWidths(m_minPrefWidth, m_maxPrefWidth);
if (m_caption)
m_minPrefWidth = max(m_minPrefWidth, m_caption->minPrefWidth());
setPrefWidthsDirty(false);
}
void RenderTable::splitColumn(int pos, int firstSpan)
{
// we need to add a new columnStruct
int oldSize = m_columns.size();
m_columns.grow(oldSize + 1);
int oldSpan = m_columns[pos].span;
ASSERT(oldSpan > firstSpan);
m_columns[pos].span = firstSpan;
memmove(m_columns.data() + pos + 1, m_columns.data() + pos, (oldSize - pos) * sizeof(ColumnStruct));
m_columns[pos + 1].span = oldSpan - firstSpan;
// change width of all rows.
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isTableSection())
static_cast<RenderTableSection*>(child)->splitColumn(pos, oldSize + 1);
}
m_columnPos.grow(numEffCols() + 1);
setNeedsLayoutAndPrefWidthsRecalc();
}
void RenderTable::appendColumn(int span)
{
// easy case.
int pos = m_columns.size();
int newSize = pos + 1;
m_columns.grow(newSize);
m_columns[pos].span = span;
// change width of all rows.
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isTableSection())
static_cast<RenderTableSection*>(child)->appendColumn(pos);
}
m_columnPos.grow(numEffCols() + 1);
setNeedsLayoutAndPrefWidthsRecalc();
}
RenderTableCol* RenderTable::colElement(int col, bool* startEdge, bool* endEdge) const
{
if (!m_hasColElements)
return 0;
RenderObject* child = firstChild();
int cCol = 0;
while (child) {
if (child->isTableCol()) {
RenderTableCol* colElem = static_cast<RenderTableCol*>(child);
int span = colElem->span();
if (!colElem->firstChild()) {
int startCol = cCol;
int endCol = cCol + span - 1;
cCol += span;
if (cCol > col) {
if (startEdge)
*startEdge = startCol == col;
if (endEdge)
*endEdge = endCol == col;
return colElem;
}
}
RenderObject* next = child->firstChild();
if (!next)
next = child->nextSibling();
if (!next && child->parent()->isTableCol())
next = child->parent()->nextSibling();
child = next;
} else if (child == m_caption)
child = child->nextSibling();
else
break;
}
return 0;
}
void RenderTable::recalcSections() const
{
m_caption = 0;
m_head = 0;
m_foot = 0;
m_firstBody = 0;
m_hasColElements = false;
// We need to get valid pointers to caption, head, foot and first body again
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
switch (child->style()->display()) {
case TABLE_CAPTION:
if (!m_caption && child->isRenderBlock()) {
m_caption = static_cast<RenderBlock*>(child);
m_caption->setNeedsLayout(true);
}
break;
case TABLE_COLUMN:
case TABLE_COLUMN_GROUP:
m_hasColElements = true;
break;
case TABLE_HEADER_GROUP:
if (child->isTableSection()) {
RenderTableSection* section = static_cast<RenderTableSection*>(child);
if (!m_head)
m_head = section;
else if (!m_firstBody)
m_firstBody = section;
section->recalcCellsIfNeeded();
}
break;
case TABLE_FOOTER_GROUP:
if (child->isTableSection()) {
RenderTableSection* section = static_cast<RenderTableSection*>(child);
if (!m_foot)
m_foot = section;
else if (!m_firstBody)
m_firstBody = section;
section->recalcCellsIfNeeded();
}
break;
case TABLE_ROW_GROUP:
if (child->isTableSection()) {
RenderTableSection* section = static_cast<RenderTableSection*>(child);
if (!m_firstBody)
m_firstBody = section;
section->recalcCellsIfNeeded();
}
break;
default:
break;
}
}
// repair column count (addChild can grow it too much, because it always adds elements to the last row of a section)
int maxCols = 0;
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isTableSection()) {
RenderTableSection* section = static_cast<RenderTableSection*>(child);
int sectionCols = section->numColumns();
if (sectionCols > maxCols)
maxCols = sectionCols;
}
}
m_columns.resize(maxCols);
m_columnPos.resize(maxCols + 1);
ASSERT(selfNeedsLayout());
m_needsSectionRecalc = false;
}
RenderObject* RenderTable::removeChildNode(RenderObject* child, bool fullRemove)
{
setNeedsSectionRecalc();
return RenderContainer::removeChildNode(child, fullRemove);
}
int RenderTable::calcBorderLeft() const
{
if (collapseBorders()) {
// Determined by the first cell of the first row. See the CSS 2.1 spec, section 17.6.2.
if (!numEffCols())
return 0;
unsigned borderWidth = 0;
const BorderValue& tb = style()->borderLeft();
if (tb.style() == BHIDDEN)
return 0;
if (tb.style() > BHIDDEN)
borderWidth = tb.width;
int leftmostColumn = style()->direction() == RTL ? numEffCols() - 1 : 0;
RenderTableCol* colGroup = colElement(leftmostColumn);
if (colGroup) {
const BorderValue& gb = style()->borderLeft();
if (gb.style() == BHIDDEN)
return 0;
if (gb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(gb.width));
}
RenderTableSection* firstNonEmptySection = m_head ? m_head : (m_firstBody ? m_firstBody : m_foot);
if (firstNonEmptySection && !firstNonEmptySection->numRows())
firstNonEmptySection = sectionBelow(firstNonEmptySection, true);
if (firstNonEmptySection) {
const BorderValue& sb = firstNonEmptySection->style()->borderLeft();
if (sb.style() == BHIDDEN)
return 0;
if (sb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(sb.width));
const RenderTableSection::CellStruct& cs = firstNonEmptySection->cellAt(0, leftmostColumn);
if (cs.cell) {
const BorderValue& cb = cs.cell->style()->borderLeft();
if (cb.style() == BHIDDEN)
return 0;
const BorderValue& rb = cs.cell->parent()->style()->borderLeft();
if (rb.style() == BHIDDEN)
return 0;
if (cb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(cb.width));
if (rb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(rb.width));
}
}
return borderWidth / 2;
}
return RenderBlock::borderLeft();
}
int RenderTable::calcBorderRight() const
{
if (collapseBorders()) {
// Determined by the last cell of the first row. See the CSS 2.1 spec, section 17.6.2.
if (!numEffCols())
return 0;
unsigned borderWidth = 0;
const BorderValue& tb = style()->borderRight();
if (tb.style() == BHIDDEN)
return 0;
if (tb.style() > BHIDDEN)
borderWidth = tb.width;
int rightmostColumn = style()->direction() == RTL ? 0 : numEffCols() - 1;
RenderTableCol* colGroup = colElement(rightmostColumn);
if (colGroup) {
const BorderValue& gb = style()->borderRight();
if (gb.style() == BHIDDEN)
return 0;
if (gb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(gb.width));
}
RenderTableSection* firstNonEmptySection = m_head ? m_head : (m_firstBody ? m_firstBody : m_foot);
if (firstNonEmptySection && !firstNonEmptySection->numRows())
firstNonEmptySection = sectionBelow(firstNonEmptySection, true);
if (firstNonEmptySection) {
const BorderValue& sb = firstNonEmptySection->style()->borderRight();
if (sb.style() == BHIDDEN)
return 0;
if (sb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(sb.width));
const RenderTableSection::CellStruct& cs = firstNonEmptySection->cellAt(0, rightmostColumn);
if (cs.cell) {
const BorderValue& cb = cs.cell->style()->borderRight();
if (cb.style() == BHIDDEN)
return 0;
const BorderValue& rb = cs.cell->parent()->style()->borderRight();
if (rb.style() == BHIDDEN)
return 0;
if (cb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(cb.width));
if (rb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<unsigned>(rb.width));
}
}
return (borderWidth + 1) / 2;
}
return RenderBlock::borderRight();
}
void RenderTable::recalcHorizontalBorders()
{
m_borderLeft = calcBorderLeft();
m_borderRight = calcBorderRight();
}
int RenderTable::borderTop() const
{
if (collapseBorders())
return outerBorderTop();
return RenderBlock::borderTop();
}
int RenderTable::borderBottom() const
{
if (collapseBorders())
return outerBorderBottom();
return RenderBlock::borderBottom();
}
int RenderTable::outerBorderTop() const
{
if (!collapseBorders())
return 0;
int borderWidth = 0;
RenderTableSection* topSection;
if (m_head)
topSection = m_head;
else if (m_firstBody)
topSection = m_firstBody;
else if (m_foot)
topSection = m_foot;
else
topSection = 0;
if (topSection) {
borderWidth = topSection->outerBorderTop();
if (borderWidth == -1)
return 0; // Overridden by hidden
}
const BorderValue& tb = style()->borderTop();
if (tb.style() == BHIDDEN)
return 0;
if (tb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<int>(tb.width / 2));
return borderWidth;
}
int RenderTable::outerBorderBottom() const
{
if (!collapseBorders())
return 0;
int borderWidth = 0;
RenderTableSection* bottomSection;
if (m_foot)
bottomSection = m_foot;
else {
RenderObject* child;
for (child = lastChild(); child && !child->isTableSection(); child = child->previousSibling())
;
bottomSection = child ? static_cast<RenderTableSection*>(child) : 0;
}
if (bottomSection) {
borderWidth = bottomSection->outerBorderBottom();
if (borderWidth == -1)
return 0; // Overridden by hidden
}
const BorderValue& tb = style()->borderBottom();
if (tb.style() == BHIDDEN)
return 0;
if (tb.style() > BHIDDEN)
borderWidth = max(borderWidth, static_cast<int>((tb.width + 1) / 2));
return borderWidth;
}
int RenderTable::outerBorderLeft() const
{
if (!collapseBorders())
return 0;
int borderWidth = 0;
const BorderValue& tb = style()->borderLeft();
if (tb.style() == BHIDDEN)
return 0;
if (tb.style() > BHIDDEN)
borderWidth = tb.width / 2;
bool allHidden = true;
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (!child->isTableSection())
continue;
int sw = static_cast<RenderTableSection*>(child)->outerBorderLeft();
if (sw == -1)
continue;
else
allHidden = false;
borderWidth = max(borderWidth, sw);
}
if (allHidden)
return 0;
return borderWidth;
}
int RenderTable::outerBorderRight() const
{
if (!collapseBorders())
return 0;
int borderWidth = 0;
const BorderValue& tb = style()->borderRight();
if (tb.style() == BHIDDEN)
return 0;
if (tb.style() > BHIDDEN)
borderWidth = (tb.width + 1) / 2;
bool allHidden = true;
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (!child->isTableSection())
continue;
int sw = static_cast<RenderTableSection*>(child)->outerBorderRight();
if (sw == -1)
continue;
else
allHidden = false;
borderWidth = max(borderWidth, sw);
}
if (allHidden)
return 0;
return borderWidth;
}
RenderTableSection* RenderTable::sectionAbove(const RenderTableSection* section, bool skipEmptySections) const
{
recalcSectionsIfNeeded();
if (section == m_head)
return 0;
RenderObject* prevSection = section == m_foot ? lastChild() : section->previousSibling();
while (prevSection) {
if (prevSection->isTableSection() && prevSection != m_head && prevSection != m_foot && (!skipEmptySections || static_cast<RenderTableSection*>(prevSection)->numRows()))
break;
prevSection = prevSection->previousSibling();
}
if (!prevSection && m_head && (!skipEmptySections || m_head->numRows()))
prevSection = m_head;
return static_cast<RenderTableSection*>(prevSection);
}
RenderTableSection* RenderTable::sectionBelow(const RenderTableSection* section, bool skipEmptySections) const
{
recalcSectionsIfNeeded();
if (section == m_foot)
return 0;
RenderObject* nextSection = section == m_head ? firstChild() : section->nextSibling();
while (nextSection) {
if (nextSection->isTableSection() && nextSection != m_head && nextSection != m_foot && (!skipEmptySections || static_cast<RenderTableSection*>(nextSection)->numRows()))
break;
nextSection = nextSection->nextSibling();
}
if (!nextSection && m_foot && (!skipEmptySections || m_foot->numRows()))
nextSection = m_foot;
return static_cast<RenderTableSection*>(nextSection);
}
RenderTableCell* RenderTable::cellAbove(const RenderTableCell* cell) const
{
recalcSectionsIfNeeded();
// Find the section and row to look in
int r = cell->row();
RenderTableSection* section = 0;
int rAbove = 0;
if (r > 0) {
// cell is not in the first row, so use the above row in its own section
section = cell->section();
rAbove = r - 1;
} else {
section = sectionAbove(cell->section(), true);
if (section)
rAbove = section->numRows() - 1;
}
// Look up the cell in the section's grid, which requires effective col index
if (section) {
int effCol = colToEffCol(cell->col());
RenderTableSection::CellStruct aboveCell;
// If we hit a span back up to a real cell.
do {
aboveCell = section->cellAt(rAbove, effCol);
effCol--;
} while (!aboveCell.cell && aboveCell.inColSpan && effCol >= 0);
return aboveCell.cell;
} else
return 0;
}
RenderTableCell* RenderTable::cellBelow(const RenderTableCell* cell) const
{
recalcSectionsIfNeeded();
// Find the section and row to look in
int r = cell->row() + cell->rowSpan() - 1;
RenderTableSection* section = 0;
int rBelow = 0;
if (r < cell->section()->numRows() - 1) {
// The cell is not in the last row, so use the next row in the section.
section = cell->section();
rBelow = r + 1;
} else {
section = sectionBelow(cell->section(), true);
if (section)
rBelow = 0;
}
// Look up the cell in the section's grid, which requires effective col index
if (section) {
int effCol = colToEffCol(cell->col());
RenderTableSection::CellStruct belowCell;
// If we hit a colspan back up to a real cell.
do {
belowCell = section->cellAt(rBelow, effCol);
effCol--;
} while (!belowCell.cell && belowCell.inColSpan && effCol >= 0);
return belowCell.cell;
} else
return 0;
}
RenderTableCell* RenderTable::cellBefore(const RenderTableCell* cell) const
{
recalcSectionsIfNeeded();
RenderTableSection* section = cell->section();
int effCol = colToEffCol(cell->col());
if (!effCol)
return 0;
// If we hit a colspan back up to a real cell.
RenderTableSection::CellStruct prevCell;
do {
prevCell = section->cellAt(cell->row(), effCol - 1);
effCol--;
} while (!prevCell.cell && prevCell.inColSpan && effCol >= 0);
return prevCell.cell;
}
RenderTableCell* RenderTable::cellAfter(const RenderTableCell* cell) const
{
recalcSectionsIfNeeded();
int effCol = colToEffCol(cell->col() + cell->colSpan());
if (effCol >= numEffCols())
return 0;
return cell->section()->cellAt(cell->row(), effCol).cell;
}
RenderBlock* RenderTable::firstLineBlock() const
{
return 0;
}
void RenderTable::updateFirstLetter()
{
}
IntRect RenderTable::getOverflowClipRect(int tx, int ty)
{
IntRect rect = RenderBlock::getOverflowClipRect(tx, ty);
// If we have a caption, expand the clip to include the caption.
// FIXME: Technically this is wrong, but it's virtually impossible to fix this
// for real until captions have been re-written.
// FIXME: This code assumes (like all our other caption code) that only top/bottom are
// supported. When we actually support left/right and stop mapping them to top/bottom,
// we might have to hack this code first (depending on what order we do these bug fixes in).
if (m_caption) {
rect.setHeight(height());
rect.setY(ty);
}
return rect;
}
}
|