summaryrefslogtreecommitdiff
path: root/sw/source/core/doc/htmltbl.cxx
blob: e3effed7250c7c21587a3b0b3e0a9510a9a91564 (plain)
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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: htmltbl.cxx,v $
 * $Revision: 1.17 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sw.hxx"
#include "hintids.hxx"

//#define TEST_DELAYED_RESIZE

#ifdef TEST_DELAYED_RESIZE
#include <vcl/sound.hxx>
#endif
#ifndef _WRKWIN_HXX //autogen
#include <vcl/wrkwin.hxx>
#endif
#ifndef _APP_HXX //autogen
#include <vcl/svapp.hxx>
#endif
#include <sot/storage.hxx>
#include <fmtornt.hxx>
#include <fmtfsize.hxx>
#include <frmfmt.hxx>
#include <docary.hxx>
#include "ndtxt.hxx"
#include "doc.hxx"
#include "swtable.hxx"
#include "rootfrm.hxx"
#include "docsh.hxx"
#include "flyfrm.hxx"
#include "poolfmt.hxx"
#include "viewsh.hxx"
#include "tabfrm.hxx"

#include "htmltbl.hxx"
#include "ndindex.hxx"

using namespace ::com::sun::star;


#define COLFUZZY 20
#define MAX_TABWIDTH (USHRT_MAX - 2001)


/*  */

class SwHTMLTableLayoutConstraints
{
    USHORT nRow;                    // Start-Zeile
    USHORT nCol;                    // Start-Spalte
    USHORT nColSpan;                // COLSPAN der Zelle

    SwHTMLTableLayoutConstraints *pNext;        // die naechste Bedingung

    ULONG nMinNoAlign, nMaxNoAlign; // Zwischenergebnisse AL-Pass 1

public:

    SwHTMLTableLayoutConstraints( ULONG nMin, ULONG nMax, USHORT nRow,
                                USHORT nCol, USHORT nColSp );
    ~SwHTMLTableLayoutConstraints();

    ULONG GetMinNoAlign() const { return nMinNoAlign; }
    ULONG GetMaxNoAlign() const { return nMaxNoAlign; }

    SwHTMLTableLayoutConstraints *InsertNext( SwHTMLTableLayoutConstraints *pNxt );
    SwHTMLTableLayoutConstraints* GetNext() const { return pNext; }

    USHORT GetRow() const { return nRow; }

    USHORT GetColSpan() const { return nColSpan; }
    USHORT GetColumn() const { return nCol; }
};

/*  */

SwHTMLTableLayoutCnts::SwHTMLTableLayoutCnts( const SwStartNode *pSttNd,
                                          SwHTMLTableLayout* pTab,
                                          BOOL bNoBrTag,
                                          SwHTMLTableLayoutCnts* pNxt ) :
    pNext( pNxt ), pBox( 0 ), pTable( pTab ), pStartNode( pSttNd ),
    nPass1Done( 0 ), nWidthSet( 0 ), bNoBreakTag( bNoBrTag )
{}

SwHTMLTableLayoutCnts::~SwHTMLTableLayoutCnts()
{
    delete pNext;
    delete pTable;
}

const SwStartNode *SwHTMLTableLayoutCnts::GetStartNode() const
{
    return pBox ? pBox->GetSttNd() : pStartNode;
}


/*  */

SwHTMLTableLayoutCell::SwHTMLTableLayoutCell( SwHTMLTableLayoutCnts *pCnts,
                                          USHORT nRSpan, USHORT nCSpan,
                                          USHORT nWidth, BOOL bPrcWidth,
                                          BOOL bNWrapOpt ) :
    pContents( pCnts ),
    nRowSpan( nRSpan ), nColSpan( nCSpan ),
    nWidthOption( nWidth ), bPrcWidthOption( bPrcWidth ),
    bNoWrapOption( bNWrapOpt )
{}

SwHTMLTableLayoutCell::~SwHTMLTableLayoutCell()
{
    if( nRowSpan==1 && nColSpan==1 )
    {
        delete pContents;
    }
}

/*  */

SwHTMLTableLayoutColumn::SwHTMLTableLayoutColumn( USHORT nWidth,
                                                  BOOL bRelWidth,
                                                  BOOL bLBorder ) :
    nMinNoAlign(MINLAY), nMaxNoAlign(MINLAY), nAbsMinNoAlign(MINLAY),
    nMin(0), nMax(0),
    nAbsColWidth(0), nRelColWidth(0),
    nWidthOption( nWidth ), bRelWidthOption( bRelWidth ),
    bLeftBorder( bLBorder )
{}


/*  */

SwHTMLTableLayoutConstraints::SwHTMLTableLayoutConstraints(
    ULONG nMin, ULONG nMax, USHORT nRw, USHORT nColumn, USHORT nColSp ):
    nRow( nRw ), nCol( nColumn ), nColSpan( nColSp ),
    pNext( 0 ),
    nMinNoAlign( nMin ), nMaxNoAlign( nMax )
{}

SwHTMLTableLayoutConstraints::~SwHTMLTableLayoutConstraints()
{
    delete pNext;
}

SwHTMLTableLayoutConstraints *SwHTMLTableLayoutConstraints::InsertNext(
    SwHTMLTableLayoutConstraints *pNxt )
{
    SwHTMLTableLayoutConstraints *pPrev = 0;
    SwHTMLTableLayoutConstraints *pConstr = this;
    while( pConstr )
    {
        if( pConstr->GetRow() > pNxt->GetRow() ||
            pConstr->GetColumn() > pNxt->GetColumn() )
            break;
        pPrev = pConstr;
        pConstr = pConstr->GetNext();
    }

    if( pPrev )
    {
        pNxt->pNext = pPrev->GetNext();
        pPrev->pNext = pNxt;
        pConstr = this;
    }
    else
    {
        pNxt->pNext = this;
        pConstr = pNxt;
    }

    return pConstr;
}

/*  */

typedef SwHTMLTableLayoutColumn *SwHTMLTableLayoutColumnPtr;
typedef SwHTMLTableLayoutCell *SwHTMLTableLayoutCellPtr;

SwHTMLTableLayout::SwHTMLTableLayout(
                        const SwTable * pSwTbl,
                        USHORT nRws, USHORT nCls, BOOL bColsOpt, BOOL bColTgs,
                        USHORT nWdth, BOOL bPrcWdth, USHORT nBorderOpt,
                        USHORT nCellPad, USHORT nCellSp, SvxAdjust eAdjust,
                        USHORT nLMargin, USHORT nRMargin,
                        USHORT nBWidth, USHORT nLeftBWidth,
                        USHORT nRightBWidth,
                        USHORT nInhLeftBWidth, USHORT nInhRightBWidth ) :
    aColumns( new SwHTMLTableLayoutColumnPtr[nCls] ),
    aCells( new SwHTMLTableLayoutCellPtr[nRws*nCls] ),
    pSwTable( pSwTbl ), pLeftFillerBox( 0 ), pRightFillerBox( 0 ),
    nMin( 0 ), nMax( 0 ),
    nRows( nRws ), nCols( nCls ),
    nLeftMargin( nLMargin ), nRightMargin( nRMargin ),
    nInhAbsLeftSpace( 0 ), nInhAbsRightSpace( 0 ),
    nRelLeftFill( 0 ), nRelRightFill( 0 ),
    nRelTabWidth( 0 ), nWidthOption( nWdth ),
    nCellPadding( nCellPad ), nCellSpacing( nCellSp ), nBorder( nBorderOpt ),
    nLeftBorderWidth( nLeftBWidth ), nRightBorderWidth( nRightBWidth ),
    nInhLeftBorderWidth( nInhLeftBWidth ),
    nInhRightBorderWidth( nInhRightBWidth ),
    nBorderWidth( nBWidth ),
    nDelayedResizeAbsAvail( 0 ), nLastResizeAbsAvail( 0 ),
    nPass1Done( 0 ), nWidthSet( 0 ), eTableAdjust( eAdjust ),
    bColsOption( bColsOpt ), bColTags( bColTgs ),
    bPrcWidthOption( bPrcWdth ), bUseRelWidth( FALSE ),
    bMustResize( TRUE ), bExportable( TRUE ), bBordersChanged( FALSE ),
    bMustNotResize( FALSE ), bMustNotRecalc( FALSE )
{
    aResizeTimer.SetTimeoutHdl( STATIC_LINK( this, SwHTMLTableLayout,
                                             DelayedResize_Impl ) );
}

SwHTMLTableLayout::~SwHTMLTableLayout()
{
    USHORT i;

    for( i = 0; i < nCols; i++ )
        delete aColumns[i];
    delete[] aColumns;

    USHORT nCount = nRows*nCols;
    for( i=0; i<nCount; i++ )
        delete aCells[i];
    delete[] aCells;
}

// Die Breiten der Umrandung werden zunaechst wie in Netscape berechnet:
// Aussere Umrandung: BORDER + CELLSPACING + CELLPADDING
// Innere Umrandung: CELLSPACING + CELLPADDING
// Allerdings wird die Breite der Umrandung im SW trotzdem beachtet, wenn
// bSwBorders gesetzt ist, damit nicht faellschlich umgebrochen wird.
// MIB 27.6.97: Dabei muss auch der Abstand zum Inhalt berueckichtigt werden,
// und zwar auch dann, wenn wenn nur die gegenueberliegende Seite
// eine Umrandung hat.
USHORT SwHTMLTableLayout::GetLeftCellSpace( USHORT nCol, USHORT nColSpan,
                                            BOOL bSwBorders ) const
{
    USHORT nSpace = nCellSpacing + nCellPadding;

    if( nCol == 0 )
    {
        nSpace = nSpace + nBorder;

        if( bSwBorders && nSpace < nLeftBorderWidth )
            nSpace = nLeftBorderWidth;
    }
    else if( bSwBorders )
    {
        if( GetColumn(nCol)->HasLeftBorder() )
        {
            if( nSpace < nBorderWidth )
                nSpace = nBorderWidth;
        }
        else if( nCol+nColSpan == nCols && nRightBorderWidth &&
                 nSpace < MIN_BORDER_DIST )
        {
            ASSERT( !nCellPadding, "GetLeftCellSpace: CELLPADDING!=0" );
            // Wenn die Gegenueberliegende Seite umrandet ist muessen
            // wir zumindest den minimalen Abstand zum Inhalt
            // beruecksichtigen. (Koennte man zusaetzlich auch an
            // nCellPadding festmachen.)
            nSpace = MIN_BORDER_DIST;
        }
    }

    return nSpace;
}

USHORT SwHTMLTableLayout::GetRightCellSpace( USHORT nCol, USHORT nColSpan,
                                             BOOL bSwBorders ) const
{
    USHORT nSpace = nCellPadding;

    if( nCol+nColSpan == nCols )
    {
        nSpace += nBorder + nCellSpacing;
        if( bSwBorders && nSpace < nRightBorderWidth )
            nSpace = nRightBorderWidth;
    }
    else if( bSwBorders && GetColumn(nCol)->HasLeftBorder() &&
             nSpace < MIN_BORDER_DIST )
    {
        ASSERT( !nCellPadding, "GetRightCellSpace: CELLPADDING!=0" );
        // Wenn die Gegenueberliegende Seite umrandet ist muessen
        // wir zumindest den minimalen Abstand zum Inhalt
        // beruecksichtigen. (Koennte man zusaetzlich auch an
        // nCellPadding festmachen.)
        nSpace = MIN_BORDER_DIST;
    }

    return nSpace;
}

void SwHTMLTableLayout::AddBorderWidth( ULONG &rMin, ULONG &rMax,
                                        ULONG &rAbsMin,
                                        USHORT nCol, USHORT nColSpan,
                                        BOOL bSwBorders ) const
{
    ULONG nAdd = GetLeftCellSpace( nCol, nColSpan, bSwBorders ) +
                 GetRightCellSpace( nCol, nColSpan, bSwBorders );

    rMin += nAdd;
    rMax += nAdd;
    rAbsMin += nAdd;
}

void SwHTMLTableLayout::SetBoxWidth( SwTableBox *pBox, USHORT nCol,
                             USHORT nColSpan ) const
{
    SwFrmFmt *pFrmFmt = pBox->GetFrmFmt();

    // die Breite der Box berechnen
    SwTwips nFrmWidth = 0;
    while( nColSpan-- )
        nFrmWidth += GetColumn( nCol++ )->GetRelColWidth();

    // und neu setzen

    pFrmFmt->SetFmtAttr( SwFmtFrmSize( ATT_VAR_SIZE, nFrmWidth, 0 ));
}

void SwHTMLTableLayout::GetAvail( USHORT nCol, USHORT nColSpan,
                                  USHORT& rAbsAvail, USHORT& rRelAvail ) const
{
    rAbsAvail = 0;
    rRelAvail = 0;
    for( USHORT i=nCol; i<nCol+nColSpan;i++ )
    {
        const SwHTMLTableLayoutColumn *pColumn = GetColumn(i);
        rAbsAvail = rAbsAvail + pColumn->GetAbsColWidth();
        rRelAvail = rRelAvail + pColumn->GetRelColWidth();
    }
}

USHORT SwHTMLTableLayout::GetBrowseWidthByVisArea( const SwDoc& rDoc )
{
    ViewShell *pVSh = 0;
    rDoc.GetEditShell( &pVSh );
    if( pVSh )
    {
        return (USHORT)pVSh->GetBrowseWidth();
    }

    return 0;
}

USHORT SwHTMLTableLayout::GetBrowseWidth( const SwDoc& rDoc )
{
    // Wenn ein Layout da ist, koennen wir die Breite dort herholen.
    const SwRootFrm *pRootFrm = rDoc.GetRootFrm();
    if( pRootFrm )
    {
        const SwFrm *pPageFrm = pRootFrm->GetLower();
        if( pPageFrm )
            return (USHORT)pPageFrm->Prt().Width();
    }

    // Sonst versuchen wir es ueber die ViewShell
    USHORT nWidth = GetBrowseWidthByVisArea( rDoc );
    if( !nWidth )
    {
        // Und wenn das auch nicht geht, gibt es noch die ActualSize an der
        // DocShell.
        if( rDoc.GetDocShell() && GetpApp() && GetpApp()->GetDefaultDevice() )
        {
            // this case shouldn't happen because the filter always waits until
            // a view has been created
/*
            nWidth = (USHORT)Application::GetDefaultDevice()
                    ->PixelToLogic( rDoc.GetDocShell()->GetActualSize(),
                                    MapMode( MAP_TWIP ) ).Width();
*/
            ASSERT( nWidth, "No browse width available" );
        }
#ifdef DBG_UTIL
        else
        {
            // und wenn das auch nicht klappt, gibt es zur Zeit keine Breite
            ASSERT( nWidth, "No browse width available" );
        }
#endif
    }
    return nWidth;
}

USHORT SwHTMLTableLayout::GetBrowseWidthByTabFrm(
    const SwTabFrm& rTabFrm ) const
{
    SwTwips nWidth = 0;

    const SwFrm *pUpper = rTabFrm.GetUpper();
    if( MayBeInFlyFrame() && pUpper->IsFlyFrm() &&
        ((const SwFlyFrm *)pUpper)->GetAnchorFrm() )
    {
        // Wenn die Tabelle in einem selbst angelegten Rahmen steht, dann ist
        // die Breite Ankers und nicht die Breite Rahmens von Bedeutung.
        // Bei Absatz-gebundenen Rahmen werden Absatz-Einzuege nicht beachtet.
        const SwFrm *pAnchor = ((const SwFlyFrm *)pUpper)->GetAnchorFrm();
        if( pAnchor->IsTxtFrm() )
            nWidth = pAnchor->Frm().Width();
        else
            nWidth = pAnchor->Prt().Width();
    }
    else
    {
        nWidth = pUpper->Prt().Width();
    }

    SwTwips nUpperDummy = 0;
    long nRightOffset = 0,
         nLeftOffset  = 0;
    rTabFrm.CalcFlyOffsets( nUpperDummy, nLeftOffset, nRightOffset );
    nWidth -= (nLeftOffset + nRightOffset);

    return nWidth < USHRT_MAX ? static_cast<USHORT>(nWidth) : USHRT_MAX;
}

USHORT SwHTMLTableLayout::GetBrowseWidthByTable( const SwDoc& rDoc ) const
{
    USHORT nBrowseWidth = 0;
    SwClientIter aIter( *(SwModify*)pSwTable->GetFrmFmt() );
    SwClient* pCli = aIter.First( TYPE( SwTabFrm ));
    if( pCli )
    {
        nBrowseWidth = GetBrowseWidthByTabFrm( *(SwTabFrm*)pCli );
    }
    else
    {
        nBrowseWidth = SwHTMLTableLayout::GetBrowseWidth( rDoc );
    }

    return nBrowseWidth;
}

const SwStartNode *SwHTMLTableLayout::GetAnyBoxStartNode() const
{
    const SwStartNode *pBoxSttNd;

    const SwTableBox* pBox = pSwTable->GetTabLines()[0]->GetTabBoxes()[0];
    while( 0 == (pBoxSttNd = pBox->GetSttNd()) )
    {
        ASSERT( pBox->GetTabLines().Count() > 0,
                "Box ohne Start-Node und Lines" );
        ASSERT( pBox->GetTabLines()[0]->GetTabBoxes().Count() > 0,
                "Line ohne Boxen" );
        pBox = pBox->GetTabLines()[0]->GetTabBoxes()[0];
    }

    return pBoxSttNd;
}

SwFrmFmt *SwHTMLTableLayout::FindFlyFrmFmt() const
{
    const SwTableNode *pTblNd = GetAnyBoxStartNode()->FindTableNode();
    ASSERT( pTblNd, "Kein Table-Node?" );
    return pTblNd->GetFlyFmt();
}

static void lcl_GetMinMaxSize( ULONG& rMinNoAlignCnts, ULONG& rMaxNoAlignCnts,
                        ULONG& rAbsMinNoAlignCnts,
#ifdef FIX41370
                        BOOL& rHR,
#endif
                        SwTxtNode *pTxtNd, ULONG nIdx, BOOL bNoBreak )
{
    pTxtNd->GetMinMaxSize( nIdx, rMinNoAlignCnts, rMaxNoAlignCnts,
                           rAbsMinNoAlignCnts );
    ASSERT( rAbsMinNoAlignCnts <= rMinNoAlignCnts,
            "GetMinMaxSize: absmin > min" );
    ASSERT( rMinNoAlignCnts <= rMaxNoAlignCnts,
            "GetMinMaxSize: max > min" );

    //Bei einen <PRE>-Absatz entspricht die maximale Breite der
    // minimalen breite
    const SwFmtColl *pColl = &pTxtNd->GetAnyFmtColl();
    while( pColl && !pColl->IsDefault() &&
            (USER_FMT & pColl->GetPoolFmtId()) )
    {
        pColl = (const SwFmtColl *)pColl->DerivedFrom();
    }

    // <NOBR> in der gesamten Zelle bezieht sich auf Text, aber nicht
    // auf Tabellen. Netscape beruecksichtigt dies nur fuer Grafiken.
    if( (pColl && RES_POOLCOLL_HTML_PRE==pColl->GetPoolFmtId()) || bNoBreak )
    {
        rMinNoAlignCnts = rMaxNoAlignCnts;
        rAbsMinNoAlignCnts = rMaxNoAlignCnts;
    }
#ifdef FIX41370
    else if( pColl && RES_POOLCOLL_HTML_HR==pColl->GetPoolFmtId() )
    {
        rHR |= !pTxtNd->HasSwAttrSet() ||
                SFX_ITEM_SET != pTxtNd->GetpSwAttrSet()
                                      ->GetItemState( RES_LR_SPACE, FALSE );
    }
#endif
}

void SwHTMLTableLayout::AutoLayoutPass1()
{
    nPass1Done++;

    ClearPass1Info();

    BOOL bFixRelWidths = FALSE;
    USHORT i;

    SwHTMLTableLayoutConstraints *pConstraints = 0;

    for( i=0; i<nCols; i++ )
    {
        SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
        pColumn->ClearPass1Info( !HasColTags() );
        USHORT nMinColSpan = USHRT_MAX; // Spaltenzahl, auf die sich dir
                                        // berechnete Breite bezieht
        USHORT nColSkip = USHRT_MAX;    // Wie viele Spalten muessen
                                        // uebersprungen werden

        for( USHORT j=0; j<nRows; j++ )
        {
            SwHTMLTableLayoutCell *pCell = GetCell(j,i);
            SwHTMLTableLayoutCnts *pCnts = pCell->GetContents();

            // fix #31488#: Zum Ermitteln der naechsten zu berechnenden
            // Spalte muessen alle Zeilen herangezogen werden
            USHORT nColSpan = pCell->GetColSpan();
            if( nColSpan < nColSkip )
                nColSkip = nColSpan;

            if( !pCnts || (pCnts && !pCnts->IsPass1Done(nPass1Done)) )
            {
                // die Zelle ist leer oder ihr Inhalt wurde nich nicht
                // bearbeitet
                if( nColSpan < nMinColSpan )
                    nMinColSpan = nColSpan;

                ULONG nMinNoAlignCell = 0;
                ULONG nMaxNoAlignCell = 0;
                ULONG nAbsMinNoAlignCell = 0;
                ULONG nMaxTableCell = 0;
                ULONG nAbsMinTableCell = 0;
#ifdef FIX41370
                BOOL bHR = FALSE;
#endif

                while( pCnts )
                {
                    const SwStartNode *pSttNd = pCnts->GetStartNode();
                    if( pSttNd )
                    {
                        const SwDoc *pDoc = pSttNd->GetDoc();
                        ULONG nIdx = pSttNd->GetIndex();
                        while( !(pDoc->GetNodes()[nIdx])->IsEndNode() )
                        {
                            SwTxtNode *pTxtNd = (pDoc->GetNodes()[nIdx])->GetTxtNode();
                            if( pTxtNd )
                            {
                                ULONG nMinNoAlignCnts = 0;
                                ULONG nMaxNoAlignCnts = 0;
                                ULONG nAbsMinNoAlignCnts = 0;

                                lcl_GetMinMaxSize( nMinNoAlignCnts,
                                                   nMaxNoAlignCnts,
                                                   nAbsMinNoAlignCnts,
#ifdef FIX41370
                                                   bHR,
#endif
                                                   pTxtNd, nIdx,
                                                   pCnts->HasNoBreakTag() );

                                if( nMinNoAlignCnts > nMinNoAlignCell )
                                    nMinNoAlignCell = nMinNoAlignCnts;
                                if( nMaxNoAlignCnts > nMaxNoAlignCell )
                                    nMaxNoAlignCell = nMaxNoAlignCnts;
                                if( nAbsMinNoAlignCnts > nAbsMinNoAlignCell )
                                    nAbsMinNoAlignCell = nAbsMinNoAlignCnts;
                            }
                            else
                            {
                                SwTableNode *pTabNd = (pDoc->GetNodes()[nIdx])->GetTableNode();
                                if( pTabNd )
                                {
                                    SwHTMLTableLayout *pChild = pTabNd->GetTable().GetHTMLTableLayout();
                                    if( pChild )
                                    {
                                        pChild->AutoLayoutPass1();
                                        ULONG nMaxTableCnts = pChild->nMax;
                                        ULONG nAbsMinTableCnts = pChild->nMin;

                                        // Eine feste Tabellen-Breite wird als Minimum
                                        // und Maximum gleichzeitig uebernommen
                                        if( !pChild->bPrcWidthOption && pChild->nWidthOption )
                                        {
                                            ULONG nTabWidth = pChild->nWidthOption;
                                            if( nTabWidth >= nAbsMinTableCnts  )
                                            {
                                                nMaxTableCnts = nTabWidth;
                                                nAbsMinTableCnts = nTabWidth;
                                            }
                                            else
                                            {
                                                nMaxTableCnts = nAbsMinTableCnts;
                                            }
                                        }

                                        if( nMaxTableCnts > nMaxTableCell )
                                            nMaxTableCell = nMaxTableCnts;
                                        if( nAbsMinTableCnts > nAbsMinTableCell )
                                            nAbsMinTableCell = nAbsMinTableCnts;
                                    }
                                    nIdx = pTabNd->EndOfSectionNode()->GetIndex();
                                }
                            }
                            nIdx++;
                        }
                    }
                    else
                    {
                        ASSERT( !this, "Sub tables in HTML import?" )
                        SwHTMLTableLayout *pChild = pCnts->GetTable();
                        pChild->AutoLayoutPass1();
                        ULONG nMaxTableCnts = pChild->nMax;
                        ULONG nAbsMinTableCnts = pChild->nMin;

                        // Eine feste Tabellen-Breite wird als Minimum
                        // und Maximum gleichzeitig uebernommen
                        if( !pChild->bPrcWidthOption && pChild->nWidthOption )
                        {
                            ULONG nTabWidth = pChild->nWidthOption;
                            if( nTabWidth >= nAbsMinTableCnts  )
                            {
                                nMaxTableCnts = nTabWidth;
                                nAbsMinTableCnts = nTabWidth;
                            }
                            else
                            {
                                nMaxTableCnts = nAbsMinTableCnts;
                            }
                        }

                        if( nMaxTableCnts > nMaxTableCell )
                            nMaxTableCell = nMaxTableCnts;
                        if( nAbsMinTableCnts > nAbsMinTableCell )
                            nAbsMinTableCell = nAbsMinTableCnts;
                    }
                    pCnts->SetPass1Done( nPass1Done );
                    pCnts = pCnts->GetNext();
                }

// War frueher hinter AddBorderWidth
                // Wenn die Breite einer Tabelle in der Zelle breiter ist als
                // das, was wir fuer sonstigen Inhalt berechnet haben, mussen
                // wir die Breite der Tabelle nutzen
                if( nMaxTableCell > nMaxNoAlignCell )
                    nMaxNoAlignCell = nMaxTableCell;
                if( nAbsMinTableCell > nAbsMinNoAlignCell )
                {
                    nAbsMinNoAlignCell = nAbsMinTableCell;
                    if( nMinNoAlignCell < nAbsMinNoAlignCell )
                        nMinNoAlignCell = nAbsMinNoAlignCell;
                    if( nMaxNoAlignCell < nMinNoAlignCell )
                        nMaxNoAlignCell = nMinNoAlignCell;
                }
// War frueher hinter AddBorderWidth

                BOOL bRelWidth = pCell->IsPrcWidthOption();
                USHORT nWidth = pCell->GetWidthOption();

                // Eine NOWRAP-Option bezieht sich auf Text und auf
                // Tabellen, wird aber bei fester Zellenbreite
                // nicht uebernommen. Stattdessen wirkt die angegebene
                // Zellenbreite wie eine Mindestbreite.
                if( pCell->HasNoWrapOption() )
                {
                    if( nWidth==0 || bRelWidth )
                    {
                        nMinNoAlignCell = nMaxNoAlignCell;
                        nAbsMinNoAlignCell = nMaxNoAlignCell;
                    }
                    else
                    {
                        if( nWidth>nMinNoAlignCell )
                            nMinNoAlignCell = nWidth;
                        if( nWidth>nAbsMinNoAlignCell )
                            nAbsMinNoAlignCell = nWidth;
                    }
                }
#ifdef FIX41370
                else if( bHR && nWidth>0 && !bRelWidth )
                {
                    // Ein kleiner Hack, um einen Bug in Netscape 4.0
                    // nachzubilden (siehe #41370#). Wenn eine Zelle eine
                    // fixe Breite besitzt und gleichzeitig ein HR, wird
                    // sie nie schmaler als die angegebene Breite.
                    // (Genaugenomen scheint die Zelle nie schmaler zu werden
                    // als die HR-Linie, denn wenn man fuer die Linie eine
                    // Breite angibt, die breiter ist als die der Zelle, dann
                    // wird die Zelle so breit wie die Linie. Das bekommen wir
                    // natuerlich nicht hin.)
                    if( nWidth>nMinNoAlignCell )
                        nMinNoAlignCell = nWidth;
                    if( nWidth>nAbsMinNoAlignCell )
                        nAbsMinNoAlignCell = nWidth;
                }
#endif

                // Mindestbreite fuer Inhalt einhalten
                if( nMinNoAlignCell < MINLAY )
                    nMinNoAlignCell = MINLAY;
                if( nMaxNoAlignCell < MINLAY )
                    nMaxNoAlignCell = MINLAY;
                if( nAbsMinNoAlignCell < MINLAY )
                    nAbsMinNoAlignCell = MINLAY;

                // Umrandung und Abstand zum Inhalt beachten.
                AddBorderWidth( nMinNoAlignCell, nMaxNoAlignCell,
                                nAbsMinNoAlignCell, i, nColSpan );

                if( 1==nColSpan )
                {
                    // die Werte direkt uebernehmen
                    pColumn->MergeMinMaxNoAlign( nMinNoAlignCell,
                                                 nMaxNoAlignCell,
                                                 nAbsMinNoAlignCell );

                    // bei den WIDTH angaben gewinnt die breiteste
                    if( !HasColTags() )
                        pColumn->MergeCellWidthOption( nWidth, bRelWidth );
                }
                else
                {
                    // die Angaben erst am Ende, und zwar zeilenweise von
                    // links nach rechts bearbeiten

                    // Wann welche Werte wie uebernommen werden ist weiter
                    // unten erklaert.
                    if( !HasColTags() && nWidth && !bRelWidth )
                    {
                        ULONG nAbsWidth = nWidth, nDummy = 0, nDummy2 = 0;
                        AddBorderWidth( nAbsWidth, nDummy, nDummy2,
                                        i, nColSpan, FALSE );

                        if( nAbsWidth >= nMinNoAlignCell )
                        {
                            nMaxNoAlignCell = nAbsWidth;
                            if( HasColsOption() )
                                nMinNoAlignCell = nAbsWidth;
                        }
                        else if( nAbsWidth >= nAbsMinNoAlignCell )
                        {
                            nMaxNoAlignCell = nAbsWidth;
                            nMinNoAlignCell = nAbsWidth;
                        }
                        else
                        {
                            nMaxNoAlignCell = nAbsMinNoAlignCell;
                            nMinNoAlignCell = nAbsMinNoAlignCell;
                        }
                    }
                    else if( HasColsOption() || HasColTags() )
                        nMinNoAlignCell = nAbsMinNoAlignCell;

                    SwHTMLTableLayoutConstraints *pConstr =
                        new SwHTMLTableLayoutConstraints( nMinNoAlignCell,
                            nMaxNoAlignCell, j, i, nColSpan );
                    if( pConstraints )
                        pConstraints = pConstraints->InsertNext( pConstr );
                    else
                        pConstraints = pConstr;
                }
            }
        }

        ASSERT( nMinColSpan>0 && nColSkip>0 && nColSkip <= nMinColSpan,
                "Layout Pass 1: Da werden Spalten vergessen!" );
        ASSERT( nMinColSpan!=USHRT_MAX,
                "Layout Pass 1: unnoetiger Schleifendurchlauf oder Bug" );

        if( 1==nMinColSpan )
        {
            // es gibt Zellen mit COLSPAN 1 und demnach auch sinnvolle
            // Werte in pColumn

            // Werte anhand folgender Tabelle (Netscape 4.0 pv 3) uebernehmen:
            //
            // WIDTH:           kein COLS       COLS
            //
            // keine            min = min       min = absmin
            //                  max = max       max = max
            //
            // >= min           min = min       min = width
            //                  max = width     max = width
            //
            // >= absmin        min = wdith(*)  min = width
            //                  max = width     max = width
            //
            // < absmin         min = absmin    min = absmin
            //                  max = absmin    max = absmin
            //
            // (*) Netscape benutzt hier die Mindestbreite ohne einen
            //     Umbruch vor der letzten Grafik. Haben wir (noch?) nicht,
            //     also belassen wir es bei width.^

            if( pColumn->GetWidthOption() && !pColumn->IsRelWidthOption() )
            {
                // absolute Breiten als Minimal- und Maximalbreite
                // uebernehmen.
                ULONG nAbsWidth = pColumn->GetWidthOption();
                ULONG nDummy = 0, nDummy2 = 0;
                AddBorderWidth( nAbsWidth, nDummy, nDummy2, i, 1, FALSE );

                if( nAbsWidth >= pColumn->GetMinNoAlign() )
                {
                    pColumn->SetMinMax( HasColsOption() ? nAbsWidth
                                                   : pColumn->GetMinNoAlign(),
                                        nAbsWidth );
                }
                else if( nAbsWidth >= pColumn->GetAbsMinNoAlign() )
                {
                    pColumn->SetMinMax( nAbsWidth, nAbsWidth );
                }
                else
                {
                    pColumn->SetMinMax( pColumn->GetAbsMinNoAlign(),
                                        pColumn->GetAbsMinNoAlign() );
                }
            }
            else
            {
                pColumn->SetMinMax( HasColsOption() ? pColumn->GetAbsMinNoAlign()
                                               : pColumn->GetMinNoAlign(),
                                    pColumn->GetMaxNoAlign() );
            }
        }
        else if( USHRT_MAX!=nMinColSpan )
        {
            // kann irgendwas !=0 sein, weil es durch die Constraints
            // angepasst wird.
            pColumn->SetMinMax( MINLAY, MINLAY );

            // die naechsten Spalten muessen nicht bearbeitet werden
            i += (nColSkip-1);
        }

        nMin += pColumn->GetMin();
        nMax += pColumn->GetMax();
        bFixRelWidths |= pColumn->IsRelWidthOption();
    }

    // jetzt noch die Constrains verarbeiten
    SwHTMLTableLayoutConstraints *pConstr = pConstraints;
    while( pConstr )
    {
        // Erstmal muss die Breite analog zu den den Spaltenbreiten
        // aufbereitet werden
        USHORT nCol = pConstr->GetColumn();
        USHORT nColSpan = pConstr->GetColSpan();
        ULONG nConstrMin = pConstr->GetMinNoAlign();
        ULONG nConstrMax = pConstr->GetMaxNoAlign();

        // jetzt holen wir uns die bisherige Breite der ueberspannten
        // Spalten
        ULONG nColsMin = 0;
        ULONG nColsMax = 0;
        for( USHORT j=nCol; j<nCol+nColSpan; j++ )
        {
            SwHTMLTableLayoutColumn *pColumn = GetColumn( j );
            nColsMin += pColumn->GetMin();
            nColsMax += pColumn->GetMax();
        }

        if( nColsMin<nConstrMin )
        {
            // den Minimalwert anteilig auf die Spalten verteilen
            ULONG nMinD = nConstrMin-nColsMin;

            if( nConstrMin > nColsMax )
            {
                // Anteilig anhand der Mindestbreiten
                USHORT nEndCol = nCol+nColSpan;
                ULONG nDiff = nMinD;
                for( USHORT ic=nCol; ic<nEndCol; ic++ )
                {
                    SwHTMLTableLayoutColumn *pColumn = GetColumn( ic );

                    ULONG nColMin = pColumn->GetMin();
                    ULONG nColMax = pColumn->GetMax();

                    nMin -= nColMin;
                    ULONG nAdd = ic<nEndCol-1 ? (nColMin * nMinD) / nColsMin
                                             : nDiff;
                    nColMin += nAdd;
                    nMin += nColMin;
                    ASSERT( nDiff >= nAdd, "Ooops: nDiff stimmt nicht mehr" );
                    nDiff -= nAdd;

                    if( nColMax < nColMin )
                    {
                        nMax -= nColMax;
                        nColsMax -= nColMax;
                        nColMax = nColMin;
                        nMax += nColMax;
                        nColsMax += nColMax;
                    }

                    pColumn->SetMinMax( nColMin, nColMax );
                }
            }
            else
            {
                // Anteilig anhand der Differenz zwischen Max und Min
                for( USHORT ic=nCol; ic<nCol+nColSpan; ic++ )
                {
                    SwHTMLTableLayoutColumn *pColumn = GetColumn( ic );

                    ULONG nDiff = pColumn->GetMax()-pColumn->GetMin();
                    if( nMinD < nDiff )
                        nDiff = nMinD;

                    pColumn->AddToMin( nDiff );

                    ASSERT( pColumn->GetMax() >= pColumn->GetMin(),
                            "Wieso ist die SPalte auf einmal zu schmal?" )

                    nMin += nDiff;
                    nMinD -= nDiff;
                }
            }
        }

        if( !HasColTags() && nColsMax<nConstrMax )
        {
            ULONG nMaxD = nConstrMax-nColsMax;

            for( USHORT ic=nCol; ic<nCol+nColSpan; ic++ )
            {
                SwHTMLTableLayoutColumn *pColumn = GetColumn( ic );

                nMax -= pColumn->GetMax();

                pColumn->AddToMax( (pColumn->GetMax() * nMaxD) / nColsMax );

                nMax += pColumn->GetMax();
            }
        }

        pConstr = pConstr->GetNext();
    }


    if( bFixRelWidths )
    {
        if( HasColTags() )
        {
            // Zum Anpassen der relativen Breiten werden im 1. Schritt die
            // Minmalbreiten aller anzupassenden Zellen jeweils mit der
            // relativen Breite einer Spalte multipliziert. Dadurch stimmen
            // dann die Breitenverhaeltnisse der Spalten untereinander.
            // Ausserdem wird der Faktor berechnet, um den die Zelle dadurch
            // breiter gworden ist als die Minmalbreite.
            // Im 2. Schritt werden dann die berechneten Breiten durch diesen
            // Faktor geteilt. Dadurch bleibt die Breite (nimd.) einer Zelle
            // erhalten und dient als Ausgangsbasis fuer die andern Breiten.
            // Es werden auch hier nur die Maximalbreiten beeinflusst!

            ULONG nAbsMin = 0;  // absolte Min-Breite alter Spalten mit
                                // relativer Breite
            ULONG nRel = 0;     // Summe der relativen Breiten aller Spalten
            for( i=0; i<nCols; i++ )
            {
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( pColumn->IsRelWidthOption() && pColumn->GetWidthOption() )
                {
                    nAbsMin += pColumn->GetMin();
                    nRel += pColumn->GetWidthOption();
                }
            }

            ULONG nQuot = ULONG_MAX;
            for( i=0; i<nCols; i++ )
            {
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( pColumn->IsRelWidthOption() )
                {
                    nMax -= pColumn->GetMax();
                    if( pColumn->GetWidthOption() && pColumn->GetMin() )
                    {
                        pColumn->SetMax( nAbsMin * pColumn->GetWidthOption() );
                        ULONG nColQuot = pColumn->GetMax() / pColumn->GetMin();
                        if( nColQuot<nQuot )
                            nQuot = nColQuot;
                    }
                }
            }
            ASSERT( 0==nRel || nQuot!=ULONG_MAX,
                    "Wo sind die relativen Spalten geblieben?" );
            for( i=0; i<nCols; i++ )
            {
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( pColumn->IsRelWidthOption() )
                {
                    if( pColumn->GetWidthOption() )
                        pColumn->SetMax( pColumn->GetMax() / nQuot );
                    else
                        pColumn->SetMax( pColumn->GetMin() );
                    ASSERT( pColumn->GetMax() >= pColumn->GetMin(),
                            "Maximale Spaltenbreite kleiner als Minimale" );
                    nMax += pColumn->GetMax();
                }
            }
        }
        else
        {
            USHORT nRel = 0;        // Summe der relativen Breiten aller Spalten
            USHORT nRelCols = 0;    // Anzahl Spalten mit relativer Angabe
            ULONG nRelMax = 0;      // Anteil am Maximum dieser Spalten
            for( i=0; i<nCols; i++ )
            {
                ASSERT( nRel<=100, "relative Breite aller Spalten>100%" );
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( pColumn->IsRelWidthOption() && pColumn->GetWidthOption() )
                {
                    // Sicherstellen, dass die relativen breiten nicht
                    // ueber 100% landen
                    USHORT nColWidth = pColumn->GetWidthOption();
                    if( nRel+nColWidth > 100 )
                    {
                        nColWidth = 100 - nRel;
                        pColumn->SetWidthOption( nColWidth, TRUE, FALSE );
                    }
                    nRelMax += pColumn->GetMax();
                    nRel = nRel + nColWidth;
                    nRelCols++;
                }
                else if( !pColumn->GetMin() )
                {
                    // Die Spalte ist leer (wurde also auschliesslich
                    // durch COLSPAN erzeugt) und darf deshalb auch
                    // keine %-Breite zugewiesen bekommen.
                    nRelCols++;
                }
            }

            // Eventuell noch vorhandene Prozente werden auf die Spalten ohne
            // eine Breiten-Angabe verteilt. Wie in Netscape werden die
            // verbleibenden Prozente enstprechend der Verhaeltnisse
            // der Maximalbreiten der in Frage kommenden Spalten
            // untereinander verteilt.
            // ??? Wie beruecksichtigen bei den Maximalbreiten auch Spalten
            // mit fester Breite. Ist das richtig???
            if( nRel < 100 && nRelCols < nCols )
            {
                USHORT nRelLeft = 100 - nRel;
                ULONG nFixMax = nMax - nRelMax;
                for( i=0; i<nCols; i++ )
                {
                    SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                    if( !pColumn->IsRelWidthOption() &&
                        !pColumn->GetWidthOption() &&
                        pColumn->GetMin() )
                    {
                        // den Rest bekommt die naechste Spalte
                        USHORT nColWidth =
                            (USHORT)((pColumn->GetMax() * nRelLeft) / nFixMax);
                        pColumn->SetWidthOption( nColWidth, TRUE, FALSE );
                    }
                }
            }

            // nun die Maximalbreiten entsprechend anpassen
            ULONG nQuotMax = ULONG_MAX;
            ULONG nOldMax = nMax;
            nMax = 0;
            for( i=0; i<nCols; i++ )
            {
                // Spalten mit %-Angaben werden enstprechend angepasst.
                // Spalten, die
                // - keine %-Angabe besitzen und in einer Tabelle mit COLS
                //   oder WIDTH vorkommen, oder
                // - als Breite 0% angegeben haben erhalten die Minimalbreite
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( pColumn->IsRelWidthOption() && pColumn->GetWidthOption() )
                {
                    ULONG nNewMax;
                    ULONG nColQuotMax;
                    if( !nWidthOption )
                    {
                        nNewMax = nOldMax * pColumn->GetWidthOption();
                        nColQuotMax = nNewMax / pColumn->GetMax();
                    }
                    else
                    {
                        nNewMax = nMin * pColumn->GetWidthOption();
                        nColQuotMax = nNewMax / pColumn->GetMin();
                    }
                    pColumn->SetMax( nNewMax );
                    if( nColQuotMax < nQuotMax )
                        nQuotMax = nColQuotMax;
                }
                else if( HasColsOption() || nWidthOption ||
                         (pColumn->IsRelWidthOption() &&
                          !pColumn->GetWidthOption()) )
                    pColumn->SetMax( pColumn->GetMin() );
            }
            // und durch den Quotienten teilen
            ASSERT( nQuotMax!=ULONG_MAX, "Wo sind die relativen Spalten geblieben?" );
            for( i=0; i<nCols; i++ )
            {
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( pColumn->IsRelWidthOption() && pColumn->GetWidthOption() )
                {
                    if( pColumn->GetWidthOption() )
                    {
                        pColumn->SetMax( pColumn->GetMax() / nQuotMax );
                        ASSERT( pColumn->GetMax() >= pColumn->GetMin(),
                                "Minimalbreite ein Spalte Groesser Maximum" );
                        if( pColumn->GetMax() < pColumn->GetMin() )
                            pColumn->SetMax( pColumn->GetMin() );
                    }
                }
                nMax += pColumn->GetMax();
            }
        }
    }

    delete pConstraints;
}

// nAbsAvail ist der verfuegbare Platz in TWIPS.
// nRelAvail ist der auf USHRT_MAX bezogene verfuegbare Platz oder 0
// nAbsSpace ist der Anteil von nAbsAvail, der durch der umgebende Zelle
//           fur die Umrandung und den Abstand zum Inhalt reserviert ist.
void SwHTMLTableLayout::AutoLayoutPass2( USHORT nAbsAvail, USHORT nRelAvail,
                                         USHORT nAbsLeftSpace,
                                         USHORT nAbsRightSpace,
                                         USHORT nParentInhAbsSpace )
{
    // Erstmal fuehren wie jede Menge Plausibilaets-Test durch

    // Eine abolute zur Verfuegung stehende Breite muss immer uebergeben
    // werden.
    ASSERT( nAbsAvail, "AutoLayout Pass 2: Keine absolute Breite gegeben" );

    // Eine realtive zur Verfuegung stehende Breite darf nur und muss fuer
    // Tabellen in Tabellen uebergeben
    ASSERT( IsTopTable() == (nRelAvail==0),
            "AutoLayout Pass 2: Rel. Breite bei Tab in Tab oder umgekehrt" );

    // Die Minimalbreite der Tabelle darf natuerlich nie groesser sein
    // als das die Maximalbreite.
    ASSERT( nMin<=nMax, "AutoLayout Pass2: nMin > nMax" );

    // Die verfuegbare Breite, fuer die die Tabelle berechnet wurde, merken.
    // (Dies ist ein guter Ort, denn hier kommer wir bei der Erstberechnung
    // der Tabelle aus dem Parser und bei jedem _Resize-Aufruf vorbei.)
    nLastResizeAbsAvail = nAbsAvail;

    // Schritt 1: Der verfuegbar Platz wird an linke/rechte Raender,
    // vorhandene Filler-Zellen und Abstande angepasst

    // Abstand zum Inhalt und Unrandung
    USHORT nAbsLeftFill = 0, nAbsRightFill = 0;
    if( !IsTopTable() &&
        GetMin() + nAbsLeftSpace + nAbsRightSpace <= nAbsAvail )
    {
        nAbsLeftFill = nAbsLeftSpace;
        nAbsRightFill = nAbsRightSpace;
    }

    // Linker und rechter Abstand
    if( nLeftMargin || nRightMargin )
    {
        if( IsTopTable() )
        {
            // fuer die Top-Table beruecksichtigen wir die Raender immer,
            // den die Minimalbreite der Tabelle wird hier nie unterschritten
            nAbsAvail -= (nLeftMargin + nRightMargin);
        }
        else if( GetMin() + nLeftMargin + nRightMargin <= nAbsAvail )
        {
            // sonst beruecksichtigen wir die Raender nur, wenn auch Platz
            // fuer sie da ist (nMin ist hier bereits berechnet!)
            nAbsLeftFill = nAbsLeftFill + nLeftMargin;
            nAbsRightFill = nAbsRightFill + nRightMargin;
        }
    }

    // Filler-Zellen
    if( !IsTopTable() )
    {
        if( pLeftFillerBox && nAbsLeftFill<MINLAY+nInhLeftBorderWidth )
            nAbsLeftFill = MINLAY+nInhLeftBorderWidth;
        if( pRightFillerBox && nAbsRightFill<MINLAY+nInhRightBorderWidth )
            nAbsRightFill = MINLAY+nInhRightBorderWidth;
    }

    // Anpassen des verfuegbaren Platzes.
    nRelLeftFill = 0;
    nRelRightFill = 0;
    if( !IsTopTable() && (nAbsLeftFill>0 || nAbsRightFill) )
    {
        ULONG nAbsLeftFillL = nAbsLeftFill, nAbsRightFillL = nAbsRightFill;

        nRelLeftFill = (USHORT)((nAbsLeftFillL * nRelAvail) / nAbsAvail);
        nRelRightFill = (USHORT)((nAbsRightFillL * nRelAvail) / nAbsAvail);

        nAbsAvail -= (nAbsLeftFill + nAbsRightFill);
        if( nRelAvail )
            nRelAvail -= (nRelLeftFill + nRelRightFill);
    }


    // Schritt 2: Die absolute Tabellenbreite wird berechnet.
    USHORT nAbsTabWidth = 0;
    bUseRelWidth = FALSE;
    if( nWidthOption )
    {
        if( bPrcWidthOption )
        {
            ASSERT( nWidthOption<=100, "Prozentangabe zu gross" );
            if( nWidthOption > 100 )
                nWidthOption = 100;

            // Die absolute Breite entspricht den angegeben Prozent der
            // zur Verfuegung stehenden Breite.
            // Top-Tabellen bekommen nur eine relative Breite, wenn der
            // verfuegbare Platz *echt groesser* ist als die Minimalbreite.
            // ACHTUNG: Das "echte groesser" ist noetig, weil der Wechsel
            // von einer relativen Breite zu einer absoluten Breite durch
            // Resize sonst zu einer Endlosschleife fuehrt.
            // Weil bei Tabellen in Rahmen kein Resize aufgerufen wird,
            // wenn der Rahmen eine nicht-relative Breite besitzt, koennen
            // wir da solche Spielchen nicht spielen
            // MIB 19.2.98: Wegen fix #47394# spielen wir solche Spielchen
            // jetzt doch. Dort war eine Grafik in einer 1%-breiten
            // Tabelle und hat da natuerlich nicht hineingepasst.
            nAbsTabWidth = (USHORT)( ((ULONG)nAbsAvail * nWidthOption) / 100 );
            if( IsTopTable() &&
                ( /*MayBeInFlyFrame() ||*/ (ULONG)nAbsTabWidth > nMin ) )
            {
                nRelAvail = USHRT_MAX;
                bUseRelWidth = TRUE;
            }
        }
        else
        {
            nAbsTabWidth = nWidthOption;
            if( nAbsTabWidth > MAX_TABWIDTH )
                nAbsTabWidth = MAX_TABWIDTH;

            // Tabellen in Tabellen duerfen niemals breiter werden als der
            // verfuegbare Platz.
            if( !IsTopTable() && nAbsTabWidth > nAbsAvail )
                nAbsTabWidth = nAbsAvail;
        }
    }

    ASSERT( IsTopTable() || nAbsTabWidth<=nAbsAvail,
            "AutoLayout Pass2: nAbsTabWidth > nAbsAvail fuer Tab in Tab" );
    ASSERT( !nRelAvail || nAbsTabWidth<=nAbsAvail,
            "AutoLayout Pass2: nAbsTabWidth > nAbsAvail fuer relative Breite" );

    // Catch fuer die beiden Asserts von oben (man weiss ja nie!)
    if( (!IsTopTable() || nRelAvail>0) && nAbsTabWidth>nAbsAvail )
        nAbsTabWidth = nAbsAvail;


    // Schritt 3: Bestimmen der Spaltenbreiten und ggf. auch der
    // absoluten und relativen Tabellenbreiten.
    if( (!IsTopTable() && nMin > (ULONG)nAbsAvail) ||
        nMin > MAX_TABWIDTH )
    {
        // Wenn
        // - das Minumum einer inneren Tabelle groesser ist als der
        //   verfuegbare Platz, oder
        // - das Minumum einer Top-Table groesser ist als USHRT_MAX
        // muss die Tabelle an den verfuegbaren Platz bzw. USHRT_MAX
        // abgepasst werden. Dabei bleiben die Verhaeltnisse der Breiten
        // untereinander erhalten.

        nAbsTabWidth = IsTopTable() ? MAX_TABWIDTH : nAbsAvail;
        nRelTabWidth = (nRelAvail ? nRelAvail : nAbsTabWidth );

        // First of all, we check wether we can fit the layout constrains,
        // that are: Every cell's width excluding the borders must be at least
        // MINLAY:

        ULONG nRealMin = 0;
        for( USHORT i=0; i<nCols; i++ )
        {
            ULONG nRealColMin = MINLAY, nDummy1, nDummy2;
            AddBorderWidth( nRealColMin, nDummy1, nDummy2, i, 1 );
            nRealMin += nRealColMin;
        }
        if( (nRealMin >= nAbsTabWidth) || (nRealMin >= nMin) )
        {
            // "Nichts geht mehr". We cannot get the minimum column widths
            // the layout wants to have.

            USHORT nAbs = 0, nRel = 0;
            SwHTMLTableLayoutColumn *pColumn;
            for( USHORT i=0; i<nCols-1; i++ )
            {
                pColumn = GetColumn( i );
                ULONG nColMin = pColumn->GetMin();
                if( nColMin <= USHRT_MAX )
                {
                    pColumn->SetAbsColWidth(
                        (USHORT)((nColMin * nAbsTabWidth) / nMin) );
                    pColumn->SetRelColWidth(
                        (USHORT)((nColMin * nRelTabWidth) / nMin) );
                }
                else
                {
                    double nColMinD = nColMin;
                    pColumn->SetAbsColWidth(
                        (USHORT)((nColMinD * nAbsTabWidth) / nMin) );
                    pColumn->SetRelColWidth(
                        (USHORT)((nColMinD * nRelTabWidth) / nMin) );
                }

                nAbs = nAbs + (USHORT)pColumn->GetAbsColWidth();
                nRel = nRel + (USHORT)pColumn->GetRelColWidth();
            }
            pColumn = GetColumn( nCols-1 );
            pColumn->SetAbsColWidth( nAbsTabWidth - nAbs );
            pColumn->SetRelColWidth( nRelTabWidth - nRel );
        }
        else
        {
            ULONG nDistAbs = nAbsTabWidth - nRealMin;
            ULONG nDistRel = nRelTabWidth - nRealMin;
            ULONG nDistMin = nMin - nRealMin;
            USHORT nAbs = 0, nRel = 0;
            SwHTMLTableLayoutColumn *pColumn;
            for( USHORT i=0; i<nCols-1; i++ )
            {
                pColumn = GetColumn( i );
                ULONG nColMin = pColumn->GetMin();
                ULONG nRealColMin = MINLAY, nDummy1, nDummy2;
                AddBorderWidth( nRealColMin, nDummy1, nDummy2, i, 1 );

                if( nColMin <= USHRT_MAX )
                {
                    pColumn->SetAbsColWidth(
                        (USHORT)((((nColMin-nRealColMin) * nDistAbs) / nDistMin) + nRealColMin) );
                    pColumn->SetRelColWidth(
                        (USHORT)((((nColMin-nRealColMin) * nDistRel) / nDistMin) + nRealColMin) );
                }
                else
                {
                    double nColMinD = nColMin;
                    pColumn->SetAbsColWidth(
                        (USHORT)((((nColMinD-nRealColMin) * nDistAbs) / nDistMin) + nRealColMin) );
                    pColumn->SetRelColWidth(
                        (USHORT)((((nColMinD-nRealColMin) * nDistRel) / nDistMin) + nRealColMin) );
                }

                nAbs = nAbs + (USHORT)pColumn->GetAbsColWidth();
                nRel = nRel + (USHORT)pColumn->GetRelColWidth();
            }
            pColumn = GetColumn( nCols-1 );
            pColumn->SetAbsColWidth( nAbsTabWidth - nAbs );
            pColumn->SetRelColWidth( nRelTabWidth - nRel );
        }
    }
    else if( nMax <= (ULONG)(nAbsTabWidth ? nAbsTabWidth : nAbsAvail) )
    {
        // Wenn
        // - die Tabelle eine fixe Breite besitzt und das Maximum der
        //   Tabelle kleiner ist, oder
        // - das Maximum kleiner ist als der verfuegbare Platz
        // kann das Maximum direkt uebernommen werden bzw. die Tabelle nur
        // unter Beruecksichtigung des Maxumums an die fixe Breite
        // angepasst werden.

        // Keine fixe Breite, dann das Maximum nehmen.
        if( !nAbsTabWidth )
            nAbsTabWidth = (USHORT)nMax;

        // Eine Top-Table darf auch beriter werden als der verfuegbare Platz.
        if( nAbsTabWidth > nAbsAvail )
        {
            ASSERT( IsTopTable(),
                    "Tabelle in Tabelle soll breiter werden als umgebende Zelle" );
            nAbsAvail = nAbsTabWidth;
        }

        // Nur den Anteil der relativen Breite verwenden, der auch fuer
        // die absolute Breite verwendet wuerde.
        ULONG nAbsTabWidthL = nAbsTabWidth;
        nRelTabWidth =
            ( nRelAvail ? (USHORT)((nAbsTabWidthL * nRelAvail) / nAbsAvail)
                        : nAbsTabWidth );

        // Gibt es Spalten mit und Spalten ohne %-Angabe?
        ULONG nFixMax = nMax;
        for( USHORT i=0; i<nCols; i++ )
        {
            const SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
            if( pColumn->IsRelWidthOption() && pColumn->GetWidthOption()>0 )
                nFixMax -= pColumn->GetMax();
        }

        if( nFixMax > 0 && nFixMax < nMax )
        {
            // ja, dann den zu verteilenden Platz nur auf die Spalten
            // mit %-Angabe verteilen.

            // In diesem (und nur in diesem) Fall gibt es Spalten,
            // die ihre Maximalbreite genau einhalten, also weder
            // schmaler noch breiter werden. Beim zurueckrechnen der
            // absoluten Breite aus der relativen Breite kann es
            // zu Rundungsfehlern kommen (bug #45598#). Um die auszugeleichen
            // werden zuerst die fixen Breiten entsprechend korrigiert
            // eingestellt und erst danach die relativen.

            USHORT nAbs = 0, nRel = 0;
            USHORT nFixedCols = 0;
            USHORT i;

            for( i = 0; i < nCols; i++ )
            {
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( !pColumn->IsRelWidthOption() || !pColumn->GetWidthOption() )
                {
                    // Die Spalte behaelt ihre Breite bei.
                    nFixedCols++;
                    ULONG nColMax = pColumn->GetMax();
                    pColumn->SetAbsColWidth( (USHORT)nColMax );

                    ULONG nRelColWidth =
                        (nColMax * nRelTabWidth) / nAbsTabWidth;
                    ULONG nChkWidth =
                        (nRelColWidth * nAbsTabWidth) / nRelTabWidth;
                    if( nChkWidth < nColMax )
                        nRelColWidth++;
                    else if( nChkWidth > nColMax )
                        nRelColWidth--;
                    pColumn->SetRelColWidth( (USHORT)nRelColWidth );

                    nAbs = nAbs + (USHORT)nColMax;
                    nRel = nRel + (USHORT)nRelColWidth;
                }
            }

            // Zu verteilende Anteile des Maximums und der relativen und
            // absoluten Breiten. nFixMax entspricht an dieser Stelle
            // nAbs, so dass man gleich nFixMax haette nehmen koennen.
            // Der Code ist so aber verstaendlicher.
            ASSERT( nFixMax == nAbs, "Zwei Schleifen, zwei Summen?" )
            ULONG nDistMax = nMax - nFixMax;
            USHORT nDistAbsTabWidth = nAbsTabWidth - nAbs;
            USHORT nDistRelTabWidth = nRelTabWidth - nRel;

            for( i=0; i<nCols; i++ )
            {
                SwHTMLTableLayoutColumn *pColumn = GetColumn( i );
                if( pColumn->IsRelWidthOption() && pColumn->GetWidthOption() > 0 )
                {
                    // Die Spalte wird anteilig breiter.
                    nFixedCols++;
                    if( nFixedCols == nCols )
                    {
                        pColumn->SetAbsColWidth( nAbsTabWidth-nAbs );
                        pColumn->SetRelColWidth( nRelTabWidth-nRel );
                    }
                    else
                    {
                        ULONG nColMax = pColumn->GetMax();
                        pColumn->SetAbsColWidth(
                            (USHORT)((nColMax * nDistAbsTabWidth) / nDistMax) );
                        pColumn->SetRelColWidth(
                            (USHORT)((nColMax * nDistRelTabWidth) / nDistMax) );
                    }
                    nAbs = nAbs + pColumn->GetAbsColWidth();
                    nRel = nRel + pColumn->GetRelColWidth();
                }
            }
            ASSERT( nCols==nFixedCols, "Spalte vergessen!" );
        }
        else
        {
            // nein, dann den zu verteilenden Platz auf alle Spalten
            // gleichmaessig vertilen.
            for( USHORT i=0; i<nCols; i++ )
            {
                ULONG nColMax = GetColumn( i )->GetMax();
                GetColumn( i )->SetAbsColWidth(
                    (USHORT)((nColMax * nAbsTabWidth) / nMax) );
                GetColumn( i )->SetRelColWidth(
                    (USHORT)((nColMax * nRelTabWidth) / nMax) );
            }
        }
    }
    else
    {
        // den ueber die Minimalbreite herausgehenden Platz entsprechend
        // den einzelnen Spalten anteilig zuschlagen
        if( !nAbsTabWidth )
            nAbsTabWidth = nAbsAvail;
        if( nAbsTabWidth < nMin )
            nAbsTabWidth = (USHORT)nMin;

        if( nAbsTabWidth > nAbsAvail )
        {
            ASSERT( IsTopTable(),
                    "Tabelle in Tabelle soll breiter werden als Platz da ist" );
            nAbsAvail = nAbsTabWidth;
        }

        ULONG nAbsTabWidthL = nAbsTabWidth;
        nRelTabWidth =
            ( nRelAvail ? (USHORT)((nAbsTabWidthL * nRelAvail) / nAbsAvail)
                        : nAbsTabWidth );
        double nW = nAbsTabWidth - nMin;
        double nD = (nMax==nMin ? 1 : nMax-nMin);
        USHORT nAbs = 0, nRel = 0;
        for( USHORT i=0; i<nCols-1; i++ )
        {
            double nd = GetColumn( i )->GetMax() - GetColumn( i )->GetMin();
            ULONG nAbsColWidth = GetColumn( i )->GetMin() + (ULONG)((nd*nW)/nD);
            ULONG nRelColWidth = nRelAvail
                                    ? (nAbsColWidth * nRelTabWidth) / nAbsTabWidth
                                    : nAbsColWidth;

            GetColumn( i )->SetAbsColWidth( (USHORT)nAbsColWidth );
            GetColumn( i )->SetRelColWidth( (USHORT)nRelColWidth );
            nAbs = nAbs + (USHORT)nAbsColWidth;
            nRel = nRel + (USHORT)nRelColWidth;
        }
        GetColumn( nCols-1 )->SetAbsColWidth( nAbsTabWidth - nAbs );
        GetColumn( nCols-1 )->SetRelColWidth( nRelTabWidth - nRel );

    }

    // Schritt 4: Fuer Tabellen in Tabellen kann es links und/oder rechts
    // noch Ausgleichzellen geben. Deren Breite wird jetzt berechnet.
    nInhAbsLeftSpace = 0;
    nInhAbsRightSpace = 0;
    if( !IsTopTable() && (nRelLeftFill>0 || nRelRightFill>0 ||
                          nAbsTabWidth<nAbsAvail) )
    {
        // Die Breite von zusaetzlichen Zellen zur Ausrichtung der
        // inneren Tabelle bestimmen
        USHORT nAbsDist = (USHORT)(nAbsAvail-nAbsTabWidth);
        USHORT nRelDist = (USHORT)(nRelAvail-nRelTabWidth);
        USHORT nParentInhAbsLeftSpace = 0, nParentInhAbsRightSpace = 0;

        // Groesse und Position der zusaetzlichen Zellen bestimmen
        switch( eTableAdjust )
        {
        case SVX_ADJUST_RIGHT:
            nAbsLeftFill = nAbsLeftFill + nAbsDist;
            nRelLeftFill = nRelLeftFill + nRelDist;
            nParentInhAbsLeftSpace = nParentInhAbsSpace;
            break;
        case SVX_ADJUST_CENTER:
            {
                USHORT nAbsLeftDist = nAbsDist / 2;
                nAbsLeftFill = nAbsLeftFill + nAbsLeftDist;
                nAbsRightFill += nAbsDist - nAbsLeftDist;
                USHORT nRelLeftDist = nRelDist / 2;
                nRelLeftFill = nRelLeftFill + nRelLeftDist;
                nRelRightFill += nRelDist - nRelLeftDist;
                nParentInhAbsLeftSpace = nParentInhAbsSpace / 2;
                nParentInhAbsRightSpace = nParentInhAbsSpace -
                                          nParentInhAbsLeftSpace;
            }
            break;
        case SVX_ADJUST_LEFT:
        default:
            nAbsRightFill = nAbsRightFill + nAbsDist;
            nRelRightFill = nRelRightFill + nRelDist;
            nParentInhAbsRightSpace = nParentInhAbsSpace;
            break;
        }

        ASSERT( !pLeftFillerBox || nRelLeftFill>0,
                "Fuer linke Filler-Box ist keine Breite da!" );
        ASSERT( !pRightFillerBox || nRelRightFill>0,
                "Fuer rechte Filler-Box ist keine Breite da!" );

        // Filler-Breiten werden auf die ausseren Spalten geschlagen, wenn
        // es nach dem ersten Durchlauf keine Boxen fuer sie gibt (nWidth>0)
        // oder ihre Breite zu klein wuerde oder wenn es COL-Tags gibt und
        // die Filler-Breite der Umrandung-Breite entspricht (dann haben wir
        // die Tabelle wahrscheinlich selbst exportiert)
        if( nRelLeftFill && !pLeftFillerBox &&
            ( nWidthSet>0 || nAbsLeftFill<MINLAY+nInhLeftBorderWidth ||
              (HasColTags() && nAbsLeftFill < nAbsLeftSpace+nParentInhAbsLeftSpace+20) ) )
//          (nAbsLeftFill<MINLAY || nAbsLeftFill<=nAbsLeftSpace) )
        {
            SwHTMLTableLayoutColumn *pColumn = GetColumn( 0 );
            pColumn->SetAbsColWidth( pColumn->GetAbsColWidth()+nAbsLeftFill );
            pColumn->SetRelColWidth( pColumn->GetRelColWidth()+nRelLeftFill );
            nRelLeftFill = 0;
            nInhAbsLeftSpace = nAbsLeftSpace + nParentInhAbsLeftSpace;
        }
        if( nRelRightFill && !pRightFillerBox &&
            ( nWidthSet>0 || nAbsRightFill<MINLAY+nInhRightBorderWidth ||
              (HasColTags() && nAbsRightFill < nAbsRightSpace+nParentInhAbsRightSpace+20) ) )
//          (nAbsRightFill<MINLAY || nAbsRightFill<=nAbsRightSpace) )
        {
            SwHTMLTableLayoutColumn *pColumn = GetColumn( nCols-1 );
            pColumn->SetAbsColWidth( pColumn->GetAbsColWidth()+nAbsRightFill );
            pColumn->SetRelColWidth( pColumn->GetRelColWidth()+nRelRightFill );
            nRelRightFill = 0;
            nInhAbsRightSpace = nAbsRightSpace + nParentInhAbsRightSpace;
        }
    }
}

static BOOL lcl_ResizeLine( const SwTableLine*& rpLine, void* pPara );

static BOOL lcl_ResizeBox( const SwTableBox*& rpBox, void* pPara )
{
    USHORT *pWidth = (USHORT *)pPara;

    if( !rpBox->GetSttNd() )
    {
        USHORT nWidth = 0;
        ((SwTableBox *)rpBox)->GetTabLines().ForEach( &lcl_ResizeLine, &nWidth );
        rpBox->GetFrmFmt()->SetFmtAttr( SwFmtFrmSize( ATT_VAR_SIZE, nWidth, 0 ));
        *pWidth = *pWidth + nWidth;
    }
    else
    {
        *pWidth = *pWidth + (USHORT)rpBox->GetFrmFmt()->GetFrmSize().GetSize().Width();
    }

    return TRUE;
}

static BOOL lcl_ResizeLine( const SwTableLine*& rpLine, void* pPara )
{
    USHORT *pWidth = (USHORT *)pPara;
#ifdef DBG_UTIL
    USHORT nOldWidth = *pWidth;
#endif
    *pWidth = 0;
    ((SwTableLine *)rpLine)->GetTabBoxes().ForEach( &lcl_ResizeBox, pWidth );

#ifdef DBG_UTIL
    ASSERT( !nOldWidth || Abs(*pWidth-nOldWidth) < COLFUZZY,
            "Zeilen einer Box sind unterschiedlich lang" );
#endif

    return TRUE;
}

void SwHTMLTableLayout::SetWidths( BOOL bCallPass2, USHORT nAbsAvail,
                                   USHORT nRelAvail, USHORT nAbsLeftSpace,
                                   USHORT nAbsRightSpace,
                                   USHORT nParentInhAbsSpace )
{
    // SetWidth muss am Ende einmal mehr fuer jede Zelle durchlaufen
    // worden sein.
    nWidthSet++;

    // Schritt 0: Wenn noetig, wird hier noch der Pass2 des Layout-Alogithmus
    // aufgerufen.
    if( bCallPass2 )
        AutoLayoutPass2( nAbsAvail, nRelAvail, nAbsLeftSpace, nAbsRightSpace,
                         nParentInhAbsSpace );

    // Schritt 1: Setzten der neuen Breite an allen Content-Boxen.
    // Da die Boxen nichts von der HTML-Tabellen-Struktur wissen, wird
    // ueber die HTML-Tabellen-Struktur iteriert. Fuer Tabellen in Tabellen
    // in Tabellen wird rekursiv SetWidth aufgerufen.
    for( USHORT i=0; i<nRows; i++ )
    {
        for( USHORT j=0; j<nCols; j++ )
        {
            SwHTMLTableLayoutCell *pCell = GetCell( i, j );

            SwHTMLTableLayoutCnts* pCntnts = pCell->GetContents();
            while( pCntnts && !pCntnts->IsWidthSet(nWidthSet) )
            {
                SwTableBox *pBox = pCntnts->GetTableBox();
                if( pBox )
                {
                    SetBoxWidth( pBox, j, pCell->GetColSpan() );
                }
                else
                {
                    USHORT nAbs = 0, nRel = 0, nLSpace = 0, nRSpace = 0,
                           nInhSpace = 0;
                    if( bCallPass2 )
                    {
                        USHORT nColSpan = pCell->GetColSpan();
                        GetAvail( j, nColSpan, nAbs, nRel );
                        nLSpace = GetLeftCellSpace( j, nColSpan );
                        nRSpace = GetRightCellSpace( j, nColSpan );
                        nInhSpace = GetInhCellSpace( j, nColSpan );
                    }
                    pCntnts->GetTable()->SetWidths( bCallPass2, nAbs, nRel,
                                                    nLSpace, nRSpace,
                                                    nInhSpace );
                }

                pCntnts->SetWidthSet( nWidthSet );
                pCntnts = pCntnts->GetNext();
            }
        }
    }

    // Schritt 2: Wenn eine Top-Tabelle vorliegt, werden jetzt die Formate
    // der Nicht-Content-Boxen angepasst. Da diese aufgrund der
    // Garbage-Collection in der HTML-Tabelle nicht bekannt sind, muessen
    // wir hier ueber die Tabelle iterieren. Bei der Gelegenheit wird auch
    // das Tabellen-Frameformat angepasst. Fuer Tabellen in Tabellen werden
    // stattdessen die Breiten der Filler-Zellen gesetzt.
    if( IsTopTable() )
    {
        USHORT nCalcTabWidth = 0;
        ((SwTable *)pSwTable)->GetTabLines().ForEach( &lcl_ResizeLine,
                                                      &nCalcTabWidth );
        ASSERT( Abs( nRelTabWidth-nCalcTabWidth ) < COLFUZZY,
                "Tabellebreite stimmt nicht mit Zeilenbreite ueberein." );

        // Beim Anpassen des Tabellen-Formats dieses locken, weil sonst
        // die Boxformate erneut angepasst werden. Ausserdem muss eine
        // evtl. vorhandene %-Angabe in jedem Fall erhalten bleiben.
        SwFrmFmt *pFrmFmt = pSwTable->GetFrmFmt();
        ((SwTable *)pSwTable)->LockModify();
        SwFmtFrmSize aFrmSize( pFrmFmt->GetFrmSize() );
        aFrmSize.SetWidth( nRelTabWidth );
        BOOL bRel = bUseRelWidth &&
                    text::HoriOrientation::FULL!=pFrmFmt->GetHoriOrient().GetHoriOrient();
        aFrmSize.SetWidthPercent( (BYTE)(bRel ? nWidthOption : 0) );
        pFrmFmt->SetFmtAttr( aFrmSize );
        ((SwTable *)pSwTable)->UnlockModify();

        // Wenn die Tabelle in einem Rahmen steht, muss auch noch dessen
        // breite angepasst werden.
        if( MayBeInFlyFrame() )
        {
            SwFrmFmt *pFlyFrmFmt = FindFlyFrmFmt();
            if( pFlyFrmFmt )
            {
                SwFmtFrmSize aFlyFrmSize( ATT_VAR_SIZE, nRelTabWidth, MINLAY );

                if( bUseRelWidth )
                {
                    // Bei %-Angaben wird die Breite auf das Minimum gesetzt.
                    aFlyFrmSize.SetWidth(  nMin > USHRT_MAX ? USHRT_MAX
                                                            : nMin );
                    aFlyFrmSize.SetWidthPercent( (BYTE)nWidthOption );
                }
                pFlyFrmFmt->SetFmtAttr( aFlyFrmSize );
            }
        }

#ifdef DBG_UTIL
        {
            // steht im tblrwcl.cxx
            extern void _CheckBoxWidth( const SwTableLine&, SwTwips );

            // checke doch mal ob die Tabellen korrekte Breiten haben
            SwTwips nSize = pSwTable->GetFrmFmt()->GetFrmSize().GetWidth();
            const SwTableLines& rLines = pSwTable->GetTabLines();
            for( USHORT n = 0; n < rLines.Count(); ++n  )
                _CheckBoxWidth( *rLines[ n ], nSize );
        }
#endif

    }
    else
    {
        if( pLeftFillerBox )
        {
            pLeftFillerBox->GetFrmFmt()->SetFmtAttr(
                SwFmtFrmSize( ATT_VAR_SIZE, nRelLeftFill, 0 ));
        }
        if( pRightFillerBox )
        {
            pRightFillerBox->GetFrmFmt()->SetFmtAttr(
                SwFmtFrmSize( ATT_VAR_SIZE, nRelRightFill, 0 ));
        }
    }
}

void SwHTMLTableLayout::_Resize( USHORT nAbsAvail, BOOL bRecalc )
{
    // Wenn bRecalc gestzt ist, hat sich am Inhalt der Tabelle etwas
    // geaendert. Es muss dann der erste Pass noch einmal durchgefuehrt
    // werden.
    if( bRecalc )
        AutoLayoutPass1();

    SwRootFrm *pRoot = (SwRootFrm*)GetDoc()->GetRootFrm();
    if ( pRoot && pRoot->IsCallbackActionEnabled() )
        pRoot->StartAllAction();

    // Sonst koennen die Breiten gesetzt werden, wobei zuvor aber jewils
    // noch der Pass 2 laufen muss.
    SetWidths( TRUE, nAbsAvail );

    if ( pRoot && pRoot->IsCallbackActionEnabled() )
        pRoot->EndAllAction( TRUE );    //True per VirDev (Browsen ruhiger)
}

IMPL_STATIC_LINK( SwHTMLTableLayout, DelayedResize_Impl, void*, EMPTYARG )
{
#ifdef TEST_DELAYED_RESIZE
    Sound::Beep( SOUND_WARNING );
#endif
    pThis->aResizeTimer.Stop();
    pThis->_Resize( pThis->nDelayedResizeAbsAvail,
                    pThis->bDelayedResizeRecalc );

    return 0;
}


BOOL SwHTMLTableLayout::Resize( USHORT nAbsAvail, BOOL bRecalc,
                                BOOL bForce, ULONG nDelay )
{
    if( 0 == nAbsAvail )
        return FALSE;
    ASSERT( IsTopTable(), "Resize darf nur an Top-Tabellen aufgerufen werden" );

    // Darf die Tabelle uberhaupt Resized werden oder soll sie es trotzdem?
    if( bMustNotResize && !bForce )
        return FALSE;

    // Darf ein Recalc der Tabelle durchgefuehrt werden?
    if( bMustNotRecalc && !bForce )
        bRecalc = FALSE;

    const SwDoc *pDoc = GetDoc();

    // Wenn es ein Layout gibt, wurde evtl. die Groesse der Root-Frames
    // und nicht die der VisArea uebergeben. Wenn wir nicht in einem Rahmen
    // stehen, muss die Tabelle allerdings fuer die VisArea berechnet werden,
    // weil sond die Umschaltung von relativ nach absolut nicht funktioniert.
    if( pDoc->GetRootFrm() && pDoc->get(IDocumentSettingAccess::BROWSE_MODE) )
    {
        USHORT nVisAreaWidth = GetBrowseWidthByVisArea( *pDoc );
        if( nVisAreaWidth < nAbsAvail && !FindFlyFrmFmt() )
            nAbsAvail = nVisAreaWidth;
    }

    if( nDelay==0 && aResizeTimer.IsActive() )
    {
        // Wenn beim Aufruf eines synchronen Resize noch ein asynchrones
        // Resize aussteht, dann werden nur die neuen Werte uebernommen.

        bRecalc |= bDelayedResizeRecalc;
        nDelayedResizeAbsAvail = nAbsAvail;
        return FALSE;
    }

    // Optimierung:
    // Wenn die Minima/Maxima nicht neu berechnet werden sollen und
    // - die Breite der Tabelle nie neu berechnet werden muss, oder
    // - die Tabelle schon fuer die uebergebene Breite berechnet wurde, oder
    // - der verfuegbare Platz kleiner oder gleich der Minimalbreite ist
    //   und die Tabelle bereits die Minimalbreite besitzt, oder
    // - der verfuegbare Platz groesser ist als die Maximalbreite und
    //   die Tabelle bereits die Maximalbreite besitzt
    // wird sich an der Tabelle nichts aendern.
    if( !bRecalc && ( !bMustResize ||
                      (nLastResizeAbsAvail==nAbsAvail) ||
                      (nAbsAvail<=nMin && nRelTabWidth==nMin) ||
                      (!bPrcWidthOption && nAbsAvail>=nMax && nRelTabWidth==nMax) ) )
        return FALSE;

    if( nDelay==HTMLTABLE_RESIZE_NOW )
    {
        if( aResizeTimer.IsActive() )
            aResizeTimer.Stop();
        _Resize( nAbsAvail, bRecalc );
    }
    else if( nDelay > 0 )
    {
        nDelayedResizeAbsAvail = nAbsAvail;
        bDelayedResizeRecalc = bRecalc;
        aResizeTimer.SetTimeout( nDelay );
        aResizeTimer.Start();
#ifdef TEST_DELAYED_RESIZE
        Sound::Beep( SOUND_DEFAULT );
#endif
    }
    else
    {
        _Resize( nAbsAvail, bRecalc );
    }

    return TRUE;
}

void SwHTMLTableLayout::BordersChanged( USHORT nAbsAvail, BOOL bRecalc )
{
    bBordersChanged = TRUE;

    Resize( nAbsAvail, bRecalc );
}