summaryrefslogtreecommitdiff
path: root/sw/source/core/layout/flowfrm.cxx
blob: 638670e7e1b5260c2fb13c817577a0154a870aa4 (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
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
/*************************************************************************
 *
 *  $RCSfile: flowfrm.cxx,v $
 *
 *  $Revision: 1.21 $
 *
 *  last change: $Author: hr $ $Date: 2003-03-27 15:40:25 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source 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.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifdef PRECOMPILED
#include "core_pch.hxx"
#endif

#pragma hdrstop

#include "pam.hxx"
#include "swtable.hxx"
#include "frame.hxx"
#include "pagefrm.hxx"
#include "flyfrm.hxx"
#include "viewsh.hxx"
#include "doc.hxx"
#include "viewimp.hxx"
#include "dflyobj.hxx"
#include "frmtool.hxx"
#include "dcontact.hxx"

#ifndef _SVX_BRKITEM_HXX //autogen
#include <svx/brkitem.hxx>
#endif
#ifndef _SVX_KEEPITEM_HXX //autogen
#include <svx/keepitem.hxx>
#endif

#ifndef _FMTSRND_HXX //autogen
#include <fmtsrnd.hxx>
#endif
#ifndef _FMTANCHR_HXX //autogen
#include <fmtanchr.hxx>
#endif
#ifndef _FMTPDSC_HXX //autogen
#include <fmtpdsc.hxx>
#endif
#ifndef _SVX_ULSPITEM_HXX //autogen
#include <svx/ulspitem.hxx>
#endif
#ifndef SW_TGRDITEM_HXX
#include <tgrditem.hxx>
#endif
#ifndef _NODE_HXX //autogen
#include <node.hxx>
#endif
#ifndef _TXTFTN_HXX //autogen
#include <txtftn.hxx>
#endif
#ifndef _FMTFTN_HXX //autogen
#include <fmtftn.hxx>
#endif
#ifndef _SVX_PGRDITEM_HXX
#include <svx/pgrditem.hxx>
#endif
#ifndef _PARATR_HXX
#include <paratr.hxx>
#endif

#include "ftnfrm.hxx"
#include "txtfrm.hxx"
#include "tabfrm.hxx"
#include "pagedesc.hxx"
#include "layact.hxx"
#include "frmsh.hxx"
#include "fmtornt.hxx"
#include "flyfrms.hxx"
#include "sectfrm.hxx"
#include "section.hxx"
#include "dbg_lay.hxx"
#include "lineinfo.hxx"

BOOL SwFlowFrm::bMoveBwdJump = FALSE;


/*************************************************************************
|*
|*  SwFlowFrm::SwFlowFrm()
|*
|*  Ersterstellung      MA 26. Apr. 95
|*  Letzte Aenderung    MA 26. Apr. 95
|*
|*************************************************************************/


SwFlowFrm::SwFlowFrm( SwFrm &rFrm ) :
    rThis( rFrm ),
    pFollow( 0 )
{
    bLockJoin = bIsFollow = bCntntLock = bOwnFtnNum =
        bFtnLock = bFlyLock = FALSE;
}


/*************************************************************************
|*
|*  SwFlowFrm::IsFollowLocked()
|*     return TRUE if any follow has the JoinLocked flag
|*
|*************************************************************************/

sal_Bool SwFlowFrm::HasLockedFollow() const
{
    const SwFlowFrm* pFrm = GetFollow();
    while( pFrm )
    {
        if( pFrm->IsJoinLocked() )
            return sal_True;
        pFrm = pFrm->GetFollow();
    }
    return sal_False;
}

/*************************************************************************
|*
|*  SwFlowFrm::IsKeepFwdMoveAllowed()
|*
|*  Ersterstellung      MA 20. Jul. 94
|*  Letzte Aenderung    MA 16. May. 95
|*
|*************************************************************************/


BOOL SwFlowFrm::IsKeepFwdMoveAllowed()
{
    //Wenn der Vorgaenger das KeepAttribut traegt und auch dessen
    //Vorgaenger usw. bis zum ersten der Kette und fuer diesen das
    //IsFwdMoveAllowed ein FALSE liefert, so ist das Moven eben nicht erlaubt.
    SwFrm *pFrm = &rThis;
    if ( !pFrm->IsInFtn() )
        do
        {   if ( pFrm->GetAttrSet()->GetKeep().GetValue() )
                pFrm = pFrm->GetIndPrev();
            else
                return TRUE;
        } while ( pFrm );

                  //Siehe IsFwdMoveAllowed()
    return pFrm ? pFrm->GetIndPrev() != 0 : FALSE;
}

/*************************************************************************
|*
|*    SwFlowFrm::CheckKeep()
|*
|*    Beschreibung
|*    Ersterstellung    MA 20. Jun. 95
|*    Letzte Aenderung  MA 09. Apr. 97
|*
|*************************************************************************/


void SwFlowFrm::CheckKeep()
{
    //Den 'letzten' Vorgaenger mit KeepAttribut anstossen, denn
    //die ganze Truppe koennte zuruckrutschen.
    SwFrm *pPre = rThis.GetIndPrev();
    if( pPre->IsSctFrm() )
    {
        SwFrm *pLast = ((SwSectionFrm*)pPre)->FindLastCntnt();
        if( pLast && pLast->FindSctFrm() == pPre )
            pPre = pLast;
        else
            return;
    }
    SwFrm* pTmp;
    BOOL bKeep;
    while ( TRUE == (bKeep = pPre->GetAttrSet()->GetKeep().GetValue()) &&
            0 != ( pTmp = pPre->GetIndPrev() ) )
    {
        if( pTmp->IsSctFrm() )
        {
            SwFrm *pLast = ((SwSectionFrm*)pTmp)->FindLastCntnt();
            if( pLast && pLast->FindSctFrm() == pTmp )
                pTmp = pLast;
            else
                break;
        }
        pPre = pTmp;
    }
    if ( bKeep )
        pPre->InvalidatePos();
}

/*************************************************************************
|*
|*  SwFlowFrm::IsKeep()
|*
|*  Ersterstellung      MA 09. Apr. 97
|*  Letzte Aenderung    MA 09. Apr. 97
|*
|*************************************************************************/


BOOL SwFlowFrm::IsKeep( const SwBorderAttrs &rAttrs ) const
{
    BOOL bKeep = !rThis.IsInFtn() && rAttrs.GetAttrSet().GetKeep().GetValue();
    //Keep Zaehlt nicht wenn die Umbrueche dagegen sprechen.
    if ( bKeep )
    {
        switch ( rAttrs.GetAttrSet().GetBreak().GetBreak() )
        {
            case SVX_BREAK_COLUMN_AFTER:
            case SVX_BREAK_COLUMN_BOTH:
            case SVX_BREAK_PAGE_AFTER:
            case SVX_BREAK_PAGE_BOTH:
                bKeep = FALSE;
        }
        if ( bKeep )
        {
            SwFrm *pNxt;
            if( 0 != (pNxt = rThis.FindNextCnt()) &&
                (!pFollow || pNxt != pFollow->GetFrm()))
            {
                const SwAttrSet* pSet = NULL;

                if ( pNxt->IsInTab() )
                {
                    SwTabFrm* pTab = pNxt->FindTabFrm();
                    if ( ! rThis.IsInTab() || rThis.FindTabFrm() != pTab )
                        pSet = &pTab->GetFmt()->GetAttrSet();
                }

                if ( ! pSet )
                    pSet = pNxt->GetAttrSet();

                ASSERT( pSet, "No AttrSet to check keep attribute" )

                if ( pSet->GetPageDesc().GetPageDesc() )
                    bKeep = FALSE;
                else switch ( pSet->GetBreak().GetBreak() )
                {
                    case SVX_BREAK_COLUMN_BEFORE:
                    case SVX_BREAK_COLUMN_BOTH:
                    case SVX_BREAK_PAGE_BEFORE:
                    case SVX_BREAK_PAGE_BOTH:
                        bKeep = FALSE;
                }
            }
        }
    }
    return bKeep;
}

/*************************************************************************
|*
|*  SwFlowFrm::BwdMoveNecessary()
|*
|*  Ersterstellung      MA 20. Jul. 94
|*  Letzte Aenderung    MA 02. May. 96
|*
|*************************************************************************/


BYTE SwFlowFrm::BwdMoveNecessary( const SwPageFrm *pPage, const SwRect &rRect )
{
    // Der return-Wert entscheidet mit,
    // ob auf Zurueckgeflossen werden muss, (3)
    // ob das gute alte WouldFit gerufen werden kann (0, 1)
    // oder ob ein Umhaengen und eine Probeformatierung sinnvoll ist (2)
    // dabei bedeutet Bit 1, dass Objekte an mir selbst verankert sind
    // und Bit 2, dass ich anderen Objekten ausweichen muss.

    //Wenn ein SurroundObj, dass einen Umfluss wuenscht mit dem Rect ueberlappt
    //ist der Fluss notwendig (weil die Verhaeltnisse nicht geschaetzt werden
    //koennen), es kann allerdings ggf. eine TestFormatierung stattfinden.
    //Wenn das SurroundObj ein Fly ist und ich selbst ein Lower bin oder der Fly
    //Lower von mir ist, so spielt er keine Rolle.
    //Wenn das SurroundObj in einem zeichengebunden Fly verankert ist, und ich
    //selbst nicht Lower dieses Zeichengebundenen Flys bin, so spielt der Fly
    //keine Rolle.
    //#32639# Wenn das Objekt bei mir verankert ist kann ich es
    //vernachlaessigen, weil es hoechstwahrscheinlich (!?) mitfliesst,
    //eine TestFormatierung ist dann allerdings nicht erlaubt!
    BYTE nRet = 0;
    SwFlowFrm *pTmp = this;
    do
    {   // Wenn an uns oder einem Follow Objekte haengen, so
        // kann keine ProbeFormatierung stattfinden, da absatzgebundene
        // nicht richtig beruecksichtigt wuerden und zeichengebundene sollten
        // gar nicht zur Probe formatiert werden.
        if( pTmp->GetFrm()->GetDrawObjs() )
            nRet = 1;
        pTmp = pTmp->GetFollow();
    } while ( !nRet && pTmp );
    if ( pPage->GetSortedObjs() )
    {
        const SwSortDrawObjs &rObjs = *pPage->GetSortedObjs();
        ULONG nIndex = ULONG_MAX;
        for ( USHORT i = 0; nRet < 3 && i < rObjs.Count(); ++i )
        {
            SdrObject *pObj = rObjs[i];
            SdrObjUserCall *pUserCall;
            const SwFrmFmt *pFmt = pObj->IsWriterFlyFrame() ?
                ((SwVirtFlyDrawObj*)pObj)->GetFmt() :
                ((SwContact*)(pUserCall = GetUserCall(pObj)))->GetFmt();
            const SwRect aRect( pObj->GetBoundRect() );
            if ( aRect.IsOver( rRect ) &&
                 pFmt->GetSurround().GetSurround() != SURROUND_THROUGHT )
            {
                if( rThis.IsLayoutFrm() && //Fly Lower von This?
                    Is_Lower_Of( &rThis, pObj ) )
                    continue;
                const SwFrm* pAnchor;
                if( pObj->IsWriterFlyFrame() )
                {
                    const SwFlyFrm *pFly = ((SwVirtFlyDrawObj*)pObj)->GetFlyFrm();
                    if ( pFly->IsAnLower( &rThis ) )//This Lower vom Fly?
                        continue;
                    pAnchor = pFly->GetAnchor();
                }
                else
                    pAnchor = ((SwDrawContact*)pUserCall)->GetAnchor();

                if ( pAnchor == &rThis )
                {
                    nRet |= 1;
                    continue;
                }

                //Nicht wenn das Objekt im Textfluss hinter mir verankert ist,
                //denn dann weiche ich ihm nicht aus.
                if ( ::IsFrmInSameKontext( pAnchor, &rThis ) )
                {
                    if ( pFmt->GetAnchor().GetAnchorId() == FLY_AT_CNTNT )
                    {
                        // Den Index des anderen erhalten wir immer ueber das Ankerattr.
                        ULONG nTmpIndex = pFmt->GetAnchor().GetCntntAnchor()->nNode.GetIndex();
                        // Jetzt wird noch ueberprueft, ob der aktuelle Absatz vor dem
                        // Anker des verdraengenden Objekts im Text steht, dann wird
                        // nicht ausgewichen.
                        // Der Index wird moeglichst ueber einen SwFmtAnchor ermittelt,
                        // da sonst recht teuer.
                        if( ULONG_MAX == nIndex )
                        {
                            const SwNode *pNode;
                            if ( rThis.IsCntntFrm() )
                                pNode = ((SwCntntFrm&)rThis).GetNode();
                            else if( rThis.IsSctFrm() )
                                pNode = ((SwSectionFmt*)((SwSectionFrm&)rThis).
                                        GetFmt())->GetSectionNode();
                            else
                            {
                                ASSERT( rThis.IsTabFrm(), "new FowFrm?" );
                                pNode = ((SwTabFrm&)rThis).GetTable()->
                                    GetTabSortBoxes()[0]->GetSttNd()->FindTableNode();
                            }
                            nIndex = pNode->GetIndex();
                        }
                        if( nIndex < nTmpIndex )
                            continue;
                    }
                }
                else
                    continue;

                nRet |= 2;
            }
        }
    }
    return nRet;
}

/*************************************************************************
|*
|*  SwFlowFrm::CutTree(), PasteTree(), MoveSubTree()
|*
|*  Beschreibung        Eine Spezialisierte Form des Cut() und Paste(), die
|*      eine ganze Kette umhaengt (naehmlich this und folgende). Dabei werden
|*      nur minimale Operationen und Benachrichtigungen ausgefuehrt.
|*  Ersterstellung      MA 18. Mar. 93
|*  Letzte Aenderung    MA 18. May. 95
|*
|*************************************************************************/


SwLayoutFrm *SwFlowFrm::CutTree( SwFrm *pStart )
{
    //Der Start und alle Nachbarn werden ausgeschnitten, sie werden aneinander-
    //gereiht und ein Henkel auf den ersten wird zurueckgeliefert.
    //Zurueckbleibende werden geeignet invalidiert.

    SwLayoutFrm *pLay = pStart->GetUpper();
    if ( pLay->IsInFtn() )
        pLay = pLay->FindFtnFrm();
    if( pLay )
    {
        SwFrm* pTmp = pStart->GetIndPrev();
        if( pTmp )
            pTmp->Prepare( PREP_QUOVADIS );
    }

    //Nur fix auschneiden und zwar so, dass klare Verhaeltnisse bei den
    //Verlassenen herrschen. Die Pointer der ausgeschnittenen Kette zeigen
    //noch wer weiss wo hin.
    if ( pStart == pStart->GetUpper()->Lower() )
        pStart->GetUpper()->pLower = 0;
    if ( pStart->GetPrev() )
    {
        pStart->GetPrev()->pNext = 0;
        pStart->pPrev = 0;
    }

    if ( pLay->IsFtnFrm() )
    {   if ( !pLay->Lower() && !pLay->IsColLocked() &&
             !((SwFtnFrm*)pLay)->IsBackMoveLocked() )
        {   pLay->Cut();
            delete pLay;
        }
        else
        {   BOOL bUnlock = !((SwFtnFrm*)pLay)->IsBackMoveLocked();
            ((SwFtnFrm*)pLay)->LockBackMove();
            pLay->InvalidateSize();
            pLay->Calc();
            SwCntntFrm *pCnt = pLay->ContainsCntnt();
            while ( pCnt && pLay->IsAnLower( pCnt ) )
            {
                //Kann sein, dass der CntFrm gelockt ist, wir wollen hier nicht
                //in eine endlose Seitenwanderung hineinlaufen und rufen das
                //Calc garnicht erst!
                ASSERT( pCnt->IsTxtFrm(), "Die Graphic ist gelandet." );
                if ( ((SwTxtFrm*)pCnt)->IsLocked() ||
                     ((SwTxtFrm*)pCnt)->GetFollow() == pStart )
                    break;
                pCnt->Calc();
                pCnt = pCnt->GetNextCntntFrm();
            }
            if( bUnlock )
                ((SwFtnFrm*)pLay)->UnlockBackMove();
        }
        pLay = 0;
    }
    return pLay;
}



BOOL SwFlowFrm::PasteTree( SwFrm *pStart, SwLayoutFrm *pParent, SwFrm *pSibling,
                           SwFrm *pOldParent )
{
    //returnt TRUE wenn in der Kette ein LayoutFrm steht.
    BOOL bRet = FALSE;

    //Die mit pStart beginnende Kette wird vor den Sibling unter den Parent
    //gehaengt. Fuer geeignete Invalidierung wird ebenfalls gesorgt.

    //Ich bekomme eine fertige Kette. Der Anfang der Kette muss verpointert
    //werden, dann alle Upper fuer die Kette und schliesslich dass Ende.
    //Unterwegs werden alle geeignet invalidiert.
    if ( pSibling )
    {
        if ( 0 != (pStart->pPrev = pSibling->GetPrev()) )
            pStart->GetPrev()->pNext = pStart;
        else
            pParent->pLower = pStart;
        pSibling->_InvalidatePos();
        pSibling->_InvalidatePrt();
    }
    else
    {
        if ( 0 == (pStart->pPrev = pParent->Lower()) )
            pParent->pLower = pStart;
        else
            pParent->Lower()->pNext = pStart;
    }
    SwFrm *pFloat = pStart;
    SwFrm *pLst;
    SWRECTFN( pParent )
    SwTwips nGrowVal = 0;
    do
    {   pFloat->pUpper = pParent;
        pFloat->_InvalidateAll();
        pFloat->CheckDirChange();

        //Ich bin Freund des TxtFrm und darf deshalb so einiges. Das mit
        //dem CacheIdx scheint etwas riskant!
        if ( pFloat->IsTxtFrm() )
        {
            if ( ((SwTxtFrm*)pFloat)->GetCacheIdx() != USHRT_MAX )
                ((SwTxtFrm*)pFloat)->Init();    //Ich bin sein Freund.
        }
        else
            bRet = TRUE;

        nGrowVal += (pFloat->Frm().*fnRect->fnGetHeight)();
        if ( pFloat->GetNext() )
            pFloat = pFloat->GetNext();
        else
        {   pLst = pFloat;
            pFloat = 0;
        }
    } while ( pFloat );

    if ( pSibling )
    {
        pLst->pNext = pSibling;
        pSibling->pPrev = pLst;
        if( pSibling->IsInFtn() )
        {
            if( pSibling->IsSctFrm() )
                pSibling = ((SwSectionFrm*)pSibling)->ContainsAny();
            if( pSibling )
                pSibling->Prepare( PREP_ERGOSUM );
        }
    }
    if ( nGrowVal )
    {
        if ( pOldParent && pOldParent->IsBodyFrm() ) //Fuer variable Seitenhoehe beim Browsen
            pOldParent->Shrink( nGrowVal PHEIGHT );
        pParent->Grow( nGrowVal PHEIGHT );
    }

    if ( pParent->IsFtnFrm() )
        ((SwFtnFrm*)pParent)->InvalidateNxtFtnCnts( pParent->FindPageFrm() );
    return bRet;
}



void SwFlowFrm::MoveSubTree( SwLayoutFrm* pParent, SwFrm* pSibling )
{
    ASSERT( pParent, "Kein Parent uebergeben." );
    ASSERT( rThis.GetUpper(), "Wo kommen wir denn her?" );

    //Sparsamer benachrichtigen wenn eine Action laeuft.
    ViewShell *pSh = rThis.GetShell();
    const SwViewImp *pImp = pSh ? pSh->Imp() : 0;
    const BOOL bComplete = pImp && pImp->IsAction() && pImp->GetLayAction().IsComplete();

    if ( !bComplete )
    {
        SwFrm *pPre = rThis.GetIndPrev();
        if ( pPre )
        {
            pPre->SetRetouche();
            pPre->InvalidatePage();
        }
        else
        {   rThis.GetUpper()->SetCompletePaint();
            rThis.GetUpper()->InvalidatePage();
        }
    }

    SwPageFrm *pOldPage = rThis.FindPageFrm();

    SwLayoutFrm *pOldParent = CutTree( &rThis );
    const BOOL bInvaLay = PasteTree( &rThis, pParent, pSibling, pOldParent );

    // Wenn durch das Cut&Paste ein leerer SectionFrm entstanden ist, sollte
    // dieser automatisch verschwinden.
    SwSectionFrm *pSct;
    if ( pOldParent && !pOldParent->Lower() &&
         (pOldParent->IsInSct() &&
          !(pSct = pOldParent->FindSctFrm())->ContainsCntnt() ) )
            pSct->DelEmpty( FALSE );
    // In einem spaltigen Bereich rufen wir lieber kein Calc "von unten"
    if( !rThis.IsInSct() )
        rThis.GetUpper()->Calc();
    else if( rThis.GetUpper()->IsSctFrm() )
    {
        SwSectionFrm* pSct = (SwSectionFrm*)rThis.GetUpper();
        BOOL bOld = pSct->IsCntntLocked();
        pSct->SetCntntLock( TRUE );
        pSct->Calc();
        if( !bOld )
            pSct->SetCntntLock( FALSE );
    }
    SwPageFrm *pPage = rThis.FindPageFrm();

    if ( pOldPage != pPage )
    {
        rThis.InvalidatePage( pPage );
        if ( rThis.IsLayoutFrm() )
        {
            SwCntntFrm *pCnt = ((SwLayoutFrm*)&rThis)->ContainsCntnt();
            if ( pCnt )
                pCnt->InvalidatePage( pPage );
        }
        else if ( pSh && pSh->GetDoc()->GetLineNumberInfo().IsRestartEachPage()
                  && pPage->FindFirstBodyCntnt() == &rThis )
        {
            rThis._InvalidateLineNum();
        }
    }
    if ( bInvaLay || (pSibling && pSibling->IsLayoutFrm()) )
        rThis.GetUpper()->InvalidatePage( pPage );
}

/*************************************************************************
|*
|*  SwFlowFrm::IsAnFollow()
|*
|*  Ersterstellung      MA 26. Apr. 95
|*  Letzte Aenderung    MA 26. Apr. 95
|*
|*************************************************************************/


BOOL SwFlowFrm::IsAnFollow( const SwFlowFrm *pAssumed ) const
{
    const SwFlowFrm *pFoll = this;
    do
    {   if ( pAssumed == pFoll )
            return TRUE;
        pFoll = pFoll->GetFollow();
    } while ( pFoll );
    return FALSE;
}

/*************************************************************************
|*
|*  SwFlowFrm::FindMaster()
|*
|*  Ersterstellung      MA 26. Apr. 95
|*  Letzte Aenderung    MA 26. Apr. 95
|*
|*************************************************************************/


SwFlowFrm *SwFlowFrm::FindMaster()
{
    ASSERT( IsFollow(), "FindMaster und kein Follow." );

    SwCntntFrm *pCnt;
    BOOL bCntnt;
    if ( rThis.IsCntntFrm() )
    {
        pCnt = (SwCntntFrm*)&rThis;
        pCnt = pCnt->GetPrevCntntFrm();

        bCntnt = TRUE;
    }
    else if( rThis.IsTabFrm() )
    {
        pCnt = rThis.GetPrevCntntFrm();

#ifndef PRODUCT
        SwCntntFrm* pTmpCnt = ((SwLayoutFrm&)rThis).ContainsCntnt();
        ASSERT( ! pTmpCnt || pTmpCnt->GetPrevCntntFrm() == pCnt,
                "Two different results for the master of a table?" )
#endif

        bCntnt = FALSE;
    }
    else
    {
        ASSERT( rThis.IsSctFrm(), "FindMaster: Funny FrameTyp" );
        return ((SwSectionFrm&)rThis).FindSectionMaster();
    }

    while ( pCnt )
    {
        if ( bCntnt )
        {
            if ( pCnt->HasFollow() && pCnt->GetFollow() == this )
                return pCnt;
        }
        else
        {   SwTabFrm  *pTab = pCnt->FindTabFrm();
            if ( pTab && pTab->GetFollow() == this )
                return pTab;
        }
        pCnt = pCnt->GetPrevCntntFrm();
    }
    ASSERT( FALSE, "Follow ist lost in Space." );
    return 0;
}

/*************************************************************************
|*
|*  SwFrm::GetLeaf()
|*
|*  Beschreibung        Liefert das naechste/vorhergehende LayoutBlatt,
|*      das _nicht_ unterhalb von this liegt (oder gar this selbst ist).
|*      Ausserdem muss dieses LayoutBlatt im gleichen Textfluss wie
|*      pAnch Ausgangsfrm liegen (Body, Ftn)
|*  Ersterstellung      MA 25. Nov. 92
|*  Letzte Aenderung    MA 25. Apr. 95
|*
|*************************************************************************/


const SwLayoutFrm *SwFrm::GetLeaf( MakePageType eMakePage, BOOL bFwd,
                                   const SwFrm *pAnch ) const
{
    //Ohne Fluss kein genuss...
    if ( IsInTab() || !(IsInDocBody() || IsInFtn() || IsInFly()) )
        return 0;

    const SwFrm *pLeaf = this;
    BOOL bFound = FALSE;

    do
    {   pLeaf = ((SwFrm*)pLeaf)->GetLeaf( eMakePage, bFwd );

        if ( pLeaf &&
            (!IsLayoutFrm() || !((SwLayoutFrm*)this)->IsAnLower( pLeaf )))
        {
            if ( pAnch->IsInDocBody() == pLeaf->IsInDocBody() &&
                 pAnch->IsInFtn()     == pLeaf->IsInFtn() )
            {
                bFound = TRUE;
            }
        }
    } while ( !bFound && pLeaf );

    return (const SwLayoutFrm*)pLeaf;
}

/*************************************************************************
|*
|*  SwFrm::GetLeaf()
|*
|*  Beschreibung        Ruft Get[Next|Prev]Leaf
|*
|*  Ersterstellung      MA 20. Mar. 93
|*  Letzte Aenderung    MA 25. Apr. 95
|*
|*************************************************************************/


SwLayoutFrm *SwFrm::GetLeaf( MakePageType eMakePage, BOOL bFwd )
{
    if ( IsInFtn() )
        return bFwd ? GetNextFtnLeaf( eMakePage ) : GetPrevFtnLeaf( eMakePage );
    if ( IsInSct() )
        return bFwd ? GetNextSctLeaf( eMakePage ) : GetPrevSctLeaf( eMakePage );
    return bFwd ? GetNextLeaf( eMakePage ) : GetPrevLeaf( eMakePage );
}



BOOL SwFrm::WrongPageDesc( SwPageFrm* pNew )
{
    //Jetzt wirds leider etwas kompliziert:
    //Ich bringe ich evtl. selbst
    //einen Pagedesc mit; der der Folgeseite muss dann damit
    //uebereinstimmen.
    //Anderfalls muss ich mir etwas genauer ansehen wo der
    //Folgepagedesc herkam.
    //Wenn die Folgeseite selbst schon sagt, dass ihr
    //Pagedesc nicht stimmt so kann ich das Teil bedenkenlos
    //auswechseln.
    //Wenn die Seite meint, dass ihr Pagedesc stimmt, so heisst
    //das leider noch immer nicht, dass ich damit etwas anfangen
    //kann: Wenn der erste BodyCntnt einen PageDesc oder einen
    //PageBreak wuenscht, so muss ich ebenfalls eine neue
    //Seite einfuegen; es sein denn die gewuenschte Seite ist
    //die richtige.
    //Wenn ich einen neue Seite eingefuegt habe, so fangen die
    //Probleme leider erst an, denn wahrscheinlich wird die dann
    //folgende Seite verkehrt gewesen und ausgewechselt worden
    //sein. Das hat zur Folge, dass ich zwar eine neue (und
    //jetzt richtige) Seite habe, die Bedingungen zum auswechseln
    //aber leider noch immer stimmen.
    //Ausweg: Vorlaeufiger Versuch, nur einmal eine neue Seite
    //einsetzen (Leerseiten werden noetigenfalls bereits von
    //InsertPage() eingefuegt.
    const SwFmtPageDesc &rFmtDesc = GetAttrSet()->GetPageDesc();

    //Mein Pagedesc zaehlt nicht, wenn ich ein Follow bin!
    SwPageDesc *pDesc = 0;
    USHORT nTmp = 0;
    SwFlowFrm *pFlow = SwFlowFrm::CastFlowFrm( this );
    if ( !pFlow || !pFlow->IsFollow() )
    {
        pDesc = (SwPageDesc*)rFmtDesc.GetPageDesc();
        if( pDesc )
        {
            if( !pDesc->GetRightFmt() )
                nTmp = 2;
            else if( !pDesc->GetLeftFmt() )
                nTmp = 1;
            else if( rFmtDesc.GetNumOffset() )
                nTmp = rFmtDesc.GetNumOffset();
        }
    }

    //Bringt der Cntnt einen Pagedesc mit oder muss zaehlt die
    //virtuelle Seitennummer des neuen Layoutleafs?
    // Bei Follows zaehlt der PageDesc nicht
    const BOOL bOdd = nTmp ? ( nTmp % 2 ? TRUE : FALSE )
                           : pNew->OnRightPage();
    if ( !pDesc )
        pDesc = pNew->FindPageDesc();
    const SwFlowFrm *pNewFlow = pNew->FindFirstBodyCntnt();
    // Haben wir uns selbst gefunden?
    if( pNewFlow == pFlow )
        pNewFlow = NULL;
    if ( pNewFlow && pNewFlow->GetFrm()->IsInTab() )
        pNewFlow = pNewFlow->GetFrm()->FindTabFrm();
    const SwPageDesc *pNewDesc= ( pNewFlow && !pNewFlow->IsFollow() )
            ? pNewFlow->GetFrm()->GetAttrSet()->GetPageDesc().GetPageDesc():0;

    return ( pNew->GetPageDesc() != pDesc ||   //  own desc ?
        pNew->GetFmt() != (bOdd ? pDesc->GetRightFmt() : pDesc->GetLeftFmt()) ||
        ( pNewDesc && pNewDesc == pDesc ) );
}


/*************************************************************************
|*
|*  SwFrm::GetNextLeaf()
|*
|*  Beschreibung        Liefert das naechste LayoutBlatt in den das
|*      Frame gemoved werden kann.
|*
|*  Ersterstellung      MA 16. Nov. 92
|*  Letzte Aenderung    MA 05. Dec. 96
|*
|*************************************************************************/

SwLayoutFrm *SwFrm::GetNextLeaf( MakePageType eMakePage )
{
    ASSERT( !IsInFtn(), "GetNextLeaf(), don't call me for Ftn." );
    ASSERT( !IsInSct(), "GetNextLeaf(), don't call me for Sections." );

    const BOOL bBody = IsInDocBody();       //Wenn ich aus dem DocBody komme
                                            //Will ich auch im Body landen.

    // Bei Flys macht es keinen Sinn, Seiten einzufuegen, wir wollen lediglich
     // die Verkettung absuchen.
    if( IsInFly() )
        eMakePage = MAKEPAGE_NONE;
    //Bei Tabellen gleich den grossen Sprung wagen, ein einfaches GetNext...
    //wuerde die erste Zellen und in der Folge alle weiteren Zellen nacheinander
    //abklappern....
    SwLayoutFrm *pLayLeaf;
    if ( IsTabFrm() )
        pLayLeaf = ((SwTabFrm*)this)->FindLastCntnt()->GetUpper();
    else
        pLayLeaf = GetNextLayoutLeaf();

    SwLayoutFrm *pOldLayLeaf = 0;           //Damit bei neu erzeugten Seiten
                                            //nicht wieder vom Anfang gesucht
                                            //wird.
    BOOL bNewPg = FALSE;    //nur einmal eine neue Seite einfuegen.

    while ( TRUE )
    {
        if ( pLayLeaf )
        {
            //Es gibt noch einen weiteren LayoutFrm, mal sehen,
            //ob er bereit ist mich aufzunehmen.
            //Dazu braucht er nur von der gleichen Art wie mein Ausgangspunkt
            //sein (DocBody bzw. Footnote.)
            if ( pLayLeaf->FindPageFrm()->IsFtnPage() )
            {   //Wenn ich bei den Endnotenseiten angelangt bin hat sichs.
                pLayLeaf = 0;
                continue;
            }
            if ( (bBody && !pLayLeaf->IsInDocBody()) || pLayLeaf->IsInTab()
                 || pLayLeaf->IsInSct() )
            {
                //Er will mich nicht; neuer Versuch, neues Glueck
                pOldLayLeaf = pLayLeaf;
                pLayLeaf = pLayLeaf->GetNextLayoutLeaf();
                continue;
            }
            //Er will mich, also ist er der gesuchte und ich bin fertig.
            //Bei einem Seitenwechsel kann es allerdings noch sein, dass
            //Der Seitentyp nicht der gewuenschte ist, in diesem Fall muessen
            //wir eine Seite des richtigen Typs einfuegen.

            if( !IsFlowFrm() && ( eMakePage == MAKEPAGE_NONE ||
                eMakePage==MAKEPAGE_APPEND || eMakePage==MAKEPAGE_NOSECTION ) )
                return pLayLeaf;

            SwPageFrm *pNew = pLayLeaf->FindPageFrm();
            if ( pNew != FindPageFrm() && !bNewPg )
            {
                if( WrongPageDesc( pNew ) )
                {
                    SwFtnContFrm *pCont = pNew->FindFtnCont();
                    if( pCont )
                    {
                        // Falls die Referenz der ersten Fussnote dieser Seite
                        // vor der Seite liegt, fuegen wir lieber keine neue Seite
                        // ein (Bug #55620#)
                        SwFtnFrm *pFtn = (SwFtnFrm*)pCont->Lower();
                        if( pFtn && pFtn->GetRef() )
                        {
                            const USHORT nRefNum = pNew->GetPhyPageNum();
                            if( pFtn->GetRef()->GetPhyPageNum() < nRefNum )
                                break;
                        }
                    }
                    //Erwischt, die folgende Seite ist verkehrt, also
                    //muss eine neue eingefuegt werden.
                    if ( eMakePage == MAKEPAGE_INSERT )
                    {
                        bNewPg = TRUE;

                        SwPageFrm *pPg = pOldLayLeaf ?
                                    pOldLayLeaf->FindPageFrm() : 0;
                        if ( pPg && pPg->IsEmptyPage() )
                            //Nicht hinter, sondern vor der EmptyPage einfuegen.
                            pPg = (SwPageFrm*)pPg->GetPrev();

                        if ( !pPg || pPg == pNew )
                            pPg = FindPageFrm();

                        InsertPage( pPg, FALSE );
                        pLayLeaf = GetNextLayoutLeaf();
                        pOldLayLeaf = 0;
                        continue;
                    }
                    else
                        pLayLeaf = 0;
                }
            }
            break;
        }
        else
        {
            //Es gibt keinen passenden weiteren LayoutFrm, also muss eine
            //neue Seite her.
            if ( eMakePage == MAKEPAGE_APPEND || eMakePage == MAKEPAGE_INSERT )
            {
                InsertPage(
                    pOldLayLeaf ? pOldLayLeaf->FindPageFrm() : FindPageFrm(),
                    FALSE );

                //und nochmal das ganze
                pLayLeaf = pOldLayLeaf ? pOldLayLeaf : GetNextLayoutLeaf();
            }
            else
                break;
        }
    }
    return pLayLeaf;
}

/*************************************************************************
|*
|*  SwFrm::GetPrevLeaf()
|*
|*  Beschreibung        Liefert das vorhergehende LayoutBlatt in das der
|*      Frame gemoved werden kann.
|*  Ersterstellung      MA 16. Nov. 92
|*  Letzte Aenderung    MA 25. Apr. 95
|*
|*************************************************************************/


SwLayoutFrm *SwFrm::GetPrevLeaf( MakePageType eMakeFtn )
{
    ASSERT( !IsInFtn(), "GetPrevLeaf(), don't call me for Ftn." );

    const BOOL bBody = IsInDocBody();       //Wenn ich aus dem DocBody komme
                                            //will ich auch im Body landen.
    const BOOL bFly  = IsInFly();

    SwLayoutFrm *pLayLeaf = GetPrevLayoutLeaf();
    SwLayoutFrm *pPrevLeaf = 0;

    while ( pLayLeaf )
    {
        if ( pLayLeaf->IsInTab() ||     //In Tabellen geht's niemals hinein.
             pLayLeaf->IsInSct() )      //In Bereiche natuerlich auch nicht!
            pLayLeaf = pLayLeaf->GetPrevLayoutLeaf();
        else if ( bBody && pLayLeaf->IsInDocBody() )
        {
            if ( pLayLeaf->Lower() )
                break;
            pPrevLeaf = pLayLeaf;
            pLayLeaf = pLayLeaf->GetPrevLayoutLeaf();
            if ( pLayLeaf )
                SwFlowFrm::SetMoveBwdJump( TRUE );
        }
        else if ( bFly )
            break;  //Cntnts in Flys sollte jedes Layout-Blatt recht sein.
        else
            pLayLeaf = pLayLeaf->GetPrevLayoutLeaf();
    }
    return pLayLeaf ? pLayLeaf : pPrevLeaf;
}

/*************************************************************************
|*
|*  SwFlowFrm::IsPrevObjMove()
|*
|*  Ersterstellung      MA 20. Feb. 96
|*  Letzte Aenderung    MA 22. Feb. 96
|*
|*************************************************************************/


BOOL SwFlowFrm::IsPrevObjMove() const
{
    //TRUE der FlowFrm soll auf einen Rahmen des Vorgaengers Ruecksicht nehmen
    //     und fuer diesen ggf. Umbrechen.

    //!!!!!!!!!!!Hack!!!!!!!!!!!
    if ( rThis.GetUpper()->GetFmt()->GetDoc()->IsBrowseMode() )
        return FALSE;

    SwFrm *pPre = rThis.FindPrev();

    if ( pPre && pPre->GetDrawObjs() )
    {
        ASSERT( SwFlowFrm::CastFlowFrm( pPre ), "new flowfrm?" );
        if( SwFlowFrm::CastFlowFrm( pPre )->IsAnFollow( this ) )
            return FALSE;
        SwFrm* pPreUp = pPre->GetUpper();
        // Wenn der Upper ein SectionFrm oder die Spalte eines SectionFrms ist,
        // duerfen wir aus diesem durchaus heraushaengen,
        // es muss stattdessen der Upper des SectionFrms beruecksichtigt werden.
        if( pPreUp->IsInSct() )
        {
            if( pPreUp->IsSctFrm() )
                pPreUp = pPreUp->GetUpper();
            else if( pPreUp->IsColBodyFrm() &&
                     pPreUp->GetUpper()->GetUpper()->IsSctFrm() )
                pPreUp = pPreUp->GetUpper()->GetUpper()->GetUpper();
        }
        const long nBottom = pPreUp->Frm().Bottom();
        const long nRight  = pPreUp->Frm().Right();
        const FASTBOOL bCol = pPreUp->IsColBodyFrm();//ColFrms jetzt mit BodyFrm
        for ( USHORT i = 0; i < pPre->GetDrawObjs()->Count(); ++i )
        {
            const SdrObject *pObj = (*pPre->GetDrawObjs())[i];
            if ( pObj->IsWriterFlyFrame() )
            {
                const SwFlyFrm *pFly = ((SwVirtFlyDrawObj*)pObj)->GetFlyFrm();

                if ( WEIT_WECH != pFly->Frm().Top() && !pFly->IsFlyInCntFrm() )
                {
                    if( pObj->GetSnapRect().Top()  > nBottom )
                        return TRUE;
                    if( bCol && pObj->GetSnapRect().Left() > nRight )
                    {
                        SwFmtHoriOrient aHori( pFly->GetFmt()->GetHoriOrient() );
                        if( FRAME == aHori.GetRelationOrient() ||
                            PRTAREA == aHori.GetRelationOrient() ||
                            REL_CHAR == aHori.GetRelationOrient() ||
                            REL_FRM_LEFT == aHori.GetRelationOrient() ||
                            REL_FRM_RIGHT == aHori.GetRelationOrient() )
                        {
                            if( HORI_NONE == aHori.GetHoriOrient() )
                            {
                                SwTwips nAdd = 0;
                                switch ( aHori.GetRelationOrient() )
                                {
                                    case PRTAREA:
                                        nAdd = pFly->Prt().Left(); break;
                                    case REL_FRM_RIGHT:
                                        nAdd = pFly->Frm().Width(); break;
                                    case REL_CHAR:
                                        if( pFly->IsFlyAtCntFrm() )
                                            nAdd = ((SwFlyAtCntFrm*)pFly)->GetLastCharX();
                                        break;
                                }
                                nAdd += aHori.GetPos();
                                if( nAdd < pPreUp->Frm().Width() &&
                                    nAdd + pFly->Frm().Width() > 0 )
                                    return TRUE;
                            }
                            else
                                return TRUE;
                        }
                    }
                }
            }
        }
    }
    return FALSE;
}

/*************************************************************************
|*
|*  BOOL SwFlowFrm::IsPageBreak()
|*
|*  Beschreibung        Wenn vor dem Frm ein harter Seitenumbruch steht UND
|*      es einen Vorgaenger auf der gleichen Seite gibt, wird TRUE
|*      zurueckgeliefert (es muss ein PageBreak erzeugt werden) FALSE sonst.
|*      Wenn in bAct TRUE uebergeben wird, gibt die Funktion dann TRUE
|*      zurueck, wenn ein PageBreak besteht.
|*      Fuer Follows wird der harte Seitenumbruch natuerlich nicht
|*      ausgewertet.
|*      Der Seitenumbruch steht im eigenen FrmFmt (BEFORE) oder im FrmFmt
|*      des Vorgaengers (AFTER). Wenn es keinen Vorgaenger auf der Seite
|*      gibt ist jede weitere Ueberlegung ueberfluessig.
|*      Ein Seitenumbruch (oder der Bedarf) liegt auch dann vor, wenn
|*      im FrmFmt ein PageDesc angegeben wird.
|*      Die Implementierung arbeitet zuaechst nur auf CntntFrms!
|*      -->Fuer LayoutFrms ist die Definition des Vorgaengers unklar.
|*  Ersterstellung      MA ??
|*  Letzte Aenderung    MA 21. Mar. 95
|*
|*************************************************************************/


BOOL SwFlowFrm::IsPageBreak( BOOL bAct ) const
{
    const SwAttrSet *pSet;
    if ( !IsFollow() && rThis.IsInDocBody() &&
         !(pSet = rThis.GetAttrSet())->GetDoc()->IsBrowseMode() )
    {
        //Vorgaenger ermitteln
        const SwFrm *pPrev = rThis.FindPrev();
        while ( pPrev && ( !pPrev->IsInDocBody() ||
                ( pPrev->IsTxtFrm() && ((SwTxtFrm*)pPrev)->IsHiddenNow() ) ) )
            pPrev = pPrev->FindPrev();

        if ( pPrev )
        {
            ASSERT( pPrev->IsInDocBody(), "IsPageBreak: Not in DocBody?" );
            if ( bAct )
            {   if ( rThis.FindPageFrm() == pPrev->FindPageFrm() )
                    return FALSE;
            }
            else
            {   if ( rThis.FindPageFrm() != pPrev->FindPageFrm() )
                    return FALSE;
            }

            const SvxBreak eBreak = pSet->GetBreak().GetBreak();
            if ( eBreak == SVX_BREAK_PAGE_BEFORE || eBreak == SVX_BREAK_PAGE_BOTH )
                return TRUE;
            else
            {
                const SvxBreak &ePrB = pPrev->GetAttrSet()->GetBreak().GetBreak();
                if ( ePrB == SVX_BREAK_PAGE_AFTER ||
                     ePrB == SVX_BREAK_PAGE_BOTH  ||
                     pSet->GetPageDesc().GetPageDesc() )
                    return TRUE;
            }
        }
    }
    return FALSE;
}

/*************************************************************************
|*
|*  BOOL SwFlowFrm::IsColBreak()
|*
|*  Beschreibung        Wenn vor dem Frm ein harter Spaltenumbruch steht UND
|*      es einen Vorgaenger in der gleichen Spalte gibt, wird TRUE
|*      zurueckgeliefert (es muss ein PageBreak erzeugt werden) FALSE sonst.
|*      Wenn in bAct TRUE uebergeben wird, gibt die Funktion dann TRUE
|*      zurueck, wenn ein ColBreak besteht.
|*      Fuer Follows wird der harte Spaltenumbruch natuerlich nicht
|*      ausgewertet.
|*      Der Spaltenumbruch steht im eigenen FrmFmt (BEFORE) oder im FrmFmt
|*      des Vorgaengers (AFTER). Wenn es keinen Vorgaenger in der Spalte
|*      gibt ist jede weitere Ueberlegung ueberfluessig.
|*      Die Implementierung arbeitet zuaechst nur auf CntntFrms!
|*      -->Fuer LayoutFrms ist die Definition des Vorgaengers unklar.
|*  Ersterstellung      MA 11. Jun. 93
|*  Letzte Aenderung    MA 21. Mar. 95
|*
|*************************************************************************/


BOOL SwFlowFrm::IsColBreak( BOOL bAct ) const
{
    if ( !IsFollow() && (rThis.IsMoveable() || bAct) )
    {
        const SwFrm *pCol = rThis.FindColFrm();
        if ( pCol )
        {
            //Vorgaenger ermitteln
            const SwFrm *pPrev = rThis.FindPrev();
            while( pPrev && ( ( !pPrev->IsInDocBody() && !rThis.IsInFly() ) ||
                   ( pPrev->IsTxtFrm() && ((SwTxtFrm*)pPrev)->IsHiddenNow() ) ) )
                    pPrev = pPrev->FindPrev();

            if ( pPrev )
            {
                if ( bAct )
                {   if ( pCol == pPrev->FindColFrm() )
                        return FALSE;
                }
                else
                {   if ( pCol != pPrev->FindColFrm() )
                        return FALSE;
                }

                const SvxBreak eBreak = rThis.GetAttrSet()->GetBreak().GetBreak();
                if ( eBreak == SVX_BREAK_COLUMN_BEFORE ||
                     eBreak == SVX_BREAK_COLUMN_BOTH )
                    return TRUE;
                else
                {
                    const SvxBreak &ePrB = pPrev->GetAttrSet()->GetBreak().GetBreak();
                    if ( ePrB == SVX_BREAK_COLUMN_AFTER ||
                         ePrB == SVX_BREAK_COLUMN_BOTH )
                        return TRUE;
                }
            }
        }
    }
    return FALSE;
}

BOOL SwFlowFrm::HasParaSpaceAtPages( BOOL bSct ) const
{
    if( rThis.IsInSct() )
    {
        const SwFrm* pTmp = rThis.GetUpper();
        while( pTmp )
        {
            if( pTmp->IsCellFrm() || pTmp->IsFlyFrm() ||
                pTmp->IsFooterFrm() || pTmp->IsHeaderFrm() ||
                ( pTmp->IsFtnFrm() && !((SwFtnFrm*)pTmp)->GetMaster() ) )
                return TRUE;
            if( pTmp->IsPageFrm() )
                return ( pTmp->GetPrev() && !IsPageBreak(TRUE) ) ? FALSE : TRUE;
            if( pTmp->IsColumnFrm() && pTmp->GetPrev() )
                return IsColBreak( TRUE );
            if( pTmp->IsSctFrm() && ( !bSct || pTmp->GetPrev() ) )
                return FALSE;
            pTmp = pTmp->GetUpper();
        }
        ASSERT( FALSE, "HasParaSpaceAtPages: Where's my page?" );
        return FALSE;
    }
    if( !rThis.IsInDocBody() || ( rThis.IsInTab() && !rThis.IsTabFrm()) ||
        IsPageBreak( TRUE ) || ( rThis.FindColFrm() && IsColBreak( TRUE ) ) )
        return TRUE;
    const SwFrm* pTmp = rThis.FindColFrm();
    if( pTmp )
    {
        if( pTmp->GetPrev() )
            return FALSE;
    }
    else
        pTmp = &rThis;
    pTmp = pTmp->FindPageFrm();
    return pTmp && !pTmp->GetPrev();
}

SwTwips SwFlowFrm::CalcUpperSpace( const SwBorderAttrs *pAttrs,
    const SwFrm* pPr ) const
{
    const SwFrm *pPre = pPr ? pPr : rThis.GetPrev();
    BOOL bInFtn = rThis.IsInFtn();
    do {
        while( pPre && ( (pPre->IsTxtFrm() && ((SwTxtFrm*)pPre)->IsHiddenNow())
               || ( pPre->IsSctFrm() && !((SwSectionFrm*)pPre)->GetSection() ) ) )
            pPre = pPre->GetPrev();
        if( !pPre && bInFtn )
        {
            bInFtn = FALSE;
            if( !rThis.IsInSct() || rThis.IsSctFrm() ||
                !rThis.FindSctFrm()->IsInFtn() )
                pPre = rThis.FindFtnFrm()->GetPrev();
            if( pPre )
            {
                pPre = ((SwFtnFrm*)pPre)->Lower();
                if( pPre )
                    while( pPre->GetNext() )
                        pPre = pPre->GetNext();
                continue;
            }
        }
        if( pPre && pPre->IsSctFrm() )
        {
            SwSectionFrm* pSect = (SwSectionFrm*)pPre;
            pPre = pSect->FindLastCntnt();
            // If the last content is in a table _inside_ the section,
            // take the table herself.
            if( pPre && pPre->IsInTab() && !pSect->IsInTab() )
                pPre = pPre->FindTabFrm();
        }
        break;
    } while( pPre );
    SwBorderAttrAccess *pAccess;
    SwFrm* pOwn;
    if( !pAttrs )
    {
        if( rThis.IsSctFrm() )
        {
            SwSectionFrm* pFoll = &((SwSectionFrm&)rThis);
            do
                pOwn = pFoll->ContainsAny();
            while( !pOwn && 0 != ( pFoll = pFoll->GetFollow() ) );
            if( !pOwn )
                return 0;
        }
        else
            pOwn = &rThis;
        pAccess= new SwBorderAttrAccess( SwFrm::GetCache(), pOwn );
        pAttrs = pAccess->Get();
    }
    else
    {
        pAccess = NULL;
        pOwn = &rThis;
    }
    SwTwips nUpper = 0;
    if( pPre )
    {
        const SvxULSpaceItem &rPrevUL = pPre->GetAttrSet()->GetULSpace();
        if( rThis.GetAttrSet()->GetDoc()->IsParaSpaceMax() )
        {
            nUpper = rPrevUL.GetLower() + pAttrs->GetULSpace().GetUpper();
            SwTwips nAdd = 0;
            if ( pOwn->IsTxtFrm() )
                nAdd = Max( nAdd, long(((SwTxtFrm&)rThis).GetLineSpace()) );
            if ( pPre->IsTxtFrm() )
                nAdd = Max( nAdd, long(((SwTxtFrm*)pPre)->GetLineSpace()) );
            nUpper += nAdd;
        }
        else
        {
            nUpper = Max( rPrevUL.GetLower(), pAttrs->GetULSpace().GetUpper() );
            if ( pOwn->IsTxtFrm() )
                nUpper = Max( nUpper, long(((SwTxtFrm*)pOwn)->GetLineSpace()) );
            if ( pPre->IsTxtFrm() )
                nUpper = Max( nUpper, long(((SwTxtFrm*)pPre)->GetLineSpace()) );
        }
    }
    else if( rThis.GetAttrSet()->GetDoc()->IsParaSpaceMaxAtPages() &&
             CastFlowFrm( pOwn )->HasParaSpaceAtPages( rThis.IsSctFrm() ) )
        nUpper = pAttrs->GetULSpace().GetUpper();

    nUpper += pAttrs->GetTopLine( &rThis );

    if( rThis.IsInDocBody() && rThis.GetAttrSet()->GetParaGrid().GetValue() )
    {
        const SwPageFrm* pPg = rThis.FindPageFrm();
        GETGRID( pPg )
        if( pGrid )
        {
            const SwFrm* pBody = pPg->FindBodyCont();
            if( pBody )
            {
                long nSum = pGrid->GetBaseHeight() + pGrid->GetRubyHeight();
                SWRECTFN( (&rThis) )
                SwTwips nOrig = (pBody->*fnRect->fnGetPrtTop)();
                SwTwips nTop = (rThis.Frm().*fnRect->fnGetTop)();
                if( bVert )
                {
                    nTop -= nUpper;
                    SwTwips nY = nOrig - nSum *( ( nOrig - nTop ) / nSum );
                    if( nY > nTop )
                        nY -= nSum;
                    nUpper = nTop + nUpper - nY;
                }
                else
                {
                    nTop += nUpper;
                    SwTwips nY = nOrig + nSum *( ( nTop - nOrig ) / nSum );
                    if( nY < nTop )
                        nY += nSum;
                    nUpper = nY - rThis.Frm().Top();
                }
            }
        }
    }

    delete pAccess;
    return nUpper;
}

/*************************************************************************
|*
|*  BOOL SwFlowFrm::CheckMoveFwd()
|*
|*  Beschreibung        Moved den Frm vorwaerts wenn es durch die aktuellen
|*      Bedingungen und Attribute notwendig erscheint.
|*  Ersterstellung      MA 05. Dec. 96
|*  Letzte Aenderung    MA 09. Mar. 98
|*
|*************************************************************************/


BOOL SwFlowFrm::CheckMoveFwd( BOOL &rbMakePage, BOOL bKeep, BOOL bMovedBwd )
{
    const SwFrm* pNxt = rThis.GetIndNext();

    if ( bKeep && //!bMovedBwd &&
         ( !pNxt || ( pNxt->IsTxtFrm() && ((SwTxtFrm*)pNxt)->IsEmptyMaster() ) ) &&
         ( 0 != (pNxt = rThis.FindNext()) ) && IsKeepFwdMoveAllowed() )
    {
        if( pNxt->IsSctFrm() )
        {   // Nicht auf leere SectionFrms hereinfallen
            const SwFrm* pTmp = NULL;
            while( pNxt && pNxt->IsSctFrm() &&
                   ( !((SwSectionFrm*)pNxt)->GetSection() ||
                     !( pTmp = ((SwSectionFrm*)pNxt)->ContainsAny() ) ) )
            {
                pNxt = pNxt->FindNext();
                pTmp = NULL;
            }
            if( pTmp )
                pNxt = pTmp; // the content of the next notempty sectionfrm
        }
        if( pNxt && pNxt->GetValidPosFlag() )
        {
            BOOL bMove = FALSE;
            const SwSectionFrm *pSct = rThis.FindSctFrm();
            if( pSct && !pSct->GetValidSizeFlag() )
            {
                const SwSectionFrm* pNxtSct = pNxt->FindSctFrm();
                if( pNxtSct && pSct->IsAnFollow( pNxtSct ) )
                    bMove = TRUE;
            }
            else
                bMove = TRUE;
            if( bMove )
            {
                //Keep together with the following frame
                MoveFwd( rbMakePage, FALSE );
                return TRUE;
            }
        }
    }

    BOOL bMovedFwd = FALSE;

    if ( rThis.GetIndPrev() )
    {
        if ( IsPrevObjMove() ) //Auf Objekte des Prev Ruecksicht nehmen?
        {
            bMovedFwd = TRUE;
            if ( !MoveFwd( rbMakePage, FALSE ) )
                rbMakePage = FALSE;
        }
        else
        {
            if ( IsPageBreak( FALSE ) )
            {
                while ( MoveFwd( rbMakePage, TRUE ) )
                        /* do nothing */;
                rbMakePage = FALSE;
                bMovedFwd = TRUE;
            }
            else if ( IsColBreak ( FALSE ) )
            {
                const SwPageFrm *pPage = rThis.FindPageFrm();
                SwFrm *pCol = rThis.FindColFrm();
                do
                {   MoveFwd( rbMakePage, FALSE );
                    SwFrm *pTmp = rThis.FindColFrm();
                    if( pTmp != pCol )
                    {
                        bMovedFwd = TRUE;
                        pCol = pTmp;
                    }
                    else
                        break;
                } while ( IsColBreak( FALSE ) );
                if ( pPage != rThis.FindPageFrm() )
                    rbMakePage = FALSE;
            }
        }
    }
    return bMovedFwd;
}

/*************************************************************************
|*
|*  BOOL SwFlowFrm::MoveFwd()
|*
|*  Beschreibung        Returnwert sagt, ob der Frm die Seite gewechselt hat.
|*  Ersterstellung      MA 05. Dec. 96
|*  Letzte Aenderung    MA 05. Dec. 96
|*
|*************************************************************************/


BOOL SwFlowFrm::MoveFwd( BOOL bMakePage, BOOL bPageBreak, BOOL bMoveAlways )
{
//!!!!MoveFtnCntFwd muss ggf. mitgepflegt werden.
    SwFtnBossFrm *pOldBoss = rThis.FindFtnBossFrm();
    if ( rThis.IsInFtn() )
        return ((SwCntntFrm&)rThis).MoveFtnCntFwd( bMakePage, pOldBoss );

    if( !IsFwdMoveAllowed() && !bMoveAlways )
    {
        BOOL bNoFwd = TRUE;
        if( rThis.IsInSct() )
        {
            SwFtnBossFrm* pBoss = rThis.FindFtnBossFrm();
            bNoFwd = !pBoss->IsInSct() || ( !pBoss->Lower()->GetNext() &&
                     !pBoss->GetPrev() );
        }
        if( bNoFwd )
        {
            //Fuer PageBreak ist das Moven erlaubt, wenn der Frm nicht
            //bereits der erste der Seite ist.
            if ( !bPageBreak )
                return FALSE;

            const SwFrm *pCol = rThis.FindColFrm();
            if ( !pCol || !pCol->GetPrev() )
                return FALSE;
        }
    }

    BOOL bSamePage = TRUE;
    SwLayoutFrm *pNewUpper =
            rThis.GetLeaf( bMakePage ? MAKEPAGE_INSERT : MAKEPAGE_NONE, TRUE );

    if ( pNewUpper )
    {
        PROTOCOL_ENTER( &rThis, PROT_MOVE_FWD, 0, 0 );
        SwPageFrm *pOldPage = pOldBoss->FindPageFrm();
        //Wir moven uns und alle direkten Nachfolger vor den ersten
        //CntntFrm unterhalb des neuen Uppers.

        // Wenn unser NewUpper in einem SectionFrm liegt, muessen wir
        // verhindern, dass sich dieser im Calc selbst zerstoert
        SwSectionFrm* pSect = pNewUpper->FindSctFrm();
        BOOL bUnlock = FALSE;
        if( pSect )
        {
            // Wenn wir nur innerhalb unseres SectionFrms die Spalte wechseln,
            // rufen wir lieber kein Calc, sonst wird noch der SectionFrm
            // formatiert, der wiederum uns ruft etc.
            if( pSect != rThis.FindSctFrm() )
            {
                bUnlock = !pSect->IsColLocked();
                pSect->ColLock();
                pNewUpper->Calc();
                if( bUnlock )
                    pSect->ColUnlock();
            }
        }
        else
            pNewUpper->Calc();

        SwFtnBossFrm *pNewBoss = pNewUpper->FindFtnBossFrm();
        BOOL bBossChg = pNewBoss != pOldBoss;
        pNewBoss = pNewBoss->FindFtnBossFrm( TRUE );
        pOldBoss = pOldBoss->FindFtnBossFrm( TRUE );
        SwPageFrm* pNewPage = pOldPage;

        //Erst die Fussnoten verschieben!
        BOOL bFtnMoved = FALSE;
        if ( pNewBoss != pOldBoss )
        {
            pNewPage = pNewBoss->FindPageFrm();
            bSamePage = pNewPage == pOldPage;
            //Damit die Fussnoten nicht auf dumme Gedanken kommen
            //setzen wir hier die Deadline.
            SWRECTFN( pOldBoss )
            SwSaveFtnHeight aHeight( pOldBoss,
                (pOldBoss->Frm().*fnRect->fnGetBottom)() );
            SwCntntFrm* pStart = rThis.IsCntntFrm() ?
                (SwCntntFrm*)&rThis : ((SwLayoutFrm&)rThis).ContainsCntnt();
            ASSERT( pStart, "MoveFwd: Missing Content" );
            SwLayoutFrm* pBody = pStart ? ( pStart->IsTxtFrm() ?
                (SwLayoutFrm*)((SwTxtFrm*)pStart)->FindBodyFrm() : 0 ) : 0;
            if( pBody )
                bFtnMoved = pBody->MoveLowerFtns( pStart, pOldBoss, pNewBoss,
                                                  FALSE);
        }
        // Bei SectionFrms ist es moeglich, dass wir selbst durch pNewUpper->Calc()
        // bewegt wurden, z. B. in den pNewUpper.
        // MoveSubTree bzw. PasteTree ist auf so etwas nicht vorbereitet.
        if( pNewUpper != rThis.GetUpper() )
        {
            MoveSubTree( pNewUpper, pNewUpper->Lower() );

            if ( bFtnMoved && !bSamePage )
            {
                pOldPage->UpdateFtnNum();
                pNewPage->UpdateFtnNum();
            }

            if( bBossChg )
            {
                rThis.Prepare( PREP_BOSS_CHGD, 0, FALSE );
                if( !bSamePage )
                {
                    ViewShell *pSh = rThis.GetShell();
                    if ( pSh && !pSh->Imp()->IsUpdateExpFlds() )
                        pSh->GetDoc()->SetNewFldLst();  //Wird von CalcLayout() hinterher erledigt!
                    pNewPage->InvalidateSpelling();
                    pNewPage->InvalidateAutoCompleteWords();
                }
            }
        }
        // OD 30.10.2002 #97265# - no <CheckPageDesc(..)> in online layout
        if ( !pNewPage->GetFmt()->GetDoc()->IsBrowseMode() )
        {
            //Bei Sections kann es passieren, das wir gleich  in den Follow geflutscht
            //sind. Dadurch wird nicht vom GetLeaf fuer die richtige Seite gesorgt.
            //Das muessen wir fuer diesen Fall pruefen.
            if ( !bSamePage && pNewUpper->IsInSct() &&
                 ( rThis.GetAttrSet()->GetPageDesc().GetPageDesc() ||
                   pOldPage->GetPageDesc()->GetFollow() != pNewPage->GetPageDesc() ) )
                SwFrm::CheckPageDescs( pNewPage, FALSE );
        }
    }
    return bSamePage;
}


/*************************************************************************
|*
|*  BOOL SwFlowFrm::MoveBwd()
|*
|*  Beschreibung        Returnwert sagt, ob der Frm die Seite wechseln soll.
|*                      Sollte von abgeleiteten Klassen gerufen werden.
|*                      Das moven selbst muessen die abgeleiteten uebernehmen.
|*  Ersterstellung      MA 05. Dec. 96
|*  Letzte Aenderung    MA 05. Dec. 96
|*
|*************************************************************************/

BOOL SwFlowFrm::MoveBwd( BOOL &rbReformat )
{
    SwFlowFrm::SetMoveBwdJump( FALSE );

    SwFtnFrm* pFtn = rThis.FindFtnFrm();
    if ( pFtn && pFtn->IsBackMoveLocked() )
        return FALSE;

    SwFtnBossFrm * pOldBoss = rThis.FindFtnBossFrm();
    SwPageFrm * const pOldPage = pOldBoss->FindPageFrm();
    SwLayoutFrm *pNewUpper = 0;
    FASTBOOL bCheckPageDescs = FALSE;

    if ( pFtn )
    {
        //Wenn die Fussnote bereits auf der gleichen Seite/Spalte wie die Referenz
        //steht, ist nix mit zurueckfliessen. Die breaks brauche fuer die
        //Fussnoten nicht geprueft zu werden.
        BOOL bEndnote = pFtn->GetAttr()->GetFtn().IsEndNote();
        SwFrm* pRef = bEndnote && pFtn->IsInSct() ?
            pFtn->FindSctFrm()->FindLastCntnt( FINDMODE_LASTCNT ) : pFtn->GetRef();
        ASSERT( pRef, "MoveBwd: Endnote for an empty section?" );
        if( !bEndnote )
            pOldBoss = pOldBoss->FindFtnBossFrm( TRUE );
        SwFtnBossFrm *pRefBoss = pRef->FindFtnBossFrm( !bEndnote );
        if ( pOldBoss != pRefBoss &&
             // OD 08.11.2002 #104840# - use <SwLayoutFrm::IsBefore(..)>
             ( !bEndnote ||
               pRefBoss->IsBefore( pOldBoss ) )
           )
            pNewUpper = rThis.GetLeaf( MAKEPAGE_FTN, FALSE );
    }
    else if ( IsPageBreak( TRUE ) ) //PageBreak zu beachten?
    {
        //Wenn auf der vorhergehenden Seite kein Frm im Body steht,
        //so ist das Zurueckfliessen trotz Pagebreak sinnvoll
        //(sonst: leere Seite).
        //Natuerlich muessen Leereseiten geflissentlich uebersehen werden!
        const SwFrm *pFlow = &rThis;
        do
        {   pFlow = pFlow->FindPrev();
        } while ( pFlow && ( pFlow->FindPageFrm() == pOldPage ||
                  !pFlow->IsInDocBody() ) );
        if ( pFlow )
        {
            long nDiff = pOldPage->GetPhyPageNum() - pFlow->GetPhyPageNum();
            if ( nDiff > 1 )
            {
                if ( ((SwPageFrm*)pOldPage->GetPrev())->IsEmptyPage() )
                    nDiff -= 1;
                if ( nDiff > 1 )
                {
                    pNewUpper = rThis.GetLeaf( MAKEPAGE_NONE, FALSE );
                    bCheckPageDescs = TRUE;
                }
            }
        }
    }
    else if ( IsColBreak( TRUE ) )
    {
        //Wenn in der vorhergehenden Spalte kein CntntFrm steht, so ist
        //das Zurueckfliessen trotz ColumnBreak sinnvoll
        //(sonst: leere Spalte).
        if( rThis.IsInSct() )
        {
            pNewUpper = rThis.GetLeaf( MAKEPAGE_NONE, FALSE );
            if( pNewUpper && !SwFlowFrm::IsMoveBwdJump() &&
                ( pNewUpper->ContainsCntnt() ||
                  ( ( !pNewUpper->IsColBodyFrm() ||
                      !pNewUpper->GetUpper()->GetPrev() ) &&
                    !pNewUpper->FindSctFrm()->GetPrev() ) ) )
                pNewUpper = 0;
        }
        else
        {
            const SwFrm *pCol = rThis.FindColFrm();
            BOOL bGoOn = TRUE;
            BOOL bJump = FALSE;
            do
            {
                if ( pCol->GetPrev() )
                    pCol = pCol->GetPrev();
                else
                {
                    bGoOn = FALSE;
                    pCol = rThis.GetLeaf( MAKEPAGE_NONE, FALSE );
                }
                if ( pCol )
                {
                    // ColumnFrms jetzt mit BodyFrm
                    SwLayoutFrm* pColBody = pCol->IsColumnFrm() ?
                        (SwLayoutFrm*)((SwLayoutFrm*)pCol)->Lower() :
                        (SwLayoutFrm*)pCol;
                    if ( pColBody->ContainsCntnt() )
                    {
                        bGoOn = FALSE; // Hier gibt's Inhalt, wir akzeptieren diese
                        // nur, wenn GetLeaf() das MoveBwdJump-Flag gesetzt hat.
                        if( SwFlowFrm::IsMoveBwdJump() )
                            pNewUpper = pColBody;
                    }
                    else
                    {
                        if( pNewUpper ) // Wir hatten schon eine leere Spalte, haben
                            bJump = TRUE;   // also eine uebersprungen
                        pNewUpper = pColBody;  // Diese leere Spalte kommt in Frage,
                                               // trotzdem weitersuchen
                    }
                }
            } while( bGoOn );
            if( bJump )
                SwFlowFrm::SetMoveBwdJump( TRUE );
        }
    }
    else //Keine Breaks also kann ich zurueckfliessen
        pNewUpper = rThis.GetLeaf( MAKEPAGE_NONE, FALSE );

    //Fuer Follows ist das zurueckfliessen nur dann erlaubt wenn in der
    //neuen Umgebung kein Nachbar existiert (denn dieses waere der Master).
    //(6677)Wenn allerdings leere Blaetter uebersprungen wurden wird doch gemoved.
    if ( pNewUpper && IsFollow() && pNewUpper->Lower() )
    {
        if ( SwFlowFrm::IsMoveBwdJump() )
        {
            //Nicht hinter den Master sondern in das naechstfolgende leere
            //Blatt moven.
            SwFrm *pFrm = pNewUpper->Lower();
            while ( pFrm->GetNext() )
                pFrm = pFrm->GetNext();
            pNewUpper = pFrm->GetLeaf( MAKEPAGE_INSERT, TRUE );
            if( pNewUpper == rThis.GetUpper() ) //Landen wir wieder an der gleichen Stelle?
                pNewUpper = NULL;           //dann eruebrigt sich das Moven
        }
        else
            pNewUpper = 0;
    }
    if ( pNewUpper && !ShouldBwdMoved( pNewUpper, TRUE, rbReformat ) )
    {
        if( !pNewUpper->Lower() )
        {
            if( pNewUpper->IsFtnContFrm() )
            {
                pNewUpper->Cut();
                delete pNewUpper;
            }
            else
            {
                SwSectionFrm* pSectFrm = pNewUpper->FindSctFrm();
                if( pSectFrm && !pSectFrm->IsColLocked() && !pSectFrm->ContainsCntnt() )
                {
                    pSectFrm->DelEmpty( TRUE );
                    delete pSectFrm;
                    rThis.bValidPos = TRUE;
                }
            }
        }
        pNewUpper = 0;
    }
    if ( pNewUpper )
    {
        PROTOCOL_ENTER( &rThis, PROT_MOVE_BWD, 0, 0 );
        if ( pNewUpper->IsFtnContFrm() )
        {
            //Kann sein, dass ich einen Container bekam.
            SwFtnFrm *pOld = rThis.FindFtnFrm();
            SwFtnFrm *pNew = new SwFtnFrm( pOld->GetFmt(),
                                           pOld->GetRef(), pOld->GetAttr() );
            if ( pOld->GetMaster() )
            {
                pNew->SetMaster( pOld->GetMaster() );
                pOld->GetMaster()->SetFollow( pNew );
            }
            pNew->SetFollow( pOld );
            pOld->SetMaster( pNew );
            pNew->Paste( pNewUpper );
            pNewUpper = pNew;
        }
        if( pNewUpper->IsFtnFrm() && rThis.IsInSct() )
        {
            SwSectionFrm* pSct = rThis.FindSctFrm();
            //Wenn wir in einem Bereich in einer Fussnote stecken, muss im
            //neuen Upper ggf. ein SwSectionFrm angelegt werden
            if( pSct->IsInFtn() )
            {
                SwFrm* pTmp = pNewUpper->Lower();
                if( pTmp )
                {
                    while( pTmp->GetNext() )
                        pTmp = pTmp->GetNext();
                    if( !pTmp->IsSctFrm() ||
                        ((SwSectionFrm*)pTmp)->GetFollow() != pSct )
                        pTmp = NULL;
                }
                if( pTmp )
                    pNewUpper = (SwSectionFrm*)pTmp;
                else
                {
                    pSct = new SwSectionFrm( *pSct, TRUE );
                    pSct->Paste( pNewUpper );
                    pSct->Init();
                    pNewUpper = pSct;
                    pSct->SimpleFormat();
                }
            }
        }
        BOOL bUnlock = FALSE;
        BOOL bFollow;
        //Section locken, sonst kann sie bei Fluss des einzigen Cntnt etwa
        //von zweiter in die erste Spalte zerstoert werden.
        SwSectionFrm* pSect = pNewUpper->FindSctFrm();
        if( pSect )
        {
            bUnlock = !pSect->IsColLocked();
            pSect->ColLock();
            bFollow = pSect->HasFollow();
        }
        pNewUpper->Calc();
        rThis.Cut();
        if( bUnlock )
        {
            if( pSect->HasFollow() != bFollow )
                pSect->_InvalidateSize();
            pSect->ColUnlock();
        }

        rThis.Paste( pNewUpper );

        SwPageFrm *pNewPage = rThis.FindPageFrm();
        if( pNewPage != pOldPage )
        {
            rThis.Prepare( PREP_BOSS_CHGD, (const void*)pOldPage, FALSE );
            ViewShell *pSh = rThis.GetShell();
            if ( pSh && !pSh->Imp()->IsUpdateExpFlds() )
                pSh->GetDoc()->SetNewFldLst();  //Wird von CalcLayout() hinterher eledigt!
            pNewPage->InvalidateSpelling();
            pNewPage->InvalidateAutoCompleteWords();
            // OD 30.10.2002 #97265# - no <CheckPageDesc(..)> in online layout
            if ( !pNewPage->GetFmt()->GetDoc()->IsBrowseMode() )
            {
                if ( bCheckPageDescs && pNewPage->GetNext() )
                    SwFrm::CheckPageDescs( (SwPageFrm*)pNewPage->GetNext(), FALSE);
                else if ( rThis.GetAttrSet()->GetPageDesc().GetPageDesc() )
                {
                    //Erste Seite wird etwa durch Ausblenden eines Bereiches leer
                    SwFrm::CheckPageDescs( (SwPageFrm*)pNewPage, FALSE);
                }
            }
        }
    }
    return pNewUpper != 0;
}

/*************************************************************************
|*
|*  SwFlowFrm::CastFlowFrm
|*
|*  Ersterstellung      MA 03. May. 95
|*  Letzte Aenderung    AMA 02. Dec. 97
|*
|*************************************************************************/

SwFlowFrm *SwFlowFrm::CastFlowFrm( SwFrm *pFrm )
{
    if ( pFrm->IsCntntFrm() )
        return (SwCntntFrm*)pFrm;
    if ( pFrm->IsTabFrm() )
        return (SwTabFrm*)pFrm;
    if ( pFrm->IsSctFrm() )
        return (SwSectionFrm*)pFrm;
    return 0;
}

const SwFlowFrm *SwFlowFrm::CastFlowFrm( const SwFrm *pFrm )
{
    if ( pFrm->IsCntntFrm() )
        return (SwCntntFrm*)pFrm;
    if ( pFrm->IsTabFrm() )
        return (SwTabFrm*)pFrm;
    if ( pFrm->IsSctFrm() )
        return (SwSectionFrm*)pFrm;
    return 0;
}