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 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#define PL_ARENA_CONST_ALIGN_MASK (sizeof(void*)-1)
#include "plarena.h"
#include "BasicTableLayoutStrategy.h"
#include "nsPresContext.h"
#include "nsTableFrame.h"
#include "nsTableColFrame.h"
#include "nsTableCellFrame.h"
#include "nsStyleConsts.h"
#include "nsVoidArray.h"
#include "nsQuickSort.h"
#define ARENA_ALLOCATE(var, pool, num, type) \
{void *_tmp_; PL_ARENA_ALLOCATE(_tmp_, pool, (num*sizeof(type))); \
var = NS_REINTERPRET_CAST(type*, _tmp_); }
// Local class used to hold information about table cells so we do not
// have to look it up multiple times
struct CellInfo {
nsTableCellFrame *cellFrame;
PRInt32 colSpan;
};
#ifdef DEBUG_TABLE_STRATEGY
static PRInt32 gsDebugCount = 0;
#endif
// The priority of allocations for columns is as follows
// 1) max(MIN, MIN_ADJ)
// 2) max (PCT, PCT_ADJ)
// 3) FIX
// 4) FIX_ADJ
// 5) max(DES_CON, DES_ADJ), but use MIN_PRO if present
// 6) for a fixed width table, the column may get more
// space if the sum of the col allocations is insufficient
// the logic here is kept in synch with that in CalculateTotals.
PRBool CanAllocate(PRInt32 aType,
PRInt32 aPrevType,
nsTableColFrame* aColFrame)
{
switch(aType) {
case PCT:
case FIX:
case DES_CON:
return (WIDTH_NOT_SET == aPrevType);
case FIX_ADJ:
return (WIDTH_NOT_SET == aPrevType) || (FIX == aPrevType);
default:
NS_ASSERTION(PR_FALSE, "invalid call");
}
return PR_FALSE;
}
// this doesn't work for a col frame which might get its width from a col
PRBool
HasPctValue(const nsIFrame* aFrame)
{
const nsStylePosition* position = aFrame->GetStylePosition();
if (eStyleUnit_Percent == position->mWidth.GetUnit()) {
float percent = position->mWidth.GetPercentValue();
if (percent > 0.0f) {
return PR_TRUE;
}
}
return PR_FALSE;
}
/* ---------- BasicTableLayoutStrategy ---------- */
MOZ_DECL_CTOR_COUNTER(BasicTableLayoutStrategy)
BasicTableLayoutStrategy::BasicTableLayoutStrategy(nsTableFrame *aFrame, PRBool aIsNavQuirks)
{
MOZ_COUNT_CTOR(BasicTableLayoutStrategy);
NS_ASSERTION(nsnull != aFrame, "bad frame arg");
mTableFrame = aFrame;
mCellSpacingTotal = 0;
mIsNavQuirksMode = aIsNavQuirks;
}
BasicTableLayoutStrategy::~BasicTableLayoutStrategy()
{
MOZ_COUNT_DTOR(BasicTableLayoutStrategy);
}
PRBool BasicTableLayoutStrategy::Initialize(const nsHTMLReflowState& aReflowState)
{
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eInit, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_TRUE);
#endif
ContinuingFrameCheck();
PRBool result = PR_TRUE;
// re-init instance variables
mCellSpacingTotal = 0;
mCols = mTableFrame->GetEffectiveCOLSAttribute();
mTableFrame->SetHasPctCol(PR_FALSE);
nscoord boxWidth = mTableFrame->CalcBorderBoxWidth(aReflowState);
PRBool hasPctCol = AssignNonPctColumnWidths(boxWidth, aReflowState);
mTableFrame->SetHasPctCol(hasPctCol);
// calc the min, desired, preferred widths from what we know so far
nscoord minWidth, prefWidth;
mTableFrame->CalcMinAndPreferredWidths(aReflowState, PR_FALSE, minWidth, prefWidth);
if (hasPctCol && mTableFrame->IsAutoWidth()) {
prefWidth = CalcPctAdjTableWidth(aReflowState, boxWidth);
}
// calc the desired width, considering if there is a specified width.
// don't use nsTableFrame::CalcDesiredWidth because it is based on table column widths.
nscoord desWidth = (mTableFrame->IsAutoWidth()) ? PR_MIN(prefWidth, aReflowState.availableWidth)
: prefWidth;
desWidth = PR_MAX(desWidth, minWidth);
mTableFrame->SetMinWidth(minWidth);
mTableFrame->SetDesiredWidth(desWidth);
mTableFrame->SetPreferredWidth(prefWidth);
mTableFrame->SetNeedStrategyInit(PR_FALSE);
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eInit, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_FALSE);
#endif
return result;
}
void BasicTableLayoutStrategy::ContinuingFrameCheck()
{
NS_ASSERTION(!mTableFrame->GetPrevInFlow(),
"never ever call me on a continuing frame!");
}
PRBool BCW_Wrapup(const nsHTMLReflowState& aReflowState,
BasicTableLayoutStrategy* aStrategy,
nsTableFrame* aTableFrame,
PRInt32* aAllocTypes)
{
if (aAllocTypes)
delete [] aAllocTypes;
#ifdef DEBUG_TABLE_STRATEGY
printf("BalanceColumnWidths ex \n"); aTableFrame->Dump(PR_FALSE, PR_TRUE, PR_FALSE);
#endif
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eBalanceCols, *aTableFrame, (nsHTMLReflowState&)aReflowState, PR_FALSE);
#endif
return PR_TRUE;
}
void
ResetPctValues(nsTableFrame* aTableFrame,
PRInt32 aNumCols)
{
// initialize the col percent and cell percent values to 0.
PRInt32 colX;
for (colX = 0; colX < aNumCols; colX++) {
nsTableColFrame* colFrame = aTableFrame->GetColFrame(colX);
if (colFrame) {
colFrame->SetWidth(PCT, WIDTH_NOT_SET);
colFrame->SetWidth(PCT_ADJ, WIDTH_NOT_SET);
}
}
}
PRBool
BasicTableLayoutStrategy::BalanceColumnWidths(const nsHTMLReflowState& aReflowState)
{
#ifdef DEBUG_TABLE_STRATEGY
printf("BalanceColumnWidths en count=%d \n", gsDebugCount++); mTableFrame->Dump(PR_FALSE, PR_TRUE, PR_FALSE);
#endif
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eBalanceCols, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_TRUE);
#endif
float p2t = mTableFrame->GetPresContext()->ScaledPixelsToTwips();
ContinuingFrameCheck();
PRInt32 numCols = mTableFrame->GetColCount();
PRBool tableIsAutoWidth = mTableFrame->IsAutoWidth();
nscoord horOffset;
// get the reduction in available horizontal space due to borders and padding
nsMargin offset = mTableFrame->GetChildAreaOffset(&aReflowState);
horOffset = offset.left + offset.right;
// determine if the table is auto/fixed and get the fixed width if available
nscoord maxWidth = mTableFrame->CalcBorderBoxWidth(aReflowState);
if (NS_UNCONSTRAINEDSIZE == maxWidth) {
maxWidth = PR_MIN(maxWidth, aReflowState.availableWidth);
if (NS_UNCONSTRAINEDSIZE == maxWidth) {
NS_ASSERTION(NS_UNCONSTRAINEDSIZE != maxWidth, "cannot balance with an unconstrained width");
return PR_FALSE;
}
}
// initialize the col percent and cell percent values to 0.
ResetPctValues(mTableFrame, numCols);
// An auto table returns a new table width based on percent cells/cols if they exist
nscoord perAdjTableWidth = 0;
if (mTableFrame->HasPctCol()) {
perAdjTableWidth = AssignPctColumnWidths(aReflowState, maxWidth, tableIsAutoWidth, p2t);
if (perAdjTableWidth > 0) {
// if an auto table has a pct col or cell, set the preferred table width
// here so that CalcPctAdjTableWidth wont't need to be called by the table
mTableFrame->SetPreferredWidth(perAdjTableWidth);
}
perAdjTableWidth = PR_MIN(perAdjTableWidth, maxWidth);
perAdjTableWidth -= horOffset;
perAdjTableWidth = PR_MAX(perAdjTableWidth, 0);
}
// reduce the maxWidth by border and padding, since we will be dealing with content width
maxWidth -= horOffset;
maxWidth = PR_MAX(0, maxWidth);
// we will re-calc mCellSpacingTotal in case longer rows were added after Initialize was called
mCellSpacingTotal = 0;
nscoord spacingX = mTableFrame->GetCellSpacingX();
PRInt32 numNonZeroWidthCols = 0;
// set the table's columns to the min width
nscoord minTableWidth = 0;
PRInt32 colX;
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord colMinWidth = colFrame->GetMinWidth();
mTableFrame->SetColumnWidth(colX, colMinWidth);
minTableWidth += colMinWidth;
if ((colFrame->GetMinWidth() > 0) || (colFrame->GetDesWidth() > 0) ||
(colFrame->GetFixWidth() > 0) || (colFrame->GetPctWidth() > 0) ||
(colFrame->GetWidth(MIN_PRO) > 0)) {
numNonZeroWidthCols++;
}
if (mTableFrame->GetNumCellsOriginatingInCol(colX) > 0) {
mCellSpacingTotal += spacingX;
}
}
if (mCellSpacingTotal > 0) {
mCellSpacingTotal += spacingX; // add last cell spacing on right
}
minTableWidth += mCellSpacingTotal;
// if the max width available is less than the min content width for fixed table, we're done
if (!tableIsAutoWidth && (maxWidth < minTableWidth)) {
return BCW_Wrapup(aReflowState, this, mTableFrame, nsnull);
}
// if the max width available is less than the min content width for auto table
// that had no % cells/cols, we're done
if (tableIsAutoWidth && (maxWidth < minTableWidth) && (0 == perAdjTableWidth)) {
return BCW_Wrapup(aReflowState, this, mTableFrame, nsnull);
}
// the following are of size NUM_WIDTHS, but only MIN_CON, DES_CON, FIX, FIX_ADJ, PCT
// are used and they account for colspan ADJusted values
PRInt32 totalWidths[NUM_WIDTHS]; // sum of col widths of a particular type
PRInt32 totalCounts[NUM_WIDTHS]; // num of cols of a particular type
PRInt32 dupedWidths[NUM_WIDTHS];
PRInt32 num0Proportional;
CalculateTotals(totalCounts, totalWidths, dupedWidths, num0Proportional);
// auto width table's adjusted width needs cell spacing
if (tableIsAutoWidth && perAdjTableWidth > 0) {
maxWidth = perAdjTableWidth;
}
nscoord totalAllocated = totalWidths[MIN_CON] + mCellSpacingTotal;
// allocate and initialize arrays indicating what col gets set
PRInt32* allocTypes = new PRInt32[numCols];
if (!allocTypes) return PR_FALSE;
for (colX = 0; colX < numCols; colX++) {
allocTypes[colX] = -1;
}
// allocate PCT/PCT_ADJ cols
if (totalCounts[PCT] > 0) {
if (totalAllocated + totalWidths[PCT] - dupedWidths[PCT] <= maxWidth) {
AllocateFully(totalAllocated, allocTypes, PCT);
//NS_WARN_IF_FALSE(totalAllocated <= maxWidth, "over allocated");
}
else {
AllocateConstrained(maxWidth - totalAllocated, PCT, PR_FALSE, allocTypes, p2t);
return BCW_Wrapup(aReflowState, this, mTableFrame, allocTypes);
}
}
// allocate FIX cols
if ((totalAllocated < maxWidth) && (totalCounts[FIX] > 0)) {
if (totalAllocated + totalWidths[FIX] - dupedWidths[FIX] <= maxWidth) {
AllocateFully(totalAllocated, allocTypes, FIX);
//NS_WARN_IF_FALSE(totalAllocated <= maxWidth, "over allocated");
}
else {
AllocateConstrained(maxWidth - totalAllocated, FIX, PR_TRUE, allocTypes, p2t);
return BCW_Wrapup(aReflowState, this, mTableFrame, allocTypes);
}
}
// allocate fixed adjusted cols
if ((totalAllocated < maxWidth) && (totalCounts[FIX_ADJ] > 0)) {
if (totalAllocated + totalWidths[FIX_ADJ] - dupedWidths[FIX_ADJ] <= maxWidth) {
AllocateFully(totalAllocated, allocTypes, FIX_ADJ);
//NS_WARN_IF_FALSE(totalAllocated <= maxWidth, "over allocated");
}
else {
AllocateConstrained(maxWidth - totalAllocated, FIX_ADJ, PR_TRUE, allocTypes, p2t);
return BCW_Wrapup(aReflowState, this, mTableFrame, allocTypes);
}
}
// allocate proportional and auto cols together
if ((totalAllocated < maxWidth) && (totalCounts[MIN_PRO] + totalCounts[DES_CON] > 0)) {
if (totalAllocated + totalWidths[MIN_PRO] - dupedWidths[MIN_PRO] +
totalWidths[DES_CON] - dupedWidths[DES_CON] <= maxWidth) {
AllocateFully(totalAllocated, allocTypes, DES_CON);
//NS_WARN_IF_FALSE(totalAllocated <= maxWidth, "over allocated");
}
else {
AllocateConstrained(maxWidth - totalAllocated, DES_CON, PR_TRUE, allocTypes, p2t);
return BCW_Wrapup(aReflowState, this, mTableFrame, allocTypes);
}
}
// if this is a nested non auto table and pass1 reflow, we are done
if ((maxWidth == NS_UNCONSTRAINEDSIZE) && (!tableIsAutoWidth)) {
return BCW_Wrapup(aReflowState, this, mTableFrame, allocTypes);
}
// allocate the rest to auto columns, with some exceptions
if ( (tableIsAutoWidth && (perAdjTableWidth - totalAllocated > 0)) ||
(!tableIsAutoWidth && (totalAllocated < maxWidth)) ) {
// determine how many cols have width either because of a min, fixed, des, or pct value
PRBool excludePct = (totalCounts[PCT] != numNonZeroWidthCols);
PRBool excludeFix = (totalCounts[PCT] + totalCounts[FIX] + totalCounts[FIX_ADJ] < numNonZeroWidthCols);
PRBool excludePro = (totalCounts[DES_CON] > 0);
PRBool exclude0Pro = (totalCounts[MIN_PRO] != num0Proportional);
if (tableIsAutoWidth) {
AllocateUnconstrained(perAdjTableWidth - totalAllocated, allocTypes, excludePct,
excludeFix, excludePro, exclude0Pro, p2t);
}
else {
AllocateUnconstrained(maxWidth - totalAllocated, allocTypes, excludePct,
excludeFix, excludePro, exclude0Pro, p2t);
}
}
return BCW_Wrapup(aReflowState, this, mTableFrame, allocTypes);
}
nscoord GetColWidth(nsTableColFrame* aColFrame,
PRInt32 aWidthType)
{
switch(aWidthType) {
case DES_CON:
return aColFrame->GetDesWidth();
case FIX:
case FIX_ADJ:
return aColFrame->GetWidth(aWidthType);
case PCT:
return aColFrame->GetPctWidth();
default:
NS_ASSERTION(PR_FALSE, "invalid call");
return WIDTH_NOT_SET;
}
}
// Allocate aWidthType values to all cols available in aAllocTypes
void BasicTableLayoutStrategy::AllocateFully(nscoord& aTotalAllocated,
PRInt32* aAllocTypes,
PRInt32 aWidthType)
{
PRInt32 numCols = mTableFrame->GetColCount();
for (PRInt32 colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
if (!CanAllocate(aWidthType, aAllocTypes[colX], colFrame)) {
continue;
}
nscoord oldWidth = mTableFrame->GetColumnWidth(colX);
nscoord newWidth = GetColWidth(colFrame, aWidthType);
// proportional and desired widths are handled together
PRBool haveProWidth = PR_FALSE;
if (DES_CON == aWidthType) {
nscoord proWidth = colFrame->GetWidth(MIN_PRO);
if (proWidth >= 0) {
haveProWidth = PR_TRUE;
newWidth = proWidth;
}
}
if (WIDTH_NOT_SET == newWidth) continue;
if (newWidth > oldWidth) {
mTableFrame->SetColumnWidth(colX, newWidth);
aTotalAllocated += newWidth - oldWidth;
}
aAllocTypes[colX] = (haveProWidth) ? MIN_PRO : aWidthType;
}
}
// This method is the last allocation method to get called, but just in case
// code gets added later after, set the allocated cols to some final value.
#define FINISHED 99
void BasicTableLayoutStrategy::AllocateUnconstrained(PRInt32 aAllocAmount,
PRInt32* aAllocTypes,
PRBool aExcludePct,
PRBool aExcludeFix,
PRBool aExcludePro,
PRBool aExclude0Pro,
float aPixelToTwips)
{
// set up allocTypes to exclude anything but auto cols if possible
PRInt32 colX;
PRInt32 numCols = mTableFrame->GetColCount();
for (colX = 0; colX < numCols; colX++) {
if (aExcludePct && (PCT == aAllocTypes[colX])) {
aAllocTypes[colX] = FINISHED;
}
else if (aExcludeFix && ((FIX == aAllocTypes[colX]) || (FIX_ADJ == aAllocTypes[colX]))) {
aAllocTypes[colX] = FINISHED;
}
else if (MIN_PRO == aAllocTypes[colX]) {
if (aExcludePro) {
aAllocTypes[colX] = FINISHED;
}
else {
if (aExclude0Pro) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
if (colFrame->GetConstraint() == e0ProportionConstraint) {
aAllocTypes[colX] = FINISHED;
}
}
}
}
}
nscoord divisor = 0;
PRInt32 numColsAllocated = 0;
PRInt32 totalAllocated = 0;
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
PRBool skipColumn = aExclude0Pro && (e0ProportionConstraint == colFrame->GetConstraint());
if (FINISHED != aAllocTypes[colX] && !skipColumn ) {
divisor += mTableFrame->GetColumnWidth(colX);
numColsAllocated++;
}
}
if (!numColsAllocated) {
// redistribute the space to all columns and prevent a division by zero
numColsAllocated = numCols;
}
for (colX = 0; colX < numCols; colX++) {
if (FINISHED != aAllocTypes[colX]) {
if (aExclude0Pro) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
if (e0ProportionConstraint == colFrame->GetConstraint()) {
continue;
}
}
nscoord oldWidth = mTableFrame->GetColumnWidth(colX);
float percent = (divisor == 0)
? (1.0f / ((float)numColsAllocated))
: ((float)oldWidth) / ((float)divisor);
nscoord addition = nsTableFrame::RoundToPixel(NSToCoordRound(((float)aAllocAmount) * percent), aPixelToTwips);
if (addition > (aAllocAmount - totalAllocated)) {
addition = nsTableFrame::RoundToPixel(aAllocAmount - totalAllocated, aPixelToTwips);
mTableFrame->SetColumnWidth(colX, oldWidth + addition);
break;
}
mTableFrame->SetColumnWidth(colX, oldWidth + addition);
totalAllocated += addition;
}
}
}
nscoord GetConstrainedWidth(nsTableColFrame* colFrame,
PRBool aConsiderPct)
{
nscoord conWidth = WIDTH_NOT_SET;
if (aConsiderPct) {
conWidth = colFrame->GetPctWidth();
}
if (conWidth <= 0 ) {
conWidth = colFrame->GetFixWidth();
}
return conWidth;
}
#define LIMIT_PCT 0 // retrict allocations to only the pct width cols
#define LIMIT_FIX 1 // retrict allocations to only the fix width cols
#define LIMIT_DES 2 // retrict allocations to only the auto width cols
#define LIMIT_NONE 3 // retrict allocations to only the auto widths unless there are none
// and then restrict fix or pct.
static int RowSortCB(const void *a, const void *b, void *)
{
const CellInfo *aa = NS_STATIC_CAST(const CellInfo*,a);
const CellInfo *bb = NS_STATIC_CAST(const CellInfo*,b);
return aa->colSpan - bb->colSpan;
}
/**
* This function finds all the rows which have a colspan greater than 1,
* and places those rows into the cellInfo array. This array is then
* sorted so that the rows with the largest colspan are last.
* @param TableFrame - The table we are working on
* @param colX - The column in the table we are considering
* @param numRows - The number of rows in the table
* @param cellInfo - The array to store the result in.
* @return - The number of entries stored in the array
*/
static PRInt32
GetSortedFrames(nsTableFrame *TableFrame,
PRInt32 colX,
PRInt32 numRows,
CellInfo *cellInfo)
{
PRInt32 rowX, index = 0;
for (rowX = 0; rowX < numRows; rowX++) {
CellInfo *inf = &cellInfo[index];
PRBool orig;
inf->cellFrame =
TableFrame->GetCellInfoAt(rowX, colX, &orig, &inf->colSpan);
// If the cell is good we keep it.
if(inf->cellFrame && orig && 1 != inf->colSpan) {
index++;
}
}
if(index>1) {
NS_QuickSort(cellInfo, index, sizeof(cellInfo[0]), RowSortCB, 0);
}
return index;
}
void
BasicTableLayoutStrategy::ComputeNonPctColspanWidths(const nsHTMLReflowState& aReflowState,
PRBool aConsiderPct,
float aPixelToTwips,
PRBool* aHasPctCol)
{
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eNonPctColspans, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_TRUE);
#endif
PRInt32 numCols = mTableFrame->GetColCount();
PRInt32 numEffCols = mTableFrame->GetEffectiveColCount();
// zero out prior ADJ values
PRInt32 colX;
for (colX = numCols - 1; colX >= 0; colX--) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
colFrame->SetWidth(MIN_ADJ, WIDTH_NOT_SET);
colFrame->SetWidth(FIX_ADJ, WIDTH_NOT_SET);
colFrame->SetWidth(DES_ADJ, WIDTH_NOT_SET);
}
// For each col, consider the cells originating in it with colspans > 1.
// Adjust the cols that each cell spans if necessary. Iterate backwards
// so that nested and/or overlaping col spans handle the inner ones first,
// ensuring more accurated calculations.
// if more than one colspan originate in one column, resort the access to
// the rows so that the inner colspans are handled first
PRInt32 numRows = mTableFrame->GetRowCount();
CellInfo* cellInfo = new CellInfo[numRows];
if(!cellInfo) {
return;
}
for (colX = numEffCols - 1; colX >= 0; colX--) { // loop only over effective columns
PRInt32 spannedRows = GetSortedFrames(mTableFrame, colX, numRows, cellInfo);
for (PRInt32 i = 0; i < spannedRows; i++) {
const CellInfo *inf = &cellInfo[i];
const nsTableCellFrame* cellFrame = inf->cellFrame;
const PRInt32 colSpan = PR_MIN(inf->colSpan, numEffCols - colX);
// set MIN_ADJ, DES_ADJ, FIX_ADJ
for (PRInt32 widthX = 0; widthX < NUM_MAJOR_WIDTHS; widthX++) {
nscoord cellWidth = 0;
if (MIN_CON == widthX) {
cellWidth = cellFrame->GetPass1MaxElementWidth();
}
else if (DES_CON == widthX) {
cellWidth = cellFrame->GetMaximumWidth();
}
else { // FIX width
// see if the cell has a style width specified
const nsStylePosition* cellPosition = cellFrame->GetStylePosition();
if (eStyleUnit_Coord == cellPosition->mWidth.GetUnit()) {
// need to add borde and padding into fixed width
nsMargin borderPadding = nsTableFrame::GetBorderPadding(nsSize(aReflowState.mComputedWidth, 0),
aPixelToTwips, cellFrame);
cellWidth = cellPosition->mWidth.GetCoordValue() + borderPadding.left + borderPadding.right;
cellWidth = PR_MAX(cellWidth, cellFrame->GetPass1MaxElementWidth());
}
}
if (0 >= cellWidth) continue;
// first allocate pct cells up to their value if aConsiderPct, then
// allocate fixed cells up to their value, then allocate auto cells
// up to their value, then allocate auto cells proportionally.
PRInt32 limit = (aConsiderPct) ? LIMIT_PCT : LIMIT_FIX;
if (MIN_CON != widthX) { // FIX and DES only need to allocate NONE
limit = LIMIT_NONE;
}
while (limit <= LIMIT_NONE) {
if (ComputeNonPctColspanWidths(widthX, cellFrame, cellWidth, colX, colSpan,
limit, aPixelToTwips)) {
break;
}
limit++;
}
}
// determine if there is a pct col if we are requested to do so
if (aHasPctCol && !*aHasPctCol) {
*aHasPctCol = HasPctValue(cellFrame);
}
}
}
delete [] cellInfo;
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eNonPctColspans, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_FALSE);
#endif
}
#ifdef DEBUG
static char* widths[] = {"MIN", "DES", "FIX"};
static char* limits[] = {"PCT", "FIX", "DES", "NONE"};
static PRInt32 dumpCount = 0;
void DumpColWidths(nsTableFrame& aTableFrame,
char* aMessage,
const nsTableCellFrame* aCellFrame,
PRInt32 aColIndex,
PRInt32 aWidthType,
PRInt32 aLimitType)
{
PRInt32 rowIndex;
aCellFrame->GetRowIndex(rowIndex);
printf ("%s (row,col)=(%d,%d) width=%s limit=%s count=%d\n", aMessage, rowIndex, aColIndex,
widths[aWidthType], limits[aLimitType], dumpCount++);
for (PRInt32 i = 0; i < aTableFrame.GetColCount(); i++) {
printf(" col %d = ", i);
nsTableColFrame* colFrame = aTableFrame.GetColFrame(i);
for (PRInt32 j = 0; j < NUM_WIDTHS; j++) {
printf("%d ", colFrame->GetWidth(j));
}
printf("\n");
}
}
#endif
// (aWidthIndex == MIN_CON) is called with:
// (aLimitType == LIMIT_PCT) to give MIN_ADJ to PCT cols (up to their PCT value)
// (aLimitType == LIMIT_FIX) to give MIN_ADJ to FIX cols (up to their FIX value)
// (aLimitType == LIMIT_DES) to give MIN_ADJ to auto cols (up to their preferred value)
// (aLimitType == LIMIT_NONE) to give MIN_ADJ to auto cols
// (aWidthIndex == FIX), (aWidthIndex == DES) is called with:
// (aLimitType == LIMIT_NONE) to give FIX_ADJ to auto cols
PRBool
BasicTableLayoutStrategy::ComputeNonPctColspanWidths(PRInt32 aWidthIndex,
const nsTableCellFrame* aCellFrame,
nscoord aCellWidth,
PRInt32 aColIndex,
PRInt32 aColSpan,
PRInt32& aLimitType,
float aPixelToTwips)
{
#ifdef DEBUG_TABLE_STRATEGY
DumpColWidths(*mTableFrame, "enter ComputeNonPctColspanWidths", aCellFrame, aColIndex, aWidthIndex, aLimitType);
#endif
PRBool result = PR_TRUE;
nscoord spanCellSpacing = 0; // total cell spacing cells being spanned
nscoord minTotal = 0; // the sum of min widths of spanned cells.
nscoord autoMinTotal = 0; // the sum of min widths of spanned auto cells.
nscoord fixMinTotal = 0; // the sum of min widths of spanned fix cells.
nscoord pctMinTotal = 0; // the sum of min widths of spanned pct cells.
nscoord pctTotal = 0; // the sum of pct widths of spanned cells.
nscoord fixTotal = 0; // the sum of fix widths of spanned cells
nscoord autoDesTotal = 0; // the sum of desired widths of spanned cells which are auto
nscoord pctLimitTotal = 0; // the sum of pct widths not exceeding aCellWidth
nscoord fixLimitTotal = 0; // the sum of fix widths not exceeding aCellWidth
nscoord desLimitTotal = 0; // the sum of des widths not exceeding aCellWidth
PRInt32 numAutoCols = 0;
nscoord spacingX = mTableFrame->GetCellSpacingX();
PRInt32 spanX;
// accumulate the various divisors to be used later
for (spanX = 0; spanX < aColSpan; spanX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(aColIndex + spanX);
if (!colFrame) continue;
nscoord minWidth = PR_MAX(colFrame->GetMinWidth(), 0);
nscoord pctWidth = PR_MAX(colFrame->GetPctWidth(), 0);
nscoord fixWidth = PR_MAX(colFrame->GetFixWidth(), 0);
nscoord desWidth = PR_MAX(colFrame->GetDesWidth(), minWidth);
minTotal += minWidth;
if (pctWidth > 0) {
pctTotal += pctWidth;
pctMinTotal += minWidth;
}
else if (fixWidth > 0) {
fixTotal += fixWidth;
fixMinTotal += minWidth;
}
if (colFrame->GetWidth(PCT) + colFrame->GetWidth(FIX) <= 0) {
autoDesTotal += desWidth;
autoMinTotal += minWidth;
numAutoCols++;
}
if ((spanX > 0) && (mTableFrame->GetNumCellsOriginatingInCol(aColIndex + spanX) > 0)) {
spanCellSpacing += spacingX;
}
}
nscoord availWidth = aCellWidth - spanCellSpacing;
if (MIN_CON == aWidthIndex) {
// MIN widths (unlike the others) are added to existing values, so subtract out the min total
availWidth -= minTotal;
pctLimitTotal = ((pctTotal - pctMinTotal) < availWidth) ? pctTotal - pctMinTotal : availWidth;
fixLimitTotal = ((fixTotal - fixMinTotal) < availWidth) ? fixTotal - fixMinTotal : availWidth;
desLimitTotal = ((autoDesTotal - autoMinTotal) < availWidth) ? autoDesTotal - autoMinTotal : availWidth;
}
else { // FIX or DES_CON with LIMIT_NONE
if (FIX == aWidthIndex) {
if (autoDesTotal > 0) {
availWidth -= pctTotal + fixTotal;
}
else if (fixTotal > 0) {
availWidth -= pctTotal;
}
}
else if (DES_CON == aWidthIndex) {
availWidth -= pctTotal + fixTotal;
}
}
// Below are some optimizations which can skip steps.
if (LIMIT_PCT == aLimitType) {
NS_ASSERTION(MIN_CON == aWidthIndex, "invalid call to ComputeNonPctColspanWidths");
// if there are no pct cols, limit fix cols
if (0 == pctTotal) {
aLimitType = LIMIT_FIX;
}
}
if (LIMIT_FIX == aLimitType) {
NS_ASSERTION(MIN_CON == aWidthIndex, "invalid call to ComputeNonPctColspanWidths");
// if there are no fix cols, limit des
if (0 == fixTotal) {
aLimitType = LIMIT_DES;
}
}
if (LIMIT_DES == aLimitType) {
NS_ASSERTION(MIN_CON == aWidthIndex, "invalid call to ComputeNonPctColspanWidths");
// if there are no auto cols, limit none
if (0 == autoDesTotal) {
aLimitType = LIMIT_NONE;
}
}
// there are some cases where allocating up to aCellWidth will not be satisfied
switch (aLimitType) {
case LIMIT_PCT:
if (pctLimitTotal < availWidth) {
// the pct cols won't satisfy the request alone
availWidth = pctLimitTotal;
result = PR_FALSE;
}
break;
case LIMIT_FIX:
if (fixLimitTotal < availWidth) {
// the fix cols won't satisfy the request alone
availWidth = fixLimitTotal;
result = PR_FALSE;
}
break;
case LIMIT_DES:
if (desLimitTotal < availWidth) {
// the auto cols won't satisfy the request alone
availWidth = desLimitTotal;
result = PR_FALSE;
}
break;
default: // LIMIT_NONE
break;
}
if (availWidth > 0) {
nscoord usedWidth = 0;
// get the correct numerator in a similar fashion to getting the divisor
for (spanX = 0; spanX < aColSpan; spanX++) {
if (usedWidth >= availWidth) break;
nsTableColFrame* colFrame = mTableFrame->GetColFrame(aColIndex + spanX);
if (!colFrame) continue;
nscoord minWidth = colFrame->GetMinWidth();
nscoord pctWidth = PR_MAX(colFrame->GetPctWidth(), 0);
nscoord fixWidth = PR_MAX(colFrame->GetFixWidth(), 0);
nscoord desWidth = PR_MAX(colFrame->GetDesWidth(), minWidth);
nscoord colWidth = PR_MAX(colFrame->GetWidth(aWidthIndex),
colFrame->GetWidth(aWidthIndex + NUM_MAJOR_WIDTHS));
colWidth = PR_MAX(colWidth, minWidth);
nscoord numeratorPct = 0;
nscoord numeratorFix = 0;
nscoord numeratorAutoDes = 0;
if (pctWidth > 0) {
numeratorPct = pctWidth;
}
else if (fixWidth > 0) {
numeratorFix = fixWidth;
}
if (colFrame->GetWidth(PCT) + colFrame->GetWidth(FIX) <= 0) {
numeratorAutoDes = desWidth;
}
nscoord divisor = 0;
nscoord numerator = 0;
// let pct cols reach their target
if (LIMIT_PCT == aLimitType) {
if (pctLimitTotal > 0) {
divisor = pctTotal - pctMinTotal;
numerator = PR_MAX(numeratorPct - minWidth, 0);
}
}
// let fix cols reach their target
else if (LIMIT_FIX == aLimitType) {
if (fixLimitTotal > 0) {
divisor = fixTotal - fixMinTotal;
numerator = PR_MAX(numeratorFix - minWidth, 0);
}
}
// let auto cols reach their target
else if (LIMIT_DES == aLimitType) {
if (desLimitTotal > 0) {
if (autoDesTotal > 0) { // there were auto cols
divisor = autoDesTotal - autoMinTotal;
numerator = PR_MAX(numeratorAutoDes - minWidth, 0);
}
else if (fixTotal > 0) { // there were fix cols but no auto
divisor = fixTotal - fixMinTotal;
numerator = PR_MAX(numeratorFix - minWidth, 0);
}
else if (pctTotal > 0) { // there were only pct cols
divisor = pctTotal - pctMinTotal;
numerator = PR_MAX(numeratorPct - minWidth, 0);
}
else continue;
}
}
else if (LIMIT_NONE == aLimitType) {
if ((MIN_CON == aWidthIndex) && (0 == minTotal)) {
// divide evenly among the spanned cols
divisor = aColSpan;
numerator = 1;
}
else {
if (autoDesTotal > 0) { // there were auto cols
divisor = autoDesTotal;
numerator = numeratorAutoDes;
}
else if (fixTotal > 0) { // there were fix cols but no auto
divisor = fixTotal;
numerator = numeratorFix;
}
else if (pctTotal > 0) { // there were only pct cols
divisor = pctTotal;
numerator = numeratorPct;
}
else continue;
}
}
// calculate the adj col width
nscoord newColAdjWidth = (0 >= divisor)
? NSToCoordRound( ((float)availWidth) / ((float)aColSpan) )
: NSToCoordRound( (((float)numerator) / ((float)divisor)) * availWidth);
newColAdjWidth = nsTableFrame::RoundToPixel(newColAdjWidth, aPixelToTwips);
// don't let the new allocation exceed the avail total
newColAdjWidth = PR_MIN(newColAdjWidth, availWidth - usedWidth);
// put the remainder of the avail total in the last spanned col
if (spanX == aColSpan - 1) {
newColAdjWidth = availWidth - usedWidth;
}
usedWidth += newColAdjWidth;
// MIN_CON gets added to what is there, the others don't
if (MIN_CON == aWidthIndex) {
newColAdjWidth += colWidth;
}
if (newColAdjWidth > colWidth) {
if (FIX == aWidthIndex) {
// a colspan cannot fix a column below what a cell desires on its own
nscoord desCon = colFrame->GetWidth(DES_CON); // do not consider DES_ADJ
if (desCon > newColAdjWidth) {
newColAdjWidth = desCon;
}
}
colFrame->SetWidth(aWidthIndex + NUM_MAJOR_WIDTHS, newColAdjWidth);
}
else {
if((newColAdjWidth > 0) && (FIX == aWidthIndex)) {
if(colFrame->GetWidth(FIX) < 0) {
nscoord desCon = colFrame->GetWidth(DES_CON);
if (desCon > newColAdjWidth) {
newColAdjWidth = desCon;
}
}
if(colFrame->GetWidth(FIX_ADJ) < newColAdjWidth) {
colFrame->SetWidth(FIX_ADJ, PR_MAX(colWidth,newColAdjWidth));
}
}
}
}
}
#ifdef DEBUG_TABLE_STRATEGY
DumpColWidths(*mTableFrame, "exit ComputeNonPctColspanWidths", aCellFrame, aColIndex, aWidthIndex, aLimitType);
#endif
return result;
}
// XXX percent left and right padding are not figured in the calculations
// The table frame needs to be used as the percent base because the reflow
// state may have an unconstrained width. There should probably be a frame
// state bit indicating that horizontal padding is percentage based.
// Determine min, desired, fixed, and proportional sizes for the cols and
// and calculate min/max table width. Return true if there is at least one pct cell or col
PRBool
BasicTableLayoutStrategy::AssignNonPctColumnWidths(nscoord aMaxWidth,
const nsHTMLReflowState& aReflowState)
{
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eNonPctCols, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_TRUE);
#endif
#ifdef DEBUG_TABLE_STRATEGY
printf("AssignNonPctColWidths en max=%d count=%d \n", aMaxWidth, gsDebugCount++); mTableFrame->Dump(PR_FALSE, PR_TRUE, PR_FALSE);
#endif
PRInt32 numRows = mTableFrame->GetRowCount();
PRInt32 numCols = mTableFrame->GetColCount();
nscoord spacingX = mTableFrame->GetCellSpacingX();
PRInt32 colX, rowX;
mCellSpacingTotal = 0;
PRBool hasPctCol = PR_FALSE; // return value
float pixelToTwips = mTableFrame->GetPresContext()->ScaledPixelsToTwips();
PRInt32 rawPropTotal = -1; // total of numbers of the type 1*, 2*, etc
PRInt32 numColsForColsAttr = 0; // Nav Quirks cols attribute for equal width cols
if (NS_STYLE_TABLE_COLS_NONE != mCols) {
numColsForColsAttr = (NS_STYLE_TABLE_COLS_ALL == mCols) ? numCols : mCols;
}
// For every column, determine it's min and desired width based on cell style
// base on cells which do not span cols. Also, determine mCellSpacingTotal
for (colX = 0; colX < numCols; colX++) {
nscoord minWidth = 0;
nscoord desWidth = 0;
nscoord fixWidth = WIDTH_NOT_SET;
// Get column frame and reset it
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
NS_ASSERTION(nsnull != colFrame, "bad col frame");
colFrame->ResetSizingInfo();
if (mTableFrame->GetNumCellsOriginatingInCol(colX) > 0) {
mCellSpacingTotal += spacingX;
}
// Scan the cells in the col that have colspan = 1 and find the maximum
// min, desired, and fixed cells.
nsTableCellFrame* fixContributor = nsnull;
nsTableCellFrame* desContributor = nsnull;
for (rowX = 0; rowX < numRows; rowX++) {
PRBool originates;
PRInt32 colSpan;
nsTableCellFrame* cellFrame = mTableFrame->GetCellInfoAt(rowX, colX, &originates, &colSpan);
// skip cells that don't originate at (rowX, colX); colspans are handled in the
// next pass, row spans don't need to be handled
if (!cellFrame || !originates || (colSpan > 1)) {
continue;
}
// these values include borders and padding
minWidth = PR_MAX(minWidth, cellFrame->GetPass1MaxElementWidth());
nscoord cellDesWidth = cellFrame->GetMaximumWidth();
if (cellDesWidth > desWidth) {
desContributor = cellFrame;
desWidth = cellDesWidth;
}
// see if the cell has a style width specified
const nsStylePosition* cellPosition = cellFrame->GetStylePosition();
if (eStyleUnit_Coord == cellPosition->mWidth.GetUnit()) {
nscoord coordValue = cellPosition->mWidth.GetCoordValue();
if (coordValue > 0) { // ignore if width == 0
// need to add border and padding into fixed width
nsSize percentBase(aReflowState.mComputedWidth, 0);
nsMargin borderPadding = nsTableFrame::GetBorderPadding(percentBase, pixelToTwips, cellFrame);
nscoord newFixWidth = coordValue + borderPadding.left + borderPadding.right;
// 2nd part of condition is Nav/IE Quirk like below
if ((newFixWidth > fixWidth) || ((newFixWidth == fixWidth) && (desContributor == cellFrame))) {
fixWidth = newFixWidth;
fixContributor = cellFrame;
}
}
}
if (!hasPctCol && HasPctValue(cellFrame)) { // see if there is a pct cell
hasPctCol = PR_TRUE;
}
}
// Nav/IE Quirk like above
if (fixWidth > 0) {
if (mIsNavQuirksMode && (desWidth > fixWidth) && (fixContributor != desContributor)) {
fixWidth = WIDTH_NOT_SET;
fixContributor = nsnull;
}
}
desWidth = PR_MAX(desWidth, minWidth);
// cache the computed column info
colFrame->SetWidth(MIN_CON, minWidth);
colFrame->SetWidth(DES_CON, desWidth);
if (fixWidth > 0) {
colFrame->SetWidth(FIX, fixWidth);
}
nsStyleCoord colStyleWidth = colFrame->GetStyleWidth();
// determine if there is a proportional column either from html4
// proportional width on a col or Nav Quirks cols attr
if (fixWidth <= 0) {
nscoord proportion = WIDTH_NOT_SET;
if (eStyleUnit_Proportional == colStyleWidth.GetUnit()) {
proportion = colStyleWidth.GetIntValue();
}
else if (colX < numColsForColsAttr) {
proportion = 1;
if ((eStyleUnit_Percent == colStyleWidth.GetUnit()) &&
(colStyleWidth.GetPercentValue() > 0.0f)) {
proportion = WIDTH_NOT_SET;
}
}
if (proportion >= 0) {
rawPropTotal = PR_MAX(rawPropTotal, 0); // it was initialized to -1
colFrame->SetWidth(MIN_PRO, proportion);
nsColConstraint colConstraint = (0 == proportion)
? e0ProportionConstraint : eProportionConstraint;
rawPropTotal += proportion;
colFrame->SetConstraint(colConstraint);
}
}
if (!hasPctCol) { // see if there is a pct col
if (eStyleUnit_Percent == colStyleWidth.GetUnit()) {
float percent = colStyleWidth.GetPercentValue();
if (percent > 0.0f) {
hasPctCol = PR_TRUE;
}
}
}
}
if (mCellSpacingTotal > 0) {
mCellSpacingTotal += spacingX; // add last cell spacing on right
}
//set the table col fixed width if present
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord fixColWidth = colFrame->GetWidth(FIX);
// use the style width of a col only if the col hasn't gotten a fixed width from any cell
if (fixColWidth <= 0) {
nsStyleCoord colStyleWidth = colFrame->GetStyleWidth();
if (eStyleUnit_Coord == colStyleWidth.GetUnit()) {
fixColWidth = colStyleWidth.GetCoordValue();
if (fixColWidth > 0) {
colFrame->SetWidth(FIX, fixColWidth);
}
}
}
}
PRBool* pctRequest = (hasPctCol) ? nsnull : &hasPctCol;
ComputeNonPctColspanWidths(aReflowState, PR_FALSE, pixelToTwips, pctRequest);
PRInt32 numEffCols = mTableFrame->GetEffectiveColCount();
// figure the proportional widths for porportional cols
if (rawPropTotal > 0) {
// find the largest combined prop size considering each prop col and
// its desired size
nscoord maxPropTotal = 0;
for (colX = 0; colX < numEffCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
nscoord rawProp = colFrame->GetWidth(MIN_PRO);
if (rawProp > 0) {
nscoord desWidth = colFrame->GetDesWidth();
nscoord propTotal = NSToCoordRound( ((float)desWidth) * ((float)rawPropTotal) / (float)rawProp );
propTotal = nsTableFrame::RoundToPixel(propTotal, pixelToTwips);
maxPropTotal = PR_MAX(maxPropTotal, propTotal);
}
}
// set MIN_PRO widths based on the maxPropTotal
for (colX = 0; colX < numEffCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord rawProp = colFrame->GetWidth(MIN_PRO);
if (0 == rawProp) {
// a 0* col gets only the min width
colFrame->SetWidth(MIN_PRO, colFrame->GetMinWidth());
}
else if ((rawProp > 0) && (rawPropTotal > 0)) {
nscoord propWidth = NSToCoordRound( ((float)maxPropTotal) * ((float)rawProp) / (float)rawPropTotal ) ;
propWidth = nsTableFrame::RoundToPixel(propWidth, pixelToTwips);
colFrame->SetWidth(MIN_PRO, PR_MAX(propWidth, colFrame->GetMinWidth()));
}
}
}
// Set the table col width for each col to the content min.
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord minWidth = colFrame->GetMinWidth();
mTableFrame->SetColumnWidth(colX, minWidth);
}
#ifdef DEBUG_TABLE_STRATEGY
printf("AssignNonPctColWidths ex\n"); mTableFrame->Dump(PR_FALSE, PR_TRUE, PR_FALSE);
#endif
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::eNonPctCols, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_FALSE);
#endif
return hasPctCol;
}
void
BasicTableLayoutStrategy::ReduceOverSpecifiedPctCols(nscoord aExcess)
{
nscoord numCols = mTableFrame->GetColCount();
for (PRInt32 colX = numCols - 1; (colX >= 0) && (aExcess > 0); colX--) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord pctWidth = colFrame->GetWidth(PCT);
nscoord reduction = 0;
if (pctWidth > 0) {
reduction = (aExcess > pctWidth) ? pctWidth : aExcess;
nscoord newPctWidth = (reduction == pctWidth) ? WIDTH_NOT_SET : pctWidth - reduction;
colFrame->SetWidth(PCT, PR_MAX(newPctWidth, colFrame->GetMinWidth()));
}
else {
nscoord pctAdjWidth = colFrame->GetWidth(PCT_ADJ);
if (pctAdjWidth > 0) {
reduction = (aExcess > pctAdjWidth) ? pctAdjWidth : aExcess;
nscoord newPctAdjWidth = (reduction == pctAdjWidth) ? WIDTH_NOT_SET : pctAdjWidth - reduction;
colFrame->SetWidth(PCT_ADJ, PR_MAX(newPctAdjWidth, colFrame->GetMinWidth()));
}
}
aExcess -= reduction;
}
}
#ifdef DEBUG_TABLE_REFLOW_TIMING
nscoord WrapupAssignPctColumnWidths(nsTableFrame* aTableFrame,
const nsHTMLReflowState& aReflowState,
nscoord aValue)
{
nsTableFrame::DebugTimeMethod(nsTableFrame::ePctCols, *aTableFrame, (nsHTMLReflowState&)aReflowState, PR_FALSE);
return aValue;
}
#else
inline nscoord WrapupAssignPctColumnWidths(nsTableFrame* aTableFrame,
const nsHTMLReflowState& aReflowState,
nscoord aValue)
{
return aValue;
}
#endif
nscoord
BasicTableLayoutStrategy::CalcPctAdjTableWidth(const nsHTMLReflowState& aReflowState,
nscoord aAvailWidthIn)
{
NS_ASSERTION(mTableFrame->IsAutoWidth() && mTableFrame->HasPctCol(), "invalid call");
PRInt32 numRows = mTableFrame->GetRowCount();
PRInt32 numCols = mTableFrame->GetColCount(); // consider cols at end without orig cells
PRInt32 colX, rowX;
float pixelToTwips = mTableFrame->GetPresContext()->ScaledPixelsToTwips();
// For an auto table, determine the potentially new percent adjusted width based
// on percent cells/cols. This probably should only be a NavQuirks thing, since
// a percentage based cell or column on an auto table should force the column to auto
nscoord basis = 0;
float* rawPctValues = new float[numCols]; // store the raw pct values, allow for spans past the effective numCols
if (!rawPctValues) return NS_ERROR_OUT_OF_MEMORY;
for (colX = 0; colX < numCols; colX++) {
rawPctValues[colX] = 0.0f;
}
nsMargin borderPadding = mTableFrame->GetContentAreaOffset(&aReflowState);
nscoord availWidth = aAvailWidthIn;
if (NS_UNCONSTRAINEDSIZE != availWidth) {
// adjust the avail width to exclude table border, padding and cell spacing
availWidth -= borderPadding.left + borderPadding.right + mCellSpacingTotal;
}
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord maxColBasis = -1;
// Scan the cells in the col
for (rowX = 0; rowX < numRows; rowX++) {
PRBool originates;
PRInt32 colSpan;
nsTableCellFrame* cellFrame = mTableFrame->GetCellInfoAt(rowX, colX, &originates, &colSpan);
if (!originates) continue; // skip cells that don't originate in the col
// see if the cell has a style percent width specified
const nsStylePosition* cellPosition = cellFrame->GetStylePosition();
if (eStyleUnit_Percent == cellPosition->mWidth.GetUnit()) {
float percent = cellPosition->mWidth.GetPercentValue();
if (percent > 0.0f) {
// calculate the preferred width of the cell based on fix, des, widths of the cols it spans
nscoord cellDesWidth = 0;
float spanPct = percent / float(colSpan);
for (PRInt32 spanX = 0; spanX < colSpan; spanX++) {
nsTableColFrame* spanFrame = mTableFrame->GetColFrame(colX + spanX);
if (!spanFrame) continue;
cellDesWidth += spanFrame->GetWidth(DES_CON); // don't consider DES_ADJ
// crudely allocate pct values to the spanning cols so that we can check if they exceed 100 pct below
rawPctValues[colX + spanX] = PR_MAX(rawPctValues[colX + spanX], spanPct);
}
// consider the cell's preferred width
cellDesWidth = PR_MAX(cellDesWidth, cellFrame->GetMaximumWidth());
nscoord colBasis = nsTableFrame::RoundToPixel(NSToCoordRound((float)cellDesWidth / percent), pixelToTwips);
maxColBasis = PR_MAX(maxColBasis, colBasis);
}
}
}
if (-1 == maxColBasis) {
// see if the col has a style percent width specified
nsStyleCoord colStyleWidth = colFrame->GetStyleWidth();
if (eStyleUnit_Percent == colStyleWidth.GetUnit()) {
float percent = colStyleWidth.GetPercentValue();
maxColBasis = 0;
if (percent > 0.0f) {
rawPctValues[colX] = PR_MAX(rawPctValues[colX], percent);
nscoord desWidth = colFrame->GetWidth(DES_CON); // don't consider DES_ADJ
maxColBasis = nsTableFrame::RoundToPixel(NSToCoordRound((float)desWidth / percent), pixelToTwips);
}
}
}
basis = PR_MAX(basis, maxColBasis);
} // end for (colX ..
float perTotal = 0.0f; // total of percentage constrained cols and/or cells in cols
PRInt32 numPerCols = 0; // number of colums that have percentage constraints
nscoord fixDesTotal = 0; // total of fix or des widths of cols
nscoord fixDesTotalNoPct = 0; // total of fix or des widths of cols without pct
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord fixWidth = colFrame->GetFixWidth();
nscoord fixDesWidth = (fixWidth > 0) ? fixWidth : colFrame->GetDesWidth();
fixDesTotal += fixDesWidth;
if (rawPctValues[colX] + perTotal > 1.0f) {
rawPctValues[colX] = PR_MAX(1.0f - perTotal, 0.0f);
}
if (rawPctValues[colX] > 0.0f) {
numPerCols++;
perTotal += rawPctValues[colX];
}
else {
fixDesTotalNoPct += fixDesWidth;
}
}
delete [] rawPctValues; // destroy the raw pct values
// If there are no pct cells or cols, there is nothing to do.
if ((0 == numPerCols) || (0.0f == perTotal)) {
NS_ASSERTION(PR_FALSE, "invalid call");
return basis;
}
// If there is only one col and it is % based, it won't affect anything
if ((1 == numCols) && (numCols == numPerCols)) {
return basis + borderPadding.left + borderPadding.right + mCellSpacingTotal;
}
// compute a basis considering total percentages and the desired width of everything else
if ((perTotal > 0.0f) && (perTotal < 1.0f)) {
nscoord otherBasis = nsTableFrame::RoundToPixel(NSToCoordRound((float)fixDesTotalNoPct / (1.0f - perTotal)), pixelToTwips);
basis = PR_MAX(basis, otherBasis);
}
else if ((fixDesTotalNoPct > 0) && (NS_UNCONSTRAINEDSIZE != availWidth)) { // make the basis as big as possible
basis = availWidth; // the 100% cols force as big a width as possible
}
basis = PR_MAX(basis, fixDesTotal);
basis = PR_MIN(basis, availWidth); // don't exceed the max we were given
if (NS_UNCONSTRAINEDSIZE != availWidth) {
// add back the table border, padding and cell spacing
basis += borderPadding.left + borderPadding.right + mCellSpacingTotal;
}
return basis;
}
// Determine percentage col widths for each col frame
nscoord
BasicTableLayoutStrategy::AssignPctColumnWidths(const nsHTMLReflowState& aReflowState,
nscoord aAvailWidth,
PRBool aTableIsAutoWidth,
float aPixelToTwips)
{
#ifdef DEBUG_TABLE_REFLOW_TIMING
nsTableFrame::DebugTimeMethod(nsTableFrame::ePctCols, *mTableFrame, (nsHTMLReflowState&)aReflowState, PR_TRUE);
#endif
mTableFrame->SetHasCellSpanningPctCol(PR_FALSE); // this gets refigured below
PRInt32 numRows = mTableFrame->GetRowCount();
PRInt32 numCols = mTableFrame->GetColCount(); // consider cols at end without orig cells
PRInt32 numEffCols = mTableFrame->GetEffectiveColCount();
nscoord spacingX = mTableFrame->GetCellSpacingX();
PRInt32 colX, rowX;
NS_ASSERTION(NS_UNCONSTRAINEDSIZE != aAvailWidth, "AssignPctColumnWidths has unconstrained avail width");
// For an auto table, determine the potentially new percent adjusted width based
// on percent cells/cols. This probably should only be a NavQuirks thing, since
// a percentage based cell or column on an auto table should force the column to auto
nscoord basis = (aTableIsAutoWidth)
? CalcPctAdjTableWidth(aReflowState, aAvailWidth)
: aAvailWidth;
// adjust the basis to exclude table border, padding and cell spacing
nsMargin borderPadding = mTableFrame->GetContentAreaOffset(&aReflowState);
basis -= borderPadding.left + borderPadding.right + mCellSpacingTotal;
nscoord colPctTotal = 0;
struct nsSpannedEle {
nsSpannedEle *next, *prev;
PRInt32 col, colSpan;
nsTableCellFrame *cellFrame;
};
nsSpannedEle *spanList = 0;
nsSpannedEle *spanTail = 0;
PLArenaPool spanArena;
PL_INIT_ARENA_POOL(&spanArena, "AssignPctColumnWidths", 512);
// Determine the percentage contribution for cols and for cells with colspan = 1
// Iterate backwards, similarly to the reasoning in AssignNonPctColumnWidths
for (colX = numCols - 1; colX >= 0; colX--) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord maxColPctWidth = WIDTH_NOT_SET;
float maxColPct = 0.0f;
nsTableCellFrame* percentContributor = nsnull;
// Scan the cells in the col that have colspan = 1; assign PER widths
for (rowX = 0; rowX < numRows; rowX++) {
PRBool originates;
PRInt32 colSpan;
nsTableCellFrame* cellFrame = mTableFrame->GetCellInfoAt(rowX, colX, &originates, &colSpan);
// skip cells that don't originate at (rowX, colX)
if (!cellFrame || !originates) {
continue;
}
// colspans are handled in the next pass, but we record them here so we dont have to
// search for the again. row spans do not need to be handled.
if(colSpan>1) {
nsSpannedEle *spanned;
ARENA_ALLOCATE(spanned, &spanArena, 1, nsSpannedEle);
if(spanned) {
spanned->col = colX;
spanned->colSpan = colSpan;
spanned->cellFrame = cellFrame;
spanned->next = spanList;
spanned->prev = nsnull;
if(spanList) {
spanList->prev = spanned;
} else {
spanTail = spanned;
}
spanList = spanned;
}
continue;
}
// see if the cell has a style percent width specified
const nsStylePosition* cellPosition = cellFrame->GetStylePosition();
if (eStyleUnit_Percent == cellPosition->mWidth.GetUnit()) {
float percent = cellPosition->mWidth.GetPercentValue();
if (percent > maxColPct) {
maxColPct = percent;
maxColPctWidth = nsTableFrame::RoundToPixel(NSToCoordRound( ((float)basis) * maxColPct ), aPixelToTwips);
percentContributor = cellFrame;
if (!mIsNavQuirksMode) {
// need to add border and padding
nsMargin cellBorderPadding = nsTableFrame::GetBorderPadding(nsSize(basis, 0), aPixelToTwips, cellFrame);
maxColPctWidth += cellBorderPadding.left + cellBorderPadding.right;
}
}
}
}
if (WIDTH_NOT_SET == maxColPctWidth) {
// see if the col has a style percent width specified
nsStyleCoord colStyleWidth = colFrame->GetStyleWidth();
if (eStyleUnit_Percent == colStyleWidth.GetUnit()) {
maxColPct = colStyleWidth.GetPercentValue();
maxColPctWidth = nsTableFrame::RoundToPixel(NSToCoordRound( ((float)basis) * maxColPct ), aPixelToTwips);
}
}
// conflicting pct/fixed widths are recorded. Nav 4.x may be changing the
// fixed width value if it exceeds the pct value and not recording the pct
// value. This is not being done and IE5 doesn't do it either.
if (maxColPctWidth > 0) {
maxColPctWidth = PR_MAX(maxColPctWidth, colFrame->GetWidth(MIN_CON));
colFrame->SetWidth(PCT, maxColPctWidth);
colPctTotal += NSToCoordRound(100.0f * (float)maxColPct);
}
}
// if the percent total went over 100%, adjustments need to be made to right most cols
if (colPctTotal > 100) {
ReduceOverSpecifiedPctCols(nsTableFrame::RoundToPixel(NSToCoordRound(((float)(colPctTotal - 100)) * 0.01f * (float)basis), aPixelToTwips));
colPctTotal = 100;
}
// check to see if a cell spans a percentage col. This will cause the MIN_ADJ,
// FIX_ADJ, and DES_ADJ values to be recomputed
PRBool done = PR_FALSE;
nsSpannedEle *spannedCell;
for(spannedCell=spanList; !done && spannedCell; spannedCell=spannedCell->next)
{
colX = spannedCell->col;
PRInt32 colSpan = spannedCell->colSpan;
// determine if the cell spans cols which have a pct value
for (PRInt32 spanX = 0; spanX < colSpan; spanX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX + spanX);
if (!colFrame) continue;
if (colFrame->GetWidth(PCT) > 0) {
mTableFrame->SetHasCellSpanningPctCol(PR_TRUE);
// recompute the MIN_ADJ, FIX_ADJ, and DES_ADJ values
ComputeNonPctColspanWidths(aReflowState, PR_TRUE, aPixelToTwips, nsnull);
done = PR_TRUE;
break;
}
}
}
// For each col, consider the cells originating in it with colspans > 1.
// Adjust the cols that each cell spans if necessary.
// if more than one colspan originate in one column, resort the access to
// the rows so that the inner colspans are handled first
CellInfo* cellInfo = nsnull;
if(spanTail)
ARENA_ALLOCATE(cellInfo, &spanArena, numRows, CellInfo);
if(cellInfo) for(spannedCell=spanTail; spannedCell;) {
PRInt32 spannedRows = 0;
colX = spannedCell->col;
do {
cellInfo[spannedRows].cellFrame = spannedCell->cellFrame;
cellInfo[spannedRows].colSpan = spannedCell->colSpan;
++spannedRows;
} while((spannedCell=spannedCell->prev) && (spannedCell->col==colX));
if(spannedRows>1) {
NS_QuickSort(cellInfo, spannedRows, sizeof(cellInfo[0]), RowSortCB, 0);
}
for (PRInt32 i = 0; i < spannedRows; i++) {
const CellInfo *inf = &cellInfo[i];
const nsTableCellFrame* cellFrame = inf->cellFrame;
const PRInt32 colSpan = PR_MIN(inf->colSpan, numEffCols - colX);
nscoord cellPctWidth = WIDTH_NOT_SET;
// see if the cell has a style percentage width specified
const nsStylePosition* cellPosition = cellFrame->GetStylePosition();
float cellPct = 0.0f;
if (eStyleUnit_Percent == cellPosition->mWidth.GetUnit()) {
cellPct = cellPosition->mWidth.GetPercentValue();
if (colSpan == numEffCols)
cellPct = 1.0f; // overwrite spurious percent colspan width's - bug 46944
cellPctWidth = nsTableFrame::RoundToPixel(NSToCoordRound( ((float)basis) * cellPct ), aPixelToTwips);
if (!mIsNavQuirksMode) {
// need to add border and padding
nsMargin cellBorderPadding = nsTableFrame::GetBorderPadding(nsSize(basis, 0), aPixelToTwips, cellFrame);
cellPctWidth += cellBorderPadding.left + cellBorderPadding.right;
}
}
if (cellPctWidth > 0) {
nscoord spanCellSpacing = 0;
nscoord spanTotal = 0;
nscoord spanTotalNoPctAdj = 0;
nscoord colPctWidthTotal = 0; // accumulate the PCT width
nscoord colPctAdjTotal = 0; // accumulate the PCT_ADJ width or the max of PCT_ADJ and PCT width if a
// PCT width is specified
PRBool canSkipPctAdj = PR_FALSE;
// accumulate the spanTotal as the max of MIN, DES, FIX, PCT
PRInt32 spanX;
for (spanX = 0; spanX < colSpan; spanX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX + spanX);
if (!colFrame) continue;
nscoord colPctWidth = colFrame->GetWidth(PCT);
if (colPctWidth > 0) { // skip pct cols
colPctWidthTotal += colPctWidth;
colPctAdjTotal += PR_MAX(colFrame->GetWidth(PCT_ADJ), colPctWidth);
continue;
}
colPctAdjTotal += PR_MAX(colFrame->GetWidth(PCT_ADJ), 0);
nscoord colWidth = PR_MAX(colFrame->GetMinWidth(), colFrame->GetFixWidth());
colWidth = PR_MAX(colWidth, colFrame->GetDesWidth()); // XXX check this
//colWidth = PR_MAX(colWidth, colFrame->GetPctWidth());
spanTotal += colWidth;
if (colFrame->GetWidth(PCT_ADJ) <= 0) { // if we have a cell that is neither PCT or PCT_ADJ we have
// other places where we can drop the width
canSkipPctAdj = PR_TRUE;
spanTotalNoPctAdj += colWidth;
}
if ((spanX > 0) && (mTableFrame->GetNumCellsOriginatingInCol(colX + spanX) > 0)) {
spanCellSpacing += spacingX;
}
}
cellPctWidth += spanCellSpacing; // add it back in since it was subtracted from aBasisIn
if (cellPctWidth <= 0) {
continue;
}
spanTotal = canSkipPctAdj ? spanTotalNoPctAdj: spanTotal; // if we can skip the PCT_ADJ ignore theire width
colPctWidthTotal = canSkipPctAdj ? colPctAdjTotal: colPctWidthTotal;
if ((PR_MAX(colPctWidthTotal, colPctAdjTotal)+ spanCellSpacing) < cellPctWidth) {
// we have something to distribute ...
// record the percent contributions for the spanned cols
PRInt32 usedColumns = colSpan;
for (spanX = colSpan-1; spanX >= 0; spanX--) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX + spanX);
if (!colFrame) continue;
if ((colFrame->GetWidth(PCT) > 0) || (canSkipPctAdj && (colFrame->GetWidth(PCT_ADJ) > 0))) {
// dont use pct cols or if we can skip the pct adj event do not take the PCT_ADJ cols
usedColumns--;
continue;
} // count the pieces
if (usedColumns == 0)
usedColumns = 1; // avoid division by 0 later
nscoord minWidth = colFrame->GetMinWidth();
nscoord colWidth = PR_MAX(minWidth, colFrame->GetFixWidth());
colWidth = PR_MAX(colWidth, colFrame->GetDesWidth()); // XXX check this
float avail = (float)PR_MAX(cellPctWidth - colPctWidthTotal, 0);
float colPctAdj = (0 == spanTotal)
? avail / ((float) usedColumns) / ((float)basis)
: (avail / (float)basis) * (((float)colWidth) / (float)spanTotal);
if (colPctAdj > 0) {
nscoord colPctAdjWidth = colFrame->GetWidth(PCT_ADJ);
nscoord newColPctAdjWidth = nsTableFrame::RoundToPixel(NSToCoordRound(colPctAdj * (float)basis), aPixelToTwips);
if (newColPctAdjWidth > colPctAdjWidth) {
newColPctAdjWidth = PR_MAX(newColPctAdjWidth, minWidth);
if (newColPctAdjWidth > colFrame->GetWidth(PCT)) {
colFrame->SetWidth(PCT_ADJ, newColPctAdjWidth);
if(0 != basis) { // I am paranoid
colPctTotal += NSToCoordRound(100.0f *(newColPctAdjWidth-colPctAdjWidth) / (float)basis);
// accumulate the new distributed percents
}
}
}
else {
usedColumns--;
colPctWidthTotal += colPctAdjWidth; // accumulate the already distributed percents
}
}
}
}
}
} // end for (index ..
} // end for (colX ..
// We are done with our spanlist, get rid of it now
PL_FinishArenaPool(&spanArena);
// if the percent total went over 100%, adjustments need to be made to right most cols
if (colPctTotal > 100) {
ReduceOverSpecifiedPctCols(nsTableFrame::RoundToPixel(NSToCoordRound(((float)(colPctTotal - 100)) * 0.01f * (float)basis), aPixelToTwips));
}
// adjust the basis to include table border, padding and cell spacing
basis += borderPadding.left + borderPadding.right + mCellSpacingTotal;
return WrapupAssignPctColumnWidths(mTableFrame, aReflowState, basis);
}
// calculate totals by width type. The logic here is kept in synch with
// that in CanAllocate. aDupedWidths (duplicatd) are widths that will be
// allocated in BalanceColumnWidths before aTotalsWidths (e.g. aTotalWidths[PCT]
// will have aDuplicatedWidths[PCT] consisting of the MIN widths of cols which
// have a PCT width).
void BasicTableLayoutStrategy::CalculateTotals(PRInt32* aTotalCounts,
PRInt32* aTotalWidths,
PRInt32* aDupedWidths,
PRInt32& a0ProportionalCount)
{
//mTableFrame->Dump(PR_TRUE, PR_FALSE);
for (PRInt32 widthX = 0; widthX < NUM_WIDTHS; widthX++) {
aTotalCounts[widthX] = 0;
aTotalWidths[widthX] = 0;
aDupedWidths[widthX] = 0;
}
a0ProportionalCount = 0;
PRInt32 numEffCols = mTableFrame->GetEffectiveColCount();
PRInt32 colX;
for (colX = 0; colX < numEffCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
nscoord minCol = colFrame->GetMinWidth();
aTotalCounts[MIN_CON]++;
aTotalWidths[MIN_CON] += minCol;
if (e0ProportionConstraint == colFrame->GetConstraint()) {
a0ProportionalCount++;
}
nscoord pct = colFrame->GetPctWidth();
nscoord fix = colFrame->GetWidth(FIX);
nscoord fixAdj = colFrame->GetWidth(FIX_ADJ);
// if there is a pct width then no others are considered
if (pct > 0) {
aTotalCounts[PCT]++;
aTotalWidths[PCT] += PR_MAX(pct, minCol);
aDupedWidths[PCT] += minCol;
}
else if ((fix > 0) || (fixAdj > 0)) {
if (fix > 0) {
aTotalCounts[FIX]++;
aTotalWidths[FIX] += PR_MAX(fix, minCol);
aDupedWidths[FIX] += minCol;
}
if (fixAdj > 0) {
if (fixAdj > fix) {
aTotalCounts[FIX_ADJ]++;
aTotalWidths[FIX_ADJ] += PR_MAX(fixAdj, minCol);
if (fix > 0) {
aDupedWidths[FIX_ADJ] += fix;
}
else { // there was no fix
aDupedWidths[FIX_ADJ] += minCol;
}
}
}
}
else if ((eProportionConstraint == colFrame->GetConstraint()) ||
(e0ProportionConstraint == colFrame->GetConstraint())) {
aTotalCounts[MIN_PRO]++;
aTotalWidths[MIN_PRO] += PR_MAX(colFrame->GetWidth(MIN_PRO), minCol);
aDupedWidths[MIN_PRO] += minCol;
}
else {
// desired alone is lowest priority
aTotalCounts[DES_CON]++;
aTotalWidths[DES_CON] += PR_MAX(colFrame->GetDesWidth(), minCol);
aDupedWidths[DES_CON] += minCol;
}
}
}
struct ColInfo {
ColInfo(nsTableColFrame* aFrame,
PRInt32 aIndex,
PRInt32 aMinWidth,
PRInt32 aWidth,
PRInt32 aMaxWidth)
: mFrame(aFrame), mIndex(aIndex), mMinWidth(aMinWidth),
mWidth(aWidth), mMaxWidth(aMaxWidth), mWeight(0)
{}
nsTableColFrame* mFrame;
PRInt32 mIndex;
PRInt32 mMinWidth;
PRInt32 mWidth;
PRInt32 mMaxWidth;
float mWeight;
};
void
AC_Wrapup(nsTableFrame* aTableFrame,
PRInt32 aNumItems,
ColInfo** aColInfo,
PRBool aAbort = PR_FALSE)
{
if (aColInfo) {
for (PRInt32 i = 0; i < aNumItems; i++) {
if (aColInfo[i]) {
if (!aAbort) {
aTableFrame->SetColumnWidth(aColInfo[i]->mIndex, aColInfo[i]->mWidth);
}
delete aColInfo[i];
}
}
delete [] aColInfo;
}
}
void
AC_Increase(PRInt32 aNumAutoCols,
ColInfo** aColInfo,
PRInt32 aDivisor,
PRInt32& aAvailWidth,
float aPixelToTwips)
{
for (PRInt32 i = 0; i < aNumAutoCols; i++) {
if ((aAvailWidth <= 0) || (aDivisor <= 0)) {
break;
}
// aDivisor represents the sum of unallocated space (diff between max and min values)
float percent = ((float)aColInfo[i]->mMaxWidth - (float)aColInfo[i]->mMinWidth) / (float)aDivisor;
aDivisor -= aColInfo[i]->mMaxWidth - aColInfo[i]->mMinWidth;
nscoord addition = nsTableFrame::RoundToPixel(NSToCoordRound(((float)(aAvailWidth)) * percent), aPixelToTwips);
// if its the last col, try to give what's left to it
if ((i == aNumAutoCols - 1) && (addition < aAvailWidth)) {
addition = aAvailWidth;
}
// don't let the addition exceed what is available to add
addition = PR_MIN(addition, aAvailWidth);
// don't go over the col max
addition = PR_MIN(addition, aColInfo[i]->mMaxWidth - aColInfo[i]->mWidth);
aColInfo[i]->mWidth += addition;
aAvailWidth -= addition;
}
}
void
AC_Decrease(PRInt32 aNumAutoCols,
ColInfo** aColInfo,
PRInt32 aDivisor,
PRInt32& aExcess,
float aPixelToTwips)
{
for (PRInt32 i = 0; i < aNumAutoCols; i++) {
if ((aExcess <= 0) || (aDivisor <= 0)) {
break;
}
float percent = ((float)aColInfo[i]->mMaxWidth) / (float)aDivisor;
aDivisor -= aColInfo[i]->mMaxWidth;
nscoord reduction = nsTableFrame::RoundToPixel(NSToCoordRound(((float)(aExcess)) * percent), aPixelToTwips);
// if its the last col, try to remove the remaining excess from it
if ((i == aNumAutoCols - 1) && (reduction < aExcess)) {
reduction = aExcess;
}
// don't let the reduction exceed what is available to reduce
reduction = PR_MIN(reduction, aExcess);
// don't go under the col min
reduction = PR_MIN(reduction, aColInfo[i]->mWidth - aColInfo[i]->mMinWidth);
aColInfo[i]->mWidth -= reduction;
aExcess -= reduction;
}
}
void
AC_Sort(ColInfo** aColInfo, PRInt32 aNumCols)
{
// sort the cols based on the Weight
for (PRInt32 j = aNumCols - 1; j > 0; j--) {
for (PRInt32 i = 0; i < j; i++) {
if (aColInfo[i]->mWeight < aColInfo[i+1]->mWeight) { // swap them
ColInfo* save = aColInfo[i];
aColInfo[i] = aColInfo[i+1];
aColInfo[i+1] = save;
}
}
}
}
// this assumes that the table has set the width for each col to be its min
void BasicTableLayoutStrategy::AllocateConstrained(PRInt32 aAvailWidth,
PRInt32 aWidthType,
PRBool aStartAtMin,
PRInt32* aAllocTypes,
float aPixelToTwips)
{
if ((0 == aAvailWidth) || (aWidthType < 0) || (aWidthType >= NUM_WIDTHS)) {
NS_ASSERTION(PR_TRUE, "invalid args to AllocateConstrained");
return;
}
PRInt32 numCols = mTableFrame->GetColCount();
PRInt32 numConstrainedCols = 0;
nscoord sumMaxConstraints = 0;
nscoord sumMinConstraints = 0;
PRInt32 colX;
// find out how many constrained cols there are
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
if (!CanAllocate(aWidthType, aAllocTypes[colX], colFrame)) {
continue;
}
numConstrainedCols++;
}
// allocate storage for the constrained cols. Only they get adjusted.
ColInfo** colInfo = new ColInfo*[numConstrainedCols];
if (!colInfo) return;
memset(colInfo, 0, numConstrainedCols * sizeof(ColInfo *));
PRInt32 maxMinDiff = 0;
PRInt32 constrColX = 0;
// set the col info entries for each constrained col
for (colX = 0; colX < numCols; colX++) {
nsTableColFrame* colFrame = mTableFrame->GetColFrame(colX);
if (!colFrame) continue;
if (!CanAllocate(aWidthType, aAllocTypes[colX], colFrame)) {
continue;
}
nscoord minWidth = mTableFrame->GetColumnWidth(colX);
nscoord maxWidth = GetColWidth(colFrame, aWidthType);
// proportional and desired widths are handled together
PRBool haveProWidth = PR_FALSE;
if (DES_CON == aWidthType) {
// Take into acount MIN_adj width as it has been included in totals before
maxWidth = PR_MAX(maxWidth, colFrame->GetWidth(MIN_ADJ));
nscoord proWidth = colFrame->GetWidth(MIN_PRO);
if (proWidth >= 0) {
haveProWidth = PR_TRUE;
maxWidth = proWidth;
}
}
if (maxWidth <= 0) continue;
sumMaxConstraints += maxWidth;
sumMinConstraints += minWidth;
maxWidth = PR_MAX(maxWidth, minWidth);
maxMinDiff += maxWidth - minWidth;
nscoord startWidth = (aStartAtMin) ? minWidth : maxWidth;
colInfo[constrColX] = new ColInfo(colFrame, colX, minWidth, startWidth, maxWidth);
if (!colInfo[constrColX]) {
AC_Wrapup(mTableFrame, numConstrainedCols, colInfo, PR_TRUE);
return;
}
aAllocTypes[colX] = (haveProWidth) ? MIN_PRO : aWidthType;
constrColX++;
}
if (constrColX < numConstrainedCols) {
// some of the constrainted cols might have been 0 and skipped
numConstrainedCols = constrColX;
}
PRInt32 i;
if (aStartAtMin) { // allocate extra space
nscoord availWidth = aAvailWidth;
for (i = 0; i < numConstrainedCols; i++) {
// the weight here is a relative metric for determining when cols reach their max constraint.
// A col with a larger weight will reach its max before one with a smaller value.
nscoord delta = colInfo[i]->mMaxWidth - colInfo[i]->mWidth;
colInfo[i]->mWeight = (delta <= 0)
? 1000000 // cols which have already reached their max get a large value
: ((float)colInfo[i]->mMaxWidth) / ((float)delta);
}
// sort the cols based on the weight so that in one pass cols with higher
// weights will get their max earlier than ones with lower weights
// This is an innefficient bubble sort, but unless there are an unlikely
// large number of cols, it is not an issue.
AC_Sort(colInfo, numConstrainedCols);
// compute the proportion to be added to each column, don't go beyond the col's
// max. This algorithm assumes that the Weight works as stated above
AC_Increase(numConstrainedCols, colInfo, sumMaxConstraints - sumMinConstraints,
availWidth, aPixelToTwips);
}
else { // reduce each col width
nscoord reduceWidth = maxMinDiff - aAvailWidth;
if (reduceWidth < 0) {
NS_ASSERTION(PR_TRUE, "AllocateConstrained called incorrectly");
AC_Wrapup(mTableFrame, numConstrainedCols, colInfo);
return;
}
for (i = 0; i < numConstrainedCols; i++) {
// the weight here is a relative metric for determining when cols reach their min.
// A col with a larger weight will reach its min before one with a smaller value.
nscoord delta = colInfo[i]->mWidth - colInfo[i]->mMinWidth;
colInfo[i]->mWeight = (delta <= 0)
? 1000000 // cols which have already reached their min get a large value
: ((float)colInfo[i]->mWidth) / ((float)delta);
}
// sort the cols based on the Weight
AC_Sort(colInfo, numConstrainedCols);
// compute the proportion to be subtracted from each column, don't go beyond
// the col's min. This algorithm assumes that the Weight works as stated above
AC_Decrease(numConstrainedCols, colInfo, sumMaxConstraints, reduceWidth, aPixelToTwips);
}
AC_Wrapup(mTableFrame, numConstrainedCols, colInfo);
}
#ifdef DEBUG_TABLE_STRATEGY
void BasicTableLayoutStrategy::Dump(PRInt32 aIndent)
{
char* indent = new char[aIndent + 1];
if (!indent) return;
for (PRInt32 i = 0; i < aIndent + 1; i++) {
indent[i] = ' ';
}
indent[aIndent] = 0;
printf("%s**START BASIC STRATEGY DUMP** table=%p cols=%X",
indent, mTableFrame, mCols);
printf("\n%s cellSpacing=%d propRatio=%.2f navQuirks=%d",
indent, mCellSpacingTotal, mMinToDesProportionRatio, mIsNavQuirksMode);
printf(" **END BASIC STRATEGY DUMP** \n");
delete [] indent;
}
#endif
|