summaryrefslogtreecommitdiff
path: root/sw/source/core/layout/flycnt.cxx
blob: 196f8c5774cdf4e380b8948988e070243f82472f (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
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
/*************************************************************************
 *
 *  $RCSfile: flycnt.cxx,v $
 *
 *  $Revision: 1.32 $
 *
 *  last change: $Author: vg $ $Date: 2003-05-22 09:47:03 $
 *
 *  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): _______________________________________
 *
 *
 ************************************************************************/


#pragma hdrstop


#ifndef _BIGINT_HXX //autogen
#include <tools/bigint.hxx>
#endif
#include "pagefrm.hxx"
#include "rootfrm.hxx"
#include "cntfrm.hxx"
#include "flyfrm.hxx"
#include "txtfrm.hxx"
#include "doc.hxx"
#include "viewsh.hxx"
#include "viewimp.hxx"
#include "pam.hxx"
#include "frmfmt.hxx"
#include "frmtool.hxx"
#include "dflyobj.hxx"
#include "hints.hxx"
#include "ndtxt.hxx"
#include "swundo.hxx"
#include "errhdl.hxx"

#ifndef _SVX_ULSPITEM_HXX //autogen
#include <svx/ulspitem.hxx>
#endif
#ifndef _SVX_LRSPITEM_HXX //autogen
#include <svx/lrspitem.hxx>
#endif

#ifndef _FMTANCHR_HXX //autogen
#include <fmtanchr.hxx>
#endif
#ifndef _FMTORNT_HXX //autogen
#include <fmtornt.hxx>
#endif
#ifndef _FMTFSIZE_HXX //autogen
#include <fmtfsize.hxx>
#endif
#ifndef _FMTSRND_HXX //autogen
#include <fmtsrnd.hxx>
#endif
#ifndef _NODE_HXX //autogen
#include <node.hxx>
#endif
#include "tabfrm.hxx"
#include "flyfrms.hxx"
#include "crstate.hxx"
#include "sectfrm.hxx"

/*************************************************************************
|*
|*  SwFlyAtCntFrm::SwFlyAtCntFrm()
|*
|*  Ersterstellung      MA 11. Nov. 92
|*  Letzte Aenderung    MA 09. Apr. 99
|*
|*************************************************************************/

SwFlyAtCntFrm::SwFlyAtCntFrm( SwFlyFrmFmt *pFmt, SwFrm *pAnch ) :
    SwFlyFreeFrm( pFmt, pAnch )
{
    bAtCnt = TRUE;
    bAutoPosition = FLY_AUTO_CNTNT == pFmt->GetAnchor().GetAnchorId();
}

/*************************************************************************
|*
|*  SwFlyAtCntFrm::CheckCharRect()
|*
|*************************************************************************/

void SwFlyAtCntFrm::CheckCharRect()
{
    SwFlyFrmFmt *pFmt = (SwFlyFrmFmt*)GetFmt();
    if( FLY_AUTO_CNTNT == pFmt->GetAnchor().GetAnchorId() && GetAnchor() )
    {
        SwRect aAutoPos;
        const SwFmtAnchor& rAnch = pFmt->GetAnchor();
        if( !rAnch.GetCntntAnchor() )
            return;
        if( ((SwTxtFrm*)GetAnchor())->GetAutoPos( aAutoPos,
              *rAnch.GetCntntAnchor() ) && aAutoPos != aLastCharRect )
        {
            SwFmtVertOrient aVert( pFmt->GetVertOrient() );
            SwFmtHoriOrient aHori( pFmt->GetHoriOrient() );
            if( ( REL_CHAR == aHori.GetRelationOrient() &&
                  aAutoPos.Left() != aLastCharRect.Left() ) ||
                  ( REL_CHAR == aVert.GetRelationOrient() &&
                    ( aAutoPos.Top() != aLastCharRect.Top() ||
                      aAutoPos.Height() != aLastCharRect.Height() ) ) )
                InvalidatePos();
            aLastCharRect = aAutoPos;
        }
    }
}

/*************************************************************************
|*
|*  SwFlyAtCntFrm::Modify()
|*
|*  Ersterstellung      MA 08. Feb. 93
|*  Letzte Aenderung    MA 23. Nov. 94
|*
|*************************************************************************/

void SwFlyAtCntFrm::Modify( SfxPoolItem *pOld, SfxPoolItem *pNew )
{
    USHORT nWhich = pNew ? pNew->Which() : 0;
    const SwFmtAnchor *pAnch = 0;
    if( RES_ATTRSET_CHG == nWhich && SFX_ITEM_SET ==
        ((SwAttrSetChg*)pNew)->GetChgSet()->GetItemState( RES_ANCHOR, FALSE,
            (const SfxPoolItem**)&pAnch ))
        ;       // Beim GetItemState wird der AnkerPointer gesetzt !

    else if( RES_ANCHOR == nWhich )
    {
        //Ankerwechsel, ich haenge mich selbst um.
        //Es darf sich nicht um einen Wechsel des Ankertyps handeln,
        //dies ist nur ueber die SwFEShell moeglich.
        pAnch = (const SwFmtAnchor*)pNew;
    }

    if( pAnch )
    {
        ASSERT( pAnch->GetAnchorId() == GetFmt()->GetAnchor().GetAnchorId(),
                "Unzulaessiger Wechsel des Ankertyps." );

        //Abmelden, neuen Anker besorgen und 'dranhaengen.
        SwRect aOld( AddSpacesToFrm() );
        SwPageFrm *pOldPage = FindPageFrm();
        const SwFrm *pOldAnchor = GetAnchor();
        SwCntntFrm *pCntnt = (SwCntntFrm*)GetAnchor();
        GetAnchor()->RemoveFly( this );

        const BOOL bBodyFtn = (pCntnt->IsInDocBody() || pCntnt->IsInFtn());

        //Den neuen Anker anhand des NodeIdx suchen, am alten und
        //neuen NodeIdx kann auch erkannt werden, in welche Richtung
        //gesucht werden muss.
        const SwNodeIndex aNewIdx( pAnch->GetCntntAnchor()->nNode );
        SwNodeIndex aOldIdx( *pCntnt->GetNode() );

        //fix: Umstellung, ehemals wurde in der do-while-Schleife nach vorn bzw.
        //nach hinten gesucht; je nachdem wie welcher Index kleiner war.
        //Das kann aber u.U. zu einer Endlosschleife fuehren. Damit
        //wenigstens die Schleife unterbunden wird suchen wir nur in eine
        //Richtung. Wenn der neue Anker nicht gefunden wird koennen wir uns
        //immer noch vom Node einen Frame besorgen. Die Change, dass dies dann
        //der richtige ist, ist gut.
        const FASTBOOL bNext = aOldIdx < aNewIdx;
        while ( pCntnt && aOldIdx != aNewIdx )
        {
            do
            {   if ( bNext )
                    pCntnt = pCntnt->GetNextCntntFrm();
                else
                    pCntnt = pCntnt->GetPrevCntntFrm();
            } while ( pCntnt &&
                      !(bBodyFtn == (pCntnt->IsInDocBody() ||
                                     pCntnt->IsInFtn())) );
            if (pCntnt)
                aOldIdx = *pCntnt->GetNode();
        }
        if ( !pCntnt )
        {
            SwCntntNode *pNode = aNewIdx.GetNode().GetCntntNode();
            pCntnt = pNode->GetFrm( &pOldAnchor->Frm().Pos(), 0, FALSE );
            ASSERT( pCntnt, "Neuen Anker nicht gefunden" );
        }
        //Flys haengen niemals an einem Follow sondern immer am
        //Master, den suchen wir uns jetzt.
        const SwFlowFrm *pFlow = pCntnt;
        while ( pFlow->IsFollow() )
            pFlow = pFlow->FindMaster();
        pCntnt = (SwCntntFrm*)pFlow->GetFrm();

        //und schwupp angehaengt das teil...
        pCntnt->AppendFly( this );
        if ( pOldPage && pOldPage != FindPageFrm() )
            NotifyBackground( pOldPage, aOld, PREP_FLY_LEAVE );

        //Fix(3495)
        _InvalidatePos();
        InvalidatePage();
        SetNotifyBack();
    }
    else
        SwFlyFrm::Modify( pOld, pNew );
}

/*************************************************************************
|*
|*  SwFlyAtCntFrm::MakeAll()
|*
|*  Beschreibung        Bei einem Absatzgebunden Fly kann es durchaus sein,
|*      das der Anker auf die Veraenderung des Flys reagiert. Auf diese
|*      Reaktion hat der Fly natuerlich auch wieder zu reagieren.
|*      Leider kann dies zu Oszillationen fuehren z.b. Der Fly will nach
|*      unten, dadurch kann der Inhalt nach oben, der TxtFrm wird kleiner,
|*      der Fly muss wieder hoeher woduch der Text wieder nach unten
|*      verdraengt wird...
|*      Um derartige Oszillationen zu vermeiden, wird ein kleiner Positions-
|*      stack aufgebaut. Wenn der Fly ein Position erreicht, die er bereits
|*      einmal einnahm, so brechen wir den Vorgang ab. Um keine Risiken
|*      einzugehen, wird der Positionsstack so aufgebaut, dass er fuenf
|*      Positionen zurueckblickt.
|*      Wenn der Stack ueberlaeuft, wird ebenfalls abgebrochen.
|*      Der Abbruch fuer dazu, dass der Fly am Ende eine unguenste Position
|*      einnimmt. Damit es nicht durch einen wiederholten Aufruf von
|*      Aussen zu einer 'grossen Oszillation' kommen kann wird im Abbruch-
|*      fall das Attribut des Rahmens auf automatische Ausrichtung oben
|*      eingestellt.
|*
|*  Ersterstellung      MA 12. Nov. 92
|*  Letzte Aenderung    MA 20. Sep. 96
|*
|*************************************************************************/
//Wir brauchen ein Paar Hilfsklassen zur Kontrolle der Ozillation und ein paar
//Funktionen um die Uebersicht zu gewaehrleisten.

class SwOszControl
{
    static const SwFlyFrm *pStk1;
    static const SwFlyFrm *pStk2;
    static const SwFlyFrm *pStk3;
    static const SwFlyFrm *pStk4;
    static const SwFlyFrm *pStk5;

    const SwFlyFrm *pFly;
    Point aStk1, aStk2, aStk3, aStk4, aStk5;

public:
    SwOszControl( const SwFlyFrm *pFrm );
    ~SwOszControl();
    FASTBOOL ChkOsz();
    static FASTBOOL IsInProgress( const SwFlyFrm *pFly );
};
const SwFlyFrm *SwOszControl::pStk1 = 0;
const SwFlyFrm *SwOszControl::pStk2 = 0;
const SwFlyFrm *SwOszControl::pStk3 = 0;
const SwFlyFrm *SwOszControl::pStk4 = 0;
const SwFlyFrm *SwOszControl::pStk5 = 0;

SwOszControl::SwOszControl( const SwFlyFrm *pFrm ) :
    pFly( pFrm )
{
    if ( !SwOszControl::pStk1 )
        SwOszControl::pStk1 = pFly;
    else if ( !SwOszControl::pStk2 )
        SwOszControl::pStk2 = pFly;
    else if ( !SwOszControl::pStk3 )
        SwOszControl::pStk3 = pFly;
    else if ( !SwOszControl::pStk4 )
        SwOszControl::pStk4 = pFly;
    else if ( !SwOszControl::pStk5 )
        SwOszControl::pStk5 = pFly;
}

SwOszControl::~SwOszControl()
{
    if ( SwOszControl::pStk1 == pFly )
        SwOszControl::pStk1 = 0;
    else if ( SwOszControl::pStk2 == pFly )
        SwOszControl::pStk2 = 0;
    else if ( SwOszControl::pStk3 == pFly )
        SwOszControl::pStk3 = 0;
    else if ( SwOszControl::pStk4 == pFly )
        SwOszControl::pStk4 = 0;
    else if ( SwOszControl::pStk5 == pFly )
        SwOszControl::pStk5 = 0;
}

FASTBOOL IsInProgress( const SwFlyFrm *pFly )
{
    return SwOszControl::IsInProgress( pFly );
}

FASTBOOL SwOszControl::IsInProgress( const SwFlyFrm *pFly )
{
    if ( SwOszControl::pStk1 && !pFly->IsLowerOf( SwOszControl::pStk1 ) )
        return TRUE;
    if ( SwOszControl::pStk2 && !pFly->IsLowerOf( SwOszControl::pStk2 ) )
        return TRUE;
    if ( SwOszControl::pStk3 && !pFly->IsLowerOf( SwOszControl::pStk3 ) )
        return TRUE;
    if ( SwOszControl::pStk4 && !pFly->IsLowerOf( SwOszControl::pStk4 ) )
        return TRUE;
    if ( SwOszControl::pStk5 && !pFly->IsLowerOf( SwOszControl::pStk5 ) )
        return TRUE;
    return FALSE;
}

FASTBOOL SwOszControl::ChkOsz()
{
    FASTBOOL bRet = TRUE;
    Point aTmp = pFly->Frm().Pos();
    if( aTmp == Point() )
        aTmp.X() = 1;
    //Ist der Stack am Ende?
    if ( aStk1 != Point() )
        return TRUE;
    if ( aTmp != aStk1 && aTmp != aStk2 && aTmp != aStk3 &&
         aTmp != aStk4 && aTmp != aStk5 )
    {
        aStk1 = aStk2;
        aStk2 = aStk3;
        aStk3 = aStk4;
        aStk4 = aStk5;
        aStk5 = aTmp;
        bRet = FALSE;
    }
    return bRet;
}

void SwFlyAtCntFrm::MakeAll()
{
    if ( !SwOszControl::IsInProgress( this ) && !IsLocked() && !IsColLocked() )
    {
        if( !GetPage() && GetAnchor() && GetAnchor()->IsInFly() )
        {
            SwFlyFrm* pFly = GetAnchor()->FindFlyFrm();
            SwPageFrm *pPage = pFly ? pFly->FindPageFrm() : NULL;
            if( pPage )
                pPage->SwPageFrm::AppendFly( this );
        }
        if( GetPage() )
        {
        //Den Anker muessen wir zwischendurch natuerlich Formatieren, damit
        //Repaints usw. stimmen sollte er natuerlich trotzdem Invalid bleiben.
        //Jetzt Stufe 2: Damit Repaints stimmen muessen alle Frms wieder Invalidiert
        //werden, die unterwegs formatiert werden.
        //Dazu werden sie ein ein PrtArr eingetragen; die Frms mit CompletePaint
        //zu flaggen scheint mir hier das Mittel der Wahl.
        //(Vielleicht sollte es einmal die Moeglichkeit geben sie einfach mit
        //Paint zu flaggen; kein Formatieren, aber ein Paint-Aufruf, vor allem
        //wohl fuer TxtFrms geeignet.
        //Jetzt Stufe 3: einfach ein globales Flag und schon flaggen sie sich
        //selbst.
            bSetCompletePaintOnInvalidate = TRUE;
            sal_Bool bLockedAnchor =
                static_cast<const SwTxtFrm*>( GetAnchor() )->IsAnyJoinLocked();
            {
                SwFlyFrmFmt *pFmt = (SwFlyFrmFmt*)GetFmt();
                const SwFmtFrmSize &rFrmSz = GetFmt()->GetFrmSize();
                if( rFrmSz.GetHeightPercent() != 0xFF &&
                    rFrmSz.GetHeightPercent() >= 100 )
                {
                    pFmt->LockModify();
                    SwFmtSurround aMain( pFmt->GetSurround() );
                    if ( aMain.GetSurround() == SURROUND_NONE )
                    {
                        aMain.SetSurround( SURROUND_THROUGHT );
                        pFmt->SetAttr( aMain );
                    }
                    pFmt->UnlockModify();
                }
            }
            SwOszControl aOszCntrl( this );

            if( !bLockedAnchor )
            {
                if( GetAnchor()->IsInSct() )
                {
                    SwSectionFrm *pSct = GetAnchor()->FindSctFrm();
                    pSct->Calc();
                }

                GetAnchor()->Calc();
            }

            SwFrm* pFooter = GetAnchor()->FindFooterOrHeader();
            if( pFooter && !pFooter->IsFooterFrm() )
                pFooter = NULL;
            FASTBOOL bOsz = FALSE;
            FASTBOOL bExtra = Lower() && Lower()->IsColumnFrm();

            do {
                SWRECTFN( this )
                Point aOldPos( (Frm().*fnRect->fnGetPos)() );
                SwFlyFreeFrm::MakeAll();
                BOOL bPosChg = aOldPos != (Frm().*fnRect->fnGetPos)();
                if( !bLockedAnchor )
                {
                    if( GetAnchor()->IsInSct() )
                    {
                        SwSectionFrm *pSct = GetAnchor()->FindSctFrm();
                        pSct->Calc();
                    }

                    GetAnchor()->Calc();
                }

                if( aOldPos != (Frm().*fnRect->fnGetPos)() ||
                    ( !GetValidPosFlag() &&( pFooter || bPosChg ) ) )
                    bOsz = aOszCntrl.ChkOsz();
                if( bExtra && Lower() && !Lower()->GetValidPosFlag() )
                {  // Wenn ein mehrspaltiger Rahmen wg. Positionswechsel ungueltige
                    // Spalten hinterlaesst, so drehen wir lieber hier eine weitere
                    // Runde und formatieren unseren Inhalt via FormatWidthCols nochmal.
                    _InvalidateSize();
                    bExtra = FALSE; // Sicherhaltshalber gibt es nur eine Ehrenrunde.
                }
            } while ( !IsValid() && !bOsz );

            if ( bOsz )
            {
                SwFlyFrmFmt *pFmt = (SwFlyFrmFmt*)GetFmt();
                pFmt->LockModify();
                SwFmtSurround aMain( pFmt->GetSurround() );
                // Im Notfall setzen wir automatisch positionierte Rahmen mit
                // Rekursion auf Durchlauf, das duerfte beruhigend wirken.
                if( IsAutoPos() && aMain.GetSurround() != SURROUND_THROUGHT )
                {
                    aMain.SetSurround( SURROUND_THROUGHT );
                    pFmt->SetAttr( aMain );
                }
                else
                {
                    SwFmtVertOrient aOrient( pFmt->GetVertOrient() );
                    aOrient.SetVertOrient( VERT_TOP );
                    pFmt->SetAttr( aOrient );
                    //Wenn der Rahmen auf "Kein Umlauf" steht, versuchen wir es mal
                    //mit Seitenumlauf.
                    if ( aMain.GetSurround() == SURROUND_NONE )
                    {
                        aMain.SetSurround( SURROUND_PARALLEL );
                        pFmt->SetAttr( aMain );
                    }
                }
                pFmt->UnlockModify();

                _InvalidatePos();
                SwFlyFreeFrm::MakeAll();
                if( !bLockedAnchor )
                    GetAnchor()->Calc();
                if ( !GetValidPosFlag() )
                {
                    SwFlyFreeFrm::MakeAll();
                    if( !bLockedAnchor )
                        GetAnchor()->Calc();
                }
                //Osz auf jeden fall zum Stehen bringen.
                bValidPos = bValidSize = bValidPrtArea = TRUE;
            }
            bSetCompletePaintOnInvalidate = FALSE;
        }
    }
}

/*************************************************************************
|*
|*  FindAnchor() und Hilfsfunktionen.
|*
|*  Beschreibung:       Sucht ausgehend von pOldAnch einen Anker fuer
|*      Absatzgebundene Objekte.
|*      Wird beim Draggen von Absatzgebundenen Objekten zur Ankeranzeige sowie
|*      fuer Ankerwechsel benoetigt.
|*  Ersterstellung      MA 22. Jun. 93
|*  Letzte Aenderung    MA 30. Jan. 95
|*
|*************************************************************************/

class SwDistance
{
public:
    SwTwips nMain, nSub;
    SwDistance() { nMain = nSub = 0; }
    SwDistance& operator=( const SwDistance &rTwo )
        { nMain = rTwo.nMain; nSub = rTwo.nSub; return *this; }
    BOOL operator<( const SwDistance& rTwo )
        { return nMain < rTwo.nMain || ( nMain == rTwo.nMain && nSub &&
          rTwo.nSub && nSub < rTwo.nSub ); }
    BOOL operator<=( const SwDistance& rTwo )
        { return nMain < rTwo.nMain || ( nMain == rTwo.nMain && ( !nSub ||
          !rTwo.nSub || nSub <= rTwo.nSub ) ); }
};

const SwFrm * MA_FASTCALL lcl_CalcDownDist( SwDistance &rRet,
                                         const Point &rPt,
                                         const SwCntntFrm *pCnt )
{
    rRet.nSub = 0;
    //Wenn der Point direkt innerhalb des Cnt steht ist die Sache klar und
    //der Cntnt hat automatisch eine Entfernung von 0
    if ( pCnt->Frm().IsInside( rPt ) )
    {
        rRet.nMain = 0;
        return pCnt;
    }
    else
    {
        const SwLayoutFrm *pUp = pCnt->IsInTab() ? pCnt->FindTabFrm()->GetUpper() : pCnt->GetUpper();
        // einspaltige Bereiche muessen zu ihrem Upper durchschalten
        while( pUp->IsSctFrm() )
            pUp = pUp->GetUpper();
        SWRECTFN( pUp )
        //Dem Textflus folgen.
        if ( pUp->Frm().IsInside( rPt ) )
        {
            if( bVert )
                rRet.nMain =  pCnt->Frm().Left() + pCnt->Frm().Width() -rPt.X();
            else
                rRet.nMain =  rPt.Y() - pCnt->Frm().Top();
            return pCnt;
        }
        else if ( rPt.Y() <= pUp->Frm().Top() )
        {
            rRet.nMain = LONG_MAX;
        }
        else if( rPt.X() < pUp->Frm().Left() &&
                 rPt.Y() <= ( bVert ? pUp->Frm().Top() : pUp->Frm().Bottom() ) )
        {
            const SwFrm *pLay = pUp->GetLeaf( MAKEPAGE_NONE, FALSE, pCnt );
            if( !pLay ||
                (bVert && (pLay->Frm().Top() + pLay->Prt().Bottom()) <rPt.Y())||
                (!bVert && (pLay->Frm().Left() + pLay->Prt().Right())<rPt.X()) )
            {
                if( bVert )
                    rRet.nMain =  pCnt->Frm().Left() + pCnt->Frm().Width()
                                  - rPt.X();
                else
                    rRet.nMain = rPt.Y() - pCnt->Frm().Top();
                return pCnt;
            }
            else
                rRet.nMain = LONG_MAX;
        }
        else
        {
            rRet.nMain = bVert ? pCnt->Frm().Left() + pCnt->Frm().Width() -
                                 (pUp->Frm().Left() + pUp->Prt().Left())
                : (pUp->Frm().Top() + pUp->Prt().Bottom()) - pCnt->Frm().Top();

            const SwFrm *pPre = pCnt;
            const SwFrm *pLay = pUp->GetLeaf( MAKEPAGE_NONE, TRUE, pCnt );
            SwTwips nFrmTop, nPrtHeight;
            BOOL bSct;
            const SwSectionFrm *pSect = pUp->FindSctFrm();
            if( pSect )
            {
                rRet.nSub = rRet.nMain;
                rRet.nMain = 0;
            }
            if( pSect && !pSect->IsAnLower( pLay ) )
            {
                bSct = FALSE;
                const SwSectionFrm* pNxtSect = pLay ? pLay->FindSctFrm() : 0;
                if( pSect->IsAnFollow( pNxtSect ) )
                {
                    if( pLay->IsVertical() )
                    {
                        nFrmTop = pLay->Frm().Left() + pLay->Frm().Width();
                        nPrtHeight = pLay->Prt().Width();
                    }
                    else
                    {
                        nFrmTop = pLay->Frm().Top();
                        nPrtHeight = pLay->Prt().Height();
                    }
                    pSect = pNxtSect;
                }
                else
                {
                    pLay = pSect->GetUpper();
                    if( pLay->IsVertical() )
                    {
                        nFrmTop = pSect->Frm().Left();
                        nPrtHeight = pSect->Frm().Left() - pLay->Frm().Left()
                                     - pLay->Prt().Left();
                    }
                    else
                    {
                        nFrmTop = pSect->Frm().Bottom();
                        nPrtHeight = pLay->Frm().Top() + pLay->Prt().Top()
                                     + pLay->Prt().Height() - pSect->Frm().Top()
                                     - pSect->Frm().Height();
                    }
                    pSect = 0;
                }
            }
            else if( pLay )
            {
                if( pLay->IsVertical() )
                {
                    nFrmTop = pLay->Frm().Left() + pLay->Frm().Width();
                    nPrtHeight = pLay->Prt().Width();
                }
                else
                {
                    nFrmTop = pLay->Frm().Top();
                    nPrtHeight = pLay->Prt().Height();
                }
                bSct = 0 != pSect;
            }
            while ( pLay && !pLay->Frm().IsInside( rPt ) &&
                    ( pLay->Frm().Top() <= rPt.Y() || pLay->IsInFly() ||
                      ( pLay->IsInSct() &&
                      pLay->FindSctFrm()->GetUpper()->Frm().Top() <= rPt.Y())) )
            {
                if ( pLay->IsFtnContFrm() )
                {
                    if ( !((SwLayoutFrm*)pLay)->Lower() )
                    {
                        SwFrm *pDel = (SwFrm*)pLay;
                        pDel->Cut();
                        delete pDel;
                        return pPre;
                    }
                    return 0;
                }
                else
                {
                    if( bSct || pSect )
                        rRet.nSub += nPrtHeight;
                    else
                        rRet.nMain += nPrtHeight;
                    pPre = pLay;
                    pLay = pLay->GetLeaf( MAKEPAGE_NONE, TRUE, pCnt );
                    if( pSect && !pSect->IsAnLower( pLay ) )
                    {   // If we're leaving a SwSectionFrm, the next Leaf-Frm
                        // is the part of the upper below the SectionFrm.
                        const SwSectionFrm* pNxtSect = pLay ?
                            pLay->FindSctFrm() : NULL;
                        bSct = FALSE;
                        if( pSect->IsAnFollow( pNxtSect ) )
                        {
                            pSect = pNxtSect;
                            if( pLay->IsVertical() )
                            {
                                nFrmTop = pLay->Frm().Left() + pLay->Frm().Width();
                                nPrtHeight = pLay->Prt().Width();
                            }
                            else
                            {
                                nFrmTop = pLay->Frm().Top();
                                nPrtHeight = pLay->Prt().Height();
                            }
                        }
                        else
                        {
                            pLay = pSect->GetUpper();
                            if( pLay->IsVertical() )
                            {
                                nFrmTop = pSect->Frm().Left();
                                nPrtHeight = pSect->Frm().Left() -
                                        pLay->Frm().Left() - pLay->Prt().Left();
                            }
                            else
                            {
                                nFrmTop = pSect->Frm().Bottom();
                                nPrtHeight = pLay->Frm().Top()+pLay->Prt().Top()
                                     + pLay->Prt().Height() - pSect->Frm().Top()
                                     - pSect->Frm().Height();
                            }
                            pSect = 0;
                        }
                    }
                    else if( pLay )
                    {
                        if( pLay->IsVertical() )
                        {
                             nFrmTop = pLay->Frm().Left() + pLay->Frm().Width();
                             nPrtHeight = pLay->Prt().Width();
                        }
                        else
                        {
                            nFrmTop = pLay->Frm().Top();
                            nPrtHeight = pLay->Prt().Height();
                        }
                        bSct = 0 != pSect;
                    }
                }
            }
            if ( pLay )
            {
                if ( pLay->Frm().IsInside( rPt ) )
                {
                    SwTwips nDiff = pLay->IsVertical() ? ( nFrmTop - rPt.X() )
                                                       : ( rPt.Y() - nFrmTop );
                    if( bSct || pSect )
                        rRet.nSub += nDiff;
                    else
                        rRet.nMain += nDiff;
                }
                if ( pLay->IsFtnContFrm() && !((SwLayoutFrm*)pLay)->Lower() )
                {
                    SwFrm *pDel = (SwFrm*)pLay;
                    pDel->Cut();
                    delete pDel;
                    return 0;
                }
                return pLay;
            }
            else
                rRet.nMain = LONG_MAX;
        }
    }
    return 0;
}

//Bug 3985, optimierungsproblem, vergleiche auch trvlfrm.cxx lcl_FindCntnt()
#pragma optimize("e",off)

ULONG MA_FASTCALL lcl_FindCntDiff( const Point &rPt, const SwLayoutFrm *pLay,
                          const SwCntntFrm *& rpCnt,
                          const BOOL bBody, const BOOL bFtn )
{
    //Sucht unterhalb von pLay den dichtesten Cnt zum Point. Der Bezugspunkt
    //der Cntnts ist immer die linke obere Ecke.
    //Der Cnt soll moeglichst ueber dem Point liegen.

#if OSL_DEBUG_LEVEL > 1
    Point arPoint( rPt );
#endif

    rpCnt = 0;
    ULONG nDistance = ULONG_MAX;
    ULONG nNearest  = ULONG_MAX;
    const SwCntntFrm *pCnt = pLay->ContainsCntnt();

    while ( pCnt && (bBody != pCnt->IsInDocBody() || bFtn != pCnt->IsInFtn()))
    {
        pCnt = pCnt->GetNextCntntFrm();
        if ( !pLay->IsAnLower( pCnt ) )
            pCnt = 0;
    }
    const SwCntntFrm *pNearest = pCnt;
    if ( pCnt )
    {
        do
        {
            //Jetzt die Entfernung zwischen den beiden Punkten berechnen.
            //'Delta' X^2 + 'Delta'Y^2 = 'Entfernung'^2
            ULONG dX = Max( pCnt->Frm().Left(), rPt.X() ) -
                       Min( pCnt->Frm().Left(), rPt.X() ),
                  dY = Max( pCnt->Frm().Top(), rPt.Y() ) -
                       Min( pCnt->Frm().Top(), rPt.Y() );
            BigInt dX1( dX ), dY1( dY );
            dX1 *= dX1; dY1 *= dY1;
            const ULONG nDiff = ::SqRt( dX1 + dY1 );
            if ( pCnt->Frm().Top() <= rPt.Y() )
            {
                if ( nDiff < nDistance )
                {   //Der ist dichter dran
                    nDistance = nNearest = nDiff;
                    rpCnt = pNearest = pCnt;
                }
            }
            else if ( nDiff < nNearest )
            {
                nNearest = nDiff;
                pNearest = pCnt;
            }
            pCnt = pCnt->GetNextCntntFrm();
            while ( pCnt &&
                    (bBody != pCnt->IsInDocBody() || bFtn != pCnt->IsInFtn()))
                pCnt = pCnt->GetNextCntntFrm();

        }  while ( pCnt && pLay->IsAnLower( pCnt ) );
    }
    if ( nDistance == ULONG_MAX )
    {   rpCnt = pNearest;
        return nNearest;
    }
    return nDistance;
}

#pragma optimize("e",on)

const SwCntntFrm * MA_FASTCALL lcl_FindCnt( const Point &rPt, const SwCntntFrm *pCnt,
                                  const BOOL bBody, const BOOL bFtn )
{
    //Sucht ausgehen von pCnt denjenigen CntntFrm, dessen linke obere
    //Ecke am dichtesten am Point liegt.
    //Liefert _immer_ einen CntntFrm zurueck.

    //Zunaechst wird versucht den dichtesten Cntnt innerhalt derjenigen
    //Seite zu suchen innerhalb derer der Cntnt steht.
    //Ausgehend von der Seite muessen die Seiten in beide
    //Richtungen beruecksichtigt werden.
    //Falls moeglich wird ein Cntnt geliefert, dessen Y-Position ueber der
    //des Point sitzt.
    const SwCntntFrm  *pRet, *pNew;
    const SwLayoutFrm *pLay = pCnt->FindPageFrm();
    ULONG nDist;

    nDist = ::lcl_FindCntDiff( rPt, pLay, pNew, bBody, bFtn );
    if ( pNew )
        pRet = pNew;
    else
    {   pRet  = pCnt;
        nDist = ULONG_MAX;
    }
    const SwCntntFrm *pNearest = pRet;
    ULONG nNearest = nDist;

    if ( pLay )
    {
        const SwLayoutFrm *pPge = pLay;
        ULONG nOldNew = ULONG_MAX;
        for ( USHORT i = 0; pPge->GetPrev() && (i < 3); ++i )
        {
            pPge = (SwLayoutFrm*)pPge->GetPrev();
            const ULONG nNew = ::lcl_FindCntDiff( rPt, pPge, pNew, bBody, bFtn );
            if ( nNew < nDist )
            {
                if ( pNew->Frm().Top() <= rPt.Y() )
                {
                    pRet = pNearest = pNew;
                    nDist = nNearest = nNew;
                }
                else if ( nNew < nNearest )
                {
                    pNearest = pNew;
                    nNearest = nNew;
                }
            }
            else if ( nOldNew != ULONG_MAX && nNew > nOldNew )
                break;
            else
                nOldNew = nNew;

        }
        pPge = pLay;
        nOldNew = ULONG_MAX;
        for ( USHORT j = 0; pPge->GetNext() && (j < 3); ++j )
        {
            pPge = (SwLayoutFrm*)pPge->GetNext();
            const ULONG nNew = ::lcl_FindCntDiff( rPt, pPge, pNew, bBody, bFtn );
            if ( nNew < nDist )
            {
                if ( pNew->Frm().Top() <= rPt.Y() )
                {
                    pRet = pNearest = pNew;
                    nDist = nNearest = nNew;
                }
                else if ( nNew < nNearest )
                {
                    pNearest = pNew;
                    nNearest = nNew;
                }
            }
            else if ( nOldNew != ULONG_MAX && nNew > nOldNew )
                break;
            else
                nOldNew = nNew;
        }
    }
    if ( (pRet->Frm().Top() > rPt.Y()) )
        return pNearest;
    else
        return pRet;
}

void lcl_PointToPrt( Point &rPoint, const SwFrm *pFrm )
{
    SwRect aTmp( pFrm->Prt() );
    aTmp += pFrm->Frm().Pos();
    if ( rPoint.X() < aTmp.Left() )
        rPoint.X() = aTmp.Left();
    else if ( rPoint.X() > aTmp.Right() )
        rPoint.X() = aTmp.Right();
    if ( rPoint.Y() < aTmp.Top() )
        rPoint.Y() = aTmp.Top();
    else if ( rPoint.Y() > aTmp.Bottom() )
        rPoint.Y() = aTmp.Bottom();

}

const SwCntntFrm *FindAnchor( const SwFrm *pOldAnch, const Point &rNew,
                              const BOOL bBodyOnly )
{
    //Zu der angegebenen DokumentPosition wird der dichteste Cnt im
    //Textfluss gesucht. AusgangsFrm ist der uebergebene Anker.
    const SwCntntFrm *pCnt;
    if ( pOldAnch->IsCntntFrm() )
        pCnt = (const SwCntntFrm*)pOldAnch;
    else
    {   Point aTmp( rNew );
        SwLayoutFrm *pTmpLay = (SwLayoutFrm*)pOldAnch;
        if( pTmpLay->IsRootFrm() )
        {
            SwRect aTmpRect( aTmp, Size(0,0) );
            pTmpLay = (SwLayoutFrm*)::FindPage( aTmpRect, pTmpLay->Lower() );
        }
        pCnt = pTmpLay->GetCntntPos( aTmp, FALSE, bBodyOnly );
    }

    //Beim Suchen darauf achten, dass die Bereiche sinnvoll erhalten
    //bleiben. D.h. in diesem Fall nicht in Header/Footer hinein und
    //nicht aus Header/Footer hinaus.
    const BOOL bBody = pCnt->IsInDocBody() || bBodyOnly;
    const BOOL bFtn  = !bBodyOnly && pCnt->IsInFtn();

    Point aNew( rNew );
    if ( bBody )
    {
        //#38848 Vom Seitenrand in den Body ziehen.
        const SwFrm *pPage = pCnt->FindPageFrm();
        ::lcl_PointToPrt( aNew, pPage->GetUpper() );
        SwRect aTmp( aNew, Size( 0, 0 ) );
        pPage = ::FindPage( aTmp, pPage );
        ::lcl_PointToPrt( aNew, pPage );
    }

    if ( pCnt->IsInDocBody() == bBody && pCnt->Frm().IsInside( aNew ) )
        return pCnt;
    else if ( pOldAnch->IsInDocBody() || pOldAnch->IsPageFrm() )
    {
        //Vielleicht befindet sich der gewuenschte Anker ja auf derselben
        //Seite wie der aktuelle Anker.
        //So gibt es kein Problem mit Spalten.
        Point aTmp( aNew );
        const SwCntntFrm *pTmp = pCnt->FindPageFrm()->
                                        GetCntntPos( aTmp, FALSE, TRUE, FALSE );
        if ( pTmp && pTmp->Frm().IsInside( aNew ) )
            return pTmp;
    }

    //Ausgehend vom Anker suche ich jetzt in beide Richtungen bis ich
    //den jeweils dichtesten gefunden habe.
    //Nicht die direkte Entfernung ist relevant sondern die Strecke die
    //im Textfluss zurueckgelegt werden muss.
    const SwCntntFrm *pUpLst;
    const SwCntntFrm *pUpFrm = pCnt;
    SwDistance nUp, nUpLst;
    ::lcl_CalcDownDist( nUp, aNew, pUpFrm );
    SwDistance nDown = nUp;
    BOOL bNegAllowed = TRUE;//Einmal aus dem negativen Bereich heraus lassen.
    do
    {
        pUpLst = pUpFrm; nUpLst = nUp;
        pUpFrm = pUpLst->GetPrevCntntFrm();
        while ( pUpFrm &&
                (bBody != pUpFrm->IsInDocBody() || bFtn != pUpFrm->IsInFtn()))
            pUpFrm = pUpFrm->GetPrevCntntFrm();
        if ( pUpFrm )
        {
            ::lcl_CalcDownDist( nUp, aNew, pUpFrm );
            //Wenn die Distanz innnerhalb einer Tabelle waechst, so lohnt es
            //sich weiter zu suchen.
            if ( pUpLst->IsInTab() && pUpFrm->IsInTab() )
            {
                while ( pUpFrm && ((nUpLst < nUp && pUpFrm->IsInTab()) ||
                        bBody != pUpFrm->IsInDocBody()) )
                {
                    pUpFrm = pUpFrm->GetPrevCntntFrm();
                    if ( pUpFrm )
                        ::lcl_CalcDownDist( nUp, aNew, pUpFrm );
                }
            }
        }
        if ( !pUpFrm )
            nUp.nMain = LONG_MAX;
        if ( nUp.nMain >= 0 && LONG_MAX != nUp.nMain )
        {
            bNegAllowed = FALSE;
            if ( nUpLst.nMain < 0 ) //nicht den falschen erwischen, wenn der Wert
                                    //gerade von negativ auf positiv gekippt ist.
            {   pUpLst = pUpFrm;
                nUpLst = nUp;
            }
        }
    } while ( pUpFrm && ( ( bNegAllowed && nUp.nMain < 0 ) || ( nUp <= nUpLst ) ) );

    const SwCntntFrm *pDownLst;
    const SwCntntFrm *pDownFrm = pCnt;
    SwDistance nDownLst;
    if ( nDown.nMain < 0 )
        nDown.nMain = LONG_MAX;
    do
    {
        pDownLst = pDownFrm; nDownLst = nDown;
        pDownFrm = pDownLst->GetNextCntntFrm();
        while ( pDownFrm &&
                (bBody != pDownFrm->IsInDocBody() || bFtn != pDownFrm->IsInFtn()))
            pDownFrm = pDownFrm->GetNextCntntFrm();
        if ( pDownFrm )
        {
            ::lcl_CalcDownDist( nDown, aNew, pDownFrm );
            if ( nDown.nMain < 0 )
                nDown.nMain = LONG_MAX;
            //Wenn die Distanz innnerhalb einer Tabelle waechst, so lohnt es
            //sich weiter zu suchen.
            if ( pDownLst->IsInTab() && pDownFrm->IsInTab() )
            {
                while ( pDownFrm && ( ( nDown.nMain != LONG_MAX && nDownLst < nDownLst
                        && pDownFrm->IsInTab()) || bBody != pDownFrm->IsInDocBody() ) )
                {
                    pDownFrm = pDownFrm->GetNextCntntFrm();
                    if ( pDownFrm )
                        ::lcl_CalcDownDist( nDown, aNew, pDownFrm );
                    if ( nDown.nMain < 0 )
                        nDown.nMain = LONG_MAX;
                }
            }
        }
        if ( !pDownFrm )
            nDown.nMain = LONG_MAX;

    } while ( pDownFrm && nDown <= nDownLst &&
              nDown.nMain != LONG_MAX && nDownLst.nMain != LONG_MAX );

    //Wenn ich in beide Richtungen keinen gefunden habe, so suche ich mir
    //denjenigen Cntnt dessen linke obere Ecke dem Point am naechsten liegt.
    //Eine derartige Situation tritt z.b. auf, wenn der Point nicht im Text-
    //fluss sondern in irgendwelchen Raendern steht.
    if ( nDownLst.nMain == LONG_MAX && nUpLst.nMain == LONG_MAX )
    {
        // #102861# If an OLE objects, which is contained in a fly frame
        // is resized in inplace mode and the new Position is outside the
        // fly frame, we do not want to leave our fly frame.
        if ( pCnt->IsInFly() )
            return pCnt;

        return ::lcl_FindCnt( aNew, pCnt, bBody, bFtn );
    }
    else
        return nDownLst < nUpLst ? pDownLst : pUpLst;
}

/*************************************************************************
|*
|*  SwFlyAtCntFrm::SetAbsPos()
|*
|*  Ersterstellung      MA 22. Jun. 93
|*  Letzte Aenderung    MA 11. Sep. 98
|*
|*************************************************************************/

void SwFlyAtCntFrm::SetAbsPos( const Point &rNew )
{
    SwPageFrm *pOldPage = FindPageFrm();
    const SwRect aOld( AddSpacesToFrm() );
    Point aNew( rNew );

    if( GetAnchor()->IsVertical() || GetAnchor()->IsRightToLeft() )

        aNew.X() += Frm().Width();
    SwCntntFrm *pCnt = (SwCntntFrm*)::FindAnchor( GetAnchor(), aNew );
    if( pCnt->IsProtected() )
        pCnt = (SwCntntFrm*)GetAnchor();

    SwPageFrm *pPage = 0;
    SWRECTFN( pCnt )
    const sal_Bool bRTL = pCnt->IsRightToLeft();

    if( ( bVert != GetAnchor()->IsVertical() ) ||
        ( bRTL != GetAnchor()->IsRightToLeft() ) )
    {
        if( bVert || bRTL )
            aNew.X() += Frm().Width();
        else
            aNew.X() -= Frm().Width();
    }

    if ( pCnt->IsInDocBody() )
    {
        //#38848 Vom Seitenrand in den Body ziehen.
        pPage = pCnt->FindPageFrm();
        ::lcl_PointToPrt( aNew, pPage->GetUpper() );
        SwRect aTmp( aNew, Size( 0, 0 ) );
        pPage = (SwPageFrm*)::FindPage( aTmp, pPage );
        ::lcl_PointToPrt( aNew, pPage );
    }

    //RelPos einstellen, nur auf Wunsch invalidieren.
    //rNew ist eine Absolute Position. Um die RelPos korrekt einzustellen
    //muessen wir uns die Entfernung von rNew zum Anker im Textfluss besorgen.
//!!!!!Hier kann Optimiert werden: FindAnchor koennte die RelPos mitliefern!
    const SwFrm *pFrm = 0;
    SwTwips nY;
    if ( pCnt->Frm().IsInside( aNew ) )
    {
        if( bVert )
            nY = pCnt->Frm().Left()+pCnt->Frm().Width()-rNew.X()-Frm().Width();
        else
            nY = rNew.Y() - pCnt->Frm().Top();
    }
    else
    {
        SwDistance aDist;
        pFrm = ::lcl_CalcDownDist( aDist, aNew, pCnt );
        nY = aDist.nMain + aDist.nSub;
    }

    SwTwips nX = 0;

    if ( pCnt->IsFollow() )
    {
        //Flys haengen niemals an einem Follow sondern immer am
        //Master, den suchen wir uns jetzt.
        const SwCntntFrm *pOriginal = pCnt;
        const SwCntntFrm *pFollow = pCnt;
        while ( pCnt->IsFollow() )
        {
            do
            {   pCnt = pCnt->GetPrevCntntFrm();
            } while ( pCnt->GetFollow() != pFollow );
            pFollow = pCnt;
        }
        SwTwips nDiff = 0;
        do
        {   const SwFrm *pUp = pFollow->GetUpper();
            if( pUp->IsVertical() )
                nDiff += pFollow->Frm().Left() + pFollow->Frm().Width()
                         - pUp->Frm().Left() - pUp->Prt().Left();
            else
                nDiff += pUp->Prt().Height() - pFollow->GetRelPos().Y();
            pFollow = pFollow->GetFollow();
        } while ( pFollow != pOriginal );
        nY += nDiff;
        if( bVert )
            nX = pCnt->Frm().Top() - pOriginal->Frm().Top();
        else
            nX = pCnt->Frm().Left() - pOriginal->Frm().Left();
    }

    if ( nY == LONG_MAX )
    {
        if( bVert )
            nY = pCnt->Frm().Left() + pCnt->Frm().Width() - rNew.X();
        else
            nY = rNew.Y() - pCnt->Frm().Top();
    }

    SwFlyFrmFmt *pFmt = (SwFlyFrmFmt*)GetFmt();
    const SwFmtSurround& rSurround = pFmt->GetSurround();
    const FASTBOOL bWrapThrough =
        rSurround.GetSurround() == SURROUND_THROUGHT;
    SwTwips nBaseOfstForFly = 0;
    const SwFrm* pTmpFrm = pFrm ? pFrm : pCnt;
    if ( pTmpFrm->IsTxtFrm() )
        nBaseOfstForFly =
            ((SwTxtFrm*)pTmpFrm)->GetBaseOfstForFly( !bWrapThrough );

    if( bVert )
    {
        if( !pFrm )
            nX += rNew.Y() - pCnt->Frm().Top() - nBaseOfstForFly;
        else
            nX = rNew.Y() - pFrm->Frm().Top() - nBaseOfstForFly;
    }
    else
    {
        if( !pFrm )
        {
            if ( pCnt->IsRightToLeft() )
                nX += pCnt->Frm().Right() - rNew.X() - Frm().Width() +
                      nBaseOfstForFly;
            else
                nX += rNew.X() - pCnt->Frm().Left() - nBaseOfstForFly;
        }
        else
        {
            if ( pFrm->IsRightToLeft() )
                nX += pFrm->Frm().Right() - rNew.X() - Frm().Width() +
                      nBaseOfstForFly;
            else
                nX = rNew.X() - pFrm->Frm().Left() - nBaseOfstForFly;
        }
    }
    GetFmt()->GetDoc()->StartUndo( UNDO_START );

    if( pCnt != GetAnchor() || ( IsAutoPos() && pCnt->IsTxtFrm() &&
                                  GetFmt()->GetDoc()->IsHTMLMode() ) )
    {
        //Das Ankerattribut auf den neuen Cnt setzen.
        SwFmtAnchor aAnch( pFmt->GetAnchor() );
        SwPosition *pPos = (SwPosition*)aAnch.GetCntntAnchor();
        if( IsAutoPos() && pCnt->IsTxtFrm() )
        {
            SwCrsrMoveState eTmpState( MV_SETONLYTEXT );
            Point aPt( rNew );
            if( pCnt->GetCrsrOfst( pPos, aPt, &eTmpState )
                && pPos->nNode == *pCnt->GetNode() )
            {
                aLastCharRect.Height( 0 );
                if( REL_CHAR == pFmt->GetVertOrient().GetRelationOrient() )
                    nY = LONG_MAX;
                if( REL_CHAR == pFmt->GetHoriOrient().GetRelationOrient() )
                    nX = LONG_MAX;
            }
            else
            {
                pPos->nNode = *pCnt->GetNode();
                pPos->nContent.Assign( pCnt->GetNode(), 0 );
            }
        }
        else
        {
            pPos->nNode = *pCnt->GetNode();
            pPos->nContent.Assign( pCnt->GetNode(), 0 );
        }
        pFmt->GetDoc()->SetAttr( aAnch, *pFmt );
    }
    else if ( pPage && pPage != GetPage() )
        GetPage()->MoveFly( this, pPage );

    const Point aRelPos = bVert ? Point( -nY, nX ) : Point( nX, nY );
    ChgRelPos( aRelPos );

    GetFmt()->GetDoc()->EndUndo( UNDO_END );

    if ( pOldPage != FindPageFrm() )
        ::Notify_Background( GetVirtDrawObj(), pOldPage, aOld, PREP_FLY_LEAVE,
                             FALSE );
}

/*************************************************************************
|*
|*  SwFlyAtCntFrm::MakeFlyPos()
|*
|*  Beschreibung:
|*
|*      virtueller Anker: Der virtuelle Anker eines Flys ist der Anker selbst
|*                        oder einer seiner Follows. Es ist genau derjenige
|*                        Cntnt, der dem Fly aktuell am naechsten liegt,
|*                        genauer der aktuellen relativen Position des Fly
|*                        (siehe auch VertPos, Fix). Es wird nur die
|*                        vertikale Entfernung gemessen.
|*                        Der virtuelle Anker fuer die Horizontale Ausrichtung
|*                        muss nicht ein CntntFrm sein, denn wenn der Fly
|*                        z.B. ueber einer leeren Spalte steht, so muss eben
|*                        der LayoutFrm als virtueller Anker dienen, der im
|*                        Textfluss des Ankers liegt.
|*
|*      HoriPos:
|*          - Automatisch: Die automatische Ausrichtung orientiert sich
|*            an einem SwFrm der folgendermassen ermittelt wird: Abhaengig
|*            vom Attriut und ausgehend vom virtuellen Anker wird der
|*            Bezugsframe gesucht (CntntFrm, LayoutFrm).
|*          - Fix: Der Wert der relativen Entfernung aus dem Attribut ist
|*                 die relative Entfernung vom virtuellen Anker.
|*      VertPos:
|*          - Automatisch: Die automatische Ausrichtung orientiert sich immer
|*            am virtuellen Anker.
|*          - Fix: Der Fly muss nicht in der Umgebung untergebracht sein, in
|*                 der sein Anker steht; er folgt aber stets dem Textfluss dem
|*                 der Anker folgt. Geclippt (Drawing) wird der Fly am Rootfrm.
|*                 Damit die erstgenannte Bedingung erreicht wird, wird der
|*                 Fly ggf. entsprechend verschoben. Dabei bleibt die relative
|*                 Position des Attributes erhalten, die tatsaechliche relative
|*                 Position verhaelt sich zu der des Attributes etwa wie ein
|*                 Teleskoparm. Der Betrag der relativen Position ist die
|*                 Entfernung zur AbsPos des Ankers im Textfluss.
|*
|*      Es wird immer zuerst die vertikale Position bestimmt, denn erst dann
|*      steht der virtuelle Anker fest.
|*      Die tatsaechliche relative Position (Member aRelPos) ist immer die
|*      die Entfernung zum Anker - sie muss also nicht mit den im Attribut
|*      angegebenen Werten uebereinstimmen, denn diese geben die Entfernung
|*      'im Textfluss' an.
|*
|*  Ersterstellung      MA 19. Nov. 92
|*  Letzte Aenderung    MA 14. Nov. 96
|*
|*************************************************************************/

inline void ValidateSz( SwFrm *pFrm )
{
    if ( pFrm )
        pFrm->bValidSize = TRUE;
}

void DeepCalc( const SwFrm *pFrm )
{
    if( pFrm->IsSctFrm() ||
        ( pFrm->IsFlyFrm() && ((SwFlyFrm*)pFrm)->IsFlyInCntFrm() ) )
        return;
    const SwFlowFrm *pFlow = SwFlowFrm::CastFlowFrm( pFrm );
    if( pFlow && pFlow->IsAnyJoinLocked() )
        return;

    USHORT nCnt = 0;

    FASTBOOL bContinue = FALSE;
    do
    {   if ( ++nCnt == 10 )
        {
            ASSERT( !nCnt, "DeepCalc: Loop detected1?" );
            break;
        }

        const FASTBOOL bSetComplete = !pFrm->IsValid();
        const SwRect aOldFrm( pFrm->Frm() );
        const SwRect aOldPrt( pFrm->Prt() );

        const SwFrm *pUp = pFrm->GetUpper();
        if ( pUp )
        {
            //Nicht weiter wenn der Up ein Fly mit Spalten ist.
            if( ( !pUp->IsFlyFrm() || !((SwLayoutFrm*)pUp)->Lower() ||
                 !((SwLayoutFrm*)pUp)->Lower()->IsColumnFrm() ) &&
                 !pUp->IsSctFrm() )
            {
                SWRECTFN( pUp )
                const Point aPt( (pUp->Frm().*fnRect->fnGetPos)() );
                ::DeepCalc( pUp );
                bContinue = aPt != (pUp->Frm().*fnRect->fnGetPos)();
            }
        }
        else
            pUp = pFrm;

        pFrm->Calc();
        if ( bSetComplete && (aOldFrm != pFrm->Frm() || aOldPrt != pFrm->Prt()))
            pFrm->SetCompletePaint();

//      bContinue = !pUp->IsValid();
        if ( pUp->IsFlyFrm() )
        {
            if ( ((SwFlyFrm*)pUp)->IsLocked() ||
                 (((SwFlyFrm*)pUp)->IsFlyAtCntFrm() &&
                  SwOszControl::IsInProgress( (const SwFlyFrm*)pUp )) )
            {
                bContinue = FALSE;
            }
        }
    } while ( bContinue );
}

//Ermittlung des virtuellen Ankers fuer die Positionierung.
//Dieser ist entweder der Anker selbst oder einer seiner Follows.

const SwCntntFrm *GetVirtualAnchor( const SwFlyAtCntFrm *pFly, xub_StrLen nOfs )
{
    const SwTxtFrm *pAct = (const SwTxtFrm*)pFly->GetAnchor();
    const SwTxtFrm* pTmp;
    do
    {
        pTmp = pAct;
        pAct = pTmp->GetFollow();
    }
    while( pAct && nOfs >= pAct->GetOfst() );
    return pTmp;
}

//Ermittlung des virtuellen Ankers, an dem sich die horizontale Ausrichtung
//orientieren muss.
//pAssumed enthaelt entweder bereits den Anker (Es ist dann der Anker des
//Flys oder einer seiner Follows) oder die Umgebung die der Orientierung,
//mangels einer besseren Moeglichkeit, dienen muss.

const SwFrm *GetVirtualHoriAnchor( const SwFrm *pAssumed, const SwFlyFrm *pFly )
{
    const SwFrm *pRet = pAssumed;

    if ( !pRet->IsCntntFrm() )
    {   //Wenn es Lower gibt, die selbst der Anker des Fly oder ein Follow
        //desselben sind, so wird derjenige ausgewaehlt, der der aktuellen
        //absoluten vertikalen Position des Fly am naechsten steht.
        //Gibt es keinen, so bleib es bei pAssumed
        const SwFrm *pFlow = ((SwLayoutFrm*)pRet)->Lower();
        SwTwips nCntDiff = LONG_MAX;
        while ( pFlow )
        {
            if ( pFlow->IsCntntFrm() &&
                 ((SwCntntFrm*)pFly->GetAnchor())->IsAnFollow( (SwCntntFrm*)pFlow ) )
            {
                SWRECTFN( pFlow )
                SwTwips nDiff = (pFly->Frm().*fnRect->fnGetTop)() -
                                (pFlow->Frm().*fnRect->fnGetTop)();
                if ( (nDiff = Abs(nDiff)) < nCntDiff )
                {
                    pRet = pFlow;           //Der ist dichter dran
                    nCntDiff = nDiff;
                }
            }
            pFlow = pFlow->GetNext();
        }
    }
    return pRet;

}

void SwFlyAtCntFrm::AssertPage()
{
    //Prueft ob der Fly an der Seite haengt, auf der er steht, falls nicht
    //wird er umgehaengt. Zur Pruefung wird nur die vertikale Ausrichtung
    //herangezogen.

    SwPageFrm *pNewPage = FindPageFrm();
    SwPageFrm *pMyPage  = pNewPage;
    BOOL bSuperfluous = FALSE;

    //#45516# Ausnahmebehandlung. Eine Tabelle ist zu gross und haengt aus der
    //Seite heraus. Der Rahmen kann dann zwar richtig bei seinem Anker stehen,
    //Positionsmaessig aber ueber der naechsten Seite haengen. Damit das dann
    //noch halbwegs brauchbar gedruckt werden kann (HTML) und der Rahmen nicht
    //wirr in der Gegend gepaintet wird, wird der Rahmen bei der Seite verankert,
    //auf der auch sein Anker sitzt.
    if ( GetAnchor()->GetValidSizeFlag() &&
         Frm().Top() >= GetAnchor()->Frm().Top() &&
         Frm().Top() < GetAnchor()->Frm().Bottom() )
    {
        pNewPage = GetAnchor()->FindPageFrm();
    }
    else
    {
        BOOL bFound = FALSE;
        const BOOL bFtn = GetAnchor()->IsInFtn();
        int nDir = INT_MAX; // 1 == Forward, 2 == Backward.
        while ( !bFound )
        {
            pNewPage->Calc();
            if ( Frm().Top() < pNewPage->Frm().Top() && pNewPage->GetPrev() )
            {
                pNewPage = (SwPageFrm*)pNewPage->GetPrev();
                // OD 19.02.2003 #105643# - skip empty page and consider empty
                // page at the beginning of the document.
                // Assumption about document layout:
                //      No two empty pages following each other.
                if ( pNewPage->IsEmptyPage() )
                {
                    if ( pNewPage->GetPrev() )
                    {
                        pNewPage = static_cast<SwPageFrm*>(pNewPage->GetPrev());
                    }
                    else
                    {
                        bFound = TRUE;
                        pNewPage = static_cast<SwPageFrm*>(pNewPage->GetNext());
                    }
                }
                if ( nDir == 2 )
                {
                    bFound = TRUE;
                    pNewPage = GetAnchor()->FindPageFrm();
                }
                else
                    nDir = 1;
            }
            else if ( Frm().Top() > pNewPage->Frm().Bottom() )
            {
                if ( nDir == 1 )
                {
                    bFound = TRUE;
                    pNewPage = GetAnchor()->FindPageFrm();
                }
                else
                {
                    nDir = 2;
                    if ( !pNewPage->GetNext() )
                    {
                        pNewPage->GetLeaf( bFtn ? MAKEPAGE_NONE : MAKEPAGE_APPEND,
                                            TRUE, GetAnchor());
                        bSuperfluous = TRUE;
                    }
                    if ( pNewPage->GetNext() )
                    {
                        pNewPage = (SwPageFrm*)pNewPage->GetNext();
                        if( pNewPage->IsEmptyPage() )
                        {
                            if( pNewPage->GetNext() )
                                pNewPage = (SwPageFrm*)pNewPage->GetNext();
                            else
                            {
                                bFound = TRUE;
                                pNewPage = (SwPageFrm*)pNewPage->GetPrev();
                            }
                        }
                    }
                    else
                        bFound = TRUE;
                }
            }
            else
                bFound = TRUE;
        }
    }

    if ( pMyPage != pNewPage )
    {
        ASSERT( IsLocked(), "AssertPage: Unlocked Frame??" );
        pMyPage->MoveFly( this, pNewPage );
        if ( bSuperfluous && pMyPage->GetPhyPageNum() > pNewPage->GetPhyPageNum() )
            ((SwRootFrm*)pNewPage->GetUpper())->SetSuperfluous();
    }

}

BOOL MA_FASTCALL lcl_IsMoveable( SwFlyFrm *pFly, SwLayoutFrm *pLay )
{
    //Waere der Anker auch in der neuen Umgebung noch moveable?
    BOOL bRet;
    SwLayoutFrm *pUp = pFly->GetAnchor()->GetUpper();
    SwFrm *pNext = pFly->GetAnchor()->GetNext();
    pFly->GetAnchor()->Remove();
    pFly->GetAnchor()->InsertBefore( pLay, pLay->Lower() );
    bRet = pFly->GetAnchor()->IsMoveable();
    pFly->GetAnchor()->Remove();
    pFly->GetAnchor()->InsertBefore( pUp, pNext );
    return bRet;

}

// Wer weicht wem aus bzw. welcher Bereich ist "linker"/"rechter" als welcher?
BOOL MA_FASTCALL lcl_Minor( SwRelationOrient eRelO, SwRelationOrient eRelO2,
    BOOL bLeft )
{
    // Die Ausweichreihenfolge der SwRelationOrient-Enums bei linker Ausrichtung
    static USHORT __READONLY_DATA aLeft[ LAST_ENUM_DUMMY ] =
        { 5, 6, 0, 1, 8, 4, 7, 2, 3 };
    // Die Ausweichreihenfolge der SwRelationOrient-Enums Ausrichtung rechts
    static USHORT __READONLY_DATA aRight[ LAST_ENUM_DUMMY ] =
        { 5, 6, 0, 8, 1, 7, 4, 2, 3 };
    // Hier wird z.B. entschieden, dass ein Rahmen im Absatzbereich
    // einem Rahmen im Seitentextbereich ausweicht usw.
    if( bLeft )
        return aLeft[ eRelO ] >= aLeft[ eRelO2 ];
    return aRight[ eRelO ] >= aRight[ eRelO2 ];
}

void SwFlyAtCntFrm::MakeFlyPos()
{
    /// OD 02.10.2002 #102646#
    /// if fly frame position is valid, nothing is to do, Thus, return
    if ( bValidPos )
    {
        return;
    }

    /// OD 02.10.2002 #102646# - NOTE
    /// declare and set <pFooter> to footer frame, if fly frame is anchored
    /// at a frame belonging to the footer.
    const SwFrm* pFooter = GetAnchor()->FindFooterOrHeader();
    if( pFooter && !pFooter->IsFooterFrm() )
        pFooter = NULL;

    /// OD 02.10.2002 #102646# - NOTE
    /// declare and set <bBrowse> to TRUE, if document is in browser mode and
    /// fly frame is anchored at the body, but not at frame belonging to a table.
    const FASTBOOL bBrowse = GetAnchor()->IsInDocBody() && !GetAnchor()->IsInTab() ?
                                 GetFmt()->GetDoc()->IsBrowseMode() : FALSE;

    /// OD 02.10.2002 #102646# - NOTE
    /// declare and init <bInvalidatePage> to FALSE, in order to invalidate
    /// page size, if <bInvalidatePage> is set during the calculation of the
    /// fly frame position.
    FASTBOOL bInvalidatePage = FALSE;

    /// OD 02.10.2002 #102646# - NOTE
    /// determine fly frame format and its left/right and its upper/lower spacing.
    SwFlyFrmFmt *pFmt = (SwFlyFrmFmt*)GetFmt();
    const SvxLRSpaceItem &rLR = pFmt->GetLRSpace();
    const SvxULSpaceItem &rUL = pFmt->GetULSpace();

    /// OD 02.10.2002 #102646# - NOTE
    /// determine, if fly frame has no surrounding.
    const SwFmtSurround& rSurround = pFmt->GetSurround();
    const FASTBOOL bNoSurround =
        rSurround.GetSurround() == SURROUND_NONE;
    const FASTBOOL bWrapThrough =
        rSurround.GetSurround() == SURROUND_THROUGHT;

    BOOL bGrow =
        !GetAnchor()->IsInTab() || !pFmt->GetFrmSize().GetHeightPercent();

    for (;;)
    {
        bValidPos = TRUE;
        if( !pFooter )
            ::DeepCalc( GetAnchor() );
        bValidPos = TRUE;

        //Die Werte in den Attributen muessen ggf. upgedated werden,
        //deshalb werden hier Attributinstanzen und Flags benoetigt.
        SwFmtVertOrient aVert( pFmt->GetVertOrient() );
        SwFmtHoriOrient aHori( pFmt->GetHoriOrient() );
        BOOL bVertChgd = FALSE,
             bHoriChgd = FALSE,
             bMoveable = GetAnchor()->IsMoveable();

        //Wird waehrend der Berechnung der vertikalen Position benutzt
        //und enthaelt hinterher den Frm, an dem sich die horizontale
        //Positionierung orientieren muss.
        const SwFrm *pOrient = GetAnchor();

        // Dies wird der Frame, der das Zeichen eines am Zeichen gebundenen
        // Rahmens enthaelt.
        const SwFrm *pAutoOrient = pOrient;

        SwRect *pAutoPos;
        if( FLY_AUTO_CNTNT == pFmt->GetAnchor().GetAnchorId() )
        {
            const SwFmtAnchor& rAnch = pFmt->GetAnchor();
            if( !aLastCharRect.Height() &&
                !((SwTxtFrm*)GetAnchor())->GetAutoPos( aLastCharRect,
                                                    *rAnch.GetCntntAnchor() ) )
                return;
            pAutoPos = &aLastCharRect;
            pAutoOrient = ::GetVirtualAnchor( this, rAnch.GetCntntAnchor()->
                                              nContent.GetIndex() );
        }
        else
            pAutoPos = NULL;

        //Horizontale und vertikale Positionen werden getrennt berechnet.
        //Sie koennen jeweils Fix oder Variabel sein.
        SWRECTFN( pAutoOrient )

        //Zuerst die vertikale Position, damit feststeht auf welcher Seite
        //bzw. in welchen Upper sich der Fly befindet.
        if ( aVert.GetVertOrient() != VERT_NONE )
        {
            pOrient = pAutoOrient;
            if( !pFooter )
                ::DeepCalc( pOrient );
            SwTwips nHeight, nAdd;
            if ( aVert.GetRelationOrient() == PRTAREA )
            {
                nHeight = (pOrient->Prt().*fnRect->fnGetHeight)();
                nAdd = (pOrient->*fnRect->fnGetTopMargin)();
            }
            else if( pAutoPos && REL_CHAR == aVert.GetRelationOrient() )
            {
                nHeight = (pAutoPos->*fnRect->fnGetHeight)();
                nAdd = (*fnRect->fnYDiff)( (pAutoPos->*fnRect->fnGetTop)(),
                                        (pOrient->Frm().*fnRect->fnGetTop)() );
            }
            else
            {   nHeight = (pOrient->Frm().*fnRect->fnGetHeight)();
                nAdd = 0;
            }
            SwTwips nRelPosY;
            SwTwips nFrmHeight = (Frm().*fnRect->fnGetHeight)();
            if ( aVert.GetVertOrient() == VERT_CENTER )
                nRelPosY = (nHeight / 2) - (nFrmHeight / 2);
            else
            {
                SwTwips nUpper = bVert ? rLR.GetRight() : rUL.GetUpper();
                if ( aVert.GetVertOrient() == VERT_BOTTOM )
                {
                    if( bNoSurround )
                        nRelPosY = nHeight + nUpper;
                    else
                        nRelPosY = nHeight - (nFrmHeight + ( bVert ?
                                              rLR.GetLeft() : rUL.GetLower()));
                }
                else if( pAutoPos && aVert.GetVertOrient() == VERT_CHAR_BOTTOM )
                {
                    nRelPosY = nHeight + nUpper;
                    if( bVert )
                        nRelPosY += aFrm.Width();
                }
                else
                    nRelPosY = nUpper;
            }
            nRelPosY += nAdd;
            SwTwips nOTop = (pOrient->Frm().*fnRect->fnGetTop)();
            SwTwips nBot = nRelPosY + nFrmHeight + (*fnRect->fnYDiff)( nOTop,
                              (pOrient->GetUpper()->*fnRect->fnGetPrtBottom)());
            if( nBot > 0 )
                nRelPosY -= nBot;
            if( nRelPosY < 0 )
                nRelPosY = 0;
            //Da die relative Position immer zum Anker relativ ist, muss dessen
            //Entfernung zum virtuellen Anker aufaddiert werden.
            if ( GetAnchor() != pOrient )
                nRelPosY += (*fnRect->fnYDiff)( nOTop,
                                      (GetAnchor()->Frm().*fnRect->fnGetTop)());
            if ( nRelPosY != aVert.GetPos() )
            {   aVert.SetPos( nRelPosY );
                bVertChgd = TRUE;
            }
            if( bVert )
                aRelPos.X() = nRelPosY;
            else
                aRelPos.Y() = nRelPosY;
        }

        pOrient = aVert.GetVertOrient() == VERT_NONE ?
                  GetAnchor()->GetUpper() : pAutoOrient->GetUpper();
        if( !pFooter )
            ::DeepCalc( pOrient );

        SwTwips nRelDiff = 0;
        if ( aVert.GetVertOrient() == VERT_NONE )
        {
            /// OD 02.10.2002 #102646# - NOTE
            /// local variable <nRel> for calculation of relative vertical
            /// distance to anchor.
            SwTwips nRel;
            if( pAutoPos && REL_CHAR == aVert.GetRelationOrient() )
            {
                nRel = (*fnRect->fnYDiff)( (pAutoPos->*fnRect->fnGetBottom)(),
                                     (pAutoOrient->Frm().*fnRect->fnGetTop)() );
                nRel -= aVert.GetPos();
                if( pAutoOrient != GetAnchor() )
                {
                    SwTxtFrm* pTmp = (SwTxtFrm*)GetAnchor();
                    SWREFRESHFN( pTmp )
                    nRel -=(*fnRect->fnYDiff)((pTmp->Frm().*fnRect->fnGetTop)(),
                                    (pTmp->GetUpper()->*fnRect->fnGetPrtTop)());
                    while( pTmp != pAutoOrient )
                    {
                        SWREFRESHFN( pTmp )
                        nRel +=(pTmp->GetUpper()->Prt().*fnRect->fnGetHeight)();
                        pTmp = pTmp->GetFollow();
                    }
                    SWREFRESHFN( pTmp )
                }
            }
            else
            {
                /// OD 02.10.2002 #102646#
                /// consider that vertical position can be relativ to "margin"
                /// or to "text area".
                /// Thus, increase <nRel> by margin height, if position is
                /// vertical to "text area"
                nRel = aVert.GetPos();
                if ( aVert.GetRelationOrient() == PRTAREA )
                {
                    nRel += (pAutoOrient->*fnRect->fnGetTopMargin)();
                }
            }

            // Einen einspaltigen Bereich koennen wir getrost ignorieren,
            // er hat keine Auswirkung auf die Fly-Position
            while( pOrient->IsSctFrm() )
                pOrient = pOrient->GetUpper();
            //pOrient ist das LayoutBlatt, das gerade verfolgt wird.
            //nRel    enthaelt die noch zu verarbeitende relative Entfernung.
            //nAvail  enthaelt die Strecke die im LayoutBlatt, das gerade
            //        verfolgt wird zur Verfuegung steht.

            if( nRel <= 0 )
            {
                if( bVert )
                    aRelPos.X() = 0;
                else
                    aRelPos.Y() = 0;
            }
            else
            {
                SWREFRESHFN( GetAnchor() )
                SwTwips nAvail =
                    (*fnRect->fnYDiff)( (pOrient->*fnRect->fnGetPrtBottom)(),
                                      (GetAnchor()->Frm().*fnRect->fnGetTop)());
                const BOOL bFtn = GetAnchor()->IsInFtn();
                while ( nRel )
                {   if ( nRel <= nAvail ||
                            (bBrowse &&
                            ((SwFrm*)pOrient)->Grow( nRel-nAvail, TRUE)) ||
                            (pOrient->IsInTab() && bGrow &&
                            ((SwFrm*)pOrient)->Grow( nRel-nAvail, TRUE)))
                    {
                        if( bVert )
                            aRelPos.X() = GetAnchor()->Frm().Left() +
                                          GetAnchor()->Frm().Width() -
                                          pOrient->Frm().Left() -
                                          pOrient->Prt().Left() - nAvail + nRel;
                        else
                            aRelPos.Y() = pOrient->Frm().Top() +
                                pOrient->Prt().Top() + pOrient->Prt().Height()
                                - nAvail + nRel - GetAnchor()->Frm().Top();
                        if ( ( bBrowse || ( pOrient->IsInTab() && bGrow ) )
                             && nRel - nAvail > 0 )
                        {
                            nRel = ((SwFrm*)pOrient)->Grow( nRel-nAvail );
                            SwFrm *pTmp = (SwFrm*) pOrient->FindPageFrm();
                            ::ValidateSz( pTmp );
                            bInvalidatePage = TRUE;
                            //Schon mal einstellen, weil wir wahrscheinlich
                            //wegen Invalidierung eine Ehrenrunde drehen.
                            if( bVert )
                                aFrm.Pos().X() = aFrm.Left() - nRel;
                            else
                                aFrm.Pos().Y() = aFrm.Top() + nRel;
                        }
                        nRel = 0;
                    }
                    else if ( bMoveable )
                    {   //Dem Textfluss folgen.
                        nRel -= nAvail;
                        const BOOL bSct = pOrient->IsInSct();
                        MakePageType eMakePage = bFtn ? MAKEPAGE_NONE
                                                      : MAKEPAGE_APPEND;
                        if( bSct )
                            eMakePage = MAKEPAGE_NOSECTION;
                        const SwFrm *pTmp = pOrient->
                            GetLeaf( eMakePage, TRUE, GetAnchor() );
                        if ( pTmp && ( !bSct || pOrient->FindSctFrm()->
                                IsAnFollow( pTmp->FindSctFrm() ) ) )
                        {
                            pOrient = pTmp;
                            bMoveable =
                                    ::lcl_IsMoveable( this, (SwLayoutFrm*)pOrient);
                            if( !pFooter )
                                ::DeepCalc( pOrient );
                            SWREFRESHFN( pOrient )
                            nAvail = (pOrient->Prt().*fnRect->fnGetHeight)();
                        }
                        else
                        {
                            // Wenn wir innerhalb des (spaltigen) Bereichs nicht genug
                            // Platz ist, wird es Zeit, diesen zu verlassen. Wir gehen
                            // also in seinen Upper und nehmen als nAvail den Platz, der
                            // hinter dem Bereich ist. Sollte dieser immer noch nicht
                            // ausreichen, wandern wir weiter, es hindert uns aber nun
                            // niemand mehr, neue Seiten anzulegen.
                            if( bSct )
                            {
                                const SwFrm* pSct = pOrient->FindSctFrm();
                                pOrient = pSct->GetUpper();
                                nAvail = (*fnRect->fnYDiff)(
                                           (pOrient->*fnRect->fnGetPrtBottom)(),
                                           (pSct->*fnRect->fnGetPrtBottom)() );
                            }
                            else
                            {
                                nRelDiff = nRel;
                                nRel = 0;
                            }
                        }
                    }
                    else
                        nRel = 0;
                }
                if ( !bValidPos )
                    continue;
            }
        }
        //Damit das Teil ggf. auf die richtige Seite gestellt und in die
        //PrtArea des LayLeaf gezogen werden kann, muss hier seine
        //absolute Position berechnet werden.
        if( bVert )
        {
            aFrm.Pos().X() = GetAnchor()->Frm().Left() - aFrm.Width() +
                             GetAnchor()->Frm().Width() - aRelPos.X() +nRelDiff;
        }
        else
            aFrm.Pos().Y() = GetAnchor()->Frm().Top() +
                             (aRelPos.Y() - nRelDiff);

        //Bei automatischer Ausrichtung nicht ueber die Oberkante hinausschiessen.
        if ( aVert.GetVertOrient() != VERT_NONE )
        {
            SwTwips nTop;
            if ( aVert.GetRelationOrient() == PRTAREA )
                nTop = (pOrient->*fnRect->fnGetPrtTop)();
            else
                nTop = (pOrient->Frm().*fnRect->fnGetTop)();
            SwTwips nTmp = (Frm().*fnRect->fnGetTop)();
            if( (nTmp = (fnRect->fnYDiff)( nTmp, nTop ) ) < 0 )
            {
                if( bVert )
                {
                    aFrm.Pos().X() += nTmp;
                    aRelPos.X() = nTop - GetAnchor()->Frm().Left()
                                       - GetAnchor()->Frm().Width();
                }
                else
                {
                    aFrm.Pos().Y() = nTop;
                    aRelPos.Y() = nTop - GetAnchor()->Frm().Top();
                }
                bHeightClipped = TRUE;
            }
        }

        const BOOL bFtn = GetAnchor()->IsInFtn();
        while( pOrient->IsSctFrm() )
            pOrient = pOrient->GetUpper();
        SwTwips nDist = (aFrm.*fnRect->fnBottomDist)(
                         (pOrient->*fnRect->fnGetPrtBottom)() );
        if( nDist < 0 )
        {
            if( ( bBrowse && GetAnchor()->IsMoveable() ) ||
                ( GetAnchor()->IsInTab() && bGrow ) )
            {
                ((SwFrm*)pOrient)->Grow( -nDist );
                SwFrm *pTmp = (SwFrm*) pOrient->FindPageFrm();
                ::ValidateSz( pTmp );
                bInvalidatePage = TRUE;
            }

            nDist = (aFrm.*fnRect->fnBottomDist)(
                     (pOrient->*fnRect->fnGetPrtBottom)() );
            while( bMoveable && nDist < 0 )
            {
                // Vorsicht, auch innerhalb von Bereichen duerfen keine neuen Seiten angelegt werden
                BOOL bSct = pOrient->IsInSct();
                if( bSct )
                {
                    const SwFrm* pTmp = pOrient->FindSctFrm()->GetUpper();
                    nDist = (aFrm.*fnRect->fnBottomDist)(
                         (pTmp->*fnRect->fnGetPrtBottom)() );
                    if( nDist < 0 )
                        pOrient = pTmp;
                    else
                        break;
                    bSct = pOrient->IsInSct();
                }
                if( !bSct && (Frm().*fnRect->fnGetTop)() ==
                             (pOrient->*fnRect->fnGetPrtTop)() )
                    //Das teil passt nimmer, da hilft auch kein moven.
                    break;

                const SwLayoutFrm *pNextLay = pOrient->GetLeaf( bSct ?
                    MAKEPAGE_NOSECTION : bFtn ? MAKEPAGE_NONE : MAKEPAGE_APPEND,
                    TRUE, GetAnchor() );
                if( pNextLay )
                {
                    SWRECTFNX( pNextLay )
                    if( !bSct || ( pOrient->FindSctFrm()->IsAnFollow(
                        pNextLay->FindSctFrm() ) &&
                        (pNextLay->Prt().*fnRectX->fnGetHeight)() ) )
                    {
                        if( !pFooter )
                            ::DeepCalc( pNextLay );
                        if( bVertX )
                            aRelPos.X() = GetAnchor()->Frm().Left() +
                                          GetAnchor()->Frm().Width() -
                                          pNextLay->Frm().Left() -
                                          pNextLay->Prt().Left()-
                                          pNextLay->Prt().Width();
                        else
                            aRelPos.Y() = pNextLay->Frm().Top() +
                                pNextLay->Prt().Top() -GetAnchor()->Frm().Top();
                        pOrient = pNextLay;
                        SWREFRESHFN( pOrient )
                        bMoveable = ::lcl_IsMoveable( this,
                                                        (SwLayoutFrm*)pOrient );
                        if ( bMoveable && !pFooter )
                            ::DeepCalc( pOrient );
                        if( bVertX )
                            aFrm.Pos().X() = GetAnchor()->Frm().Left()
                                             + GetAnchor()->Frm().Width()
                                             - aRelPos.X() - aFrm.Width();
                        else
                            aFrm.Pos().Y() = GetAnchor()->Frm().Top()
                                             + aRelPos.Y();
                        nDist = (aFrm.*fnRect->fnBottomDist)(
                             (pOrient->*fnRect->fnGetPrtBottom)() );
                    }
                }
                else if( bSct )
                {
                    // Wenn wir innerhalb des Bereich nicht genug Platz haben, gucken
                    // wir uns mal die Seite an.
                    const SwFrm* pTmp = pOrient->FindSctFrm()->GetUpper();
                    nDist = (aFrm.*fnRect->fnBottomDist)(
                         (pTmp->*fnRect->fnGetPrtBottom)() );
                    if( nDist < 0 )
                        pOrient = pTmp;
                    else
                        break;
                }
                else
                    bMoveable = FALSE;
            }
        }
        AssertPage();

        //Horizontale Ausrichtung.
        //Die absolute Pos in der vertikalen muss schon mal eingestellt
        //werden, sonst habe ich Schwierigkeiten den virtuellen Anker
        //zu ermitteln.
        if( bVert )
            aFrm.Pos().X() = GetAnchor()->Frm().Left() - aFrm.Width() +
                             GetAnchor()->Frm().Width() - aRelPos.X();
        else
            aFrm.Pos().Y() = aRelPos.Y() + GetAnchor()->Frm().Top();
        //Den Frm besorgen, an dem sich die horizontale Ausrichtung orientiert.
        pOrient = ::GetVirtualHoriAnchor( pOrient, this );

        if( !pFooter )
            ::DeepCalc( pOrient );

        // Achtung: pPage ist nicht unbedingt ein PageFrm, es kann auch ein
        // SwFlyFrm oder SwCellFrm dahinterstecken
        const SwFrm *pPage = pOrient;
        while( !pPage->IsPageFrm() && !pPage->IsFlyFrm() && !pPage->IsCellFrm() )
        {
            ASSERT( pPage->GetUpper(), "MakeFlyPos: No Page/FlyFrm Found" );
            pPage = pPage->GetUpper();
        }

        const BOOL bEven = !pPage->OnRightPage();
        const BOOL bToggle = aHori.IsPosToggle() && bEven;
        BOOL bTmpToggle = bToggle;
        BOOL bPageRel = FALSE;
        SwTwips nWidth, nAdd;
        SWREFRESHFN( pOrient )
        switch ( aHori.GetRelationOrient() )
        {
            case PRTAREA:
            {
                nWidth = (pOrient->Prt().*fnRect->fnGetWidth)();
                nAdd = (pOrient->*fnRect->fnGetLeftMargin)();
                if ( pOrient->IsTxtFrm() )
                    nAdd += ((SwTxtFrm*)pOrient)->GetBaseOfstForFly( !bWrapThrough );
                break;
            }
            case REL_PG_LEFT:
                bTmpToggle = !bToggle;
                // kein break;
            case REL_PG_RIGHT:
            {
                if ( bTmpToggle )    // linker Seitenrand
                {
                    nAdd = (*fnRect->fnXDiff)((pPage->Frm().*fnRect->fnGetLeft)(),
                                         (pOrient->Frm().*fnRect->fnGetLeft)());
                    nWidth = (pPage->*fnRect->fnGetLeftMargin)();
                }
                else            // rechter Seitenrand
                {
                    nAdd = (*fnRect->fnXDiff)((pPage->*fnRect->fnGetPrtRight)(),
                                         (pOrient->Frm().*fnRect->fnGetLeft)());
                    nWidth = (pPage->*fnRect->fnGetRightMargin)();
                }
                bPageRel = TRUE;
                break;
            }
            case REL_FRM_LEFT:
                bTmpToggle = !bToggle;
                // kein break;
            case REL_FRM_RIGHT:
            {
                if ( bTmpToggle )    // linker Absatzrand
                {
                    nWidth = (pOrient->*fnRect->fnGetLeftMargin)();
                    nAdd = 0;
                }
                else            // rechter Absatzrand
                {
                    nWidth = (pOrient->*fnRect->fnGetRightMargin)();
                    nAdd = (pOrient->Frm().*fnRect->fnGetWidth)()-nWidth;
                }
                break;
            }
            case REL_CHAR:
            {
                if( pAutoPos )
                {
                    nWidth = 0;
                    nAdd = (*fnRect->fnXDiff)( (pAutoPos->*fnRect->fnGetLeft)(),
                                    (pAutoOrient->Frm().*fnRect->fnGetLeft)() );
                    break;
                }
                // No Break!
            }
            case REL_PG_PRTAREA:
            {
                nWidth = (pPage->Prt().*fnRect->fnGetWidth)();
                nAdd = (*fnRect->fnXDiff)( (pPage->*fnRect->fnGetPrtLeft)(),
                                         (pOrient->Frm().*fnRect->fnGetLeft)());
                bPageRel = TRUE;
                break;
            }
            case REL_PG_FRAME:
            {
                nWidth = (pPage->Frm().*fnRect->fnGetWidth)();
                nAdd = (*fnRect->fnXDiff)( (pPage->Frm().*fnRect->fnGetLeft)(),
                                         (pOrient->Frm().*fnRect->fnGetLeft)());
                bPageRel = TRUE;
                break;
            }
            default:
            {
                nWidth = (pOrient->Frm().*fnRect->fnGetWidth)();
                nAdd = pOrient->IsTxtFrm() ?
                       ((SwTxtFrm*)pOrient)->GetBaseOfstForFly( !bWrapThrough ) :
                       0;
                break;
            }
        }
        SwTwips nRelPosX = nAdd;
        sal_Bool bR2L = GetAnchor()->IsRightToLeft();
        if ( aHori.GetHoriOrient() == HORI_NONE )
        {
            if( pAutoPos && REL_CHAR == aHori.GetRelationOrient() )
            {
                if( bR2L )
                    nRelPosX -= aHori.GetPos();
                else
                    nRelPosX += aHori.GetPos();
            }
            else if( bToggle || ( !aHori.IsPosToggle() && bR2L ) )
                nRelPosX = nWidth - aFrm.Width() - aHori.GetPos() +
                            ( bR2L ? nAdd : 0 );
            else
                nRelPosX += aHori.GetPos();
            //Da die relative Position immer zum Anker relativ ist,
            //muss dessen Entfernung zum virtuellen Anker aufaddiert werden.
            if ( GetAnchor() != pOrient )
            {
                long nTmp = (pOrient->Frm().*fnRect->fnGetLeft)();
                nRelPosX += (*fnRect->fnXDiff)( nTmp,
                                    (GetAnchor()->Frm().*fnRect->fnGetLeft)() );
                //fix(18546): Kleine Notbremse, wenn der Rahmen jetzt so positioniert
                //wird, dass er den Anker verdraengt, muessen wir unbedingt agieren.
                //fix(22698): in Ergaenzung zu obigem Bug passen wir jetzt etwas
                //grundlicher auf.
                if( bVert )
                {
                    if( !bPageRel && nTmp > pAnchor->Frm().Bottom() &&
                        Frm().Right() > GetAnchor()->Frm().Left() )
                    {
                        nTmp = nRelPosX + GetAnchor()->Frm().Top();
                        if( nTmp < GetAnchor()->Frm().Bottom() )
                            nRelPosX = GetAnchor()->Frm().Height() + 1;
                    }
                }
                else
                {
                    if( !bPageRel && nTmp > pAnchor->Frm().Right() &&
                        Frm().Top() < GetAnchor()->Frm().Bottom() )
                    {
                        nTmp = aRelPos.X() + GetAnchor()->Frm().Left();
                        if ( nTmp < GetAnchor()->Frm().Right() )
                            nRelPosX = GetAnchor()->Frm().Width()+1;
                    }
                }
            }
            if( bVert )
            {
                if( GetAnchor()->Frm().Top() + nRelPosX + aFrm.Height() >
                    pPage->Frm().Bottom() )
                    nRelPosX = pPage->Frm().Bottom() - GetAnchor()->Frm().Top()
                                  - aFrm.Height();
                if( GetAnchor()->Frm().Top() + nRelPosX < pPage->Frm().Top() )
                    nRelPosX = pPage->Frm().Top() - GetAnchor()->Frm().Top();
            }
            else
            {
                if( GetAnchor()->Frm().Left() + nRelPosX + aFrm.Width() >
                    pPage->Frm().Right() )
                    nRelPosX = pPage->Frm().Right() -
                               GetAnchor()->Frm().Left() - aFrm.Width();
                if( GetAnchor()->Frm().Left() + nRelPosX < pPage->Frm().Left() )
                    nRelPosX = pPage->Frm().Left() - GetAnchor()->Frm().Left();
            }
        }
        else
        {
            SwHoriOrient eHOri = aHori.GetHoriOrient();
            SwRelationOrient eRelO = aHori.GetRelationOrient();
            if( bToggle )
            {
                if( HORI_RIGHT == eHOri )
                    eHOri = HORI_LEFT;
                else if( HORI_LEFT == eHOri )
                    eHOri = HORI_RIGHT;
                if( REL_PG_RIGHT == eRelO )
                    eRelO = REL_PG_LEFT;
                else if( REL_PG_LEFT == eRelO )
                    eRelO = REL_PG_RIGHT;
                else if( REL_FRM_RIGHT == eRelO )
                    eRelO = REL_FRM_LEFT;
                else if( REL_FRM_LEFT == eRelO )
                    eRelO = REL_FRM_RIGHT;
            }
            if( bVert )
            {
                if ( eHOri == HORI_CENTER )
                    nRelPosX = (nWidth / 2) - (aFrm.Height() / 2);
                else if ( eHOri == HORI_RIGHT )
                    nRelPosX = nWidth - (aFrm.Height() + rUL.GetLower());
                else
                    nRelPosX = rUL.GetUpper();
                nRelPosX += nAdd;

                if( GetAnchor() != pOrient )
                    nRelPosX += pOrient->Frm().Top() -
                                GetAnchor()->Frm().Top();

                if( GetAnchor()->Frm().Top() + nRelPosX + aFrm.Height() >
                    pPage->Frm().Bottom() )
                    nRelPosX = pPage->Frm().Bottom() - GetAnchor()->Frm().Top()
                                - aFrm.Height();
                if( GetAnchor()->Frm().Top() + nRelPosX < pPage->Frm().Top() )
                    nRelPosX = pPage->Frm().Top() - GetAnchor()->Frm().Top();
            }
            else
            {
                if ( eHOri == HORI_CENTER )
                    nRelPosX = (nWidth / 2) - (aFrm.Width() / 2);
                else if ( eHOri == HORI_RIGHT )
                    nRelPosX = nWidth - (aFrm.Width() + rLR.GetRight());
                else
                    nRelPosX = rLR.GetLeft();
                nRelPosX += nAdd;

                //Da die relative Position immer zum Anker relativ ist,
                //muss dessen Entfernung zum virtuellen Anker aufaddiert werden.
                if( GetAnchor() != pOrient )
                    nRelPosX += pOrient->Frm().Left() -
                                GetAnchor()->Frm().Left();
                if( GetAnchor()->Frm().Left() + nRelPosX + aFrm.Width() >
                    pPage->Frm().Right() )
                    nRelPosX = pPage->Frm().Right() - GetAnchor()->Frm().Left()
                                - aFrm.Width();
                if( GetAnchor()->Frm().Left() + nRelPosX < pPage->Frm().Left() )
                    nRelPosX = pPage->Frm().Left() - GetAnchor()->Frm().Left();
            }

            //Es muss allen Rahmen ausgewichen werden, die die selbe
            //automatische Ausrichtung haben und die unter dem Rahmen liegen.
            if ( HORI_CENTER != eHOri && REL_CHAR != eRelO )
            {
                Point aTmpPos = (GetAnchor()->Frm().*fnRect->fnGetPos)();
                if( bVert )
                {
                    aTmpPos.X() -= aRelPos.X() + aFrm.Width();
                    aTmpPos.Y() += nRelPosX;
                }
                else
                {
                    aTmpPos.X() += nRelPosX;
                    aTmpPos.Y() += aRelPos.Y();
                }
                SwRect aTmpFrm( aTmpPos, Frm().SSize() );
                const UINT32 nMyOrd = GetVirtDrawObj()->GetOrdNum();
                const SwPageFrm *pPage = FindPageFrm();
                SwOrderIter aIter( pPage, TRUE );
                const SwFlyFrm *pFly = ((SwVirtFlyDrawObj*)aIter.Bottom())->GetFlyFrm();
                const SwFrm *pKontext = ::FindKontext( GetAnchor(), FRM_COLUMN );
                ULONG nMyIndex = ((SwTxtFrm*)GetAnchor())->GetTxtNode()->GetIndex();
                while ( pFly && nMyOrd > pFly->GetVirtDrawObj()->GetOrdNumDirect() )
                {
                    if ( pFly->IsFlyAtCntFrm() && //pFly->IsValid() &&
                         (pFly->Frm().*fnRect->fnBottomDist)(
                            (aTmpFrm.*fnRect->fnGetTop)() ) < 0 &&
                         (aTmpFrm.*fnRect->fnBottomDist)(
                            (pFly->Frm().*fnRect->fnGetTop)() ) < 0 &&
                         ::FindKontext( pFly->GetAnchor(), FRM_COLUMN ) == pKontext )
                    {
                        ULONG nOtherIndex = ((SwTxtFrm*)pFly->GetAnchor())
                                            ->GetTxtNode()->GetIndex();
                        if( nMyIndex >= nOtherIndex )
                        {
                            const SwFmtHoriOrient &rHori =
                                                pFly->GetFmt()->GetHoriOrient();
                            SwRelationOrient eRelO2 = rHori.GetRelationOrient();
                            if( REL_CHAR != eRelO2 )
                            {
                                SwHoriOrient eHOri2 = rHori.GetHoriOrient();
                                if( bEven && rHori.IsPosToggle() )
                                {
                                    if( HORI_RIGHT == eHOri2 )
                                        eHOri2 = HORI_LEFT;
                                    else if( HORI_LEFT == eHOri2 )
                                        eHOri2 = HORI_RIGHT;
                                    if( REL_PG_RIGHT == eRelO2 )
                                        eRelO2 = REL_PG_LEFT;
                                    else if( REL_PG_LEFT == eRelO2 )
                                        eRelO2 = REL_PG_RIGHT;
                                    else if( REL_FRM_RIGHT == eRelO2 )
                                        eRelO2 = REL_FRM_LEFT;
                                    else if( REL_FRM_LEFT == eRelO2 )
                                        eRelO2 = REL_FRM_RIGHT;
                                }
                                if ( eHOri2 == eHOri &&
                                    lcl_Minor( eRelO, eRelO2, HORI_LEFT == eHOri ) )
                                {
                                    //Die Berechnung wird dadurch etwas aufwendiger, das die
                                    //Ausgangsbasis der Flys unterschiedlich sein koennen.
                                    if( bVert )
                                    {
                                        const SvxULSpaceItem &rULI = pFly->GetFmt()->GetULSpace();
                                        const SwTwips nFlyTop = pFly->Frm().Top() - rULI.GetUpper();
                                        const SwTwips nFlyBot = pFly->Frm().Bottom() + rULI.GetLower();
                                        if( nFlyTop <= aTmpFrm.Bottom() + rUL.GetLower() &&
                                            nFlyBot >= aTmpFrm.Top() - rUL.GetUpper() )
                                        {
                                            if ( eHOri == HORI_LEFT )
                                            {
                                                SwTwips nTmp = nFlyBot + 1
                                                    + rUL.GetUpper()
                                                    - GetAnchor()->Frm().Top();
                                                if( nTmp > nRelPosX &&
                                                    nTmp + Frm().Height() +
                                                    GetAnchor()->Frm().Top() +
                                                    rUL.GetLower() <=
                                                    pPage->Frm().Height() +
                                                    pPage->Frm().Top() )
                                                {
                                                    nRelPosX = nTmp;
                                                }
                                            }
                                            else if ( eHOri == HORI_RIGHT )
                                            {
                                                SwTwips nTmp = nFlyTop - 1
                                                    - rUL.GetLower()
                                                    - Frm().Height()
                                                    - GetAnchor()->Frm().Top();
                                                if( nTmp < nRelPosX &&
                                                    nTmp - rUL.GetUpper() +
                                                    GetAnchor()->Frm().Top()
                                                    >= pPage->Frm().Top() )
                                                {
                                                    nRelPosX = nTmp;
                                                }
                                            }
                                            aTmpFrm.Pos().Y() = GetAnchor()->Frm().Top() + nRelPosX;
                                        }
                                    }
                                    else
                                    {
                                        const SvxLRSpaceItem &rLRI = pFly->GetFmt()->GetLRSpace();
                                        const SwTwips nFlyLeft = pFly->Frm().Left() - rLRI.GetLeft();
                                        const SwTwips nFlyRight = pFly->Frm().Right() + rLRI.GetRight();
                                        if( nFlyLeft <= aTmpFrm.Right() + rLR.GetRight() &&
                                            nFlyRight >= aTmpFrm.Left() - rLR.GetLeft() )
                                        {
                                            if ( eHOri == HORI_LEFT )
                                            {
                                                SwTwips nTmp = nFlyRight + 1
                                                    + rLR.GetLeft()
                                                    - GetAnchor()->Frm().Left();
                                                if( nTmp > nRelPosX &&
                                                    nTmp + Frm().Width() +
                                                    GetAnchor()->Frm().Left() +
                                                    rLR.GetRight() <=
                                                    pPage->Frm().Width() +
                                                    pPage->Frm().Left() )
                                                {
                                                    nRelPosX = nTmp;
                                                }
                                            }
                                            else if ( eHOri == HORI_RIGHT )
                                            {
                                                SwTwips nTmp = nFlyLeft - 1
                                                    - rLR.GetRight()
                                                    - Frm().Width()
                                                    - GetAnchor()->Frm().Left();
                                                if( nTmp < nRelPosX &&
                                                    nTmp - rLR.GetLeft() +
                                                    GetAnchor()->Frm().Left()
                                                    >= pPage->Frm().Left() )
                                                {
                                                    nRelPosX = nTmp;
                                                }
                                            }
                                            aTmpFrm.Pos().X() = GetAnchor()->Frm().Left() + nRelPosX;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    pFly = ((SwVirtFlyDrawObj*)aIter.Next())->GetFlyFrm();
                }
            }

            if ( aHori.GetPos() != nRelPosX )
            {   aHori.SetPos( nRelPosX );
                bHoriChgd = TRUE;
            }
        }
        if( bVert )
            aRelPos.Y() = nRelPosX;
        else
            aRelPos.X() = nRelPosX;
        AssertPage();

        //Die AbsPos ergibt sich aus der Absoluten Position des Ankers
        //plus der relativen Position
        if( bVert )
        {
            aFrm.Pos().X() = GetAnchor()->Frm().Left() +
                             GetAnchor()->Frm().Width() -
                             aFrm.Width() - aRelPos.X();
            aFrm.Pos().Y() = GetAnchor()->Frm().Top() + aRelPos.Y();
            AssertPage();
        }
        else
            aFrm.Pos( aRelPos + GetAnchor()->Frm().Pos() );
        //Und ggf. noch die aktuellen Werte im Format updaten, dabei darf
        //zu diesem Zeitpunkt natuerlich kein Modify verschickt werden.
        pFmt->LockModify();
        if ( bVertChgd )
            pFmt->SetAttr( aVert );
        if ( bHoriChgd )
            pFmt->SetAttr( aHori );
        pFmt->UnlockModify();

        break;
    } /// OD 02.10.2002 #102646# - End of loop

    if ( bInvalidatePage )
        FindPageFrm()->InvalidateSize();
    if ( !bValidPos && !GetAnchor()->IsValid() )
    {
//      ASSERT( StackHack::IsLocked(), "invalid Anchor" );
        bValidPos = TRUE;
    }
}