summaryrefslogtreecommitdiff
path: root/sw/source/core/doc/doctxm.cxx
blob: a117df7d1c147f1d04cb4ec63b67ea51b60010db (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
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: doctxm.cxx,v $
 * $Revision: 1.53 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

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


#include <limits.h>
#include <hintids.hxx>

#define _SVSTDARR_STRINGSSORT
#include <svtools/svstdarr.hxx>
#include <svx/langitem.hxx>
#include <svx/brkitem.hxx>
#include <svx/tstpitem.hxx>
#include <svx/lrspitem.hxx>
#include <sot/clsids.hxx>
#include <docsh.hxx>
#include <ndole.hxx>
#include <txttxmrk.hxx>
#include <fmtinfmt.hxx>
#include <fmtpdsc.hxx>
#include <frmfmt.hxx>
#include <fmtfsize.hxx>
#include <frmatr.hxx>
#include <pagedesc.hxx>
#include <doc.hxx>
#include <pagefrm.hxx>
#include <ndtxt.hxx>
#include <swtable.hxx>
#include <doctxm.hxx>
#include <txmsrt.hxx>
#include <rolbck.hxx>
#include <poolfmt.hxx>
#include <txtfrm.hxx>
#include <rootfrm.hxx>
#include <undobj.hxx>
#include <swundo.hxx>
#include <mdiexp.hxx>
#include <docary.hxx>
#include <charfmt.hxx>
#include <fchrfmt.hxx>
#include <fldbas.hxx>
#include <fmtfld.hxx>
#include <txtfld.hxx>
#include <expfld.hxx>
#include <chpfld.hxx>
#include <mvsave.hxx>
#include <node2lay.hxx>
#include <SwStyleNameMapper.hxx>
#include <breakit.hxx>
#include <editsh.hxx>
#include <scriptinfo.hxx>

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

const sal_Unicode cNumRepl		= '@';
const sal_Unicode cEndPageNum 	= '~';
const sal_Char __FAR_DATA sPageDeli[] = ", ";

SV_IMPL_PTRARR(SwTOXSortTabBases, SwTOXSortTabBasePtr)

TYPEINIT2( SwTOXBaseSection, SwTOXBase, SwSection );	// fuers RTTI

struct LinkStruct
{
    SwFmtINetFmt	aINetFmt;
    xub_StrLen nStartTextPos, nEndTextPos;

    LinkStruct( const String& rURL, xub_StrLen nStart, xub_StrLen nEnd )
        : aINetFmt( rURL, aEmptyStr),
        nStartTextPos( nStart),
        nEndTextPos(nEnd) {}
};

typedef LinkStruct* LinkStructPtr;
SV_DECL_PTRARR(LinkStructArr, LinkStructPtr, 0, 5 )
SV_IMPL_PTRARR(LinkStructArr, LinkStructPtr)

USHORT SwDoc::GetTOIKeys( SwTOIKeyType eTyp, SvStringsSort& rArr ) const
{
    if( rArr.Count() )
        rArr.Remove( USHORT(0), rArr.Count() );

    // dann mal ueber den Pool und alle Primary oder Secondary heraussuchen
    const SwTxtTOXMark* pMark;
    const SfxPoolItem* pItem;
    const SwTOXType* pTOXType;
    USHORT i, nMaxItems = GetAttrPool().GetItemCount( RES_TXTATR_TOXMARK );
    for( i = 0; i < nMaxItems; ++i )
        if( 0 != (pItem = GetAttrPool().GetItem( RES_TXTATR_TOXMARK, i ) ) &&
            0!= ( pTOXType = ((SwTOXMark*)pItem)->GetTOXType()) &&
            TOX_INDEX == pTOXType->GetType() &&
            0 != ( pMark = ((SwTOXMark*)pItem)->GetTxtTOXMark() ) &&
            pMark->GetpTxtNd() &&
            pMark->GetpTxtNd()->GetNodes().IsDocNodes() )
        {
            const String* pStr;
            if( TOI_PRIMARY == eTyp )
                pStr = &((SwTOXMark*)pItem)->GetPrimaryKey();
            else
                pStr = &((SwTOXMark*)pItem)->GetSecondaryKey();

            if( pStr->Len() )
                rArr.Insert( (StringPtr)pStr );
        }

    return rArr.Count();
}

/*--------------------------------------------------------------------
     Beschreibung: aktuelle Verzeichnismarkierungen ermitteln
 --------------------------------------------------------------------*/


USHORT SwDoc::GetCurTOXMark( const SwPosition& rPos,
                                SwTOXMarks& rArr ) const
{
    // suche an der Position rPos nach allen SwTOXMark's
    SwTxtNode* pTxtNd = GetNodes()[ rPos.nNode ]->GetTxtNode();
    // kein TextNode oder kein HintsArray vorhanden ??
    if( !pTxtNd || !pTxtNd->GetpSwpHints() )
        return 0;

    const SwpHints & rHts = *pTxtNd->GetpSwpHints();
    const SwTxtAttr* pHt;
    xub_StrLen nSttIdx;
    const xub_StrLen *pEndIdx;

    xub_StrLen nAktPos = rPos.nContent.GetIndex();

    for( USHORT n = 0; n < rHts.Count(); ++n )
    {
        if( RES_TXTATR_TOXMARK != (pHt = rHts[n])->Which() )
            continue;
        if( ( nSttIdx = *pHt->GetStart() ) < nAktPos )
        {
            // pruefe Ende mit ab
            if( 0 == ( pEndIdx = pHt->GetEnd() ) ||
                *pEndIdx <= nAktPos )
                continue;		// weiter suchen
        }
        else if( nSttIdx > nAktPos )
            // ist Start vom Hint groesser als rPos, dann abbrechen. Denn
            // die Attribute sind nach Start sortiert !
            break;

        const SwTOXMark* pTMark = &pHt->GetTOXMark();
        rArr.Insert( pTMark, rArr.Count() );
    }
    return rArr.Count();
}

/*--------------------------------------------------------------------
     Beschreibung: Marke loeschen
 --------------------------------------------------------------------*/

void SwDoc::DeleteTOXMark( const SwTOXMark* pTOXMark )
{
    // hole den TextNode und
    const SwTxtTOXMark* pTxtTOXMark = pTOXMark->GetTxtTOXMark();
    ASSERT( pTxtTOXMark, "Kein TxtTOXMark, kann nicht geloescht werden" );

    SwTxtNode& rTxtNd = const_cast<SwTxtNode&>(pTxtTOXMark->GetTxtNode());
    ASSERT( rTxtNd.GetpSwpHints(), "kann nicht geloescht werden" );

    if( DoesUndo() )
    {
        // fuers Undo die Attribute sichern
        ClearRedo();
        SwUndoResetAttr* pUndo = new SwUndoResetAttr(
            SwPosition( rTxtNd, SwIndex( &rTxtNd, *pTxtTOXMark->GetStart() ) ),
            RES_TXTATR_TOXMARK );
        AppendUndo( pUndo );

        SwRegHistory aRHst( rTxtNd, &pUndo->GetHistory() );
        rTxtNd.GetpSwpHints()->Register( &aRHst );
    }

    rTxtNd.DeleteAttribute( const_cast<SwTxtTOXMark*>(pTxtTOXMark) );

    if ( DoesUndo() )
    {
        if( rTxtNd.GetpSwpHints() )
            rTxtNd.GetpSwpHints()->DeRegister();
    }
    SetModified();
}

/*--------------------------------------------------------------------
     Beschreibung: Traveln zwischen TOXMarks
 --------------------------------------------------------------------*/

class CompareNodeCntnt
{
    ULONG nNode;
    xub_StrLen nCntnt;
public:
    CompareNodeCntnt( ULONG nNd, xub_StrLen nCnt )
        : nNode( nNd ), nCntnt( nCnt ) {}

    int operator==( const CompareNodeCntnt& rCmp )
        { return nNode == rCmp.nNode && nCntnt == rCmp.nCntnt; }
    int operator!=( const CompareNodeCntnt& rCmp )
        { return nNode != rCmp.nNode || nCntnt != rCmp.nCntnt; }
    int operator< ( const CompareNodeCntnt& rCmp )
        { return nNode < rCmp.nNode ||
            ( nNode == rCmp.nNode && nCntnt < rCmp.nCntnt); }
    int operator<=( const CompareNodeCntnt& rCmp )
        { return nNode < rCmp.nNode ||
            ( nNode == rCmp.nNode && nCntnt <= rCmp.nCntnt); }
    int operator> ( const CompareNodeCntnt& rCmp )
        { return nNode > rCmp.nNode ||
            ( nNode == rCmp.nNode && nCntnt > rCmp.nCntnt); }
    int operator>=( const CompareNodeCntnt& rCmp )
        { return nNode > rCmp.nNode ||
            ( nNode == rCmp.nNode && nCntnt >= rCmp.nCntnt); }
};

const SwTOXMark& SwDoc::GotoTOXMark( const SwTOXMark& rCurTOXMark,
                                    SwTOXSearch eDir, BOOL bInReadOnly )
{
    const SwTxtTOXMark* pMark = rCurTOXMark.GetTxtTOXMark();
    ASSERT(pMark, "pMark==0 Ungueltige TxtTOXMark");

    const SwTxtNode *pTOXSrc = pMark->GetpTxtNd();

    CompareNodeCntnt aAbsIdx( pTOXSrc->GetIndex(), *pMark->GetStart() );
    CompareNodeCntnt aPrevPos( 0, 0 );
    CompareNodeCntnt aNextPos( ULONG_MAX, STRING_NOTFOUND );
    CompareNodeCntnt aMax( 0, 0 );
    CompareNodeCntnt aMin( ULONG_MAX, STRING_NOTFOUND );

    const SwTOXMark*	pNew	= 0;
    const SwTOXMark*	pMax	= &rCurTOXMark;
    const SwTOXMark*	pMin	= &rCurTOXMark;

    const SwModify* pType = rCurTOXMark.GetRegisteredIn();
    SwClientIter	aIter( *(SwModify*)pType );

    const SwTOXMark* pTOXMark;
    const SwCntntFrm* pCFrm;
    Point aPt;
    for( pTOXMark = (SwTOXMark*)aIter.First( TYPE( SwTOXMark )); pTOXMark;
         pTOXMark = (SwTOXMark*)aIter.Next() )
    {
        if( pTOXMark != &rCurTOXMark &&
            0 != ( pMark = pTOXMark->GetTxtTOXMark()) &&
            0 != ( pTOXSrc = pMark->GetpTxtNd() ) &&
            0 != ( pCFrm = pTOXSrc->GetFrm( &aPt, 0, FALSE )) &&
            ( bInReadOnly || !pCFrm->IsProtected() ))
        {
            CompareNodeCntnt aAbsNew( pTOXSrc->GetIndex(), *pMark->GetStart() );
            switch( eDir )
            {
                //Die untenstehenden etwas komplizierter ausgefallen Ausdruecke
                //dienen dazu auch ueber Eintraege auf der selben (!) Position
                //traveln zu koennen. Wenn einer Zeit hat mag er sie mal
                //optimieren.

            case TOX_SAME_PRV:
                if( pTOXMark->GetText() != rCurTOXMark.GetText() )
                    break;
                /* no break here */
            case TOX_PRV:
                if ( (aAbsNew < aAbsIdx && aAbsNew > aPrevPos &&
                      aPrevPos != aAbsIdx && aAbsNew != aAbsIdx ) ||
                     (aAbsIdx == aAbsNew &&
                      (ULONG(&rCurTOXMark) > ULONG(pTOXMark) &&
                       (!pNew ||
                        (pNew && (aPrevPos < aAbsIdx ||
                                  ULONG(pNew) < ULONG(pTOXMark)))))) ||
                     (aPrevPos == aAbsNew && aAbsIdx != aAbsNew &&
                      ULONG(pTOXMark) > ULONG(pNew)) )
                {
                    pNew = pTOXMark;
                    aPrevPos = aAbsNew;
                    if ( aAbsNew >= aMax )
                    {
                        aMax = aAbsNew;
                        pMax = pTOXMark;
                    }
                }
                break;

            case TOX_SAME_NXT:
                if( pTOXMark->GetText() != rCurTOXMark.GetText() )
                    break;
                /* no break here */
            case TOX_NXT:
                if ( (aAbsNew > aAbsIdx && aAbsNew < aNextPos &&
                      aNextPos != aAbsIdx && aAbsNew != aAbsIdx ) ||
                     (aAbsIdx == aAbsNew &&
                      (ULONG(&rCurTOXMark) < ULONG(pTOXMark) &&
                       (!pNew ||
                        (pNew && (aNextPos > aAbsIdx ||
                                  ULONG(pNew) > ULONG(pTOXMark)))))) ||
                     (aNextPos == aAbsNew && aAbsIdx != aAbsNew &&
                      ULONG(pTOXMark) < ULONG(pNew)) )
                {
                    pNew = pTOXMark;
                    aNextPos = aAbsNew;
                    if ( aAbsNew <= aMin )
                    {
                        aMin = aAbsNew;
                        pMin = pTOXMark;
                    }
                }
                break;
            }
        }
    }


    // kein Nachfolger wurde gefunden
    // Min oder Max benutzen
    if(!pNew)
    {
        switch(eDir)
        {
        case TOX_PRV:
        case TOX_SAME_PRV:
            pNew = pMax;
            break;
        case TOX_NXT:
        case TOX_SAME_NXT:
            pNew = pMin;
            break;
        default:
            pNew = &rCurTOXMark;
        }
    }
    return *pNew;
}

/*  */

const SwTOXBaseSection* SwDoc::InsertTableOf( const SwPosition& rPos,
                                                const SwTOXBase& rTOX,
                                                const SfxItemSet* pSet,
                                                BOOL bExpand )
{
    StartUndo( UNDO_INSTOX, NULL );

    SwTOXBaseSection* pNew = new SwTOXBaseSection( rTOX );
    String sSectNm( rTOX.GetTOXName() );
    sSectNm = GetUniqueTOXBaseName( *rTOX.GetTOXType(), &sSectNm );
    pNew->SetTOXName(sSectNm);
    pNew->SwSection::SetName(sSectNm);
    SwPaM aPam( rPos );
    SwSection* pSect = InsertSwSection( aPam, *pNew, pSet, false );
    if( pSect )
    {
        SwSectionNode* pSectNd = pSect->GetFmt()->GetSectionNode();
        SwSection* pCl = pNew;
        pSect->GetFmt()->Add( pCl );
        pSectNd->SetNewSection( pNew );

        if( bExpand )
        {
            // OD 19.03.2003 #106329# - add value for 2nd parameter = true to
            // indicate, that a creation of a new table of content has to be performed.
            // Value of 1st parameter = default value.
            pNew->Update( 0, true );
        }
        else if( 1 == rTOX.GetTitle().Len() && IsInReading() )
        // insert title of TOX
        {
            // then insert the headline section
            SwNodeIndex aIdx( *pSectNd, +1 );

            SwTxtNode* pHeadNd = GetNodes().MakeTxtNode( aIdx,
                            GetTxtCollFromPool( RES_POOLCOLL_STANDARD ) );

            String sNm( pNew->GetTOXName() );
// ??Resource
sNm.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "_Head" ));

            SwSection aSect( TOX_HEADER_SECTION, sNm );

            SwNodeIndex aStt( *pHeadNd ); aIdx--;
            SwSectionFmt* pSectFmt = MakeSectionFmt( 0 );
            GetNodes().InsertSection( aStt, *pSectFmt, aSect, &aIdx,
                                                TRUE, FALSE );
        }
    }
    else
        delete pNew, pNew = 0;

    EndUndo( UNDO_INSTOX, NULL );

    return pNew;
}



const SwTOXBaseSection* SwDoc::InsertTableOf( ULONG nSttNd, ULONG nEndNd,
                                                const SwTOXBase& rTOX,
                                                const SfxItemSet* pSet )
{
    // check for recursiv TOX
    SwNode* pNd = GetNodes()[ nSttNd ];
    SwSectionNode* pSectNd = pNd->FindSectionNode();
    while( pSectNd )
    {
        SectionType eT = pSectNd->GetSection().GetType();
        if( TOX_HEADER_SECTION == eT || TOX_CONTENT_SECTION == eT )
            return 0;
        pSectNd = pSectNd->StartOfSectionNode()->FindSectionNode();
    }

    // create SectionNode around the Nodes
    SwTOXBaseSection* pNew = new SwTOXBaseSection( rTOX );

    String sSectNm( rTOX.GetTOXName() );
    sSectNm = GetUniqueTOXBaseName(*rTOX.GetTOXType(), &sSectNm);
    pNew->SetTOXName(sSectNm);
    pNew->SwSection::SetName(sSectNm);

    SwNodeIndex aStt( GetNodes(), nSttNd ), aEnd( GetNodes(), nEndNd );
    SwSectionFmt* pFmt = MakeSectionFmt( 0 );
    if(pSet)
        pFmt->SetFmtAttr(*pSet);

//	--aEnd;		// im InsertSection ist Ende inclusive

    pSectNd = GetNodes().InsertSection( aStt, *pFmt, *pNew, &aEnd );
    if( pSectNd )
    {
        SwSection* pCl = pNew;
        pFmt->Add( pCl );
        pSectNd->SetNewSection( pNew );
    }
    else
    {
        delete pNew, pNew = 0;
        DelSectionFmt( pFmt );
    }

    return pNew;
}

/*--------------------------------------------------------------------
     Beschreibung: Aktuelles Verzeichnis ermitteln
 --------------------------------------------------------------------*/

const SwTOXBase* SwDoc::GetCurTOX( const SwPosition& rPos ) const
{
    const SwNode& rNd = rPos.nNode.GetNode();
    const SwSectionNode* pSectNd = rNd.FindSectionNode();
    while( pSectNd )
    {
        SectionType eT = pSectNd->GetSection().GetType();
        if( TOX_CONTENT_SECTION == eT )
        {
            ASSERT( pSectNd->GetSection().ISA( SwTOXBaseSection ),
                    "keine TOXBaseSection!" );
            SwTOXBaseSection& rTOXSect = (SwTOXBaseSection&)
                                                pSectNd->GetSection();
            return &rTOXSect;
        }
        pSectNd = pSectNd->StartOfSectionNode()->FindSectionNode();
    }
    return 0;
}
/* -----------------01.09.99 16:01-------------------

 --------------------------------------------------*/
const SwAttrSet& SwDoc::GetTOXBaseAttrSet(const SwTOXBase& rTOXBase) const
{
    ASSERT( rTOXBase.ISA( SwTOXBaseSection ), "no TOXBaseSection!" );
    const SwTOXBaseSection& rTOXSect = (const SwTOXBaseSection&)rTOXBase;
    SwSectionFmt* pFmt = rTOXSect.GetFmt();
    ASSERT( pFmt, "invalid TOXBaseSection!" );
    return pFmt->GetAttrSet();
}
/* -----------------02.09.99 07:48-------------------

 --------------------------------------------------*/
const SwTOXBase* SwDoc::GetDefaultTOXBase( TOXTypes eTyp, BOOL bCreate )
{
    SwTOXBase** prBase = 0;
    switch(eTyp)
    {
    case  TOX_CONTENT: 			prBase = &pDefTOXBases->pContBase; break;
    case  TOX_INDEX:            prBase = &pDefTOXBases->pIdxBase;  break;
    case  TOX_USER:             prBase = &pDefTOXBases->pUserBase; break;
    case  TOX_TABLES:           prBase = &pDefTOXBases->pTblBase;  break;
    case  TOX_OBJECTS:          prBase = &pDefTOXBases->pObjBase;  break;
    case  TOX_ILLUSTRATIONS:    prBase = &pDefTOXBases->pIllBase;  break;
    case  TOX_AUTHORITIES:		prBase = &pDefTOXBases->pAuthBase; break;
    }
    if(!(*prBase) && bCreate)
    {
        SwForm aForm(eTyp);
        const SwTOXType* pType = GetTOXType(eTyp, 0);
        (*prBase) = new SwTOXBase(pType, aForm, 0, pType->GetTypeName());
    }
    return (*prBase);
}
/* -----------------02.09.99 08:06-------------------

 --------------------------------------------------*/
void	SwDoc::SetDefaultTOXBase(const SwTOXBase& rBase)
{
    SwTOXBase** prBase = 0;
    switch(rBase.GetType())
    {
    case  TOX_CONTENT: 			prBase = &pDefTOXBases->pContBase; break;
    case  TOX_INDEX:            prBase = &pDefTOXBases->pIdxBase;  break;
    case  TOX_USER:             prBase = &pDefTOXBases->pUserBase; break;
    case  TOX_TABLES:           prBase = &pDefTOXBases->pTblBase;  break;
    case  TOX_OBJECTS:          prBase = &pDefTOXBases->pObjBase;  break;
    case  TOX_ILLUSTRATIONS:    prBase = &pDefTOXBases->pIllBase;  break;
    case  TOX_AUTHORITIES:		prBase = &pDefTOXBases->pAuthBase; break;
    }
    if(*prBase)
        delete (*prBase);
    (*prBase) = new SwTOXBase(rBase);
}

/*--------------------------------------------------------------------
     Beschreibung: Verzeichnis loeschen
 --------------------------------------------------------------------*/


BOOL SwDoc::DeleteTOX( const SwTOXBase& rTOXBase, BOOL bDelNodes )
{
    // its only delete the TOX, not the nodes
    BOOL bRet = FALSE;
    ASSERT( rTOXBase.ISA( SwTOXBaseSection ), "keine TOXBaseSection!" );

    const SwTOXBaseSection& rTOXSect = (const SwTOXBaseSection&)rTOXBase;
    SwSectionFmt* pFmt = rTOXSect.GetFmt();
    if( pFmt )
    {
        StartUndo( UNDO_CLEARTOXRANGE, NULL );

        /* Save the start node of the TOX' section. */
        SwSectionNode * pMyNode = pFmt->GetSectionNode();
        /* Save start node of section's surrounding. */
        SwNode * pStartNd = pMyNode->StartOfSectionNode();

        /* Look for point where to move the cursors in the area to
           delete to. This is done by first searching forward from the
           end of the TOX' section. If no content node is found behind
           the TOX one is searched before it. If this is not
           successfull, too, insert new text node behind the end of
           the TOX' section. The cursors from the TOX' section will be
           moved to the content node found or the new text node. */

        /* Set PaM to end of TOX' section and search following content node.

           aSearchPam will contain the point where to move the cursors
           to. */
        SwPaM aSearchPam(*pMyNode->EndOfSectionNode());
        SwPosition aEndPos(*pStartNd->EndOfSectionNode(), 0);
        if (! aSearchPam.Move() /* no content node found */
            || *aSearchPam.GetPoint() >= aEndPos /* content node found
                                                    outside surrounding */
            )
        {
            /* Set PaM to beginning of TOX' section and search previous
               content node */
            SwPaM aTmpPam(*pMyNode);
            aSearchPam = aTmpPam;
            SwPosition aStartPos(*pStartNd, 0);

            if ( ! aSearchPam.Move(fnMoveBackward) /* no content node found */
                 || *aSearchPam.GetPoint() <= aStartPos  /* content node
                                                            found outside
                                                            surrounding */
                 )
            {
                /* There is no content node in the surrounding of
                   TOX'. Append text node behind TOX' section. */

                SwPosition aInsPos(*pMyNode->EndOfSectionNode(), 0);
                AppendTxtNode(aInsPos);

                SwPaM aTmpPam1(aInsPos);
                aSearchPam = aTmpPam1;
            }
        }


        /* PaM containing the TOX. */
        SwPaM aPam(*pMyNode->EndOfSectionNode(), *pMyNode);

        /* Move cursors contained in TOX to point determined above. */
        PaMCorrAbs(aPam, *aSearchPam.GetPoint());

        if( !bDelNodes )
        {
            SwSections aArr( 0, 4 );
            USHORT nCnt = pFmt->GetChildSections( aArr, SORTSECT_NOT, FALSE );
            for( USHORT n = 0; n < nCnt; ++n )
            {
                SwSection* pSect = aArr[ n ];
                if( TOX_HEADER_SECTION == pSect->GetType() )
                {
                    DelSectionFmt( pSect->GetFmt(), bDelNodes );
                }
            }
        }

        DelSectionFmt( pFmt, bDelNodes );

        EndUndo( UNDO_CLEARTOXRANGE, NULL );
        bRet = TRUE;
    }

    return bRet;
}

/*--------------------------------------------------------------------
     Beschreibung:	Verzeichnistypen verwalten
 --------------------------------------------------------------------*/

USHORT SwDoc::GetTOXTypeCount(TOXTypes eTyp) const
{
    const SwTOXTypePtr * ppTTypes = pTOXTypes->GetData();
    USHORT nCnt = 0;
    for( USHORT n = 0; n < pTOXTypes->Count(); ++n, ++ppTTypes )
        if( eTyp == (*ppTTypes)->GetType() )
            ++nCnt;
    return nCnt;
}
/*--------------------------------------------------------------------

 --------------------------------------------------------------------*/
const SwTOXType* SwDoc::GetTOXType( TOXTypes eTyp, USHORT nId ) const
{
    const SwTOXTypePtr * ppTTypes = pTOXTypes->GetData();
    USHORT nCnt = 0;
    for( USHORT n = 0; n < pTOXTypes->Count(); ++n, ++ppTTypes )
        if( eTyp == (*ppTTypes)->GetType() && nCnt++ == nId )
            return (*ppTTypes);
    return 0;
}

/*--------------------------------------------------------------------

 --------------------------------------------------------------------*/
const SwTOXType* SwDoc::InsertTOXType( const SwTOXType& rTyp )
{
    SwTOXType * pNew = new SwTOXType( rTyp );
    pTOXTypes->Insert( pNew, pTOXTypes->Count() );
    return pNew;
}
/*--------------------------------------------------------------------

 --------------------------------------------------------------------*/
String SwDoc::GetUniqueTOXBaseName( const SwTOXType& rType,
                                    const String* pChkStr ) const
{
    USHORT n;
    const SwSectionNode* pSectNd;
    const SwSection* pSect;

    if(pChkStr && !pChkStr->Len())
        pChkStr = 0;
    String aName( rType.GetTypeName() );
    xub_StrLen nNmLen = aName.Len();

    USHORT nNum = 0;
    USHORT nTmp = 0;
    USHORT nFlagSize = ( pSectionFmtTbl->Count() / 8 ) +2;
    BYTE* pSetFlags = new BYTE[ nFlagSize ];
    memset( pSetFlags, 0, nFlagSize );

    for( n = 0; n < pSectionFmtTbl->Count(); ++n )
        if( 0 != ( pSectNd = (*pSectionFmtTbl)[ n ]->GetSectionNode( FALSE ) )&&
             TOX_CONTENT_SECTION == (pSect = &pSectNd->GetSection())->GetType())
        {
            const String& rNm = pSect->GetName();
            if( rNm.Match( aName ) == nNmLen )
            {
                // Nummer bestimmen und das Flag setzen
                nNum = (USHORT)rNm.Copy( nNmLen ).ToInt32();
                if( nNum-- && nNum < pSectionFmtTbl->Count() )
                    pSetFlags[ nNum / 8 ] |= (0x01 << ( nNum & 0x07 ));
            }
            if( pChkStr && pChkStr->Equals( rNm ) )
                pChkStr = 0;
        }

    if( !pChkStr )
    {
        // alle Nummern entsprechend geflag, also bestimme die richtige Nummer
        nNum = pSectionFmtTbl->Count();
        for( n = 0; n < nFlagSize; ++n )
            if( 0xff != ( nTmp = pSetFlags[ n ] ))
            {
                // also die Nummer bestimmen
                nNum = n * 8;
                while( nTmp & 1 )
                    ++nNum, nTmp >>= 1;
                break;
            }
    }
    delete [] pSetFlags;
    if( pChkStr )
        return *pChkStr;
    return aName += String::CreateFromInt32( ++nNum );
}

/*--------------------------------------------------------------------

 --------------------------------------------------------------------*/
BOOL SwDoc::SetTOXBaseName(const SwTOXBase& rTOXBase, const String& rName)
{
    ASSERT( rTOXBase.ISA( SwTOXBaseSection ),
                    "keine TOXBaseSection!" );
    SwTOXBaseSection* pTOX = (SwTOXBaseSection*)&rTOXBase;

    String sTmp = GetUniqueTOXBaseName(*rTOXBase.GetTOXType(), &rName);
    BOOL bRet = sTmp == rName;
    if(bRet)
    {
        pTOX->SetTOXName(rName);
        pTOX->SwTOXBaseSection::SetName(rName);
        SetModified();
    }
    return bRet;
}

/*  */

const SwTxtNode* lcl_FindChapterNode( const SwNode& rNd, BYTE nLvl = 0 )
{
    const SwNode* pNd = &rNd;
    if( pNd->GetNodes().GetEndOfExtras().GetIndex() > pNd->GetIndex() )
    {
        // then find the "Anchor" (Body) position
        Point aPt;
        SwNode2Layout aNode2Layout( *pNd, pNd->GetIndex() );
        const SwFrm* pFrm = aNode2Layout.GetFrm( &aPt, 0, FALSE );

        if( pFrm )
        {
            SwPosition aPos( *pNd );
            pNd = GetBodyTxtNode( *pNd->GetDoc(), aPos, *pFrm );
            ASSERT( pNd,	"wo steht der Absatz" );
        }
    }
    return pNd ? pNd->FindOutlineNodeOfLevel( nLvl ) : 0;
}


/*--------------------------------------------------------------------
     Beschreibung: Verzeichnis-Klasse
 --------------------------------------------------------------------*/

SwTOXBaseSection::SwTOXBaseSection( const SwTOXBase& rBase )
    : SwTOXBase( rBase ), SwSection( TOX_CONTENT_SECTION, aEmptyStr )
{
    SetProtect( rBase.IsProtected() );
    SwSection::SetName( GetTOXName() );
}


SwTOXBaseSection::~SwTOXBaseSection()
{
}


BOOL SwTOXBaseSection::SetPosAtStartEnd( SwPosition& rPos, BOOL bAtStart ) const
{
    BOOL bRet = FALSE;
    const SwSectionNode* pSectNd = GetFmt()->GetSectionNode();
    if( pSectNd )
    {
        SwCntntNode* pCNd;
        xub_StrLen nC = 0;
        if( bAtStart )
        {
            rPos.nNode = *pSectNd;
            pCNd = pSectNd->GetDoc()->GetNodes().GoNext( &rPos.nNode );
        }
        else
        {
            rPos.nNode = *pSectNd->EndOfSectionNode();
            pCNd = pSectNd->GetDoc()->GetNodes().GoPrevious( &rPos.nNode );
            if( pCNd ) nC = pCNd->Len();
        }
        rPos.nContent.Assign( pCNd, nC );
        bRet = TRUE;
    }
    return bRet;
}

/*--------------------------------------------------------------------
     Beschreibung: Verzeichnisinhalt zusammensammeln
 --------------------------------------------------------------------*/

void SwTOXBaseSection::Update(const SfxItemSet* pAttr,
                              const bool        _bNewTOX )
{
    const SwSectionNode* pSectNd;
    if( !SwTOXBase::GetRegisteredIn()->GetDepends() ||
        !GetFmt() || 0 == (pSectNd = GetFmt()->GetSectionNode() ) ||
        !pSectNd->GetNodes().IsDocNodes() ||
        IsHiddenFlag() )
        return;

    SwDoc* pDoc = (SwDoc*)pSectNd->GetDoc();

    DBG_ASSERT(pDoc != NULL, "Where is the document?");

    if(pAttr && pDoc && GetFmt())
        pDoc->ChgFmt(*GetFmt(), *pAttr);

    // OD 18.03.2003 #106329# - determine default page description, which
    // will be used by the content nodes, if no approriate one is found.
    const SwPageDesc* pDefaultPageDesc;
    {
        pDefaultPageDesc =
            pSectNd->GetSection().GetFmt()->GetPageDesc().GetPageDesc();
        if ( !_bNewTOX && !pDefaultPageDesc )
        {
            // determine page description of table-of-content
            sal_uInt32 nPgDescNdIdx = pSectNd->GetIndex() + 1;
            sal_uInt32* pPgDescNdIdx = &nPgDescNdIdx;
            pDefaultPageDesc = pSectNd->FindPageDesc( FALSE, pPgDescNdIdx );
            if ( nPgDescNdIdx < pSectNd->GetIndex() )
            {
                pDefaultPageDesc = 0;
            }
        }
        // OD 28.04.2003 #109166# - consider end node of content section in the
        // node array.
        if ( !pDefaultPageDesc &&
             ( pSectNd->EndOfSectionNode()->GetIndex() <
                 (pSectNd->GetNodes().GetEndOfContent().GetIndex() - 1) )
           )
        {
            // determine page description of content after table-of-content
            SwNodeIndex aIdx( *(pSectNd->EndOfSectionNode()) );
            const SwCntntNode* pNdAfterTOX = pSectNd->GetNodes().GoNext( &aIdx );
            const SwAttrSet& aNdAttrSet = pNdAfterTOX->GetSwAttrSet();
            const SvxBreak eBreak = aNdAttrSet.GetBreak().GetBreak();
            if ( !( eBreak == SVX_BREAK_PAGE_BEFORE ||
                    eBreak == SVX_BREAK_PAGE_BOTH )
               )
            {
                pDefaultPageDesc = pNdAfterTOX->FindPageDesc( FALSE );
            }
        }
        // OD 28.04.2003 #109166# - consider start node of content section in
        // the node array.
        if ( !pDefaultPageDesc &&
             ( pSectNd->GetIndex() >
                 (pSectNd->GetNodes().GetEndOfContent().StartOfSectionIndex() + 1) )
           )
        {
            // determine page description of content before table-of-content
            SwNodeIndex aIdx( *pSectNd );
            pDefaultPageDesc =
                pSectNd->GetNodes().GoPrevious( &aIdx )->FindPageDesc( FALSE );

        }
        if ( !pDefaultPageDesc )
        {
            // determine default page description
            pDefaultPageDesc =
                &const_cast<const SwDoc *>(pDoc)->GetPageDesc( 0 );
        }
    }

    pDoc->SetModified();

    // get current Language
    SwTOXInternational aIntl(  GetLanguage(),
                               TOX_INDEX == GetTOXType()->GetType() ?
                               GetOptions() : 0,
                               GetSortAlgorithm() );

    aSortArr.DeleteAndDestroy( 0, aSortArr.Count() );

    // find the first layout node for this TOX, if it only find the content
    // in his own chapter
    const SwTxtNode* pOwnChapterNode = IsFromChapter()
            ? ::lcl_FindChapterNode( *pSectNd, 0 )
            : 0;

    SwNode2Layout aN2L( *pSectNd );
    ((SwSectionNode*)pSectNd)->DelFrms();

    // remove old content an insert one empty textnode (to hold the layout!)
    SwTxtNode* pFirstEmptyNd;
    {
        pDoc->DeleteRedline( *pSectNd, true, USHRT_MAX );

        SwNodeIndex aSttIdx( *pSectNd, +1 );
        SwNodeIndex aEndIdx( *pSectNd->EndOfSectionNode() );
        pFirstEmptyNd = pDoc->GetNodes().MakeTxtNode( aEndIdx,
                        pDoc->GetTxtCollFromPool( RES_POOLCOLL_TEXT ) );

        {
            // Task 70995 - save and restore PageDesc and Break Attributes
            SwNodeIndex aNxtIdx( aSttIdx );
            const SwCntntNode* pCNd = aNxtIdx.GetNode().GetCntntNode();
            if( !pCNd )
                pCNd = pDoc->GetNodes().GoNext( &aNxtIdx );
            if( pCNd->HasSwAttrSet() )
            {
                SfxItemSet aBrkSet( pDoc->GetAttrPool(), aBreakSetRange );
                aBrkSet.Put( *pCNd->GetpSwAttrSet() );
                if( aBrkSet.Count() )
                    pFirstEmptyNd->SetAttr( aBrkSet );
            }
        }
        aEndIdx--;
        SwPosition aPos( aEndIdx, SwIndex( pFirstEmptyNd, 0 ));
        pDoc->CorrAbs( aSttIdx, aEndIdx, aPos, TRUE );

        // delete all before
        DelFlyInRange( aSttIdx, aEndIdx );
        _DelBookmarks( aSttIdx, aEndIdx );

        pDoc->GetNodes().Delete( aSttIdx, aEndIdx.GetIndex() - aSttIdx.GetIndex() );

    }

    //
    // insert title of TOX
    if( GetTitle().Len() )
    {
        // then insert the headline section
        SwNodeIndex aIdx( *pSectNd, +1 );

        SwTxtNode* pHeadNd = pDoc->GetNodes().MakeTxtNode( aIdx,
                                GetTxtFmtColl( FORM_TITLE ) );
        pHeadNd->InsertText( GetTitle(), SwIndex( pHeadNd ) );

        String sNm( GetTOXName() );
// ??Resource
sNm.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "_Head" ));

        SwSection aSect( TOX_HEADER_SECTION, sNm );

        SwNodeIndex aStt( *pHeadNd ); aIdx--;
        SwSectionFmt* pSectFmt = pDoc->MakeSectionFmt( 0 );
        pDoc->GetNodes().InsertSection( aStt, *pSectFmt, aSect, &aIdx,
                                        TRUE, FALSE );
    }

    // jetzt waere ein prima Zeitpunkt, um die Numerierung zu updaten
    pDoc->UpdateNumRule();

    if( GetCreateType() & nsSwTOXElement::TOX_MARK )
        UpdateMarks( aIntl, pOwnChapterNode );

    if( GetCreateType() & nsSwTOXElement::TOX_OUTLINELEVEL )
        UpdateOutline( pOwnChapterNode );

    if( GetCreateType() & nsSwTOXElement::TOX_TEMPLATE )
        UpdateTemplate( pOwnChapterNode );

    if( GetCreateType() & nsSwTOXElement::TOX_OLE ||
            TOX_OBJECTS == SwTOXBase::GetType())
        UpdateCntnt( nsSwTOXElement::TOX_OLE, pOwnChapterNode );

    if( GetCreateType() & nsSwTOXElement::TOX_TABLE ||
            (TOX_TABLES == SwTOXBase::GetType() && IsFromObjectNames()) )
        UpdateTable( pOwnChapterNode );

    if( GetCreateType() & nsSwTOXElement::TOX_GRAPHIC ||
        (TOX_ILLUSTRATIONS == SwTOXBase::GetType() && IsFromObjectNames()))
        UpdateCntnt( nsSwTOXElement::TOX_GRAPHIC, pOwnChapterNode );

    if( GetSequenceName().Len() && !IsFromObjectNames() &&
        (TOX_TABLES == SwTOXBase::GetType() ||
         TOX_ILLUSTRATIONS == SwTOXBase::GetType() ) )
        UpdateSequence( pOwnChapterNode );

    if( GetCreateType() & nsSwTOXElement::TOX_FRAME )
        UpdateCntnt( nsSwTOXElement::TOX_FRAME, pOwnChapterNode );

    if(TOX_AUTHORITIES == SwTOXBase::GetType())
        UpdateAuthorities( aIntl );

    // Bei Bedarf Alphadelimitter einfuegen (nur bei Stichwoertern)
    //
    if( TOX_INDEX == SwTOXBase::GetType() &&
        ( GetOptions() & nsSwTOIOptions::TOI_ALPHA_DELIMITTER ) )
        InsertAlphaDelimitter( aIntl );

    // sortierte Liste aller Verzeichnismarken und Verzeichnisbereiche
    void* p = 0;
    String* pStr = 0;
    USHORT nCnt = 0, nFormMax = GetTOXForm().GetFormMax();
    SvStringsDtor aStrArr( (BYTE)nFormMax );
    SvPtrarr aCollArr( (BYTE)nFormMax );
    for( ; nCnt < nFormMax; ++nCnt )
    {
        aCollArr.Insert( p, nCnt );
        aStrArr.Insert( pStr, nCnt );
    }

    SwNodeIndex aInsPos( *pFirstEmptyNd, 1 );
    for( nCnt = 0; nCnt < aSortArr.Count(); ++nCnt )
    {
        ::SetProgressState( 0, pDoc->GetDocShell() );

        // setze den Text in das Verzeichniss
        USHORT nLvl = aSortArr[ nCnt ]->GetLevel();
        SwTxtFmtColl* pColl = (SwTxtFmtColl*)aCollArr[ nLvl ];
        if( !pColl )
        {
            pColl = GetTxtFmtColl( nLvl );
            aCollArr.Remove( nLvl );
            p = pColl;
            aCollArr.Insert( p , nLvl );
        }

        // Generierung: dynamische TabStops setzen
        SwTxtNode* pTOXNd = pDoc->GetNodes().MakeTxtNode( aInsPos , pColl );
        aSortArr[ nCnt ]->pTOXNd = pTOXNd;

        // Generierung: Form auswerten und Platzhalter
        //				fuer die Seitennummer eintragen
        //if it is a TOX_INDEX and the SwForm IsCommaSeparated()
        // then a range of entries must be generated into one paragraph
        USHORT nRange = 1;
        if(TOX_INDEX == SwTOXBase::GetType() &&
                GetTOXForm().IsCommaSeparated() &&
                aSortArr[nCnt]->GetType() == TOX_SORT_INDEX)
        {
            const SwTOXMark& rMark = aSortArr[nCnt]->pTxtMark->GetTOXMark();
            const String sPrimKey = rMark.GetPrimaryKey();
            const String sSecKey = rMark.GetSecondaryKey();
            const SwTOXMark* pNextMark = 0;
            while(aSortArr.Count() > (nCnt + nRange)&&
                    aSortArr[nCnt + nRange]->GetType() == TOX_SORT_INDEX &&
                    0 != (pNextMark = &(aSortArr[nCnt + nRange]->pTxtMark->GetTOXMark())) &&
                    pNextMark->GetPrimaryKey() == sPrimKey &&
                    pNextMark->GetSecondaryKey() == sSecKey)
                nRange++;
        }
        // OD 18.03.2003 #106329# - pass node index of table-of-content section
        // and default page description to method <GenerateText(..)>.
        GenerateText( nCnt, nRange, aStrArr, pSectNd->GetIndex(), pDefaultPageDesc );
        nCnt += nRange - 1;
    }

    // delete the first dummy node and remove all Cursor into the prev node
    aInsPos = *pFirstEmptyNd;
    {
        SwPaM aCorPam( *pFirstEmptyNd );
        aCorPam.GetPoint()->nContent.Assign( pFirstEmptyNd, 0 );
        if( !aCorPam.Move( fnMoveForward ) )
            aCorPam.Move( fnMoveBackward );
        SwNodeIndex aEndIdx( aInsPos, 1 );
        pDoc->CorrAbs( aInsPos, aEndIdx, *aCorPam.GetPoint(), TRUE );

        // Task 70995 - save and restore PageDesc and Break Attributes
        if( pFirstEmptyNd->HasSwAttrSet() )
        {
            if( GetTitle().Len() )
                aEndIdx = *pSectNd;
            else
                aEndIdx = *pFirstEmptyNd;
            SwCntntNode* pCNd = pDoc->GetNodes().GoNext( &aEndIdx );
            if( pCNd ) // Robust against defect documents, e.g. i60336
                pCNd->SetAttr( *pFirstEmptyNd->GetpSwAttrSet() );
        }
    }

    // now create the new Frames
    ULONG nIdx = pSectNd->GetIndex();
    // don't delete if index is empty
    if(nIdx + 2 < pSectNd->EndOfSectionIndex())
        pDoc->GetNodes().Delete( aInsPos, 1 );

    aN2L.RestoreUpperFrms( pDoc->GetNodes(), nIdx, nIdx + 1 );
    if(pDoc->GetRootFrm())
        SwFrm::CheckPageDescs( (SwPageFrm*)pDoc->GetRootFrm()->Lower() );

    SetProtect( SwTOXBase::IsProtected() );
}

/*--------------------------------------------------------------------
     Beschreibung: AlphaDelimitter einfuegen
 --------------------------------------------------------------------*/


void SwTOXBaseSection::InsertAlphaDelimitter( const SwTOXInternational& rIntl )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    String sDeli, sLastDeli;
    USHORT	i = 0;
    while( i < aSortArr.Count() )
    {
        ::SetProgressState( 0, pDoc->GetDocShell() );

        USHORT nLevel = aSortArr[i]->GetLevel();

        // Alpha-Delimitter ueberlesen
        if( nLevel == FORM_ALPHA_DELIMITTER )
            continue;

        String sMyString, sMyStringReading;
        aSortArr[i]->GetTxt( sMyString, sMyStringReading );

        sDeli = rIntl.GetIndexKey( sMyString, sMyStringReading,
                                   aSortArr[i]->GetLocale() );

        // Delimitter schon vorhanden ??
        if( sDeli.Len() && sLastDeli != sDeli )
        {
            // alle kleiner Blank wollen wir nicht haben -> sind Sonderzeichen
            if( ' ' <= sDeli.GetChar( 0 ) )
            {
                SwTOXCustom* pCst = new SwTOXCustom( sDeli, aEmptyStr, FORM_ALPHA_DELIMITTER,
                                                     rIntl, aSortArr[i]->GetLocale() );
                aSortArr.Insert( pCst, i++ );
            }
            sLastDeli = sDeli;
        }

        // Skippen bis gleibhes oder kleineres Level erreicht ist
        do {
            i++;
        } while (i < aSortArr.Count() && aSortArr[i]->GetLevel() > nLevel);
    }
}

/*--------------------------------------------------------------------
     Beschreibung: Template  auswerten
 --------------------------------------------------------------------*/

SwTxtFmtColl* SwTOXBaseSection::GetTxtFmtColl( USHORT nLevel )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    const String& rName = GetTOXForm().GetTemplate( nLevel );
    SwTxtFmtColl* pColl = rName.Len() ? pDoc->FindTxtFmtCollByName(rName) :0;
    if( !pColl )
    {
        USHORT nPoolFmt = 0;
        const TOXTypes eMyType = SwTOXBase::GetType();
        switch( eMyType )
        {
        case TOX_INDEX:			nPoolFmt = RES_POOLCOLL_TOX_IDXH; 		break;
        case TOX_USER:
            if( nLevel < 6 )
                nPoolFmt = RES_POOLCOLL_TOX_USERH;
            else
                nPoolFmt = RES_POOLCOLL_TOX_USER6 - 6;
            break;
        case TOX_ILLUSTRATIONS: nPoolFmt = RES_POOLCOLL_TOX_ILLUSH; 	break;
        case TOX_OBJECTS:		nPoolFmt = RES_POOLCOLL_TOX_OBJECTH; 	break;
        case TOX_TABLES:		nPoolFmt = RES_POOLCOLL_TOX_TABLESH; 	break;
        case TOX_AUTHORITIES:	nPoolFmt = RES_POOLCOLL_TOX_AUTHORITIESH; break;

        case TOX_CONTENT:
            // im Content Bereich gibt es einen Sprung!
            if( nLevel < 6 )
                nPoolFmt = RES_POOLCOLL_TOX_CNTNTH;
            else
                nPoolFmt = RES_POOLCOLL_TOX_CNTNT6 - 6;
            break;
        }

        if(eMyType == TOX_AUTHORITIES && nLevel)
            nPoolFmt = nPoolFmt + 1;
        else if(eMyType == TOX_INDEX && nLevel)
        {
            //pool: Level 1,2,3, Delimiter
            //SwForm: Delimiter, Level 1,2,3
            nPoolFmt += 1 == nLevel ? nLevel + 3 : nLevel - 1;
        }
        else
            nPoolFmt = nPoolFmt + nLevel;
        pColl = pDoc->GetTxtCollFromPool( nPoolFmt );
    }
    return pColl;
}


/*--------------------------------------------------------------------
     Beschreibung: Aus Markierungen erzeugen
 --------------------------------------------------------------------*/

void SwTOXBaseSection::UpdateMarks( const SwTOXInternational& rIntl,
                                    const SwTxtNode* pOwnChapterNode )
{
    const SwModify* pType = SwTOXBase::GetRegisteredIn();
    if( !pType->GetDepends() )
        return;

    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    TOXTypes eTOXTyp = GetTOXType()->GetType();
    SwClientIter aIter( *(SwModify*)pType );

    SwTxtTOXMark* pTxtMark;
    SwTOXMark* pMark;
    for( pMark = (SwTOXMark*)aIter.First( TYPE( SwTOXMark )); pMark;
        pMark = (SwTOXMark*)aIter.Next() )
    {
        ::SetProgressState( 0, pDoc->GetDocShell() );

        if( pMark->GetTOXType()->GetType() == eTOXTyp &&
            0 != ( pTxtMark = pMark->GetTxtTOXMark() ) )
        {
            const SwTxtNode* pTOXSrc = pTxtMark->GetpTxtNd();
            // nur TOXMarks einfuegen die im Doc stehen
            // nicht die, die im UNDO stehen
            //
            // if selected use marks from the same chapter only
            if( pTOXSrc->GetNodes().IsDocNodes() &&
                pTOXSrc->GetTxt().Len() && pTOXSrc->GetDepends() &&
                pTOXSrc->GetFrm() &&
               (!IsFromChapter() || ::lcl_FindChapterNode( *pTOXSrc, 0 ) == pOwnChapterNode ) &&
               !pTOXSrc->HasHiddenParaField() &&
               !SwScriptInfo::IsInHiddenRange( *pTOXSrc, *pTxtMark->GetStart() ) )
            {
                SwTOXSortTabBase* pBase = 0;
                if(TOX_INDEX == eTOXTyp)
                {
                    // Stichwortverzeichnismarkierung
                    lang::Locale aLocale;
                    if ( pBreakIt->GetBreakIter().is() )
                    {
                        aLocale = pBreakIt->GetLocale(
                                        pTOXSrc->GetLang( *pTxtMark->GetStart() ) );
                    }

                    pBase = new SwTOXIndex( *pTOXSrc, pTxtMark,
                                            GetOptions(), FORM_ENTRY, rIntl, aLocale );
                    InsertSorted(pBase);
                    if(GetOptions() & nsSwTOIOptions::TOI_KEY_AS_ENTRY &&
                        pTxtMark->GetTOXMark().GetPrimaryKey().Len())
                    {
                        pBase = new SwTOXIndex( *pTOXSrc, pTxtMark,
                                                GetOptions(), FORM_PRIMARY_KEY, rIntl, aLocale );
                        InsertSorted(pBase);
                        if(pTxtMark->GetTOXMark().GetSecondaryKey().Len())
                        {
                            pBase = new SwTOXIndex( *pTOXSrc, pTxtMark,
                                                    GetOptions(), FORM_SECONDARY_KEY, rIntl, aLocale );
                            InsertSorted(pBase);
                        }
                    }
                }
                else if( TOX_USER == eTOXTyp ||
                    pMark->GetLevel() <= GetLevel())
                {	// Inhaltsberzeichnismarkierung
                    // also used for user marks
                    pBase = new SwTOXContent( *pTOXSrc, pTxtMark, rIntl );
                    InsertSorted(pBase);
                }
            }
        }
    }
}


/*--------------------------------------------------------------------
     Beschreibung:	Verzeichnisinhalt aus Gliederungsebene generieren
 --------------------------------------------------------------------*/


void SwTOXBaseSection::UpdateOutline( const SwTxtNode* pOwnChapterNode )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    SwNodes& rNds = pDoc->GetNodes();

    const SwOutlineNodes& rOutlNds = rNds.GetOutLineNds();
    for( USHORT n = 0; n < rOutlNds.Count(); ++n )
    {
        ::SetProgressState( 0, pDoc->GetDocShell() );
        SwTxtNode* pTxtNd = rOutlNds[ n ]->GetTxtNode();
        if( pTxtNd && pTxtNd->Len() && pTxtNd->GetDepends() &&
            //USHORT(pTxtNd->GetTxtColl()->GetOutlineLevel()+1) <= GetLevel() &&	//#outline level,zhaojianwei
            USHORT( pTxtNd->GetAttrOutlineLevel()) <= GetLevel() &&	//<-end,zhaojianwei
            pTxtNd->GetFrm() &&
           !pTxtNd->HasHiddenParaField() &&
           !pTxtNd->HasHiddenCharAttribute( true ) &&
            ( !IsFromChapter() ||
               ::lcl_FindChapterNode( *pTxtNd, 0 ) == pOwnChapterNode ))
        {
            SwTOXPara * pNew = new SwTOXPara( *pTxtNd, nsSwTOXElement::TOX_OUTLINELEVEL );
            InsertSorted( pNew );
        }
    }
}

/*--------------------------------------------------------------------
     Beschreibung: Verzeichnisinhalt aus Vorlagenbereichen generieren
 --------------------------------------------------------------------*/

void SwTOXBaseSection::UpdateTemplate( const SwTxtNode* pOwnChapterNode )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    for(USHORT i = 0; i < MAXLEVEL; i++)
    {
        String sTmpStyleNames = GetStyleNames(i);
        USHORT nTokenCount = sTmpStyleNames.GetTokenCount(TOX_STYLE_DELIMITER);
        for( USHORT nStyle = 0; nStyle < nTokenCount; ++nStyle )
        {
            SwTxtFmtColl* pColl = pDoc->FindTxtFmtCollByName(
                                    sTmpStyleNames.GetToken( nStyle,
                                                    TOX_STYLE_DELIMITER ));
            //TODO: no outline Collections in content indexes if OutlineLevels are already included
            if( !pColl ||
                ( TOX_CONTENT == SwTOXBase::GetType() &&
                  GetCreateType() & nsSwTOXElement::TOX_OUTLINELEVEL &&
                  //NO_NUMBERING != pColl->GetOutlineLevel() ) )//#outline level,zhaojianwei
                    pColl->IsAssignedToListLevelOfOutlineStyle()) )//<-end,zhaojianwei
                  continue;

            SwClientIter aIter( *pColl );
            SwTxtNode* pTxtNd = (SwTxtNode*)aIter.First( TYPE( SwTxtNode ));
            for( ; pTxtNd; pTxtNd = (SwTxtNode*)aIter.Next() )
            {
                ::SetProgressState( 0, pDoc->GetDocShell() );

                if( pTxtNd->GetTxt().Len() && pTxtNd->GetFrm() &&
                    pTxtNd->GetNodes().IsDocNodes() &&
                    ( !IsFromChapter() || pOwnChapterNode ==
                        ::lcl_FindChapterNode( *pTxtNd, 0 ) ) )
                {
                    SwTOXPara * pNew = new SwTOXPara( *pTxtNd, nsSwTOXElement::TOX_TEMPLATE, i + 1 );
                    InsertSorted(pNew);
                }
            }
        }
    }
}

/* -----------------14.07.99 09:59-------------------
    Description: generate content from sequence fields
 --------------------------------------------------*/
void SwTOXBaseSection::UpdateSequence( const SwTxtNode* pOwnChapterNode )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    SwFieldType* pSeqFld = pDoc->GetFldType(RES_SETEXPFLD, GetSequenceName(), false);
    if(!pSeqFld)
        return;

    SwClientIter aIter( *pSeqFld );
    SwFmtFld* pFmtFld = (SwFmtFld*)aIter.First( TYPE( SwFmtFld ));
    for( ; pFmtFld; pFmtFld = (SwFmtFld*)aIter.Next() )
    {
        const SwTxtFld* pTxtFld = pFmtFld->GetTxtFld();
        if(!pTxtFld)
            continue;
        const SwTxtNode& rTxtNode = pTxtFld->GetTxtNode();
        ::SetProgressState( 0, pDoc->GetDocShell() );

        if( rTxtNode.GetTxt().Len() && rTxtNode.GetFrm() &&
            rTxtNode.GetNodes().IsDocNodes() &&
            ( !IsFromChapter() ||
                ::lcl_FindChapterNode( rTxtNode, 0 ) == pOwnChapterNode ) )
        {
            SwTOXPara * pNew = new SwTOXPara( rTxtNode, nsSwTOXElement::TOX_SEQUENCE, 1 );
            //set indexes if the number or the reference text are to be displayed
            if( GetCaptionDisplay() == CAPTION_TEXT )
            {
                pNew->SetStartIndex(
                    SwGetExpField::GetReferenceTextPos( *pFmtFld, *pDoc ));
            }
            else if(GetCaptionDisplay() == CAPTION_NUMBER)
            {
                pNew->SetEndIndex(*pTxtFld->GetStart() + 1);
            }
            InsertSorted(pNew);
        }
    }
}
/* -----------------15.09.99 14:18-------------------

 --------------------------------------------------*/
void SwTOXBaseSection::UpdateAuthorities( const SwTOXInternational& rIntl )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    SwFieldType* pAuthFld = pDoc->GetFldType(RES_AUTHORITY, aEmptyStr, false);
    if(!pAuthFld)
        return;

    SwClientIter aIter( *pAuthFld );
    SwFmtFld* pFmtFld = (SwFmtFld*)aIter.First( TYPE( SwFmtFld ));
    for( ; pFmtFld; pFmtFld = (SwFmtFld*)aIter.Next() )
    {
        const SwTxtFld* pTxtFld = pFmtFld->GetTxtFld();
        //undo
        if(!pTxtFld)
            continue;
        const SwTxtNode& rTxtNode = pTxtFld->GetTxtNode();
        ::SetProgressState( 0, pDoc->GetDocShell() );

//		const SwTxtNode* pChapterCompareNode = 0;

        if( rTxtNode.GetTxt().Len() && rTxtNode.GetFrm() &&
            rTxtNode.GetNodes().IsDocNodes() /*&&
            (!IsFromChapter() || pChapterCompareNode == pOwnChapterNode) */)
        {
            //#106485# the body node has to be used!
            SwCntntFrm *pFrm = rTxtNode.GetFrm();
            SwPosition aFldPos(rTxtNode);
            const SwTxtNode* pTxtNode = 0;
            if(pFrm && !pFrm->IsInDocBody())
                pTxtNode = GetBodyTxtNode( *pDoc, aFldPos, *pFrm );
            if(!pTxtNode)
                pTxtNode = &rTxtNode;
            SwTOXAuthority* pNew = new SwTOXAuthority( *pTxtNode, *pFmtFld, rIntl );

            InsertSorted(pNew);
        }
    }
}

long lcl_IsSOObject( const SvGlobalName& rFactoryNm )
{
    static struct _SoObjType {
        long nFlag;
        // GlobalNameId
        struct _GlobalNameIds {
            UINT32 n1;
            USHORT n2, n3;
            BYTE b8, b9, b10, b11, b12, b13, b14, b15;
        } aGlNmIds[4];
    } aArr[] = {
        { nsSwTOOElements::TOO_MATH,
          { {SO3_SM_CLASSID_60},{SO3_SM_CLASSID_50},
            {SO3_SM_CLASSID_40},{SO3_SM_CLASSID_30} } },
        { nsSwTOOElements::TOO_CHART,
          { {SO3_SCH_CLASSID_60},{SO3_SCH_CLASSID_50},
            {SO3_SCH_CLASSID_40},{SO3_SCH_CLASSID_30} } },
        { nsSwTOOElements::TOO_CALC,
          { {SO3_SC_CLASSID_60},{SO3_SC_CLASSID_50},
            {SO3_SC_CLASSID_40},{SO3_SC_CLASSID_30} } },
        { nsSwTOOElements::TOO_DRAW_IMPRESS,
          { {SO3_SIMPRESS_CLASSID_60},{SO3_SIMPRESS_CLASSID_50},
            {SO3_SIMPRESS_CLASSID_40},{SO3_SIMPRESS_CLASSID_30} } },
        { nsSwTOOElements::TOO_DRAW_IMPRESS,
          { {SO3_SDRAW_CLASSID_60},{SO3_SDRAW_CLASSID_50}}},
        { 0,{{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0} } }
    };

    long nRet = 0;
    for( const _SoObjType* pArr = aArr; !nRet && pArr->nFlag; ++pArr )
        for ( int n = 0; n < 4; ++n )
        {
            const _SoObjType::_GlobalNameIds& rId = pArr->aGlNmIds[ n ];
            if( !rId.n1 )
                break;
            SvGlobalName aGlbNm( rId.n1, rId.n2, rId.n3,
                        rId.b8, rId.b9, rId.b10, rId.b11,
                        rId.b12, rId.b13, rId.b14, rId.b15 );
            if( rFactoryNm == aGlbNm )
            {
                nRet = pArr->nFlag;
                break;
            }
        }

    return nRet;
}

void SwTOXBaseSection::UpdateCntnt( SwTOXElement eMyType,
                                    const SwTxtNode* pOwnChapterNode )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    SwNodes& rNds = pDoc->GetNodes();
    // auf den 1. Node der 1. Section
    ULONG nIdx = rNds.GetEndOfAutotext().StartOfSectionIndex() + 2,
         nEndIdx = rNds.GetEndOfAutotext().GetIndex();

    while( nIdx < nEndIdx )
    {
        ::SetProgressState( 0, pDoc->GetDocShell() );

        SwNode* pNd = rNds[ nIdx ];
        SwCntntNode* pCNd = 0;
        switch( eMyType )
        {
        case nsSwTOXElement::TOX_FRAME:
            if( !pNd->IsNoTxtNode() )
            {
                pCNd = pNd->GetCntntNode();
                if( !pCNd )
                {
                    SwNodeIndex aTmp( *pNd );
                    pCNd = rNds.GoNext( &aTmp );
                }
            }
            break;
        case nsSwTOXElement::TOX_GRAPHIC:
            if( pNd->IsGrfNode() )
                pCNd = (SwCntntNode*)pNd;
            break;
        case nsSwTOXElement::TOX_OLE:
            if( pNd->IsOLENode() )
            {
                BOOL bInclude = TRUE;
                if(TOX_OBJECTS == SwTOXBase::GetType())
                {
                    SwOLENode* pOLENode = pNd->GetOLENode();
                    long nMyOLEOptions = GetOLEOptions();
                    SwOLEObj& rOLEObj = pOLENode->GetOLEObj();

                    if( rOLEObj.IsOleRef() )	//Noch nicht geladen
                    {
                        SvGlobalName aTmpName = SvGlobalName( rOLEObj.GetOleRef()->getClassID() );
                        long nObj = ::lcl_IsSOObject( aTmpName );
                        bInclude = ( (nMyOLEOptions & nsSwTOOElements::TOO_OTHER) && 0 == nObj)
                                                    || (0 != (nMyOLEOptions & nObj));
                    }
                    else
                    {
                        DBG_ERROR("OLE-object nicht geladen?");
                        bInclude = FALSE;
                    }
                }

                if(bInclude)
                    pCNd = (SwCntntNode*)pNd;
            }
            break;
        default: break;
        }

        if( pCNd )
        {
            //find node in body text
            int nSetLevel = USHRT_MAX;

            //#111105# tables of tables|illustrations|objects don't support hierarchies
            if( IsLevelFromChapter() &&
                    TOX_TABLES != SwTOXBase::GetType() &&
                    TOX_ILLUSTRATIONS != SwTOXBase::GetType() &&
                    TOX_OBJECTS != SwTOXBase::GetType() )
            {
                const SwTxtNode* pOutlNd = ::lcl_FindChapterNode( *pCNd,
                                                        MAXLEVEL - 1 );
                if( pOutlNd )
                {
                    //USHORT nTmp = pOutlNd->GetTxtColl()->GetOutlineLevel();//#outline level,zhaojianwei
                    //if( nTmp < NO_NUMBERING )
                    //	nSetLevel = nTmp + 1;
                    if( pOutlNd->GetTxtColl()->IsAssignedToListLevelOfOutlineStyle())
                        nSetLevel = pOutlNd->GetTxtColl()->GetAttrOutlineLevel() ;//<-end,zhaojianwei
                }
            }

            if( pCNd->GetFrm() && ( !IsFromChapter() ||
                    ::lcl_FindChapterNode( *pCNd, 0 ) == pOwnChapterNode ))
            {
                SwTOXPara * pNew = new SwTOXPara( *pCNd, eMyType,
                            ( USHRT_MAX != nSetLevel )
                            ? static_cast<USHORT>(nSetLevel)
                            : FORM_ALPHA_DELIMITTER );
                InsertSorted( pNew );
            }
        }

        nIdx = pNd->StartOfSectionNode()->EndOfSectionIndex() + 2;  // 2 == End-/StartNode
    }
}

/*--------------------------------------------------------------------
     Beschreibung:	Tabelleneintraege zusammensuchen
 --------------------------------------------------------------------*/

void SwTOXBaseSection::UpdateTable( const SwTxtNode* pOwnChapterNode )
{
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    SwNodes& rNds = pDoc->GetNodes();
    const SwFrmFmts& rArr = *pDoc->GetTblFrmFmts();

    for( USHORT n = 0; n < rArr.Count(); ++n )
    {
        ::SetProgressState( 0, pDoc->GetDocShell() );

        SwTable* pTmpTbl = SwTable::FindTable( rArr[ n ] );
        SwTableBox* pFBox;
        if( pTmpTbl && 0 != (pFBox = pTmpTbl->GetTabSortBoxes()[0] ) &&
            pFBox->GetSttNd() && pFBox->GetSttNd()->GetNodes().IsDocNodes() )
        {
            const SwTableNode* pTblNd = pFBox->GetSttNd()->FindTableNode();
            SwNodeIndex aCntntIdx( *pTblNd, 1 );

            SwCntntNode* pCNd;
            while( 0 != ( pCNd = rNds.GoNext( &aCntntIdx ) ) &&
                aCntntIdx.GetIndex() < pTblNd->EndOfSectionIndex() )
            {
                if( pCNd->GetFrm() && (!IsFromChapter() ||
                    ::lcl_FindChapterNode( *pCNd, 0 ) == pOwnChapterNode ))
                {
                    SwTOXTable * pNew = new SwTOXTable( *pCNd );
                    if( IsLevelFromChapter() && TOX_TABLES != SwTOXBase::GetType())
                    {
                        const SwTxtNode* pOutlNd =
                            ::lcl_FindChapterNode( *pCNd, MAXLEVEL - 1 );
                        if( pOutlNd )
                        {
                            //USHORT nTmp = pOutlNd->GetTxtColl()->GetOutlineLevel();//#outline level,zhaojianwei
                            //if( nTmp < NO_NUMBERING )
                            //	pNew->SetLevel( nTmp + 1 );
                            if( pOutlNd->GetTxtColl()->IsAssignedToListLevelOfOutlineStyle())
                            {
                                const int nTmp = pOutlNd->GetTxtColl()->GetAttrOutlineLevel();
                                pNew->SetLevel( static_cast<USHORT>(nTmp) );//<-end ,zhaojianwei
                            }
                        }
                    }
                    InsertSorted(pNew);
                    break;
                }
            }
        }
    }
}

/*--------------------------------------------------------------------
     Beschreibung:	String generieren anhand der Form
                    SonderZeichen 0-31 und 255 entfernen
 --------------------------------------------------------------------*/

String lcl_GetNumString( const SwTOXSortTabBase& rBase, sal_Bool bUsePrefix, BYTE nLevel )
{
    String sRet;

    if( !rBase.pTxtMark && rBase.aTOXSources.Count() > 0 )
    {	// nur wenn es keine Marke ist
        const SwTxtNode* pNd = rBase.aTOXSources[0].pNd->GetTxtNode();
        if( pNd )
        {
            const SwNumRule* pRule = pNd->GetNumRule();

            if( pRule && pNd->GetActualListLevel() < MAXLEVEL )
                sRet = pNd->GetNumString(bUsePrefix, nLevel);
        }
    }
    return sRet;
}

// OD 18.03.2003 #106329# - add parameter <_TOXSectNdIdx> and <_pDefaultPageDesc>
// in order to control, which page description is used, no appropriate one is found.
void SwTOXBaseSection::GenerateText( USHORT nArrayIdx,
                                     USHORT nCount,
                                     SvStringsDtor& ,
                                     const sal_uInt32   _nTOXSectNdIdx,
                                     const SwPageDesc*  _pDefaultPageDesc )
{
    LinkStructArr	aLinkArr;
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();
    ::SetProgressState( 0, pDoc->GetDocShell() );

    //pTOXNd is only set at the first mark
    SwTxtNode* pTOXNd = (SwTxtNode*)aSortArr[nArrayIdx]->pTOXNd;
    String& rTxt = (String&)pTOXNd->GetTxt();
    rTxt.Erase();
    for(USHORT nIndex = nArrayIdx; nIndex < nArrayIdx + nCount; nIndex++)
    {
        if(nIndex > nArrayIdx)
            rTxt.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ", " )); // comma separation
        // String mit dem Pattern aus der Form initialisieren
        const SwTOXSortTabBase& rBase = *aSortArr[nIndex];
        USHORT nLvl = rBase.GetLevel();
        ASSERT( nLvl < GetTOXForm().GetFormMax(), "ungueltiges FORM_LEVEL");

        SvxTabStopItem aTStops( 0, 0, SVX_TAB_ADJUST_DEFAULT, RES_PARATR_TABSTOP );
        xub_StrLen nLinkStartPosition = STRING_NOTFOUND;
        String  sLinkCharacterStyle; //default to "Default" character style - which is none
        String sURL;
        // create an enumerator
        // #i21237#
        SwFormTokens aPattern = GetTOXForm().GetPattern(nLvl);
        SwFormTokens::iterator aIt = aPattern.begin();
        // remove text from node
        while(aIt != aPattern.end()) // #i21237#
        {
            SwFormToken aToken = *aIt; // #i21237#
            xub_StrLen nStartCharStyle = rTxt.Len();
            switch( aToken.eTokenType )
            {
            case TOKEN_ENTRY_NO:
                // fuer Inhaltsverzeichnis Numerierung
                rTxt.Insert( lcl_GetNumString( rBase, aToken.nChapterFormat == CF_NUMBER, static_cast<BYTE>(aToken.nOutlineLevel - 1)) );
                break;

            case TOKEN_ENTRY_TEXT:
                {
                    SwIndex aIdx( pTOXNd, rTxt.Len() );
                    rBase.FillText( *pTOXNd, aIdx );
                }
                break;

            case TOKEN_ENTRY:
                {
                    // fuer Inhaltsverzeichnis Numerierung
                    rTxt.Insert( lcl_GetNumString( rBase, sal_True, MAXLEVEL ));

                    SwIndex aIdx( pTOXNd, rTxt.Len() );
                    rBase.FillText( *pTOXNd, aIdx );
                }
                break;

            case TOKEN_TAB_STOP:
                if (aToken.bWithTab) // #i21237#
                    rTxt.Append('\t');
                //

                if(SVX_TAB_ADJUST_END > aToken.eTabAlign)
                {
                    const SvxLRSpaceItem& rLR =
                        (SvxLRSpaceItem&)pTOXNd->
                        SwCntntNode::GetAttr( RES_LR_SPACE, TRUE );

                    long nTabPosition = aToken.nTabStopPosition;
                    if( !GetTOXForm().IsRelTabPos() && rLR.GetTxtLeft() )
                        nTabPosition -= rLR.GetTxtLeft();
                    aTStops.Insert( SvxTabStop( nTabPosition,
                                                aToken.eTabAlign,
                                                cDfltDecimalChar,
                                                aToken.cTabFillChar ));
                }
                else
                {
                    const SwPageDesc* pPageDesc = ((SwFmtPageDesc&)pTOXNd->
                                SwCntntNode::GetAttr( RES_PAGEDESC )).GetPageDesc();

                    BOOL bCallFindRect = TRUE;
                    long nRightMargin;
                    if( pPageDesc )
                    {
                        const SwFrm* pFrm = pTOXNd->GetFrm( 0, 0, TRUE );
                        if( !pFrm || 0 == ( pFrm = pFrm->FindPageFrm() ) ||
                            pPageDesc != ((SwPageFrm*)pFrm)->GetPageDesc() )
                            // dann muss man ueber den PageDesc gehen
                            bCallFindRect = FALSE;
                    }

                    SwRect aNdRect;
                    if( bCallFindRect )
                        aNdRect = pTOXNd->FindLayoutRect( TRUE );

                    if( aNdRect.IsEmpty() )
                    {
                        // dann hilft alles nichts, wir muessen ueber die Seiten-
                        // vorlage gehen.
                        // OD 18.03.2003 #106329# - call
                        sal_uInt32 nPgDescNdIdx = pTOXNd->GetIndex() + 1;
                        sal_uInt32* pPgDescNdIdx = &nPgDescNdIdx;
                        pPageDesc = pTOXNd->FindPageDesc( FALSE, pPgDescNdIdx );
                        if ( !pPageDesc ||
                             *pPgDescNdIdx < _nTOXSectNdIdx )
                        {
                            // use default page description, if none is found
                            // or the found one is given by a node before the
                            // table-of-content section.
                            pPageDesc = _pDefaultPageDesc;
                        }

                        const SwFrmFmt& rPgDscFmt = pPageDesc->GetMaster();
                        nRightMargin = rPgDscFmt.GetFrmSize().GetWidth() -
                                         rPgDscFmt.GetLRSpace().GetLeft() -
                                         rPgDscFmt.GetLRSpace().GetRight();
                    }
                    else
                        nRightMargin = aNdRect.Width();
                    //#i24363# tab stops relative to indent
                    if( pDoc->get(IDocumentSettingAccess::TABS_RELATIVE_TO_INDENT) )
                    {
                        //left margin of paragraph style
                        const SvxLRSpaceItem& rLRSpace = pTOXNd->GetTxtColl()->GetLRSpace();
                        nRightMargin -= rLRSpace.GetLeft();
                        nRightMargin -= rLRSpace.GetTxtFirstLineOfst();
                    }

                    aTStops.Insert( SvxTabStop( nRightMargin, SVX_TAB_ADJUST_RIGHT,
                                                cDfltDecimalChar,
                                                aToken.cTabFillChar ));
                }
                break;

            case TOKEN_TEXT:
                rTxt.Append( aToken.sText );
                break;

            case TOKEN_PAGE_NUMS:
                    // Platzhalter fuer Seitennummer(n) es wird nur der erste beachtet
                    //
                {
                    // Die Anzahl der gleichen Eintrage bestimmt die Seitennummern-Pattern
                    //
                    USHORT nSize = rBase.aTOXSources.Count();
                    if( nSize > 0 )
                    {
                        String aInsStr( cNumRepl );
                        for(USHORT i=1; i < nSize; ++i)
                        {
                            aInsStr.AppendAscii( sPageDeli );
                            aInsStr += cNumRepl;
                        }
                        aInsStr += cEndPageNum;
                        rTxt.Append( aInsStr );
                    }
//						// Tab entfernen, wenn keine Seitennummer
//					else if( rTxt.Len() && '\t' == rTxt.GetChar( rTxt.Len() - 1 ))
//						rTxt.Erase( rTxt.Len()-1, 1 );
                }
                break;

            case TOKEN_CHAPTER_INFO:
                {
                    // ein bischen trickreich: suche irgend einen Frame
                    const SwTOXSource* pTOXSource = 0;
                    if(rBase.aTOXSources.Count())
                        pTOXSource = &rBase.aTOXSources[0];

                    // --> OD 2008-02-14 #i53420#
//                    if( pTOXSource && pTOXSource->pNd
//                        pTOXSource->pNd->IsTxtNode() )
                    if ( pTOXSource && pTOXSource->pNd &&
                         pTOXSource->pNd->IsCntntNode() )
                    // <--
                    {
                        const SwCntntFrm* pFrm = pTOXSource->pNd->GetFrm();
                        if( pFrm )
                        {
                            SwChapterFieldType aFldTyp;
                            SwChapterField aFld( &aFldTyp, aToken.nChapterFormat );
                            aFld.SetLevel( static_cast<BYTE>(aToken.nOutlineLevel - 1) );
                            // --> OD 2008-02-14 #i53420#
//                            aFld.ChangeExpansion( pFrm, (SwTxtNode*)pTOXSource->pNd, TRUE );
                            aFld.ChangeExpansion( pFrm,
                                dynamic_cast<const SwCntntNode*>(pTOXSource->pNd),
                                TRUE );
                            // <--
                            //---> i89791
                            // OD 2008-06-26 - continue to support CF_NUMBER
                            // and CF_NUM_TITLE in order to handle ODF 1.0/1.1
                            // written by OOo 3.x in the same way as OOo 2.x
                            // would handle them.
                            if ( CF_NUM_NOPREPST_TITLE == aToken.nChapterFormat ||
                                 CF_NUMBER == aToken.nChapterFormat )
                                rTxt.Insert(aFld.GetNumber()); //get the string number without pre/postfix
                            else if ( CF_NUMBER_NOPREPST == aToken.nChapterFormat ||
                                      CF_NUM_TITLE == aToken.nChapterFormat )
                            //<---
                            {
                                rTxt += aFld.GetNumber();
                                rTxt += ' ';
                                rTxt += aFld.GetTitle();
                            }
                            else if(CF_TITLE == aToken.nChapterFormat)
                                rTxt += aFld.GetTitle();
                        }
                    }
                }
                break;

            case TOKEN_LINK_START:
                nLinkStartPosition = rTxt.Len();
                sLinkCharacterStyle = aToken.sCharStyleName;
            break;

            case TOKEN_LINK_END:
                    //TODO: only paired start/end tokens are valid
                if( STRING_NOTFOUND != nLinkStartPosition)
                {
                    SwIndex aIdx( pTOXNd, nLinkStartPosition );
                    //pTOXNd->Erase( aIdx, SwForm::nFormLinkSttLen );
                    xub_StrLen nEnd = rTxt.Len();

                    if( !sURL.Len() )
                    {
                        sURL = rBase.GetURL();
                        if( !sURL.Len() )
                            break;
                    }
                    LinkStruct* pNewLink = new LinkStruct(sURL, nLinkStartPosition,
                                                    nEnd);
                    pNewLink->aINetFmt.SetVisitedFmt(sLinkCharacterStyle);
                    pNewLink->aINetFmt.SetINetFmt(sLinkCharacterStyle);
                    if(sLinkCharacterStyle.Len())
                    {
                        USHORT nPoolId =
                            SwStyleNameMapper::GetPoolIdFromUIName( sLinkCharacterStyle, nsSwGetPoolIdFromName::GET_POOLID_CHRFMT );
                        pNewLink->aINetFmt.SetVisitedFmtId(nPoolId);
                        pNewLink->aINetFmt.SetINetFmtId(nPoolId);
                    }
                    else
                    {
                        pNewLink->aINetFmt.SetVisitedFmtId(USHRT_MAX);
                        pNewLink->aINetFmt.SetINetFmtId(USHRT_MAX);
                    }
                    aLinkArr.Insert( pNewLink, aLinkArr.Count() );
                    nLinkStartPosition = STRING_NOTFOUND;
                    sLinkCharacterStyle.Erase();
                }
                break;

            case TOKEN_AUTHORITY:
                {
                    ToxAuthorityField eField = (ToxAuthorityField)aToken.nAuthorityField;
                    SwIndex aIdx( pTOXNd, rTxt.Len() );
                    rBase.FillText( *pTOXNd, aIdx, static_cast<USHORT>(eField) );
                }
                break;
            case TOKEN_END: break;
            }

            if( aToken.sCharStyleName.Len() )
            {
                SwCharFmt* pCharFmt;
                if(	USHRT_MAX != aToken.nPoolId )
                    pCharFmt = pDoc->GetCharFmtFromPool( aToken.nPoolId );
                else
                    pCharFmt = pDoc->FindCharFmtByName( aToken.sCharStyleName);

                if (pCharFmt)
                {
                    SwFmtCharFmt aFmt( pCharFmt );
                    pTOXNd->InsertItem( aFmt, nStartCharStyle,
                        rTxt.Len(), nsSetAttrMode::SETATTR_DONTEXPAND );
                }
            }

            aIt++; // #i21237#
        }

        pTOXNd->SetAttr( aTStops );
    }

    if(aLinkArr.Count())
        for(USHORT i = 0; i < aLinkArr.Count(); ++i )
        {
            LinkStruct* pTmp = aLinkArr.GetObject(i);
            pTOXNd->InsertItem( pTmp->aINetFmt, pTmp->nStartTextPos,
                            pTmp->nEndTextPos);
        }
}

/*--------------------------------------------------------------------
     Beschreibung: Seitennummer errechnen und nach dem Formatieren
                   eintragen
 --------------------------------------------------------------------*/

void SwTOXBaseSection::UpdatePageNum()
{
    if( !aSortArr.Count() )
        return ;

    // die aktuellen Seitennummern ins Verzeichnis eintragen
    SwPageFrm*	pAktPage	= 0;
    USHORT		nPage		= 0;
    SwDoc* pDoc = (SwDoc*)GetFmt()->GetDoc();

    SwTOXInternational aIntl( GetLanguage(),
                              TOX_INDEX == GetTOXType()->GetType() ?
                              GetOptions() : 0,
                              GetSortAlgorithm() );

    for( USHORT nCnt = 0; nCnt < aSortArr.Count(); ++nCnt )
    {
        // Schleife ueber alle SourceNodes
        SvUShorts aNums;		//Die Seitennummern
        SvPtrarr  aDescs;		//Die PageDescriptoren passend zu den Seitennummern.
        SvUShorts* pMainNums = 0; // contains page numbers of main entries

        // process run in lines
        USHORT nRange = 0;
        if(GetTOXForm().IsCommaSeparated() &&
                aSortArr[nCnt]->GetType() == TOX_SORT_INDEX)
        {
            const SwTOXMark& rMark = aSortArr[nCnt]->pTxtMark->GetTOXMark();
            const String sPrimKey = rMark.GetPrimaryKey();
            const String sSecKey = rMark.GetSecondaryKey();
            const SwTOXMark* pNextMark = 0;
            while(aSortArr.Count() > (nCnt + nRange)&&
                    aSortArr[nCnt + nRange]->GetType() == TOX_SORT_INDEX &&
                    0 != (pNextMark = &(aSortArr[nCnt + nRange]->pTxtMark->GetTOXMark())) &&
                    pNextMark->GetPrimaryKey() == sPrimKey &&
                    pNextMark->GetSecondaryKey() == sSecKey)
                nRange++;
        }
        else
            nRange = 1;

        for(USHORT nRunInEntry = nCnt; nRunInEntry < nCnt + nRange; nRunInEntry++)
        {
            SwTOXSortTabBase* pSortBase = aSortArr[nRunInEntry];
            USHORT nSize = pSortBase->aTOXSources.Count();
            USHORT i;
            for( USHORT j = 0; j < nSize; ++j )
            {
                ::SetProgressState( 0, pDoc->GetDocShell() );

                SwTOXSource& rTOXSource = pSortBase->aTOXSources[j];
                if( rTOXSource.pNd )
                {
                    SwCntntFrm* pFrm = rTOXSource.pNd->GetFrm();
                    ASSERT( pFrm || pDoc->IsUpdateTOX(), "TOX, no Frame found");
                    if( !pFrm )
                        continue;
                    if( pFrm->IsTxtFrm() && ((SwTxtFrm*)pFrm)->HasFollow() )
                    {
                        // dann suche den richtigen heraus
                        SwTxtFrm* pNext = (SwTxtFrm*)pFrm;
                        while( 0 != ( pNext = (SwTxtFrm*)pFrm->GetFollow() )
                                && rTOXSource.nPos >= pNext->GetOfst() )
                            pFrm = pNext;
                    }

                    SwPageFrm*	pTmpPage = pFrm->FindPageFrm();
                    if( pTmpPage != pAktPage )
                    {
                        nPage		= pTmpPage->GetVirtPageNum();
                        pAktPage	= pTmpPage;
                    }

                    // sortiert einfuegen
                    for( i = 0; i < aNums.Count() && aNums[i] < nPage; ++i )
                        ;

                    if( i >= aNums.Count() || aNums[ i ] != nPage )
                    {
                        aNums.Insert( nPage, i );
                        aDescs.Insert( (void*)pAktPage->GetPageDesc(), i );
                    }
                    // is it a main entry?
                    if(TOX_SORT_INDEX == pSortBase->GetType() &&
                        rTOXSource.bMainEntry)
                    {
                        if(!pMainNums)
                            pMainNums = new SvUShorts;
                        pMainNums->Insert(nPage, pMainNums->Count());
                    }
                }
            }
            // einfuegen der Seitennummer in den Verzeichnis-Text-Node
            const SwTOXSortTabBase* pBase = aSortArr[ nCnt ];
            if(pBase->pTOXNd)
            {
                const SwTxtNode* pTxtNd = pBase->pTOXNd->GetTxtNode();
                ASSERT( pTxtNd, "kein TextNode, falsches Verzeichnis" );

                _UpdatePageNum( (SwTxtNode*)pTxtNd, aNums, aDescs, pMainNums,
                                aIntl );
            }
            DELETEZ(pMainNums);
            aNums.Remove(0, aNums.Count());
        }
    }
    // nach dem Setzen der richtigen Seitennummer, das Mapping-Array
    // wieder loeschen !!
    aSortArr.DeleteAndDestroy( 0, aSortArr.Count() );
}


/*--------------------------------------------------------------------
     Beschreibung: Austausch der Seitennummer-Platzhalter
 --------------------------------------------------------------------*/

// search for the page no in the array of main entry page numbers
BOOL lcl_HasMainEntry( const SvUShorts* pMainEntryNums, USHORT nToFind )
{
    for(USHORT i = 0; pMainEntryNums && i < pMainEntryNums->Count(); ++i)
        if(nToFind == (*pMainEntryNums)[i])
            return TRUE;
    return FALSE;
}

void SwTOXBaseSection::_UpdatePageNum( SwTxtNode* pNd,
                                    const SvUShorts& rNums,
                                    const SvPtrarr & rDescs,
                                    const SvUShorts* pMainEntryNums,
                                    const SwTOXInternational& rIntl )
{
    //collect starts end ends of main entry character style
    SvUShorts* pCharStyleIdx = pMainEntryNums ? new SvUShorts : 0;

    String sSrchStr( cNumRepl );
    sSrchStr.AppendAscii( sPageDeli ) += cNumRepl;
    xub_StrLen nStartPos = pNd->GetTxt().Search( sSrchStr );
    ( sSrchStr = cNumRepl ) += cEndPageNum;
    xub_StrLen nEndPos = pNd->GetTxt().Search( sSrchStr );
    USHORT i;

    if( STRING_NOTFOUND == nEndPos || !rNums.Count() )
        return;

    if( STRING_NOTFOUND == nStartPos || nStartPos > nEndPos)
        nStartPos = nEndPos;

    USHORT nOld = rNums[0],
           nBeg = nOld,
           nCount  = 0;
    String aNumStr( SvxNumberType( ((SwPageDesc*)rDescs[0])->GetNumType() ).
                    GetNumStr( nBeg ) );
    if( pCharStyleIdx && lcl_HasMainEntry( pMainEntryNums, nBeg ))
    {
        USHORT nTemp = 0;
        pCharStyleIdx->Insert( nTemp, pCharStyleIdx->Count());
    }

    // Platzhalter loeschen
    SwIndex aPos(pNd, nStartPos);
    SwCharFmt* pPageNoCharFmt = 0;
    SwpHints* pHints = pNd->GetpSwpHints();
    if(pHints)
        for(USHORT nHintIdx = 0; nHintIdx < pHints->GetStartCount(); nHintIdx++)
        {
            SwTxtAttr* pAttr = pHints->GetStart(nHintIdx);
            xub_StrLen nTmpEnd = pAttr->GetEnd() ? *pAttr->GetEnd() : 0;
            if(	nStartPos >= *pAttr->GetStart() &&
                (nStartPos + 2) <= nTmpEnd &&
                pAttr->Which() == RES_TXTATR_CHARFMT)
            {
                pPageNoCharFmt = pAttr->GetCharFmt().GetCharFmt();
                break;
            }
        }
    pNd->EraseText(aPos, nEndPos - nStartPos + 2);

    for( i = 1; i < rNums.Count(); ++i)
    {
        SvxNumberType aType( ((SwPageDesc*)rDescs[i])->GetNumType() );
        if( TOX_INDEX == SwTOXBase::GetType() )
        {	// Zusammenfassen f. ff.
            // Alle folgenden aufaddieren
            // break up if main entry starts or ends and
            // insert a char style index
            BOOL bMainEntryChanges = lcl_HasMainEntry(pMainEntryNums, nOld)
                    != lcl_HasMainEntry(pMainEntryNums, rNums[i]);

            if(nOld == rNums[i]-1 && !bMainEntryChanges &&
                0 != (GetOptions() & (nsSwTOIOptions::TOI_FF|nsSwTOIOptions::TOI_DASH)))
                nCount++;
            else
            {
                // ff. f. alten Wert flushen
                if(GetOptions() & nsSwTOIOptions::TOI_FF)
                {
                    if ( nCount >= 1 )
                        aNumStr += rIntl.GetFollowingText( nCount > 1 );
                }
                else
                {
                    if(nCount >= 2 )
                        aNumStr += '-';
                    else if(nCount == 1 )
                        aNumStr.AppendAscii( sPageDeli );
//#58127# Wenn nCount == 0, dann steht die einzige Seitenzahl schon im aNumStr!
                    if(nCount)
                        aNumStr += aType.GetNumStr( nBeg + nCount );
                }

                // neuen String anlegen
                nBeg	 = rNums[i];
                aNumStr.AppendAscii( sPageDeli );
                //the change of the character style must apply after sPageDeli is appended
                if(pCharStyleIdx && bMainEntryChanges)
                    pCharStyleIdx->Insert(aNumStr.Len(),
                                                    pCharStyleIdx->Count());
                aNumStr += aType.GetNumStr( nBeg );
                nCount	 = 0;
            }
            nOld = rNums[i];
        }
        else
        {	// Alle Nummern eintragen
            aNumStr += aType.GetNumStr( USHORT(rNums[i]) );
            if(i != (rNums.Count()-1))
                aNumStr.AppendAscii( sPageDeli );
        }
    }
    // Bei Ende und ff. alten Wert flushen
    if( TOX_INDEX == SwTOXBase::GetType() )
    {
        if(GetOptions() & nsSwTOIOptions::TOI_FF)
        {
            if( nCount >= 1 )
                aNumStr += rIntl.GetFollowingText( nCount > 1 );
        }
        else
        {
            if(nCount >= 2)
                aNumStr +='-';
            else if(nCount == 1)
                aNumStr.AppendAscii( sPageDeli );
//#58127# Wenn nCount == 0, dann steht die einzige Seitenzahl schon im aNumStr!
            if(nCount)
                aNumStr += SvxNumberType( ((SwPageDesc*)rDescs[i-1])->
                                GetNumType() ).GetNumStr( nBeg+nCount );
        }
    }
    pNd->InsertText( aNumStr, aPos,
           static_cast<IDocumentContentOperations::InsertFlags>(
               IDocumentContentOperations::INS_EMPTYEXPAND |
               IDocumentContentOperations::INS_FORCEHINTEXPAND) );
    if(pPageNoCharFmt)
    {
        SwFmtCharFmt aCharFmt( pPageNoCharFmt );
        pNd->InsertItem(aCharFmt, nStartPos, nStartPos + aNumStr.Len(), nsSetAttrMode::SETATTR_DONTEXPAND);
    }

    //now the main entries should get there character style
    if(pCharStyleIdx && pCharStyleIdx->Count() && GetMainEntryCharStyle().Len())
    {
        // eventually the last index must me appended
        if(pCharStyleIdx->Count()&0x01)
            pCharStyleIdx->Insert(aNumStr.Len(), pCharStyleIdx->Count());

        //search by name
        SwDoc* pDoc = pNd->GetDoc();
        USHORT nPoolId = SwStyleNameMapper::GetPoolIdFromUIName( GetMainEntryCharStyle(), nsSwGetPoolIdFromName::GET_POOLID_CHRFMT );
        SwCharFmt* pCharFmt = 0;
        if(USHRT_MAX != nPoolId)
            pCharFmt = pDoc->GetCharFmtFromPool(nPoolId);
        else
            pCharFmt = pDoc->FindCharFmtByName( GetMainEntryCharStyle() );
        if(!pCharFmt)
            pCharFmt = pDoc->MakeCharFmt(GetMainEntryCharStyle(), 0);

        //find the page numbers in aNumStr and set the character style
        xub_StrLen nOffset = pNd->GetTxt().Len() - aNumStr.Len();
        SwFmtCharFmt aCharFmt(pCharFmt);
        for(USHORT j = 0; j < pCharStyleIdx->Count(); j += 2)
        {
            xub_StrLen nStartIdx = (*pCharStyleIdx)[j] + nOffset;
            xub_StrLen nEndIdx = (*pCharStyleIdx)[j + 1]  + nOffset;
            pNd->InsertItem(aCharFmt, nStartIdx, nEndIdx, nsSetAttrMode::SETATTR_DONTEXPAND);
        }

    }
    delete pCharStyleIdx;
}


/*--------------------------------------------------------------------
     Beschreibung: Sortiert einfuegen in das SortArr
 --------------------------------------------------------------------*/

void SwTOXBaseSection::InsertSorted(SwTOXSortTabBase* pNew)
{
    Range aRange(0, aSortArr.Count());
    if( TOX_INDEX == SwTOXBase::GetType() && pNew->pTxtMark )
    {
        const SwTOXMark& rMark = pNew->pTxtMark->GetTOXMark();
        // Schluessel auswerten
        // Den Bereich ermitteln, in dem einzufuegen ist
        if( 0 == (GetOptions() & nsSwTOIOptions::TOI_KEY_AS_ENTRY) &&
            rMark.GetPrimaryKey().Len() )
        {
            aRange = GetKeyRange( rMark.GetPrimaryKey(),
                                  rMark.GetPrimaryKeyReading(),
                                  *pNew, FORM_PRIMARY_KEY, aRange );

            if( rMark.GetSecondaryKey().Len() )
                aRange = GetKeyRange( rMark.GetSecondaryKey(),
                                      rMark.GetSecondaryKeyReading(),
                                      *pNew, FORM_SECONDARY_KEY, aRange );
        }
    }
    //search for identical entries and remove the trailing one
    if(TOX_AUTHORITIES == SwTOXBase::GetType())
    {
        for(short i = (short)aRange.Min(); i < (short)aRange.Max(); ++i)
        {
            SwTOXSortTabBase* pOld = aSortArr[i];
            if(*pOld == *pNew)
            {
                if(*pOld < *pNew)
                {
                    delete pNew;
                    return;
                }
                else
                {
                    // remove the old content
                    aSortArr.DeleteAndDestroy( i, 1 );
                    aRange.Max()--;
                    break;
                }
            }
        }
    }

    // find position and insert
    //
    short i;

    for( i = (short)aRange.Min(); i < (short)aRange.Max(); ++i)
    {	// nur auf gleicher Ebene pruefen
        //
        SwTOXSortTabBase* pOld = aSortArr[i];
        if(*pOld == *pNew)
        {
            if(TOX_AUTHORITIES != SwTOXBase::GetType())
            {
                // Eigener Eintrag fuer Doppelte oder Keywords
                //
                if( pOld->GetType() == TOX_SORT_CUSTOM &&
                    pNew->GetOptions() & nsSwTOIOptions::TOI_KEY_AS_ENTRY)
                    continue;

                if(!(pNew->GetOptions() & nsSwTOIOptions::TOI_SAME_ENTRY))
                {	// Eigener Eintrag
                    aSortArr.Insert(pNew, i );
                    return;
                }
                // Eintrag schon vorhanden in Referenzliste aufnehmen
                pOld->aTOXSources.Insert( pNew->aTOXSources[0],
                                            pOld->aTOXSources.Count() );

                delete pNew;
                return;
            }
#ifdef DBG_UTIL
            else
                DBG_ERROR("Bibliography entries cannot be found here");
#endif
        }
        if(*pNew < *pOld)
            break;
    }
    // SubLevel Skippen
    while( TOX_INDEX == SwTOXBase::GetType() && i < aRange.Max() &&
          aSortArr[i]->GetLevel() > pNew->GetLevel() )
        i++;

    // An Position i wird eingefuegt
    aSortArr.Insert(pNew, i );
}

/*--------------------------------------------------------------------
     Beschreibung: Schluessel-Bereich suchen und evtl einfuegen
 --------------------------------------------------------------------*/

Range SwTOXBaseSection::GetKeyRange(const String& rStr, const String& rStrReading,
                                    const SwTOXSortTabBase& rNew,
                                    USHORT nLevel, const Range& rRange )
{
    const SwTOXInternational& rIntl = *rNew.pTOXIntl;
    String sToCompare(rStr);
    String sToCompareReading(rStrReading);

    if( 0 != (nsSwTOIOptions::TOI_INITIAL_CAPS & GetOptions()) )
    {
        String sUpper( rIntl.ToUpper( sToCompare, 0 ));
        sToCompare.Erase( 0, 1 ).Insert( sUpper, 0 );
    }

    ASSERT(rRange.Min() >= 0 && rRange.Max() >= 0, "Min Max < 0");

    const USHORT nMin = (USHORT)rRange.Min();
    const USHORT nMax = (USHORT)rRange.Max();

    USHORT i;

    for( i = nMin; i < nMax; ++i)
    {
        SwTOXSortTabBase* pBase = aSortArr[i];

        String sMyString, sMyStringReading;
        pBase->GetTxt( sMyString, sMyStringReading );

        if( rIntl.IsEqual( sMyString, sMyStringReading, pBase->GetLocale(),
                           sToCompare, sToCompareReading, rNew.GetLocale() )  &&
                    pBase->GetLevel() == nLevel &&
                    pBase->GetType() == TOX_SORT_CUSTOM )
            break;
    }
    if(i == nMax)
    {	// Falls nicht vorhanden erzeugen und einfuegen
        //
        SwTOXCustom* pKey = new SwTOXCustom( sToCompare, sToCompareReading, nLevel, rIntl,
                                             rNew.GetLocale() );
        for(i = nMin; i < nMax; ++i)
        {
            if(nLevel == aSortArr[i]->GetLevel() &&  *pKey < *(aSortArr[i]))
                break;
        }
        aSortArr.Insert(pKey, i );
    }
    USHORT nStart = i+1;
    USHORT nEnd   = aSortArr.Count();

    // Ende des Bereiches suchen
    for(i = nStart; i < aSortArr.Count(); ++i)
    {
        if(aSortArr[i]->GetLevel() <= nLevel)
        {	nEnd = i;
            break;
        }
    }
    return Range(nStart, nEnd);
}


BOOL SwTOXBase::IsTOXBaseInReadonly() const
{
    const SwTOXBaseSection *pSect = PTR_CAST(SwTOXBaseSection, this);
    BOOL bRet = FALSE;
    const SwSectionNode* pSectNode;
    if(pSect && pSect->GetFmt() &&
            0 != (pSectNode = pSect->GetFmt()->GetSectionNode()))
    {
        const SwDocShell* pDocSh;
        bRet = (0 != (pDocSh = pSectNode->GetDoc()->GetDocShell()) &&
                                                    pDocSh->IsReadOnly()) ||
            (0 != (pSectNode = pSectNode->StartOfSectionNode()->FindSectionNode())&&
                    pSectNode->GetSection().IsProtectFlag());

    }
    return bRet;
}
/* -----------------17.08.99 13:29-------------------

 --------------------------------------------------*/
const SfxItemSet* SwTOXBase::GetAttrSet() const
{
    const SwTOXBaseSection *pSect = PTR_CAST(SwTOXBaseSection, this);
    if(pSect && pSect->GetFmt())
        return &pSect->GetFmt()->GetAttrSet();
    return 0;
}

void SwTOXBase::SetAttrSet( const SfxItemSet& rSet )
{
    SwTOXBaseSection *pSect = PTR_CAST(SwTOXBaseSection, this);
    if( pSect && pSect->GetFmt() )
        pSect->GetFmt()->SetFmtAttr( rSet );
}

BOOL SwTOXBase::GetInfo( SfxPoolItem& rInfo ) const
{
    switch( rInfo.Which() )
    {
    case RES_CONTENT_VISIBLE:
        {
            SwTOXBaseSection *pSect = PTR_CAST(SwTOXBaseSection, this);
            if( pSect && pSect->GetFmt() )
                pSect->GetFmt()->GetInfo( rInfo );
        }
        return FALSE;
    }
    return TRUE;
}

/*  */