summaryrefslogtreecommitdiff
path: root/sw/source/filter/rtf/rtffly.cxx
blob: 5ae0dddad67566aeed21b0565b358f37541a6ef9 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sw.hxx"
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
#include <hintids.hxx>
#include <tools/list.hxx>
#include <tools/cachestr.hxx>
#include <svtools/rtftoken.h>
#include <svl/itemiter.hxx>
#include <editeng/prntitem.hxx>
#include <editeng/opaqitem.hxx>
#include <editeng/protitem.hxx>
#include <editeng/ulspitem.hxx>
#include <editeng/lrspitem.hxx>
#include <editeng/boxitem.hxx>
#include <editeng/frmdiritem.hxx>
#include <fmtfsize.hxx>
#include <fmtanchr.hxx>
#include <fmtpdsc.hxx>
#include <fmtsrnd.hxx>
#include <fmtclds.hxx>
#include <fmtcntnt.hxx>
#include <frmatr.hxx>
#include <doc.hxx>
#include <pam.hxx>
#include <ndtxt.hxx>
#include <shellio.hxx>
#include <swparrtf.hxx>
#include <grfatr.hxx>
#include <paratr.hxx>
#include <rtf.hxx>
#include <ndgrf.hxx>
#include <pagedesc.hxx>
#include <swtable.hxx>
#include <txtflcnt.hxx>
#include <fmtflcnt.hxx>
#include <fltini.hxx>
#include <deque>
#include <map>
#include <utility>
// --> OD 2004-06-30 #i27767#
#include <fmtwrapinfluenceonobjpos.hxx>
// <--
#include <editeng/brshitem.hxx>
#include <fmtfollowtextflow.hxx>
// --> OD, FLR 2006-02-16 #131205#
#include "dcontact.hxx"
// <--


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

#define ANCHOR(p)   ((SwFmtAnchor*)p)

// steht in shellio.hxx
extern SwCntntNode* GoNextNds( SwNodeIndex * pIdx, BOOL bChk );

SV_IMPL_PTRARR( SwFlySaveArr, SwFlySave* )

inline const SwFmtFrmSize GetFrmSize(const SfxItemSet& rSet, BOOL bInP=TRUE)
{
    return (const SwFmtFrmSize&)rSet.Get(RES_FRM_SIZE,bInP);
}

SwFlySave::SwFlySave(const SwPaM& rPam, SfxItemSet& rSet)
    : aFlySet(rSet), nSttNd(rPam.GetPoint()->nNode), nEndNd(nSttNd), nEndCnt(0),
     nPageWidth(ATT_MIN_SIZE), nDropLines(0), nDropAnchor(0)
{
}

int SwFlySave::IsEqualFly( const SwPaM& rPos, SfxItemSet& rSet )
{
    if( rSet.Count() != aFlySet.Count() || nDropAnchor )
        return FALSE;

    // nur TextNodes zusammenfassen
    if( nSttNd == nEndNd && nEndNd.GetNode().IsNoTxtNode() )
        return FALSE;

    // teste auf gleiche / naechste Position
    if( rPos.GetPoint()->nNode.GetIndex() == nEndNd.GetIndex() )
    {
        if( 1 < (rPos.GetPoint()->nContent.GetIndex() - nEndCnt) )
            return FALSE;
    }
    else if( rPos.GetPoint()->nContent.GetIndex() )
        return FALSE;
    else
    {
        SwNodeIndex aIdx( nEndNd );
        SwCntntNode* pCNd = rPos.GetDoc()->GetNodes()[ aIdx ]->GetCntntNode();
        if( !GoNextNds( &aIdx, TRUE ) ||
            aIdx.GetIndex() != rPos.GetPoint()->nNode.GetIndex() ||
            ( pCNd && pCNd->Len() != nEndCnt ))
        {
            return FALSE;
        }
    }

    if( rSet.Count() )
    {
        SfxItemIter aIter( rSet );
        const SfxPoolItem *pItem, *pCurr = aIter.GetCurItem();
        while( TRUE )
        {
            if( SFX_ITEM_SET != aFlySet.GetItemState( pCurr->Which(),
                FALSE, &pItem ) ||
                // Ankerattribute gesondert behandeln
                ( RES_ANCHOR == pCurr->Which()
                    ? (ANCHOR(pCurr)->GetAnchorId() != ANCHOR(pItem)->GetAnchorId() ||
                       ANCHOR(pCurr)->GetPageNum() != ANCHOR(pItem)->GetPageNum())
                    : *pItem != *pCurr ))
                        return FALSE;

            if( aIter.IsAtEnd() )
                break;
            pCurr = aIter.NextItem();
        }
    }
    return TRUE;
}

void SwFlySave::SetFlySize( const SwTableNode& rTblNd )
{
    // sollte der Fly kleiner als diese Tabelle sein, dann
    // korrigiere diesen (nur bei abs. Angaben!)
    SwTwips nWidth = rTblNd.GetTable().GetFrmFmt()->GetFrmSize().GetWidth();
    const SwFmtFrmSize& rSz = GetFrmSize( aFlySet );
    if( nWidth > rSz.GetWidth() )
        aFlySet.Put( SwFmtFrmSize( rSz.GetHeightSizeType(), nWidth, rSz.GetHeight() ));
}

BOOL lcl_HasBreakAttrs( const SwCntntNode& rNd )
{
    BOOL bRet = FALSE;
    const SfxItemSet& rSet = rNd.GetSwAttrSet();
    const SfxPoolItem* pItem;
    if( SFX_ITEM_SET == rSet.GetItemState( RES_BREAK, TRUE, &pItem ) &&
        SVX_BREAK_NONE != ((SvxFmtBreakItem*)pItem)->GetBreak() )
        bRet = TRUE;
    else if( SFX_ITEM_SET == rSet.GetItemState( RES_PAGEDESC, TRUE, &pItem )&&
         0 != ((SwFmtPageDesc*)pItem)->GetPageDesc() )
        bRet = TRUE;
    return bRet;
}


void lcl_CpyBreakAttrs( SwCntntNode* pSrcNd, SwCntntNode* pDstNd,
                        SwNodeIndex* pNewIdx )
{
    const SfxItemSet* pSet;
    if( pSrcNd && pDstNd && 0 != ( pSet = pSrcNd->GetpSwAttrSet() ) )
    {
        const SfxPoolItem *pDescItem, *pBreakItem;

        if( SFX_ITEM_SET != pSet->GetItemState( RES_BREAK,
                                        FALSE, &pBreakItem ) )
            pBreakItem = 0;

        if( SFX_ITEM_SET != pSet->GetItemState( RES_PAGEDESC,
                                        FALSE, &pDescItem ) )
            pDescItem = 0;

        if( pDescItem || pBreakItem )
        {
            if( lcl_HasBreakAttrs( *pDstNd ))
            {
                SwPosition aPos( *pDstNd, SwIndex( pDstNd ));
                aPos.nNode--;
                pDstNd->GetDoc()->AppendTxtNode( aPos );
                if( pNewIdx )
                    *pNewIdx = aPos.nNode;

                SwCntntNode* pOldNd = pDstNd;
                pDstNd = aPos.nNode.GetNode().GetCntntNode();
                pDstNd->ChgFmtColl( pOldNd->GetFmtColl() );
                if( pDstNd->HasSwAttrSet() )
                {
                    SfxItemSet aSet( *pDstNd->GetpSwAttrSet() );
                    aSet.ClearItem( RES_BREAK );
                    aSet.ClearItem( RES_PAGEDESC );
                    pDstNd->SetAttr( aSet );
                }
            }
            if( pBreakItem )
            {
                pDstNd->SetAttr( *pBreakItem );
                pSrcNd->ResetAttr( RES_BREAK );
            }
            if( pDescItem )
            {
                pDstNd->SetAttr( *pDescItem );
                pSrcNd->ResetAttr( RES_PAGEDESC );
            }
        }
    }
}

void SwRTFParser::SetFlysInDoc()
{
    // !! von Oben abarbeiten, CntntPos ist kein Index !
    SwNodes & rNds = pDoc->GetNodes();
    typedef std::pair<SwFlyFrmFmt*, SwFmtAnchor> frameEntry;
    typedef std::deque<frameEntry> rtfframesAtIndex;
    typedef std::map<const SwNode*, rtfframesAtIndex> rtfFmtMap;
    rtfFmtMap aPrevFmts;

    SwFrmFmt* pParent = pDoc->GetFrmFmtFromPool( RES_POOLFRM_FRAME );
    for( USHORT n = 0; n < aFlyArr.Count(); ++n )
    {
        SwFlySave* pFlySave = aFlyArr[ n ];

        ASSERT( !pFlySave->nSttNd.GetNode().FindFlyStartNode(),
                "Content vom Fly steht in einem Fly" );
        ASSERT( pFlySave->nSttNd.GetIndex() <= pFlySave->nEndNd.GetIndex(),
                "Fly hat falschen Bereich" );



        //JP 21.09.98: wenn ein DropCap ist, dann Text im Node belassen, am
        //              Absatz das Absatz Attribut setzen. Ggfs noch die
        //              FontSize zuruecksetzen, damit das DropCap nicht zu
        //              gro? wird.
        if( pFlySave->nDropAnchor )
        {
            SwTxtNode* pSttNd = pFlySave->nSttNd.GetNode().GetTxtNode();
            SwTxtNode* pEndNd = pFlySave->nEndNd.GetNode().GetTxtNode();
            if( pSttNd && pEndNd &&
                pSttNd->GetIndex() + 1 == pEndNd->GetIndex()
                && pSttNd->GetTxt().Len()>0 /* #i38227# leave drop caps with no content as fly frames */ )
            {
                ULONG nPos = pSttNd->GetIndex();
                SwDoc * pDoc1 = pSttNd->GetDoc();

                BOOL bJoined;
                {
                    SwPaM aTmp( *pSttNd, pSttNd->GetTxt().Len(), *pEndNd, 0 );
                    bJoined = pDoc1->DeleteAndJoin( aTmp );
                }

                SwTxtNode * pNd = (pDoc1->GetNodes()[nPos])->GetTxtNode();

                if( bJoined && pNd != NULL)
                {
                    SwFmtDrop aDropCap;
                    aDropCap.GetLines() = (BYTE)pFlySave->nDropLines;
                    aDropCap.GetChars() = 1;

                    SwIndex aIdx( pEndNd );
                    pNd->RstAttr( aIdx, 1, RES_CHRATR_FONTSIZE );
                    pNd->SetAttr( aDropCap );
                }
                delete pFlySave;
                continue;
            }
        }

        // liegt Ende und Start vom Naechsten im gleichen Node, dann muss
        // gesplittet werden
        if( n + 1 < aFlyArr.Count() && pFlySave->nEndCnt &&
            pFlySave->nEndNd == aFlyArr[ n + 1 ]->nSttNd )
        {
            SwCntntNode* pCNd = rNds[ pFlySave->nEndNd ]->GetCntntNode();
            if( pCNd )
            {
                SwPosition aPos( pFlySave->nEndNd,
                                SwIndex( pCNd, pFlySave->nEndCnt ));
                pDoc->SplitNode( aPos, false );
                pFlySave->nEndNd--;
            }
            else
                pFlySave->nEndCnt = 0;
        }

        // verschiebe den Inhalt von diesem Anchor in den Auto-TextBereich
        // und erzeuge dadurch den richtigen SwG-Rahmen
        SwNodeRange aRg(pFlySave->nSttNd, 0, pFlySave->nEndNd, 0);
        //Make a new section, unless there is no content at all
        const bool bMakeEmptySection = aRg.aStart < aRg.aEnd || ((aRg.aStart == aRg.aEnd) && pFlySave->nEndCnt);

        {
            // Nur TextNodes koennen in Tabellen stehen !!
            const SwNode* pNd = &pFlySave->nSttNd.GetNode();
            if( pNd->IsNoTxtNode() )
            {
                // die Size muss noch korrigiert werden!
                nAktPageDesc = 0;       // Standart PageDesc
                if( SFX_ITEM_SET != pFlySave->aFlySet.GetItemState(
                    RES_FRM_SIZE, FALSE ) )
                    _SetPictureSize( *(SwNoTxtNode*)pNd, aRg.aStart,
                                    pFlySave->aFlySet );
                if( 0 != ( pNd = pNd->FindTableNode() ) )
                    pFlySave->SetFlySize( *(SwTableNode*)pNd );
            }
            else
            {
                // Take care for table nodes
                pNd = pNd->GetNodes()[ pNd->GetIndex() - 2 ]->GetTableNode();
                if( pNd ) // if the table starts imediately before aRg -> expand aRg
                    aRg.aStart = *pNd;

                if( bMakeEmptySection )
                {
                    pNd = &aRg.aEnd.GetNode();
                    ULONG nSectEnd = pNd->EndOfSectionIndex()+1;

                    if (!pNd->IsTableNode() && 0 !=(pNd = pNd->FindTableNode())
                        && (pNd->GetIndex() >= aRg.aStart.GetNode().GetIndex()) )
                    {
                        const SwNode* pTblBxNd;

                        // Ende der Tabelle ist hinter dieser Box ??
                        if( pNd->EndOfSectionIndex() == nSectEnd )
                            aRg.aEnd = nSectEnd+1;
                        // is the end in the first box of the table, then
                        // move before the table (Bug 67663)
                        // but the range must not become emtpy, i.e. aStart==aEnd
                        // because otherwise we will get a crash (126506) later on
                        else if( 0 != ( pTblBxNd = aRg.aEnd.GetNode().
                                                FindTableBoxStartNode()) &&
                                 pTblBxNd->GetIndex() - 1 == pNd->GetIndex() &&
                                 &aRg.aStart.GetNode() != pNd )
                            aRg.aEnd = *pNd;
                        else
                        {
                            // Tabelle ist noch groesser, also splitte sie hier.
                            rNds.SplitTable( aRg.aEnd, TRUE );
                            aRg.aEnd = pNd->EndOfSectionIndex() + 1;
                        }
                    }
                }
            }
        }

        // vorm verschieben muss sich der Index auf die alte Position
        // gemerkt werden, der Index wird mit verschoben !!!

        SwNodeIndex aTmpIdx( rNds.GetEndOfAutotext() );
        SwStartNode* pSttNd = bMakeEmptySection
                ? rNds.MakeEmptySection( aTmpIdx, SwFlyStartNode )
                : rNds.MakeTextSection( aTmpIdx, SwFlyStartNode,
                        (SwTxtFmtColl*)pDoc->GetDfltTxtFmtColl() );

        // das ist die Verankerungs-Position (fuers Layout!)
        pFlySave->nSttNd = aRg.aStart.GetIndex()-1;
        if( bMakeEmptySection )
        {
            // check: the move does not clear the surrounded section. If all
            // nodes moved away, then create a new TxtNode
            {
                // i76403: an empty selection is not a good idea
                if( aRg.aStart == aRg.aEnd && aRg.aStart.GetNode().GetTxtNode() )
                    aRg.aEnd++;
                SwNodeIndex aPrev( aRg.aStart, -1 );
                if( aPrev.GetNode().IsStartNode() &&
                    aPrev.GetNode().EndOfSectionNode() == &aRg.aEnd.GetNode())
                {
                    // create new txtnode, because the section does never be empty
                    pDoc->GetNodes().MakeTxtNode( aRg.aEnd,
                            (SwTxtFmtColl*)pDoc->GetDfltTxtFmtColl() );
                    aRg.aEnd--;
                }
            }
            aTmpIdx = *pSttNd->EndOfSectionNode();
            pDoc->MoveNodeRange( aRg, aTmpIdx,
                IDocumentContentOperations::DOC_MOVEDEFAULT );
        }

        // patch from cmc for #i52542#
        if (pSttNd->GetIndex() + 1 == pSttNd->EndOfSectionIndex())
        {
            ASSERT(!this, "nothing in this frame, not legal");
            delete pFlySave;
            continue;
        }

        pFlySave->aFlySet.Put( SwFmtCntnt( pSttNd ));

        CalculateFlySize( pFlySave->aFlySet, pFlySave->nSttNd,
                          pFlySave->nPageWidth );

                // THIS >>>>>
        // if the section only contains one Node and this has a
        // border or backgorund, then put it to the frame
        // Not in our own RTF-Format!
                // <<<<< DOES NOT MAKE SENSE TO ME (flr)
        // #102781#. Added support for transparent frames.
        if( pSttNd->GetIndex() + 1 != pSttNd->EndOfSectionIndex() &&
            !bSwPageDesc )
        {
            SwCntntNode* pSrcNd = pDoc->GetNodes()[ pSttNd->GetIndex() + 1 ]->GetCntntNode();
            SfxItemSet aTmpSet( pDoc->GetAttrPool(),
                                    RES_BACKGROUND, RES_BOX );
            const SvxBrushItem* pBackgroundBrush = (const SvxBrushItem*)pFlySave->aFlySet.GetItem(RES_BACKGROUND, FALSE);
            if( pSrcNd && pSrcNd->HasSwAttrSet() )
                aTmpSet.Put( *pSrcNd->GetpSwAttrSet() );
            if (pBackgroundBrush)
            {
                aTmpSet.Put(*pBackgroundBrush, RES_BACKGROUND);
            }
            else
            {
                pBackgroundBrush = (const SvxBrushItem*)aTmpSet.GetItem(RES_BACKGROUND, FALSE);
                if (pBackgroundBrush)
                {
                    Color& rBackgroundColor = const_cast<SvxBrushItem*>(pBackgroundBrush)->GetColor();
                    rBackgroundColor.SetTransparency(0xFE);
                }
                else
                {
                    Color aColor = Color(0xff, 0xff, 0xff);
                    aColor.SetTransparency( 0xFE);
                    SvxBrushItem aBrush(aColor, RES_BACKGROUND);
                    aTmpSet.Put(aBrush, RES_BACKGROUND);
                }
            }
            // #117914# Topic 6.
            pFlySave->aFlySet.Put( aTmpSet );
            if( pSrcNd && pSrcNd->HasSwAttrSet() )
            {
                pSrcNd->ResetAttr( RES_BACKGROUND, RES_BOX );
            }
        }

        SwFlyFrmFmt* pFmt = pDoc->MakeFlyFrmFmt( aEmptyStr, pParent );
        pFmt->SetFmtAttr( pFlySave->aFlySet );
        const SwFmtAnchor& rAnchor = pFmt->GetAnchor();
        if (FLY_AS_CHAR != rAnchor.GetAnchorId())
        {
            // korrigiere noch den Absatz, ist immer der vorhergehende !
            // JP 20.09.95: wenn es diesen gibt! (DocAnfang!)

            //JP 02.08.99: that is wrong. The anchor is ever the NEXT!
            //JP 05.08.99: there are an Bug in the ExportFilter which will
            //              be fixed in the Version 517 - by SWG-Export
            //              the fly will be after the paragraph - but in RTF
            //              the flys will be before the paragraph.
            if( !bSwPageDesc || 5430 < GetVersionNo() )
                pFlySave->nSttNd++;

//            if( !pFlySave->nSttNd.GetNode().IsCntntNode() )
            {
                // Seitenumbrueche in den Bodybereich verschieben!
                SwCntntNode* pSrcNd = aRg.aStart.GetNode().GetCntntNode();
                SwCntntNode* pDstNd = pFlySave->nSttNd.GetNode().GetCntntNode();
                if( !pDstNd )
                    pDstNd = pDoc->GetNodes().GoNext( &pFlySave->nSttNd );

                ::lcl_CpyBreakAttrs( pSrcNd, pDstNd, &pFlySave->nSttNd );
            }

            const SwNodeIndex aSttNd(*pSttNd);
            SwNodeIndex aEndNd(*pSttNd->EndOfSectionNode());
            aEndNd--;

            SwPosition aPos( pFlySave->nSttNd );
            SwFmtAnchor aAnchor(rAnchor);
            aAnchor.SetAnchor(&aPos);

            const SwNode *pCurrentAnchor = &(pFlySave->nSttNd.GetNode());
            aPrevFmts[pCurrentAnchor].push_back(frameEntry(pFmt, aAnchor));

            while (aEndNd > aSttNd)
            {
                typedef rtfframesAtIndex::iterator myIter;
                rtfframesAtIndex &rDeque = aPrevFmts[&(aEndNd.GetNode())];
                myIter aEnd = rDeque.end();
                for (myIter aIter = rDeque.begin(); aIter != aEnd; ++aIter)
                {
                    aIter->second.SetAnchor(&aPos);
                    // --> OD 2004-06-30 #i27767# - push on front to keep order
                    // of objects for the correct object positioning
                    //aPrevFmts[pCurrentAnchor].push_back(*aIter);
                    aPrevFmts[pCurrentAnchor].push_front(*aIter);
                }
                rDeque.clear();
                aEndNd--;
           }
        }

        // --> OD, FLR 2006-02-16 #131205#
        // Create draw contact object, which also creates a <SdrObject> instance,
        // in order to set the order number.
        // The order number is assumed to be the order of the text flow.
        SwFlyDrawContact* pContact =
                new SwFlyDrawContact( pFmt,
                                      pFmt->GetDoc()->GetOrCreateDrawModel() );
        pContact->GetMaster()->SetOrdNum( n );
        // <--

        delete pFlySave;
    }

    typedef rtfFmtMap::reverse_iterator myriter;
    myriter aEnd = aPrevFmts.rend();
    for(myriter aIter = aPrevFmts.rbegin(); aIter != aEnd; ++aIter)
    {
        rtfframesAtIndex &rDeque = aIter->second;
        typedef rtfframesAtIndex::iterator myIter;
        myIter aQEnd = rDeque.end();
        for (myIter aQIter = rDeque.begin(); aQIter != aQEnd; ++aQIter)
        {
            frameEntry &rEntry = *aQIter;
            SwFlyFrmFmt *pFrm = rEntry.first;
            SwFmtAnchor &rAnchor = rEntry.second;
            pFrm->SetFmtAttr(rAnchor);
        }
    }

    aFlyArr.Remove(0, aFlyArr.Count());
}

// clips the text box to the min or max position if it is outside our min or max boundry
long SwRTFParser::GetSafePos(long nPos)
{
    if(nPos > SHRT_MAX)
        nPos = SHRT_MAX;
    else if(nPos < SHRT_MIN)
        nPos = SHRT_MIN;

    return nPos;
}

void SwRTFParser::ReadFly( int nToken, SfxItemSet* pSet )
{
    // ein Set fuer die FrmFmt-Attribute
    SfxItemSet aSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN, RES_FRMATR_END-1 );
    if( !IsNewDoc() )
        Reader::ResetFrmFmtAttrs( aSet );

    // der Fly beginnt immer in einem neuen Absatz
    if( pPam->GetPoint()->nContent.GetIndex() )
        InsertPara();

    // RTF-Defaults setzen:
    // --> OD 2004-06-24 #i27767#
    SwFmtAnchor aAnchor( FLY_AT_PARA );

    SwFmtHoriOrient aHori( 0, text::HoriOrientation::LEFT, text::RelOrientation::FRAME );
    SwFmtVertOrient aVert( 0, text::VertOrientation::TOP, text::RelOrientation::FRAME );
    // <--
    SvxFrameDirectionItem aFrmDir( FRMDIR_HORI_LEFT_TOP, RES_FRAMEDIR );

    USHORT nCols = USHRT_MAX, nColSpace = USHRT_MAX, nAktCol = 0;
    SvUShorts aColumns;

    BOOL bChkDropCap = 0 == pSet;
    USHORT nDropCapLines = 0, nDropCapAnchor = 0;
    int nNumOpenBrakets = GetOpenBrakets();

    if( !pSet )
    {
        pSet = &aSet;
    }
    else
    {
        // die Werte aus dem uebergebenen!
        aAnchor = (SwFmtAnchor&)pSet->Get( RES_ANCHOR );
        aHori = (SwFmtHoriOrient&)pSet->Get( RES_HORI_ORIENT );
        aVert = (SwFmtVertOrient&)pSet->Get( RES_VERT_ORIENT );
    }

    // dann sammel mal alle Attribute zusammen
    int bWeiter = TRUE;
    int nAppliedProps=0;
    do {
        USHORT nVal = USHORT(nTokenValue);
        /*
        #i5263#
        Assume that a property genuinely contributes towards creating a frame,
        and if turns out to be a non contributing one reduce the count.
        */
        ++nAppliedProps;
        switch( nToken )
        {
        case RTF_ABSW:
            {
                SwFmtFrmSize aSz( ATT_MIN_SIZE, nTokenValue, 0 );
                const SfxPoolItem* pItem;
                if( SFX_ITEM_SET == pSet->GetItemState( RES_FRM_SIZE, TRUE,
                    &pItem ))
                {
                    aSz.SetHeightSizeType( ((SwFmtFrmSize*)pItem)->GetHeightSizeType() );
                    aSz.SetHeight( ((SwFmtFrmSize*)pItem)->GetHeight() );
                }
                if( MINFLY > nTokenValue )  nTokenValue = MINFLY;
                aSet.Put( aSz );
            }
            break;
        case RTF_ABSH:
            {
                SwFmtFrmSize aSz( ATT_MIN_SIZE, 0, MINFLY );
                const SfxPoolItem* pItem;
                if( SFX_ITEM_SET == pSet->GetItemState( RES_FRM_SIZE, TRUE,
                    &pItem ))
                {
                    aSz.SetWidth( ((SwFmtFrmSize*)pItem)->GetWidth() );
                }

                if( 0 > nTokenValue )
                {
                    nTokenValue = -nTokenValue;
                    aSz.SetHeightSizeType( ATT_FIX_SIZE );
                }
                if( MINFLY > nTokenValue )  nTokenValue = MINFLY;
                aSz.SetHeight( nTokenValue );
                aSet.Put( aSz );
            }
            break;

        case RTF_NOWRAP:
            {
                pSet->Put( SwFmtSurround( SURROUND_NONE ));
            }
            break;
        case RTF_DXFRTEXT:
                {
                    SvxULSpaceItem aUL( RES_UL_SPACE );
                    SvxLRSpaceItem aLR( RES_LR_SPACE );
                    aUL.SetUpper( nVal );   aUL.SetLower( nVal );
                    aLR.SetLeft( nVal );    aLR.SetRight( nVal );
                    pSet->Put( aUL );
                    pSet->Put( aLR );
                }
                break;

        case RTF_DFRMTXTX:
                {
                    SvxLRSpaceItem aLR( RES_LR_SPACE );
                    aLR.SetLeft( nVal );    aLR.SetRight( nVal );
                    pSet->Put( aLR );
                }
                break;
        case RTF_DFRMTXTY:
                {
                    SvxULSpaceItem aUL( RES_UL_SPACE );
                    aUL.SetUpper( nVal );   aUL.SetLower( nVal );
                    pSet->Put( aUL );
                }
                break;

        case RTF_POSNEGX:
        case RTF_POSX:      aHori.SetHoriOrient( text::HoriOrientation::NONE );
                            aHori.SetPos( GetSafePos((long)nTokenValue) );
                            break;
        case RTF_POSXC:     aHori.SetHoriOrient( text::HoriOrientation::CENTER );     break;
        case RTF_POSXI:     aHori.SetHoriOrient( text::HoriOrientation::LEFT );
                            aHori.SetPosToggle( TRUE );
                            break;
        case RTF_POSXO:     aHori.SetHoriOrient( text::HoriOrientation::RIGHT );
                            aHori.SetPosToggle( TRUE );
                            break;
        case RTF_POSXL:     aHori.SetHoriOrient( text::HoriOrientation::LEFT );       break;
        case RTF_POSXR:     aHori.SetHoriOrient( text::HoriOrientation::RIGHT );      break;

        case RTF_POSNEGY:
        case RTF_POSY:      aVert.SetVertOrient( text::VertOrientation::NONE );
                            aVert.SetPos( GetSafePos((long)nTokenValue) );
                            break;
        case RTF_POSYT:     aVert.SetVertOrient( text::VertOrientation::TOP );    break;
        case RTF_POSYB:     aVert.SetVertOrient( text::VertOrientation::BOTTOM ); break;
        case RTF_POSYC:     aVert.SetVertOrient( text::VertOrientation::CENTER ); break;

        case RTF_PHMRG:     aHori.SetRelationOrient( text::RelOrientation::PAGE_PRINT_AREA ); break;
        case RTF_PVMRG:     aVert.SetRelationOrient( text::RelOrientation::PAGE_PRINT_AREA ); break;
        case RTF_PHPG:      aHori.SetRelationOrient( text::RelOrientation::PAGE_FRAME ); break;
        case RTF_PVPG:      aVert.SetRelationOrient( text::RelOrientation::PAGE_FRAME );break;
        case RTF_PHCOL:     aHori.SetRelationOrient( text::RelOrientation::FRAME ); break;
        case RTF_PVPARA:    aVert.SetRelationOrient( text::RelOrientation::FRAME ); break;

        case RTF_POSYIL:
            break;
        case RTF_ABSLOCK:
            /*
            #i5263#
            Not sufficient to make a frame at least word won't do it with just
            an abslock
            */
            --nAppliedProps;
            break;
        case RTF_FRMTXLRTB:
            aFrmDir.SetValue( FRMDIR_HORI_LEFT_TOP );
            break;
        case RTF_FRMTXTBRL:
            aFrmDir.SetValue( FRMDIR_HORI_RIGHT_TOP );
            break;
        case RTF_FRMTXLRTBV:
            aFrmDir.SetValue( FRMDIR_VERT_TOP_LEFT );
            break;
        case RTF_FRMTXTBRLV:
            aFrmDir.SetValue( FRMDIR_VERT_TOP_RIGHT );
            break;

        case RTF_DROPCAPLI:                         // Dropcaps !!
                if( bChkDropCap )
                {
                    nDropCapLines = USHORT( nTokenValue );
                    if( !nDropCapAnchor )
                        nDropCapAnchor = 1;
                }
                break;
        case RTF_DROPCAPT:
                if( bChkDropCap )
                {
                    nDropCapAnchor = USHORT( nTokenValue );
                    if( !nDropCapLines )
                        nDropCapLines = 3;
                }
                break;


        // fuer die "alten" Writer - haben die Spaltigkeit falsch heraus-
        // geschrieben
        case RTF_COLS:          nCols = USHORT( nTokenValue );      break;
        case RTF_COLSX:         nColSpace = USHORT( nTokenValue );  break;
        case RTF_COLNO:
            nAktCol = USHORT( nTokenValue );
            if( RTF_COLW == GetNextToken() )
            {
                USHORT nWidth = USHORT( nTokenValue ), nSpace = 0;
                if( RTF_COLSR == GetNextToken() )
                    nSpace = USHORT( nTokenValue );
                else
                    SkipToken( -1 );        // wieder zurueck

                if( --nAktCol == ( aColumns.Count() / 2 ) )
                {
                    aColumns.Insert( nWidth + nSpace, aColumns.Count() );
                    aColumns.Insert( nSpace, aColumns.Count() );
                }
            }
            break;

        case '{':
            {
                short nSkip = 0;
                if( RTF_IGNOREFLAG != ( nToken = GetNextToken() ))
                {
                    if( RTF_SHADINGDEF == (nToken & ~0xff) )
                    {
                        ReadBackgroundAttr( nToken, aSet );
                        GetNextToken();     // Klammer ueberlesen
                    }
                    else
                        nSkip = -1;
                }
                else if( RTF_APOCTL ==
                    ((nToken = GetNextToken() ) & ~(0xff | RTF_SWGDEFS)) )
                {
                    bReadSwFly = true;      // alles kommt in den akt. Fly
                    SvxLRSpaceItem aLR( RES_LR_SPACE );
                    SvxULSpaceItem aUL( RES_UL_SPACE );
                    nCols = USHRT_MAX;      // neu aufsetzen
                    nColSpace = USHRT_MAX;
                    do {
                    nVal = USHORT(nTokenValue);
                    switch( nToken )
                    {
                    // Swg-Frame-Tokens
                    case RTF_FLYPRINT:
                        {
                            pSet->Put( SvxPrintItem( RES_PRINT, FALSE ));
                        }
                        break;
                    case RTF_FLYOPAQUE:
                        {
                            pSet->Put( SvxOpaqueItem( RES_OPAQUE, FALSE ));
                        }
                        break;

                    case RTF_FLYPRTCTD:
                        {
                            RTFProtect aP( (BYTE)nTokenValue );
                            SvxProtectItem aProtectItem( RES_PROTECT );
                            aProtectItem.SetCntntProtect( aP.GetCntnt() );
                            aProtectItem.SetSizeProtect( aP.GetSize() );
                            aProtectItem.SetPosProtect( aP.GetPos() );
                            pSet->Put( aProtectItem );
                        }
                        break;

                    case RTF_FLYMAINCNT:
                        {
                            RTFSurround aMC( (BYTE)nTokenValue );
                            SwFmtSurround aSurr( (SwSurround)aMC.GetOrder());
                            if( aMC.GetGoldCut() )
                                aSurr.SetSurround( SURROUND_IDEAL );
                            pSet->Put( aSurr );
                        }
                        break;
                    case RTF_FLYVERT:
                        {
                            RTFVertOrient aVO( nVal );
                            aVert.SetVertOrient( aVO.GetOrient() );
                            aVert.SetRelationOrient( aVO.GetRelation() );
                        }
                        break;
                    case RTF_FLYHORZ:
                        {
                            RTFHoriOrient aHO( nVal );
                            aHori.SetHoriOrient( aHO.GetOrient() );
                            aHori.SetRelationOrient( aHO.GetRelation() );
                        }
                        break;
                    case RTF_FLYOUTLEFT:        aLR.SetLeft( nVal );        break;
                    case RTF_FLYOUTRIGHT:       aLR.SetRight( nVal );       break;
                    case RTF_FLYOUTUPPER:       aUL.SetUpper( nVal );       break;
                    case RTF_FLYOUTLOWER:       aUL.SetLower( nVal );       break;
                    case RTF_FLYANCHOR:
                            switch( GetNextToken() )
                            {
                            case RTF_FLY_PAGE:
                                aAnchor.SetType( FLY_AT_PAGE );
                                aAnchor.SetPageNum( USHORT(nTokenValue));
                                aAnchor.SetAnchor( 0 );
                                break;

                            case RTF_FLY_CNTNT:
                                {
                                    SwNodeIndex aIdx( pPam->GetPoint()->nNode );
                                    pDoc->GetNodes().GoPrevious( &aIdx );
                                    SwPosition aPos( aIdx );
                                    aAnchor.SetType( FLY_AT_PARA );
                                    aAnchor.SetAnchor( &aPos );
                                }
                                break;

// JP 26.09.94: die Bindung an die Spalte gibt es nicht mehr !!
//                          case RTF_FLY_COLUMN:
                            }
                            break;
                    case RTF_COLS:  nCols = USHORT( nTokenValue );      break;
                    case RTF_COLSX: nColSpace = USHORT( nTokenValue );  break;
                    case RTF_COLNO:
                        nAktCol = USHORT( nTokenValue );
                        if( RTF_COLW == GetNextToken() )
                        {
                            USHORT nWidth = USHORT( nTokenValue ), nSpace = 0;
                            if( RTF_COLSR == GetNextToken() )
                                nSpace = USHORT( nTokenValue );
                            else
                                SkipToken( -1 );        // wieder zurueck

                            if( --nAktCol == ( aColumns.Count() / 2 ) )
                            {
                                aColumns.Insert( nWidth + nSpace, aColumns.Count() );
                                aColumns.Insert( nSpace, aColumns.Count() );
                            }
                        }
                        break;

                    case '{':
                        if( RTF_BRDBOX == ( nToken = GetNextToken() ) )
                            ReadBorderAttr( nToken, aSet );
                        else if( RTF_SHADINGDEF == (nToken & ~0xff ) )
                            ReadBackgroundAttr( nToken, aSet );
                        else if( RTF_IGNOREFLAG == nToken )
                        {
                            int bSkipGrp = TRUE;
                            switch( nToken = GetNextToken() )
                            {
                            case RTF_SHADOW:
                            case RTF_BRDBOX:
                                ReadAttr( SkipToken( -2 ), &aSet );
                                bSkipGrp = FALSE;
                                break;

                            case RTF_BRDRT:
                            case RTF_BRDRB:
                            case RTF_BRDRR:
                            case RTF_BRDRL:
                                bSkipGrp = FALSE;
                                ReadBorderAttr( SkipToken( -2 ), aSet );
                                break;
                            }

                                // keine weitere Klammer mehr ueberlesen!!!
                            if( !bSkipGrp )
                                break;

                            SkipGroup();
                        }
                        else
                            SkipGroup();
                        GetNextToken();     // Klammer ueberlesen
                        break;
                    }
                    } while( IsParserWorking() &&
                                '}' != ( nToken = GetNextToken() ));

                    if( aUL.GetUpper() || aUL.GetLower() )
                        pSet->Put( aUL );
                    if( aLR.GetLeft() || aLR.GetRight() )
                        pSet->Put( aLR );
                }
                else if( RTF_BRDBOX == nToken )
                    ReadBorderAttr( nToken, aSet );
                else if( RTF_SHADOW == nToken )
                    ReadAttr( SkipToken( -2 ), &aSet );
                else if( RTF_SHADINGDEF == (nToken & ~0xff ) )
                    ReadBackgroundAttr( nToken, aSet );
                else if( RTF_UNKNOWNCONTROL == nToken )
                    SkipGroup();
                else
                    nSkip = -2;

                if( nSkip )
                {
                    nToken = SkipToken( nSkip );
                    bWeiter = FALSE;
                }
            }
            break;

        default:
            --nAppliedProps; //Not sufficient to make a frame
            bWeiter = FALSE;
        }

        if( bWeiter )
            nToken = GetNextToken();
    } while( bWeiter && IsParserWorking() );

    pSet->Put( aAnchor );
    pSet->Put( aHori );
    pSet->Put( aVert );

    // --> OD 2004-06-30 #i27767# - set wrapping style influence
    // --> OD 2004-10-18 #i35017# - constant name has changed
    pSet->Put( SwFmtWrapInfluenceOnObjPos(
                    text::WrapInfluenceOnPosition::ONCE_SUCCESSIVE ));
    // <--

    SwFmtFollowTextFlow aFollowTextFlow( FALSE );
    pSet->Put( aFollowTextFlow );

    if( !( aFrmDir == pSet->Get( RES_FRAMEDIR )) )
        pSet->Put( aFrmDir );

    if( nCols && USHRT_MAX != nCols )
    {
        SwFmtCol aCol;
        if( USHRT_MAX == nColSpace )
            nColSpace = 720;

        ULONG nWidth = USHRT_MAX;
        aCol.Init( nCols, nColSpace, USHORT( nWidth ) );
        if( nCols == ( aColumns.Count() / 2 ) )
        {
            for( USHORT n = 0, i = 0; n < aColumns.Count(); n += 2, ++i )
            {
                SwColumn* pCol = aCol.GetColumns()[ i ];
                ULONG nTmp = aColumns[ n ];
                nTmp *= USHRT_MAX;
                nTmp /= nWidth;
                pCol->SetWishWidth( USHORT(nTmp) );
/*
    JP 07.07.95: der Dialog kennt nur eine Breite fuer alle Spalten
                 darum hier nicht weiter beachten
                nTmp = aColumns[ n+1 ];
                if( nTmp )
                    pCol->SetRight( USHORT(nTmp) );
                else
                    pCol->SetRight( 0 );
                pCol->SetLeft( 0 );
*/
            }
        }
        pSet->Put( aCol );
    }

    if( pSet != &aSet )         // wurde der Set uebergeben, dann wars das
        return ;

    // ein neues FlyFormat anlegen oder das alte benutzen ?
    // (teste ob es die selben Attribute besitzt!)
    SwFlySave* pFlySave = 0;
    USHORT nFlyArrCnt = aFlyArr.Count();
    /*
    #i5263#
    There were not enough frame properties found to actually justify creating
    an absolutely positioned frame.
    */
    if (nAppliedProps)
    {
        if( !nFlyArrCnt ||
            !( pFlySave = aFlyArr[ nFlyArrCnt-1 ])->IsEqualFly( *pPam, aSet ))
        {
            pFlySave = new SwFlySave( *pPam, aSet );
            Size aPgSize;
            GetPageSize( aPgSize );
            pFlySave->nPageWidth = aPgSize.Width();

            if( nDropCapAnchor )
            {
                pFlySave->nDropAnchor = nDropCapAnchor;
                pFlySave->nDropLines = nDropCapLines;
            }
            if (nFlyArrCnt >0){
                SwFlySave* pFlySavePrev = aFlyArr[nFlyArrCnt-1];
                if (pFlySave->nSttNd.GetIndex() < pFlySavePrev->nEndNd.GetIndex())
                {
                     pFlySavePrev->nEndNd=pFlySave->nSttNd;
                }
            }
            aFlyArr.Insert(  pFlySave, nFlyArrCnt++ );
            // --> OD 2008-12-22 #i83368# - reset
            mbReadCellWhileReadSwFly = false;
            // <--
        }
    }

    SetPardTokenRead( FALSE );
    const SwTableNode* pTblNd = pPam->GetNode()->FindTableNode();

    while( !IsPardTokenRead() && IsParserWorking() )
    {
        if( RTF_PARD == nToken || nNumOpenBrakets > GetOpenBrakets() )
            break;

        NextToken( nToken );

        if( !IsPardTokenRead() )
        {
            // #102781#. Added support for transparent frames.
            if (nToken == RTF_CBPAT && nFlyArrCnt > 0)
            {
                USHORT _index=USHORT(nTokenValue);
                const Color& rColor = GetColor(_index);
                SvxBrushItem aBrush(rColor, RES_BACKGROUND);
                SwFlySave* pFS = aFlyArr[nFlyArrCnt-1];
                pFS->aFlySet.Put(aBrush, RES_BACKGROUND);
            }

            nToken = GetNextToken();

            // BUG 22036: kommt zwischen Fly-Attributen ein unbekanntes,
            //              dann erzeuge nie 2 FlyFrames, sondern fasse
            //              beide zusammen !!!
            while( RTF_APOCTL == ( nToken & ~(0xff | RTF_SWGDEFS) ))
            {
                if( RTF_FLY_INPARA == nToken )
                    break;

                if( RTF_IGNOREFLAG == SkipToken( -1 ) )
                {
                    if( '{' == SkipToken( -1 ) )
                        nToken = '{';
                    else
                        SkipToken( 2 );
                }
                else
                    SkipToken( 1 );

                ReadFly( nToken, pFlySave ? &pFlySave->aFlySet : 0);
                nToken = GetNextToken();
            }
        }
    }

    /*
    #i5263#
    There were enough frame properties found to actually justify creating
    an absolutely positioned frame.
    */
    if (!nAppliedProps)
    {
        bReadSwFly = false;
        SkipToken( -1 );
        return;
    }

    if( pTblNd && !pPam->GetPoint()->nContent.GetIndex() &&
        pTblNd->EndOfSectionIndex() + 1 ==
            pPam->GetPoint()->nNode.GetIndex() )
    {
        // nicht mehr in der Tabelle, sondern dahinter ?
        // Dann aber wieder zurueck in die Tabelle
        pPam->Move( fnMoveBackward );
    }
    else
        pTblNd = 0;

    // wurde garnichts eingefuegt?
    if( !pTblNd &&
        pPam->GetPoint()->nNode == pFlySave->nSttNd &&
        !pPam->GetPoint()->nContent.GetIndex() )
    {
//      // dann erzeuge mindestens einen leeren TextNode
//      pDoc->AppendTxtNode(*pPam);
        // dann zerstoere den FlySave wieder.
        aFlyArr.DeleteAndDestroy( --nFlyArrCnt );

    }
    else
    {
        BOOL bMovePaM = 0 != pTblNd;

        pFlySave->nEndNd = pPam->GetPoint()->nNode;
        pFlySave->nEndCnt = pPam->GetPoint()->nContent.GetIndex();

        if( bMovePaM )
            pPam->Move( fnMoveForward );

        pTblNd = pFlySave->nSttNd.GetNode().FindTableNode();
        if( pTblNd && !pFlySave->nEndCnt &&
            pTblNd == pFlySave->nEndNd.GetNode().FindTableNode() )
        {
            // dann teste mal, ob das \pard nicht zu spaet kam und
            // eigentlich in die vorherige Zelle gehoert
            const SwStartNode* pSttBoxNd = pFlySave->nSttNd.GetNode().
                                            FindTableBoxStartNode(),
                            * pEndBoxNd = pFlySave->nEndNd.GetNode().
                                            FindTableBoxStartNode();
            if( pSttBoxNd && pEndBoxNd &&
                bMovePaM ? ( pSttBoxNd == pEndBoxNd )
                         : ( pSttBoxNd->EndOfSectionIndex() + 1 ==
                                pEndBoxNd->GetIndex() &&
                                pEndBoxNd->GetIndex() + 1 ==
                                pFlySave->nEndNd.GetIndex() ))
            {
                // dann gehoert das Ende in die vorherige Box!
                SwPosition aPos( *pPam->GetPoint() );
                pPam->GetPoint()->nNode = *pSttBoxNd->EndOfSectionNode();
                pPam->Move( fnMoveBackward, fnGoNode );

                DelLastNode();

                pPam->GetPoint()->nNode = *pSttBoxNd->EndOfSectionNode();
                pPam->Move( fnMoveBackward, fnGoNode );

                pFlySave->nEndNd = pPam->GetPoint()->nNode;
                pFlySave->nEndCnt = pPam->GetPoint()->nContent.GetIndex();

                *pPam->GetPoint() = aPos;
            }
        }
        else if( !bReadSwFly && !pFlySave->nEndCnt &&
            pFlySave->nSttNd.GetIndex() + 1 == pFlySave->nEndNd.GetIndex() &&
            pFlySave->nSttNd.GetNode().IsTxtNode() )
        {

            SwTxtNode* pTxtNd = pFlySave->nSttNd.GetNode().GetTxtNode();
            SwTxtFlyCnt* pFlyCnt = 0;
            if( 1 == pTxtNd->GetTxt().Len() &&
                0 != (pFlyCnt = static_cast<SwTxtFlyCnt*>(
                        pTxtNd->GetTxtAttrForCharAt(0, RES_TXTATR_FLYCNT))) &&
                pFlyCnt->GetFlyCnt().GetFrmFmt() )
            {
                // then move the content into the surrounded fly
                SwFrmFmt* pFlyFmt = pFlyCnt->GetFlyCnt().GetFrmFmt();
                const SwNodeIndex* pFlySNd = pFlyFmt->GetCntnt().GetCntntIdx();
                SwNodeRange aRg( *pFlySNd, 1,
                                 *pFlySNd->GetNode().EndOfSectionNode(), 0 );

                // merge the itemsets
                SwFmtFrmSize aSz1( (SwFmtFrmSize&)pFlyFmt->GetAttrSet().
                                                Get( RES_FRM_SIZE ));
                SwFmtFrmSize aSz2( (SwFmtFrmSize&)pFlySave->aFlySet.
                                                Get( RES_FRM_SIZE ));
                // if
                if( !aRg.aStart.GetNode().IsNoTxtNode() ||
                    !aSz1.GetHeight() || !aSz1.GetWidth() ||
                    !aSz2.GetHeight() || !aSz2.GetWidth() ||
                    ( aSz1.GetHeight() == aSz2.GetHeight() &&
                      aSz1.GetWidth() == aSz2.GetWidth() ) )
                {
                    SfxItemSet aDiffs( pFlyFmt->GetAttrSet() );
                    aDiffs.ClearItem( RES_ANCHOR );
                    aDiffs.ClearItem( RES_FRM_SIZE );
                    aDiffs.ClearItem( RES_CNTNT );
                    aDiffs.Differentiate( pFlySave->aFlySet );
                    pFlySave->aFlySet.Put( aDiffs );

                    BOOL bSet = FALSE;
                    if( aSz1.GetHeight() && !aSz2.GetHeight() )
                    {
                        bSet = TRUE;
                        aSz2.SetHeight( aSz1.GetHeight() );
                    }
                    if( aSz1.GetWidth() && !aSz2.GetWidth() )
                    {
                        bSet = TRUE;
                        aSz2.SetWidth( aSz1.GetWidth() );
                    }
                    if( bSet )
                        pFlySave->aFlySet.Put( aSz2 );

                    // move any PageBreak/Desc Attr to the next Para
                    {
                        SwCntntNode* pSrcNd = pFlySave->nSttNd.GetNode().GetCntntNode();
                        SwCntntNode* pDstNd = pFlySave->nEndNd.GetNode().GetCntntNode();

                        ::lcl_CpyBreakAttrs( pSrcNd, pDstNd, &pFlySave->nEndNd );
                    }

                    // create new txtnode, because the section does never be empty
                    pDoc->GetNodes().MakeTxtNode( aRg.aStart,
                                (SwTxtFmtColl*)pDoc->GetDfltTxtFmtColl() );

                    SwNodeIndex aTmp( pFlySave->nSttNd, +1 );
                    pDoc->MoveNodeRange( aRg, aTmp,
                            IDocumentContentOperations::DOC_MOVEDEFAULT );

                    // now delete the redundant txtnode
                    pDoc->GetNodes().Delete( pFlySave->nSttNd, 1 );
                }
            }
        }
    }

    bReadSwFly = false;
    SkipToken( -1 );
}


void SwRTFParser::InsPicture( const String& rGrfNm, const Graphic* pGrf,
                                const SvxRTFPictureType* pPicType )
{
    // kennzeichen fuer Swg-Dokumente:
    // (dann ist das FlyFmt fuer die Grafik!)
    SwGrfNode * pGrfNd;
    // --> OD 2008-12-22 #i83368#
    // Assure that graphic node is enclosed by fly frame node.
//    if( bReadSwFly )
    if ( bReadSwFly && !mbReadCellWhileReadSwFly )
    // <--
    {
        // erzeuge nur einen normalen GrafikNode und ersetze diesen gegen
        // den vorhandenen Textnode
        SwNodeIndex& rIdx = pPam->GetPoint()->nNode;
        pGrfNd = pDoc->GetNodes().MakeGrfNode( rIdx,
                    rGrfNm, aEmptyStr,    // Name der Graphic !!
                    pGrf,
                    (SwGrfFmtColl*)pDoc->GetDfltGrfFmtColl() );

        if( pGrfAttrSet )
            pGrfNd->SetAttr( *pGrfAttrSet );

        SwFlySave* pFlySave = aFlyArr[ aFlyArr.Count()-1 ];
        pFlySave->nSttNd = rIdx.GetIndex() - 1;

        if( 1 < aFlyArr.Count() )
        {
            pFlySave = aFlyArr[ aFlyArr.Count() - 2 ];
            if( pFlySave->nEndNd == rIdx )
                pFlySave->nEndNd = rIdx.GetIndex() - 1;
        }
    }
    else
    {
        // wenn normale RTF-Grafik, dann steht diese im Textfluss !
        SwAttrSet aFlySet( pDoc->GetAttrPool(), RES_OPAQUE, /*RES_OPAQUE,
                                                RES_VERT_ORIENT,*/ RES_ANCHOR );
        const SwPosition* pPos = pPam->GetPoint();

        SwFmtAnchor aAnchor( FLY_AS_CHAR );
        aAnchor.SetAnchor( pPos );
        aFlySet.Put( aAnchor );
        aFlySet.Put( SwFmtVertOrient( 0, text::VertOrientation::TOP ));

        if (pDoc->IsInHeaderFooter(pPos->nNode))
        {
            SvxOpaqueItem aOpaqueItem(RES_OPAQUE, FALSE);
            SwFmtSurround aSurroundItem(SURROUND_THROUGHT);
            aFlySet.Put(aOpaqueItem);
            aFlySet.Put(aSurroundItem);
        }

        SwFrmFmt* pFlyFmt = pDoc->Insert( *pPam,
                    rGrfNm, aEmptyStr,      // Name der Graphic !!
                    pGrf,
                    &aFlySet,               // Attribute fuer den FlyFrm
                    pGrfAttrSet, NULL );            // Attribute fuer die Grafik

        pGrfNd = pDoc->GetNodes()[ pFlyFmt->GetCntnt().GetCntntIdx()->
                                            GetIndex()+1 ]->GetGrfNode();

        _SetPictureSize( *pGrfNd, pPos->nNode,
                        (SfxItemSet&)pFlyFmt->GetAttrSet(),
                        pPicType );
    }

    if( pGrfAttrSet )
        DELETEZ( pGrfAttrSet );
}

void SwRTFParser::_SetPictureSize( const SwNoTxtNode& rNd,
                                    const SwNodeIndex& rAnchor,
                                    SfxItemSet& rSet,
                                    const SvxRTFPictureType* pPicType )
{
    Size aSize( ((SwNoTxtNode&)rNd).GetTwipSize() );
    if( pPicType )
    {
        if( rNd.IsGrfNode() )
        {
            if( SvxRTFPictureType::WIN_METAFILE != pPicType->eStyle &&
                pPicType->nGoalWidth && pPicType->nGoalHeight )
            {
                aSize.Width() = pPicType->nGoalWidth;
                aSize.Height() =pPicType->nGoalHeight;
            }
            else if( SvxRTFPictureType::MAC_QUICKDRAW == pPicType->eStyle )
            {
                // IMMER auf 72 DPI bezogen, also 1pt == 20 Twip !!
                aSize.Width() = pPicType->nWidth * 20;
                aSize.Height() = pPicType->nHeight * 20;
            }
            else
            {
                // von 100TH_MM nach TWIP umrechenen!
                // #117879# when \picwgoal resp \pichgoal are present, then use them.
                //          The values of \picwgoal and \picwgoal are already given in twips.
                aSize.Width() = (pPicType->nGoalWidth?pPicType->nGoalWidth:(pPicType->nWidth*144)/254);
                aSize.Height() = (pPicType->nGoalHeight?pPicType->nGoalHeight:(pPicType->nHeight*144)/254);
            }
            ((SwGrfNode&)rNd).SetTwipSize( aSize );
        }

        if( 100 != pPicType->nScalX )
            aSize.Width() = (((long)pPicType->nScalX) * ( aSize.Width() -
                        ( pPicType->nCropL + pPicType->nCropR ))) / 100L;

        if( 100 != pPicType->nScalY )
            aSize.Height() = (((long)pPicType->nScalY) * ( aSize.Height() -
                        ( pPicType->nCropT + pPicType->nCropB ))) / 100L;
    }

    //steht der Fly etwa in einer Tabelle ?
    const SwNode* pAnchorNd = pDoc->GetNodes()[ rAnchor ];
    const SwTableNode* pTblNd = pAnchorNd->FindTableNode();
    if( pTblNd )
    {
        // Box feststellen:
        const SwTableBox* pBox = pTblNd->GetTable().GetTblBox(
                                pAnchorNd->StartOfSectionIndex() );
        if( pBox )
        {
            long nBoxWidth = pBox->GetFrmFmt()->GetFrmSize().GetWidth();
            if( aSize.Width() > nBoxWidth )
                aSize.Width() = nBoxWidth;
        }
    }

    //JP 8.11.2001: bug 94450 - if no size exist, then the size is set by
    //              the swapin of the graphic.
    SwGrfNode* pGrfNd;
    if( !aSize.Width() && !aSize.Height() &&
        0 != (pGrfNd = (SwGrfNode*)rNd.GetGrfNode() ) && pGrfNd->IsGrfLink() )
        pGrfNd->SetChgTwipSize( TRUE );

        // min. Werte einhalten !!
    if( aSize.Width() < MINFLY )
        aSize.Width() = MINFLY;
    if( aSize.Height() < MINFLY)
        aSize.Height() = MINFLY;

    if( pPicType )
    {
        BOOL bChg = FALSE;
        SwCropGrf aCrop;

/*
 JP 28.07.99: Bug 67800 - no crop by MAC_QUICKDRAW. At time i dont know why
                            it has been coded. But this has used for any
                            RTF-File, but i dont found them.
        if( SvxRTFPictureType::MAC_QUICKDRAW == pPicType->eStyle )
        {
            // evt. ein wenig Croppen ??
            // IMMER auf 72 DPI bezogen, also 1pt == 20 Twip !!
            long nTmp = pPicType->nWidth * 20;
            if( nTmp != aSize.Width() )
            {
                // in der Breite (also rechts) croppen
                aCrop.Right() = nTmp - aSize.Width();
                aSize.Width() = nTmp;
                bChg = TRUE;
            }

            nTmp = pPicType->nHeight * 20;
            if( nTmp != aSize.Height() )
            {
                // in der Hoehe (also unten) croppen
                aCrop.Bottom() = nTmp - aSize.Height();
                aSize.Height() = nTmp;
                bChg = TRUE;
            }
        }
*/
        if( pPicType->nCropT )
        {
            aCrop.SetTop( pPicType->nCropT );
            bChg = TRUE;
        }
        if( pPicType->nCropB )
        {
            aCrop.SetBottom( pPicType->nCropB );
            bChg = TRUE;
        }
        if( pPicType->nCropL )
        {
            aCrop.SetLeft( pPicType->nCropL );
            bChg = TRUE;
        }
        if( pPicType->nCropR )
        {
            aCrop.SetRight( pPicType->nCropR );
            bChg = TRUE;
        }

        if( bChg )
        {
            // dann mal an die CropWerte an die GrafikSize anpassen.
            ((SwNoTxtNode&)rNd).SetAttr( aCrop );
        }
    }
    rSet.Put( SwFmtFrmSize( ATT_FIX_SIZE, aSize.Width(), aSize.Height() ));
}

void SwRTFParser::GetPageSize( Size& rSize )
{
    ASSERT(!maSegments.empty(), "not possible");
    if (maSegments.empty())
    {
        rSize.Width() = 12240 - 1800 - 1800;
        rSize.Height() = 15840 - 1440 - 1440;
        return;
    }

    const rtfSection &rSect = maSegments.back();

    rSize.Width() = rSect.maPageInfo.mnPgwsxn - rSect.maPageInfo.mnMarglsxn - rSect.maPageInfo.mnMargrsxn;
    rSize.Height() = rSect.maPageInfo.mnPghsxn - rSect.maPageInfo.mnMargtsxn - rSect.maPageInfo.mnMargbsxn;

    long nCols = rSect.NoCols();
    if (1 < nCols)
    {
        rSize.Width() /= nCols;
        rSize.Height() /= nCols;
    }
}

void SwRTFParser::ReadBitmapData()
{
    Graphic aGrf;
    SvxRTFPictureType aPicType;
    if( ReadBmpData( aGrf, aPicType ) )
        InsPicture( aEmptyStr, &aGrf, &aPicType );
}

#ifdef READ_OLE_OBJECT
void SwRTFParser::ReadOLEData()
{
    SvCacheStream aTmpFile( 0xA000 );
    Graphic aGrf;
    SvxRTFPictureType aPicType, aOleType;

    int nToken, bValidOle = TRUE, bWeiter = TRUE;
    int nOpenBrakets = 1;       // die erste wurde schon vorher erkannt !!

    String* pStr = 0;
    String sObjClass, sObjName, sObjData;

    while( nOpenBrakets && IsParserWorking() && bWeiter && bValidOle )
    {
        nToken = GetNextToken();
        USHORT nVal = USHORT( nTokenValue );
        switch( nToken )
        {
        case '}':       --nOpenBrakets; pStr = 0; break;
        case '{':
            {
                if( RTF_IGNOREFLAG != GetNextToken() )
                    nToken = SkipToken( -1 );
                else if( RTF_UNKNOWNCONTROL != GetNextToken() )
                    nToken = SkipToken( -2 );
                else
                {
                    // gleich herausfiltern
                    ReadUnknownData();
                    nToken = GetNextToken();
                    if( '}' != nToken )
                        eState = SVPAR_ERROR;
                    break;
                }
                ++nOpenBrakets;
            }
            break;

        case RTF_OBJECT:
        case RTF_OBJEMB:        // default ist embedded
        case RTF_LINKSELF:      // ??
        case RTF_OBJLOCK:       // ??
        case RTF_OBJUPDATE:     // ??
        case RTF_OBJTIME:       // ??
        case RTF_OBJSETSIZE:
        case RTF_OBJALIGN:
        case RTF_OBJTRANSY:
        case RTF_OBJATTPH:
            break;

        case RTF_OBJLINK:       // ?? welche Daten sind das ??
        case RTF_OBJAUTLINK:    // ??       -""-            ??
        case RTF_OBJSUB:
        case RTF_OBJPUB:
        case RTF_OBJICEMB:
        case RTF_OBJOCX:
        case RTF_OBJHTML:
        case RTF_OBJALIAS:
        case RTF_OBJSECT:
            bValidOle = FALSE;      // diese Typen koennen wir nicht
            break;

        case RTF_OBJCLASS:
            // Daten lesen
            pStr = &sObjClass;
            break;

        case RTF_OBJNAME:
            // Daten lesen
            pStr = &sObjName;
            break;

        case RTF_OBJDATA:
            pStr = &sObjData;
            break;

        case RTF_RESULT:
            {
                // hier weitermachen, wenn das OLE-Object ungueltig ist
                bWeiter = FALSE;
            }
            break;
        case RTF_RSLTBMP:           // diese sollten wir ignorieren
        case RTF_RSLTMERGE:
        case RTF_RSLTPICT:
        case RTF_RSLTRTF:
        case RTF_RSLTTXT:
            break;

        case RTF_OBJW:          aOleType.nWidth = nVal; break;
        case RTF_OBJH:          aOleType.nHeight = nVal; break;
        case RTF_OBJCROPT:      aOleType.nCropT = (short)nTokenValue; break;
        case RTF_OBJCROPB:      aOleType.nCropB = (short)nTokenValue; break;
        case RTF_OBJCROPL:      aOleType.nCropL = (short)nTokenValue; break;
        case RTF_OBJCROPR:      aOleType.nCropR = (short)nTokenValue; break;
        case RTF_OBJSCALEX:     aOleType.nScalX = nVal; break;
        case RTF_OBJSCALEY:     aOleType.nScalY = nVal; break;

        case RTF_TEXTTOKEN:
            if( 1 < nOpenBrakets && pStr )
            {
                if( pStr == &sObjData )
                {
                    xub_StrLen nHexLen = HexToBin( aToken );
                    if( STRING_NOTFOUND != nHexLen )
                        bValidOle = FALSE;
                    else
                    {
                        aTmpFile.Write( (sal_Char*)aToken.GetBuffer(), nHexLen );
                        bValidOle = 0 == aTmpFile.GetError();
                    }
                }
                else
                    *pStr += aToken;
            }
            break;
        }
    }

    if( bValidOle )
    {
        bValidOle = FALSE;      // erstmal
    }

    if( !bWeiter )      // dann stehen wir noch im Result
    {
        // ist das Ole-Object Ok?
        // -> dann solange SkipGroup rufen, bis zur letzten
        //      schliessenden Klammer
        // ansonsten alle Token verarbeiten, bis zur letzten
        //      schliessenden Klammer

        bWeiter = TRUE;
        while( nOpenBrakets && IsParserWorking() && bWeiter )
        {
            switch( nToken = GetNextToken() )
            {
            case '}':       --nOpenBrakets; break;
            case '{':       ++nOpenBrakets;  break;
            }
            if( nOpenBrakets && !bValidOle )
                NextToken( nToken );
        }
    }

    if( !bValidOle && '}' != nToken )
        SkipGroup();

    SkipToken( -1 );        // die schliesende Klammer wird "oben" ausgewertet
}
#endif

/* vi:set tabstop=4 shiftwidth=4 expandtab: */

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */