summaryrefslogtreecommitdiffstats
path: root/src/qt3support/text/q3richtext_p.h
blob: e07bb71bbe6b1680cde727652062d65c2cd10965 (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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef Q3RICHTEXT_P_H
#define Q3RICHTEXT_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of a number of Qt sources files.  This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

#include "QtGui/qapplication.h"
#include "QtGui/qcolor.h"
#include "QtCore/qhash.h"
#include "QtGui/qfont.h"
#include "QtGui/qfontmetrics.h"
#include "QtGui/qlayout.h"
#include "QtCore/qmap.h"
#include "QtCore/qvector.h"
#include "QtCore/qstack.h"
#include "QtCore/qlist.h"
#include "QtCore/qobject.h"
#include "QtGui/qpainter.h"
#include "QtGui/qpixmap.h"
#include "QtCore/qrect.h"
#include "QtCore/qsize.h"
#include "QtCore/qstring.h"
#include "QtCore/qstringlist.h"
#include "Qt3Support/q3stylesheet.h"
#include "Qt3Support/q3mimefactory.h"

QT_BEGIN_NAMESPACE

#ifndef QT_NO_RICHTEXT

class Q3TextDocument;
class Q3TextString;
class Q3TextPreProcessor;
class Q3TextFormat;
class Q3TextCursor;
class Q3TextParagraph;
class Q3TextFormatter;
class Q3TextIndent;
class Q3TextFormatCollection;
class Q3StyleSheetItem;
#ifndef QT_NO_TEXTCUSTOMITEM
class Q3TextCustomItem;
#endif
class Q3TextFlow;
struct QBidiContext;

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextStringChar
{
    friend class Q3TextString;

public:
    // this is never called, initialize variables in Q3TextString::insert()!!!
    Q3TextStringChar() : nobreak(false), lineStart(0), type(Regular) {p.format=0;}
    ~Q3TextStringChar();

    struct CustomData
    {
        Q3TextFormat *format;
#ifndef QT_NO_TEXTCUSTOMITEM
        Q3TextCustomItem *custom;
#endif
        QString anchorName;
        QString anchorHref;
    };
    enum Type { Regular=0, Custom=1, Anchor=2, CustomAnchor=3 };

    QChar c;
    // this is the same struct as in qtextengine_p.h. Don't change!
    uchar softBreak      :1;     // Potential linebreak point
    uchar whiteSpace     :1;     // A unicode whitespace character, except NBSP, ZWNBSP
    uchar charStop       :1;     // Valid cursor position (for left/right arrow)
    uchar nobreak        :1;

    uchar lineStart : 1;
    uchar /*Type*/ type : 2;
    uchar bidiLevel       :7;
    uchar rightToLeft : 1;

    int x;
    union {
        Q3TextFormat* format;
        CustomData* custom;
    } p;


    int height() const;
    int ascent() const;
    int descent() const;
    bool isCustom() const { return (type & Custom) != 0; }
    Q3TextFormat *format() const;
#ifndef QT_NO_TEXTCUSTOMITEM
    Q3TextCustomItem *customItem() const;
#endif
    void setFormat(Q3TextFormat *f);
#ifndef QT_NO_TEXTCUSTOMITEM
    void setCustomItem(Q3TextCustomItem *i);
#endif

#ifndef QT_NO_TEXTCUSTOMITEM
    void loseCustomItem();
#endif


    bool isAnchor() const { return (type & Anchor) != 0; }
    bool isLink() const { return isAnchor() && p.custom->anchorHref.count(); }
    QString anchorName() const;
    QString anchorHref() const;
    void setAnchor(const QString& name, const QString& href);

    Q3TextStringChar(const Q3TextStringChar &) {
        Q_ASSERT(false);
    }
private:
    Q3TextStringChar &operator=(const Q3TextStringChar &) {
        //abort();
        return *this;
    }
    friend class Q3TextParagraph;
};

Q_DECLARE_TYPEINFO(Q3TextStringChar, Q_PRIMITIVE_TYPE);

class Q_COMPAT_EXPORT Q3TextString
{
public:

    Q3TextString();
    Q3TextString(const Q3TextString &s);
    virtual ~Q3TextString();

    static QString toString(const QVector<Q3TextStringChar> &data);
    QString toString() const;

    inline Q3TextStringChar &at(int i) const {
        return const_cast<Q3TextString *>(this)->data[i];
    }
    inline int length() const { return data.size(); }

    int width(int idx) const;

    void insert(int index, const QString &s, Q3TextFormat *f);
    void insert(int index, const QChar *unicode, int len, Q3TextFormat *f);
    void insert(int index, Q3TextStringChar *c, bool doAddRefFormat = false);
    void truncate(int index);
    void remove(int index, int len);
    void clear();

    void setFormat(int index, Q3TextFormat *f, bool useCollection);

    void setBidi(bool b) { bidi = b; }
    bool isBidi() const;
    bool isRightToLeft() const;
    QChar::Direction direction() const;
    void setDirection(QChar::Direction dr) { dir = dr; bidiDirty = true; }

    QVector<Q3TextStringChar> rawData() const { return data; }

    void operator=(const QString &s) { clear(); insert(0, s, 0); }
    void operator+=(const QString &s) { insert(length(), s, 0); }
    void prepend(const QString &s) { insert(0, s, 0); }
    int appendParagraphs( Q3TextParagraph *start, Q3TextParagraph *end );

    // return next and previous valid cursor positions.
    bool validCursorPosition(int idx);
    int nextCursorPosition(int idx);
    int previousCursorPosition(int idx);

private:
    void checkBidi() const;

    QVector<Q3TextStringChar> data;
    QString stringCache;
    uint bidiDirty : 1;
    uint bidi : 1; // true when the paragraph has right to left characters
    uint rightToLeft : 1;
    uint dir : 5;
};

inline bool Q3TextString::isBidi() const
{
    if (bidiDirty)
        checkBidi();
    return bidi;
}

inline bool Q3TextString::isRightToLeft() const
{
    if (bidiDirty)
        checkBidi();
    return rightToLeft;
}

inline QString Q3TextString::toString() const
{
    if (bidiDirty)
        checkBidi();
    return stringCache;
}

inline QChar::Direction Q3TextString::direction() const
{
    return rightToLeft ? QChar::DirR : QChar::DirL;
}

inline int Q3TextString::nextCursorPosition(int next)
{
    if (bidiDirty)
        checkBidi();

    const Q3TextStringChar *c = data.data();
    int len = length();

    if (next < len - 1) {
        next++;
        while (next < len - 1 && !c[next].charStop)
            next++;
    }
    return next;
}

inline int Q3TextString::previousCursorPosition(int prev)
{
    if (bidiDirty)
        checkBidi();

    const Q3TextStringChar *c = data.data();

    if (prev) {
        prev--;
        while (prev && !c[prev].charStop)
            prev--;
    }
    return prev;
}

inline bool Q3TextString::validCursorPosition(int idx)
{
    if (bidiDirty)
        checkBidi();

    return (at(idx).charStop);
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextCursor
{
public:
    Q3TextCursor(Q3TextDocument * = 0);
    Q3TextCursor(const Q3TextCursor &c);
    Q3TextCursor &operator=(const Q3TextCursor &c);
    virtual ~Q3TextCursor();

    bool operator==(const Q3TextCursor &c) const;
    bool operator!=(const Q3TextCursor &c) const { return !(*this == c); }

    inline Q3TextParagraph *paragraph() const { return para; }

    Q3TextDocument *document() const;
    int index() const;

    void gotoPosition(Q3TextParagraph* p, int index = 0);
    void setIndex(int index) { gotoPosition(paragraph(), index); }
    void setParagraph(Q3TextParagraph*p) { gotoPosition(p, 0); }

    void gotoLeft();
    void gotoRight();
    void gotoNextLetter();
    void gotoPreviousLetter();
    void gotoUp();
    void gotoDown();
    void gotoLineEnd();
    void gotoLineStart();
    void gotoHome();
    void gotoEnd();
    void gotoPageUp(int visibleHeight);
    void gotoPageDown(int visibleHeight);
    void gotoNextWord(bool onlySpace = false);
    void gotoPreviousWord(bool onlySpace = false);
    void gotoWordLeft();
    void gotoWordRight();

    void insert(const QString &s, bool checkNewLine, QVector<Q3TextStringChar> *formatting = 0);
    void splitAndInsertEmptyParagraph(bool ind = true, bool updateIds = true);
    bool remove();
    bool removePreviousChar();
    void indent();

    bool atParagStart();
    bool atParagEnd();

    int x() const; // x in current paragraph
    int y() const; // y in current paragraph

    int globalX() const;
    int globalY() const;

    Q3TextParagraph *topParagraph() const { return paras.isEmpty() ? para : paras.first(); }
    int offsetX() const { return ox; } // inner document  offset
    int offsetY() const { return oy; } // inner document offset
    int totalOffsetX() const; // total document offset
    int totalOffsetY() const; // total document offset

    bool place(const QPoint &pos, Q3TextParagraph *s) { return place(pos, s, false); }
    bool place(const QPoint &pos, Q3TextParagraph *s, bool link);
    void restoreState();


    int nestedDepth() const { return (int)indices.count(); } //### size_t/int cast
    void oneUp() { if (!indices.isEmpty()) pop(); }
    void setValid(bool b) { valid = b; }
    bool isValid() const { return valid; }

    void fixCursorPosition();
private:
    enum Operation { EnterBegin, EnterEnd, Next, Prev, Up, Down };

    void push();
    void pop();
    bool processNesting(Operation op);
    void invalidateNested();
    void gotoIntoNested(const QPoint &globalPos);

    Q3TextParagraph *para;
    int idx, tmpX;
    int ox, oy;
    QStack<int> indices;
    QStack<Q3TextParagraph*> paras;
    QStack<int> xOffsets;
    QStack<int> yOffsets;
    uint valid : 1;

};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextCommand
{
public:
    enum Commands { Invalid, Insert, Delete, Format, Style };

    Q3TextCommand(Q3TextDocument *dc) : doc(dc), cursor(dc) {}
    virtual ~Q3TextCommand();

    virtual Commands type() const;

    virtual Q3TextCursor *execute(Q3TextCursor *c) = 0;
    virtual Q3TextCursor *unexecute(Q3TextCursor *c) = 0;

protected:
    Q3TextDocument *doc;
    Q3TextCursor cursor;

};

class Q_COMPAT_EXPORT Q3TextCommandHistory
{
public:
    Q3TextCommandHistory(int s) : current(-1), steps(s) {  }
    virtual ~Q3TextCommandHistory(); // ### why is it virtual?

    void clear();

    void addCommand(Q3TextCommand *cmd);
    Q3TextCursor *undo(Q3TextCursor *c);
    Q3TextCursor *redo(Q3TextCursor *c);

    bool isUndoAvailable();
    bool isRedoAvailable();

    void setUndoDepth(int depth) { steps = depth; }
    int undoDepth() const { return steps; }

    int historySize() const { return history.count(); }
    int currentPosition() const { return current; }

private:
    QList<Q3TextCommand *> history;
    int current, steps;
};

inline Q3TextCommandHistory::~Q3TextCommandHistory()
{
    clear();
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ifndef QT_NO_TEXTCUSTOMITEM
class Q_COMPAT_EXPORT Q3TextCustomItem
{
public:
    Q3TextCustomItem(Q3TextDocument *p)
        :  xpos(0), ypos(-1), width(-1), height(0), parent(p)
    {}
    virtual ~Q3TextCustomItem();
    virtual void draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch,
                      const QPalette &pal, bool selected) = 0;

    virtual void adjustToPainter(QPainter*);

    enum Placement { PlaceInline = 0, PlaceLeft, PlaceRight };
    virtual Placement placement() const;
    bool placeInline() { return placement() == PlaceInline; }

    virtual bool ownLine() const;
    virtual void resize(int nwidth);
    virtual void invalidate();
    virtual int ascent() const { return height; }

    virtual bool isNested() const;
    virtual int minimumWidth() const;

    virtual QString richText() const;

    int xpos; // used for floating items
    int ypos; // used for floating items
    int width;
    int height;

    QRect geometry() const { return QRect(xpos, ypos, width, height); }

    virtual bool enter(Q3TextCursor *, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy, bool atEnd = false);
    virtual bool enterAt(Q3TextCursor *, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy, const QPoint &);
    virtual bool next(Q3TextCursor *, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);
    virtual bool prev(Q3TextCursor *, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);
    virtual bool down(Q3TextCursor *, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);
    virtual bool up(Q3TextCursor *, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);

    void setParagraph(Q3TextParagraph *p) { parag = p; }
    Q3TextParagraph *paragraph() const { return parag; }

    Q3TextDocument *parent;
    Q3TextParagraph *parag;

    virtual void pageBreak(int  y, Q3TextFlow* flow);
};
#endif


#ifndef QT_NO_TEXTCUSTOMITEM
class Q_COMPAT_EXPORT Q3TextImage : public Q3TextCustomItem
{
public:
    Q3TextImage(Q3TextDocument *p, const QMap<QString, QString> &attr, const QString& context,
                Q3MimeSourceFactory &factory);
    virtual ~Q3TextImage();

    Placement placement() const { return place; }
    void adjustToPainter(QPainter*);
    int minimumWidth() const { return width; }

    QString richText() const;

    void draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch,
               const QPalette &pal, bool selected);

private:
    QRegion* reg;
    QPixmap pm;
    Placement place;
    int tmpwidth, tmpheight;
    QMap<QString, QString> attributes;
    QString imgId;

};
#endif

#ifndef QT_NO_TEXTCUSTOMITEM
class Q_COMPAT_EXPORT Q3TextHorizontalLine : public Q3TextCustomItem
{
public:
    Q3TextHorizontalLine(Q3TextDocument *p, const QMap<QString, QString> &attr, const QString& context,
                         Q3MimeSourceFactory &factory);
    virtual ~Q3TextHorizontalLine();

    void adjustToPainter(QPainter*);
    void draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch,
              const QPalette &pal, bool selected);
    QString richText() const;

    bool ownLine() const { return true; }

private:
    int tmpheight;
    QColor color;
    bool shade;

};
#endif

class Q_COMPAT_EXPORT Q3TextFlow
{
    friend class Q3TextDocument;
#ifndef QT_NO_TEXTCUSTOMITEM
    friend class Q3TextTableCell;
#endif

public:
    Q3TextFlow();
    virtual ~Q3TextFlow();

    virtual void setWidth(int width);
    int width() const;

    virtual void setPageSize(int ps);
    int pageSize() const { return pagesize; }

    virtual int adjustLMargin(int yp, int h, int margin, int space);
    virtual int adjustRMargin(int yp, int h, int margin, int space);

#ifndef QT_NO_TEXTCUSTOMITEM
    virtual void registerFloatingItem(Q3TextCustomItem* item);
    virtual void unregisterFloatingItem(Q3TextCustomItem* item);
#endif
    virtual QRect boundingRect() const;
    virtual void drawFloatingItems(QPainter* p, int cx, int cy, int cw, int ch,
                                   const QPalette &pal, bool selected);

    virtual int adjustFlow(int  y, int w, int h); // adjusts y according to the defined pagesize. Returns the shift.

    virtual bool isEmpty();

    void clear();

private:
    int w;
    int pagesize;

#ifndef QT_NO_TEXTCUSTOMITEM
    QList<Q3TextCustomItem *> leftItems;
    QList<Q3TextCustomItem *> rightItems;
#endif
};

inline int Q3TextFlow::width() const { return w; }

#ifndef QT_NO_TEXTCUSTOMITEM
class Q3TextTable;

class Q_COMPAT_EXPORT Q3TextTableCell : public QLayoutItem
{
    friend class Q3TextTable;

public:
    Q3TextTableCell(Q3TextTable* table,
                    int row, int column,
                    const QMap<QString, QString> &attr,
                    const Q3StyleSheetItem* style,
                    const Q3TextFormat& fmt, const QString& context,
                    Q3MimeSourceFactory &factory, Q3StyleSheet *sheet, const QString& doc);
    virtual ~Q3TextTableCell();

    QSize sizeHint() const ;
    QSize minimumSize() const ;
    QSize maximumSize() const ;
    Qt::Orientations expandingDirections() const;
    bool isEmpty() const;
    void setGeometry(const QRect&) ;
    QRect geometry() const;

    bool hasHeightForWidth() const;
    int heightForWidth(int) const;

    void adjustToPainter(QPainter*);

    int row() const { return row_; }
    int column() const { return col_; }
    int rowspan() const { return rowspan_; }
    int colspan() const { return colspan_; }
    int stretch() const { return stretch_; }

    Q3TextDocument* richText()  const { return richtext; }
    Q3TextTable* table() const { return parent; }

    void draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch,
               const QPalette &cg, bool selected);

    QBrush *backGround() const { return background; }
    virtual void invalidate();

    int verticalAlignmentOffset() const;
    int horizontalAlignmentOffset() const;

private:
    QRect geom;
    Q3TextTable* parent;
    Q3TextDocument* richtext;
    int row_;
    int col_;
    int rowspan_;
    int colspan_;
    int stretch_;
    int maxw;
    int minw;
    bool hasFixedWidth;
    QBrush *background;
    int cached_width;
    int cached_sizehint;
    QMap<QString, QString> attributes;
    int align;
};
#endif


#ifndef QT_NO_TEXTCUSTOMITEM
class Q_COMPAT_EXPORT Q3TextTable: public Q3TextCustomItem
{
    friend class Q3TextTableCell;

public:
    Q3TextTable(Q3TextDocument *p, const QMap<QString, QString> &attr);
    virtual ~Q3TextTable();

    void adjustToPainter(QPainter *p);
    void pageBreak(int  y, Q3TextFlow* flow);
    void draw(QPainter* p, int x, int y, int cx, int cy, int cw, int ch,
               const QPalette &pal, bool selected);

    bool noErase() const { return true; }
    bool ownLine() const { return true; }
    Placement placement() const { return place; }
    bool isNested() const { return true; }
    void resize(int nwidth);
    virtual void invalidate();

    virtual bool enter(Q3TextCursor *c, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy, bool atEnd = false);
    virtual bool enterAt(Q3TextCursor *c, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy, const QPoint &pos);
    virtual bool next(Q3TextCursor *c, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);
    virtual bool prev(Q3TextCursor *c, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);
    virtual bool down(Q3TextCursor *c, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);
    virtual bool up(Q3TextCursor *c, Q3TextDocument *&doc, Q3TextParagraph *&parag, int &idx, int &ox, int &oy);

    QString richText() const;

    int minimumWidth() const;

    QList<Q3TextTableCell *> tableCells() const { return cells; }

    bool isStretching() const { return stretch; }

private:
    void format(int w);
    void addCell(Q3TextTableCell* cell);

private:
    QGridLayout* layout;
    QList<Q3TextTableCell *> cells;
    int cachewidth;
    int fixwidth;
    int cellpadding;
    int cellspacing;
    int border;
    int outerborder;
    int stretch;
    int innerborder;
    int us_cp, us_ib, us_b, us_ob, us_cs;
    int us_fixwidth;
    QMap<QString, QString> attributes;
    QMap<Q3TextCursor*, int> currCell;
    Placement place;
    void adjustCells(int y , int shift);
    int pageBreakFor;
};
#endif
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ifndef QT_NO_TEXTCUSTOMITEM
class Q3TextTableCell;
class Q3TextParagraph;
#endif

struct Q_COMPAT_EXPORT Q3TextDocumentSelection
{
    Q3TextCursor startCursor, endCursor;
    bool swapped;
    Q_DUMMY_COMPARISON_OPERATOR(Q3TextDocumentSelection)
};

class Q_COMPAT_EXPORT Q3TextDocument : public QObject
{
    Q_OBJECT

#ifndef QT_NO_TEXTCUSTOMITEM
    friend class Q3TextTableCell;
#endif
    friend class Q3TextCursor;
    friend class Q3TextEdit;
    friend class Q3TextParagraph;

public:
    enum SelectionIds {
        Standard = 0,
        Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to
        // remove multiple lines with removeSelectedText()
    };

    Q3TextDocument(Q3TextDocument *p);
    virtual ~Q3TextDocument();

    Q3TextDocument *parent() const { return par; }
    Q3TextParagraph *parentParagraph() const { return parentPar; }

    void setText(const QString &text, const QString &context);
    QMap<QString, QString> attributes() const { return attribs; }
    void setAttributes(const QMap<QString, QString> &attr) { attribs = attr; }

    QString text() const;
    QString text(int parag) const;
    QString originalText() const;

    int x() const;
    int y() const;
    int width() const;
    int widthUsed() const;
    int visibleWidth() const;
    int height() const;
    void setWidth(int w);
    int minimumWidth() const;
    bool setMinimumWidth(int needed, int used = -1, Q3TextParagraph *parag = 0);

    void setY(int y);
    int leftMargin() const;
    void setLeftMargin(int lm);
    int rightMargin() const;
    void setRightMargin(int rm);

    Q3TextParagraph *firstParagraph() const;
    Q3TextParagraph *lastParagraph() const;
    void setFirstParagraph(Q3TextParagraph *p);
    void setLastParagraph(Q3TextParagraph *p);

    void invalidate();

    void setPreProcessor(Q3TextPreProcessor *sh);
    Q3TextPreProcessor *preProcessor() const;

    void setFormatter(Q3TextFormatter *f);
    Q3TextFormatter *formatter() const;

    void setIndent(Q3TextIndent *i);
    Q3TextIndent *indent() const;

    QColor selectionColor(int id) const;
    QColor selectionTextColor(int id) const;
    bool hasSelectionTextColor(int id) const;
    void setSelectionColor(int id, const QColor &c);
    void setSelectionTextColor(int id, const QColor &b);
    bool hasSelection(int id, bool visible = false) const;
    void setSelectionStart(int id, const Q3TextCursor &cursor);
    bool setSelectionEnd(int id, const Q3TextCursor &cursor);
    void selectAll(int id);
    bool removeSelection(int id);
    void selectionStart(int id, int &paragId, int &index);
    Q3TextCursor selectionStartCursor(int id);
    Q3TextCursor selectionEndCursor(int id);
    void selectionEnd(int id, int &paragId, int &index);
    void setFormat(int id, Q3TextFormat *f, int flags);
    int numSelections() const { return nSelections; }
    void addSelection(int id);

    QString selectedText(int id, bool asRichText = false) const;
    void removeSelectedText(int id, Q3TextCursor *cursor);
    void indentSelection(int id);

    Q3TextParagraph *paragAt(int i) const;

    void addCommand(Q3TextCommand *cmd);
    Q3TextCursor *undo(Q3TextCursor *c = 0);
    Q3TextCursor *redo(Q3TextCursor *c  = 0);
    Q3TextCommandHistory *commands() const { return commandHistory; }

    Q3TextFormatCollection *formatCollection() const;

    bool find(Q3TextCursor &cursor, const QString &expr, bool cs, bool wo, bool forward);

    void setTextFormat(Qt::TextFormat f);
    Qt::TextFormat textFormat() const;

    bool inSelection(int selId, const QPoint &pos) const;

    Q3StyleSheet *styleSheet() const { return sheet_; }
#ifndef QT_NO_MIME
    Q3MimeSourceFactory *mimeSourceFactory() const { return factory_; }
#endif
    QString context() const { return contxt; }

    void setStyleSheet(Q3StyleSheet *s);
    void setDefaultFormat(const QFont &font, const QColor &color);
#ifndef QT_NO_MIME
    void setMimeSourceFactory(Q3MimeSourceFactory *f) { if (f) factory_ = f; }
#endif
    void setContext(const QString &c) { if (!c.isEmpty()) contxt = c; }

    void setUnderlineLinks(bool b);
    bool underlineLinks() const { return underlLinks; }

    void setPaper(QBrush *brush) { if (backBrush) delete backBrush; backBrush = brush; }
    QBrush *paper() const { return backBrush; }

    void doLayout(QPainter *p, int w);
    void draw(QPainter *p, const QRect& rect, const QPalette &pal, const QBrush *paper = 0);

    void drawParagraph(QPainter *p, Q3TextParagraph *parag, int cx, int cy, int cw, int ch,
                    QPixmap *&doubleBuffer, const QPalette &pal,
                    bool drawCursor, Q3TextCursor *cursor, bool resetChanged = true);
    Q3TextParagraph *draw(QPainter *p, int cx, int cy, int cw, int ch, const QPalette &pal,
                      bool onlyChanged = false, bool drawCursor = false, Q3TextCursor *cursor = 0,
                      bool resetChanged = true);

#ifndef QT_NO_TEXTCUSTOMITEM
    static Q3TextCustomItem* tag(Q3StyleSheet *sheet, const QString& name,
                                 const QMap<QString, QString> &attr,
                                 const QString& context,
                                 const Q3MimeSourceFactory& factory,
                                 bool emptyTag, Q3TextDocument *doc);
#endif

#ifndef QT_NO_TEXTCUSTOMITEM
    void registerCustomItem(Q3TextCustomItem *i, Q3TextParagraph *p);
    void unregisterCustomItem(Q3TextCustomItem *i, Q3TextParagraph *p);
#endif

    void setFlow(Q3TextFlow *f);
    void takeFlow();
    Q3TextFlow *flow() const { return flow_; }
    bool isPageBreakEnabled() const { return pages; }
    void setPageBreakEnabled(bool b) { pages = b; }

    void setUseFormatCollection(bool b) { useFC = b; }
    bool useFormatCollection() const { return useFC; }

#ifndef QT_NO_TEXTCUSTOMITEM
    Q3TextTableCell *tableCell() const { return tc; }
    void setTableCell(Q3TextTableCell *c) { tc = c; }
#endif

    void setPlainText(const QString &text);
    void setRichText(const QString &text, const QString &context, const Q3TextFormat *initialFormat = 0);
    QString richText() const;
    QString plainText() const;

    bool focusNextPrevChild(bool next);

    int alignment() const;
    void setAlignment(int a);

    int *tabArray() const;
    int tabStopWidth() const;
    void setTabArray(int *a);
    void setTabStops(int tw);

    void setUndoDepth(int depth) { commandHistory->setUndoDepth(depth); }
    int undoDepth() const { return commandHistory->undoDepth(); }

    int length() const;
    void clear(bool createEmptyParag = false);

    virtual Q3TextParagraph *createParagraph(Q3TextDocument *, Q3TextParagraph *pr = 0, Q3TextParagraph *nx = 0, bool updateIds = true);
    void insertChild(Q3TextDocument *dc) { childList.append(dc); }
    void removeChild(Q3TextDocument *dc) { childList.removeAll(dc); }
    QList<Q3TextDocument *> children() const { return childList; }

    bool hasFocusParagraph() const;
    QString focusHref() const;
    QString focusName() const;

    void invalidateOriginalText() { oTextValid = false; oText = QLatin1String(""); }

Q_SIGNALS:
    void minimumWidthChanged(int);

private:
    Q_DISABLE_COPY(Q3TextDocument)

    void init();
    QPixmap *bufferPixmap(const QSize &s);
    // HTML parser
    bool hasPrefix(const QChar* doc, int length, int pos, QChar c);
    bool hasPrefix(const QChar* doc, int length, int pos, const QString& s);
#ifndef QT_NO_TEXTCUSTOMITEM
    Q3TextCustomItem* parseTable(const QMap<QString, QString> &attr, const Q3TextFormat &fmt,
                                 const QChar* doc, int length, int& pos, Q3TextParagraph *curpar);
#endif
    bool eatSpace(const QChar* doc, int length, int& pos, bool includeNbsp = false);
    bool eat(const QChar* doc, int length, int& pos, QChar c);
    QString parseOpenTag(const QChar* doc, int length, int& pos, QMap<QString, QString> &attr, bool& emptyTag);
    QString parseCloseTag(const QChar* doc, int length, int& pos);
    QChar parseHTMLSpecialChar(const QChar* doc, int length, int& pos);
    QString parseWord(const QChar* doc, int length, int& pos, bool lower = true);
    QChar parseChar(const QChar* doc, int length, int& pos, Q3StyleSheetItem::WhiteSpaceMode wsm);
    void setRichTextInternal(const QString &text, Q3TextCursor* cursor = 0, const Q3TextFormat *initialFormat = 0);
    void setRichTextMarginsInternal(QList< QVector<Q3StyleSheetItem *> *>& styles, Q3TextParagraph* stylesPar);

    struct Q_COMPAT_EXPORT Focus {
        Q3TextParagraph *parag;
        int start, len;
        QString href;
        QString name;
    };

    int cx, cy, cw, vw;
    Q3TextParagraph *fParag, *lParag;
    Q3TextPreProcessor *pProcessor;
    struct SelectionColor {
        QColor background;
        QColor text;
    };
    QMap<int, SelectionColor> selectionColors;
    QMap<int, Q3TextDocumentSelection> selections;
    Q3TextCommandHistory *commandHistory;
    Q3TextFormatter *pFormatter;
    Q3TextIndent *indenter;
    Q3TextFormatCollection *fCollection;
    Qt::TextFormat txtFormat;
    uint preferRichText : 1;
    uint pages : 1;
    uint useFC : 1;
    uint withoutDoubleBuffer : 1;
    uint underlLinks : 1;
    uint nextDoubleBuffered : 1;
    uint oTextValid : 1;
    uint mightHaveCustomItems : 1;
    int align;
    int nSelections;
    Q3TextFlow *flow_;
    Q3TextDocument *par;
    Q3TextParagraph *parentPar;
#ifndef QT_NO_TEXTCUSTOMITEM
    Q3TextTableCell *tc;
#endif
    QBrush *backBrush;
    QPixmap *buf_pixmap;
    Focus focusIndicator;
    int minw;
    int wused;
    int leftmargin;
    int rightmargin;
    Q3TextParagraph *minwParag, *curParag;
    Q3StyleSheet* sheet_;
#ifndef QT_NO_MIME
    Q3MimeSourceFactory* factory_;
#endif
    QString contxt;
    QMap<QString, QString> attribs;
    int *tArray;
    int tStopWidth;
    int uDepth;
    QString oText;
    QList<Q3TextDocument *> childList;
    QColor linkColor, bodyText;
    double scaleFontsFactor;

    short list_tm,list_bm, list_lm, li_tm, li_bm, par_tm, par_bm;
};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


class Q_COMPAT_EXPORT Q3TextDeleteCommand : public Q3TextCommand
{
public:
    Q3TextDeleteCommand(Q3TextDocument *dc, int i, int idx, const QVector<Q3TextStringChar> &str,
                        const QByteArray& oldStyle);
    Q3TextDeleteCommand(Q3TextParagraph *p, int idx, const QVector<Q3TextStringChar> &str);
    virtual ~Q3TextDeleteCommand();

    Commands type() const { return Delete; }
    Q3TextCursor *execute(Q3TextCursor *c);
    Q3TextCursor *unexecute(Q3TextCursor *c);

protected:
    int id, index;
    Q3TextParagraph *parag;
    QVector<Q3TextStringChar> text;
    QByteArray styleInformation;

};

class Q_COMPAT_EXPORT Q3TextInsertCommand : public Q3TextDeleteCommand
{
public:
    Q3TextInsertCommand(Q3TextDocument *dc, int i, int idx, const QVector<Q3TextStringChar> &str,
                        const QByteArray& oldStyleInfo)
        : Q3TextDeleteCommand(dc, i, idx, str, oldStyleInfo) {}
    Q3TextInsertCommand(Q3TextParagraph *p, int idx, const QVector<Q3TextStringChar> &str)
        : Q3TextDeleteCommand(p, idx, str) {}
    virtual ~Q3TextInsertCommand() {}

    Commands type() const { return Insert; }
    Q3TextCursor *execute(Q3TextCursor *c) { return Q3TextDeleteCommand::unexecute(c); }
    Q3TextCursor *unexecute(Q3TextCursor *c) { return Q3TextDeleteCommand::execute(c); }

};

class Q_COMPAT_EXPORT Q3TextFormatCommand : public Q3TextCommand
{
public:
    Q3TextFormatCommand(Q3TextDocument *dc, int sid, int sidx, int eid, int eidx, const QVector<Q3TextStringChar> &old, Q3TextFormat *f, int fl);
    virtual ~Q3TextFormatCommand();

    Commands type() const { return Format; }
    Q3TextCursor *execute(Q3TextCursor *c);
    Q3TextCursor *unexecute(Q3TextCursor *c);

protected:
    int startId, startIndex, endId, endIndex;
    Q3TextFormat *format;
    QVector<Q3TextStringChar> oldFormats;
    int flags;

};

class Q_COMPAT_EXPORT Q3TextStyleCommand : public Q3TextCommand
{
public:
    Q3TextStyleCommand(Q3TextDocument *dc, int fParag, int lParag, const QByteArray& beforeChange );
    virtual ~Q3TextStyleCommand() {}

    Commands type() const { return Style; }
    Q3TextCursor *execute(Q3TextCursor *c);
    Q3TextCursor *unexecute(Q3TextCursor *c);

    static QByteArray readStyleInformation( Q3TextDocument* dc, int fParag, int lParag);
    static void writeStyleInformation( Q3TextDocument* dc, int fParag, const QByteArray& style);

private:
    int firstParag, lastParag;
    QByteArray before;
    QByteArray after;
};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

struct Q_COMPAT_EXPORT Q3TextParagraphSelection
{
    int start, end;
    Q_DUMMY_COMPARISON_OPERATOR(Q3TextParagraphSelection)
};

struct Q_COMPAT_EXPORT QTextLineStart
{
    QTextLineStart() : y(0), baseLine(0), h(0)
    {  }
    QTextLineStart(int y_, int bl, int h_) : y(y_), baseLine(bl), h(h_),
        w(0)
    {  }

public:
    int y, baseLine, h;
    int w;
};

class Q_COMPAT_EXPORT Q3TextParagraphData
{
public:
    Q3TextParagraphData() {}
    virtual ~Q3TextParagraphData();
    virtual void join(Q3TextParagraphData *);
};

class Q3TextParagraphPseudoDocument;

class Q3SyntaxHighlighter;

class Q_COMPAT_EXPORT Q3TextParagraph
{
    friend class Q3TextDocument;
    friend class Q3TextCursor;
    friend class Q3SyntaxHighlighter;

public:
    Q3TextParagraph(Q3TextDocument *dc, Q3TextParagraph *pr = 0, Q3TextParagraph *nx = 0, bool updateIds = true);
    ~Q3TextParagraph();

    Q3TextString *string() const;
    Q3TextStringChar *at(int i) const; // maybe remove later
    int leftGap() const;
    int length() const; // maybe remove later

    void setListStyle(Q3StyleSheetItem::ListStyle ls) { lstyle = ls; changed = true; }
    Q3StyleSheetItem::ListStyle listStyle() const { return (Q3StyleSheetItem::ListStyle)lstyle; }
    void setListItem(bool li);
    bool isListItem() const { return litem; }
    void setListValue(int v) { list_val = v; }
    int listValue() const { return list_val > 0 ? list_val : -1; }

    void setListDepth(int depth);
    int listDepth() const { return ldepth; }

//     void setFormat(Q3TextFormat *fm);
//     Q3TextFormat *paragFormat() const;

    inline Q3TextDocument *document() const {
        if (hasdoc) return (Q3TextDocument*) docOrPseudo;
        return 0;
    }
    Q3TextParagraphPseudoDocument *pseudoDocument() const;

    QRect rect() const;
    void setHeight(int h) { r.setHeight(h); }
    void show();
    void hide();
    bool isVisible() const { return visible; }

    Q3TextParagraph *prev() const;
    Q3TextParagraph *next() const;
    void setPrev(Q3TextParagraph *s);
    void setNext(Q3TextParagraph *s);

    void insert(int index, const QString &s);
    void insert(int index, const QChar *unicode, int len);
    void append(const QString &s, bool reallyAtEnd = false);
    void truncate(int index);
    void remove(int index, int len);
    void join(Q3TextParagraph *s);

    void invalidate(int chr);

    void move(int &dy);
    void format(int start = -1, bool doMove = true);

    bool isValid() const;
    bool hasChanged() const;
    void setChanged(bool b, bool recursive = false);

    int lineHeightOfChar(int i, int *bl = 0, int *y = 0) const;
    Q3TextStringChar *lineStartOfChar(int i, int *index = 0, int *line = 0) const;
    int lines() const;
    Q3TextStringChar *lineStartOfLine(int line, int *index = 0) const;
    int lineY(int l) const;
    int lineBaseLine(int l) const;
    int lineHeight(int l) const;
    void lineInfo(int l, int &y, int &h, int &bl) const;

    void setSelection(int id, int start, int end);
    void removeSelection(int id);
    int selectionStart(int id) const;
    int selectionEnd(int id) const;
    bool hasSelection(int id) const;
    bool hasAnySelection() const;
    bool fullSelected(int id) const;

    void setEndState(int s);
    int endState() const;

    void setParagId(int i);
    int paragId() const;

    bool firstPreProcess() const;
    void setFirstPreProcess(bool b);

    void indent(int *oldIndent = 0, int *newIndent = 0);

    void setExtraData(Q3TextParagraphData *data);
    Q3TextParagraphData *extraData() const;

    QMap<int, QTextLineStart*> &lineStartList();

    void setFormat(int index, int len, Q3TextFormat *f, bool useCollection = true, int flags = -1);

    void setAlignment(int a);
    int alignment() const;

    void paint(QPainter &painter, const QPalette &pal, Q3TextCursor *cursor = 0,
                bool drawSelections = false, int clipx = -1, int clipy = -1,
                int clipw = -1, int cliph = -1);

    int topMargin() const;
    int bottomMargin() const;
    int leftMargin() const;
    int firstLineMargin() const;
    int rightMargin() const;
    int lineSpacing() const;

#ifndef QT_NO_TEXTCUSTOMITEM
    void registerFloatingItem(Q3TextCustomItem *i);
    void unregisterFloatingItem(Q3TextCustomItem *i);
#endif

    void setFullWidth(bool b) { fullWidth = b; }
    bool isFullWidth() const { return fullWidth; }

#ifndef QT_NO_TEXTCUSTOMITEM
    Q3TextTableCell *tableCell() const;
#endif

    QBrush *background() const;

    int documentWidth() const;
    int documentVisibleWidth() const;
    int documentX() const;
    int documentY() const;
    Q3TextFormatCollection *formatCollection() const;
    Q3TextFormatter *formatter() const;

    int nextTab(int i, int x);
    int *tabArray() const;
    void setTabArray(int *a);
    void setTabStops(int tw);

    void adjustToPainter(QPainter *p);

    void setNewLinesAllowed(bool b);
    bool isNewLinesAllowed() const;

    QString richText() const;

    void addCommand(Q3TextCommand *cmd);
    Q3TextCursor *undo(Q3TextCursor *c = 0);
    Q3TextCursor *redo(Q3TextCursor *c  = 0);
    Q3TextCommandHistory *commands() const;
    void copyParagData(Q3TextParagraph *parag);

    void setBreakable(bool b) { breakable = b; }
    bool isBreakable() const { return breakable; }

    void setBackgroundColor(const QColor &c);
    QColor *backgroundColor() const { return bgcol; }
    void clearBackgroundColor();

    void setMovedDown(bool b) { movedDown = b; }
    bool wasMovedDown() const { return movedDown; }

    void setDirection(QChar::Direction);
    QChar::Direction direction() const;
    void setPaintDevice(QPaintDevice *pd) { paintdevice = pd; }

    void readStyleInformation(QDataStream& stream);
    void writeStyleInformation(QDataStream& stream) const;

protected:
    void setColorForSelection(QColor &c, QPainter &p, const QPalette &pal, int selection);
    void drawLabel(QPainter* p, int x, int y, int w, int h, int base, const QPalette &pal);
    void drawString(QPainter &painter, const QString &str, int start, int len, int xstart,
                             int y, int baseLine, int w, int h, bool drawSelections, int fullSelectionWidth,
                             Q3TextStringChar *formatChar, const QPalette &pal,
                             bool rightToLeft);

private:
    QMap<int, Q3TextParagraphSelection> &selections() const;
#ifndef QT_NO_TEXTCUSTOMITEM
    QList<Q3TextCustomItem *> &floatingItems() const;
#endif
    inline QBrush backgroundBrush(const QPalette &pal) {
        if (bgcol)
            return *bgcol;
        return pal.brush(QPalette::Base);
    }
    void invalidateStyleCache();

    QMap<int, QTextLineStart*> lineStarts;
    QRect r;
    Q3TextParagraph *p, *n;
    void *docOrPseudo;
    uint changed : 1;
    uint firstFormat : 1;
    uint firstPProcess : 1;
    uint needPreProcess : 1;
    uint fullWidth : 1;
    uint lastInFrame : 1;
    uint visible : 1;
    uint breakable : 1;
    uint movedDown : 1;
    uint mightHaveCustomItems : 1;
    uint hasdoc : 1;
    uint litem : 1; // whether the paragraph is a list item
    uint rtext : 1; // whether the paragraph needs rich text margin
    signed int align : 5;
    uint /*Q3StyleSheetItem::ListStyle*/ lstyle : 4;
    int invalid;
    int state, id;
    Q3TextString *str;
    QMap<int, Q3TextParagraphSelection> *mSelections;
#ifndef QT_NO_TEXTCUSTOMITEM
    QList<Q3TextCustomItem *> *mFloatingItems;
#endif
    short utm, ubm, ulm, urm, uflm, ulinespacing;
    short tabStopWidth, minwidth;
    int *tArray;
    Q3TextParagraphData *eData;
    short list_val;
    ushort ldepth;
    QColor *bgcol;
    QPaintDevice *paintdevice;
};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextFormatter
{
public:
    Q3TextFormatter();
    virtual ~Q3TextFormatter();

    virtual int format(Q3TextDocument *doc, Q3TextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts) = 0;
    virtual int formatVertically(Q3TextDocument* doc, Q3TextParagraph* parag);

    bool isWrapEnabled(Q3TextParagraph *p) const { if (!wrapEnabled) return false; if (p && !p->isBreakable()) return false; return true;}
    int wrapAtColumn() const { return wrapColumn;}
    virtual void setWrapEnabled(bool b);
    virtual void setWrapAtColumn(int c);
    virtual void setAllowBreakInWords(bool b) { biw = b; }
    bool allowBreakInWords() const { return biw; }

    int minimumWidth() const { return thisminw; }
    int widthUsed() const { return thiswused; }

protected:
    virtual QTextLineStart *formatLine(Q3TextParagraph *parag, Q3TextString *string, QTextLineStart *line, Q3TextStringChar *start,
                                               Q3TextStringChar *last, int align = Qt::AlignAuto, int space = 0);
#ifndef QT_NO_COMPLEXTEXT
    virtual QTextLineStart *bidiReorderLine(Q3TextParagraph *parag, Q3TextString *string, QTextLineStart *line, Q3TextStringChar *start,
                                                    Q3TextStringChar *last, int align, int space);
#endif
    void insertLineStart(Q3TextParagraph *parag, int index, QTextLineStart *ls);

    int thisminw;
    int thiswused;

private:
    bool wrapEnabled;
    int wrapColumn;
    bool biw;
};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextFormatterBreakInWords : public Q3TextFormatter
{
public:
    Q3TextFormatterBreakInWords();
    virtual ~Q3TextFormatterBreakInWords() {}

    int format(Q3TextDocument *doc, Q3TextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts);

};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextFormatterBreakWords : public Q3TextFormatter
{
public:
    Q3TextFormatterBreakWords();
    virtual ~Q3TextFormatterBreakWords() {}

    int format(Q3TextDocument *doc, Q3TextParagraph *parag, int start, const QMap<int, QTextLineStart*> &oldLineStarts);

};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextIndent
{
public:
    Q3TextIndent();
    virtual ~Q3TextIndent() {}

    virtual void indent(Q3TextDocument *doc, Q3TextParagraph *parag, int *oldIndent = 0, int *newIndent = 0) = 0;

};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextPreProcessor
{
public:
    enum Ids {
        Standard = 0
    };

    Q3TextPreProcessor();
    virtual ~Q3TextPreProcessor() {}

    virtual void process(Q3TextDocument *doc, Q3TextParagraph *, int, bool = true) = 0;
    virtual Q3TextFormat *format(int id) = 0;

};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextFormat
{
    friend class Q3TextFormatCollection;
    friend class Q3TextDocument;

public:
    enum Flags {
        NoFlags,
        Bold = 1,
        Italic = 2,
        Underline = 4,
        Family = 8,
        Size = 16,
        Color = 32,
        Misspelled = 64,
        VAlign = 128,
        StrikeOut= 256,
        Font = Bold | Italic | Underline | Family | Size | StrikeOut,
        Format = Font | Color | Misspelled | VAlign
    };

    enum VerticalAlignment { AlignNormal, AlignSuperScript, AlignSubScript };

    Q3TextFormat();
    virtual ~Q3TextFormat();

    Q3TextFormat(const Q3StyleSheetItem *s);
    Q3TextFormat(const QFont &f, const QColor &c, Q3TextFormatCollection *parent = 0);
    Q3TextFormat(const Q3TextFormat &fm);
    Q3TextFormat makeTextFormat(const Q3StyleSheetItem *style, const QMap<QString,QString>& attr, double scaleFontsFactor) const;
    Q3TextFormat& operator=(const Q3TextFormat &fm);
    QColor color() const;
    QFont font() const;
    QFontMetrics fontMetrics() const { return fm; }
    bool isMisspelled() const;
    VerticalAlignment vAlign() const;
    int minLeftBearing() const;
    int minRightBearing() const;
    int width(const QChar &c) const;
    int width(const QString &str, int pos) const;
    int height() const;
    int ascent() const;
    int descent() const;
    int leading() const;
    bool useLinkColor() const;

    void setBold(bool b);
    void setItalic(bool b);
    void setUnderline(bool b);
    void setStrikeOut(bool b);
    void setFamily(const QString &f);
    void setPointSize(int s);
    void setFont(const QFont &f);
    void setColor(const QColor &c);
    void setMisspelled(bool b);
    void setVAlign(VerticalAlignment a);

    bool operator==(const Q3TextFormat &f) const;
    Q3TextFormatCollection *parent() const;
    const QString &key() const;

    static QString getKey(const QFont &f, const QColor &c, bool misspelled, VerticalAlignment vAlign);

    void addRef();
    void removeRef();

    QString makeFormatChangeTags(Q3TextFormat* defaultFormat, Q3TextFormat *f, const QString& oldAnchorHref, const QString& anchorHref) const;
    QString makeFormatEndTags(Q3TextFormat* defaultFormat, const QString& anchorHref) const;

    static void setPainter(QPainter *p);
    static QPainter* painter();

    bool fontSizesInPixels() { return usePixelSizes; }

protected:
    virtual void generateKey();

private:
    void update();
    static void applyFont(const QFont &f);

private:
    QFont fn;
    QColor col;
    QFontMetrics fm;
    uint missp : 1;
    uint linkColor : 1;
    uint usePixelSizes : 1;
    int leftBearing, rightBearing;
    VerticalAlignment ha;
    uchar widths[256];
    int hei, asc, dsc;
    Q3TextFormatCollection *collection;
    int ref;
    QString k;
    int logicalFontSize;
    int stdSize;
    static QPainter *pntr;
    static QFontMetrics *pntr_fm;
    static int pntr_asc;
    static int pntr_hei;
    static int pntr_ldg;
    static int pntr_dsc;

};

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class Q_COMPAT_EXPORT Q3TextFormatCollection
{
    friend class Q3TextDocument;
    friend class Q3TextFormat;

public:
    Q3TextFormatCollection();
    virtual ~Q3TextFormatCollection();

    void setDefaultFormat(Q3TextFormat *f);
    Q3TextFormat *defaultFormat() const;
    virtual Q3TextFormat *format(Q3TextFormat *f);
    virtual Q3TextFormat *format(Q3TextFormat *of, Q3TextFormat *nf, int flags);
    virtual Q3TextFormat *format(const QFont &f, const QColor &c);
    virtual void remove(Q3TextFormat *f);
    virtual Q3TextFormat *createFormat(const Q3TextFormat &f) { return new Q3TextFormat(f); }
    virtual Q3TextFormat *createFormat(const QFont &f, const QColor &c) { return new Q3TextFormat(f, c, this); }

    void updateDefaultFormat(const QFont &font, const QColor &c, Q3StyleSheet *sheet);

    QPaintDevice *paintDevice() const { return paintdevice; }
    void setPaintDevice(QPaintDevice *);

private:
    void updateKeys();

private:
    Q3TextFormat *defFormat, *lastFormat, *cachedFormat;
    QHash<QString, Q3TextFormat *> cKey;
    Q3TextFormat *cres;
    QFont cfont;
    QColor ccol;
    QString kof, knf;
    int cflags;

    QPaintDevice *paintdevice;
};

class Q_COMPAT_EXPORT Q3TextParagraphPseudoDocument
{
public:
    Q3TextParagraphPseudoDocument();
    ~Q3TextParagraphPseudoDocument();
    QRect docRect;
    Q3TextFormatter *pFormatter;
    Q3TextCommandHistory *commandHistory;
    int minw;
    int wused;
    Q3TextFormatCollection collection;
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

inline int Q3TextParagraph::length() const
{
    return str->length();
}

inline QRect Q3TextParagraph::rect() const
{
    return r;
}

inline int Q3TextCursor::index() const
{
    return idx;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

inline int Q3TextDocument::x() const
{
    return cx;
}

inline int Q3TextDocument::y() const
{
    return cy;
}

inline int Q3TextDocument::width() const
{
    return qMax(cw, flow_->width());
}

inline int Q3TextDocument::visibleWidth() const
{
    return vw;
}

inline Q3TextParagraph *Q3TextDocument::firstParagraph() const
{
    return fParag;
}

inline Q3TextParagraph *Q3TextDocument::lastParagraph() const
{
    return lParag;
}

inline void Q3TextDocument::setFirstParagraph(Q3TextParagraph *p)
{
    fParag = p;
}

inline void Q3TextDocument::setLastParagraph(Q3TextParagraph *p)
{
    lParag = p;
}

inline void Q3TextDocument::setWidth(int w)
{
    cw = qMax(w, minw);
    flow_->setWidth(cw);
    vw = w;
}

inline int Q3TextDocument::minimumWidth() const
{
    return minw;
}

inline void Q3TextDocument::setY(int y)
{
    cy = y;
}

inline int Q3TextDocument::leftMargin() const
{
    return leftmargin;
}

inline void Q3TextDocument::setLeftMargin(int lm)
{
    leftmargin = lm;
}

inline int Q3TextDocument::rightMargin() const
{
    return rightmargin;
}

inline void Q3TextDocument::setRightMargin(int rm)
{
    rightmargin = rm;
}

inline Q3TextPreProcessor *Q3TextDocument::preProcessor() const
{
    return pProcessor;
}

inline void Q3TextDocument::setPreProcessor(Q3TextPreProcessor * sh)
{
    pProcessor = sh;
}

inline void Q3TextDocument::setFormatter(Q3TextFormatter *f)
{
    delete pFormatter;
    pFormatter = f;
}

inline Q3TextFormatter *Q3TextDocument::formatter() const
{
    return pFormatter;
}

inline void Q3TextDocument::setIndent(Q3TextIndent *i)
{
    indenter = i;
}

inline Q3TextIndent *Q3TextDocument::indent() const
{
    return indenter;
}

inline QColor Q3TextDocument::selectionColor(int id) const
{
    const Q3TextDocument *p = this;
    while (p->par)
        p = p->par;
    return p->selectionColors[id].background;
}

inline QColor Q3TextDocument::selectionTextColor(int id) const
{
    const Q3TextDocument *p = this;
    while (p->par)
        p = p->par;
    return p->selectionColors[id].text;
}

inline bool Q3TextDocument::hasSelectionTextColor(int id) const
{
    const Q3TextDocument *p = this;
    while (p->par)
        p = p->par;
    return p->selectionColors.contains(id);
}

inline void Q3TextDocument::setSelectionColor(int id, const QColor &c)
{
    Q3TextDocument *p = this;
    while (p->par)
        p = p->par;
    p->selectionColors[id].background = c;
}

inline void Q3TextDocument::setSelectionTextColor(int id, const QColor &c)
{
    Q3TextDocument *p = this;
    while (p->par)
        p = p->par;
    p->selectionColors[id].text = c;
}

inline Q3TextFormatCollection *Q3TextDocument::formatCollection() const
{
    return fCollection;
}

inline int Q3TextDocument::alignment() const
{
    return align;
}

inline void Q3TextDocument::setAlignment(int a)
{
    align = a;
}

inline int *Q3TextDocument::tabArray() const
{
    return tArray;
}

inline int Q3TextDocument::tabStopWidth() const
{
    return tStopWidth;
}

inline void Q3TextDocument::setTabArray(int *a)
{
    tArray = a;
}

inline void Q3TextDocument::setTabStops(int tw)
{
    tStopWidth = tw;
}

inline QString Q3TextDocument::originalText() const
{
    if (oTextValid)
        return oText;
    return text();
}

inline void Q3TextDocument::setFlow(Q3TextFlow *f)
{
    if (flow_)
        delete flow_;
    flow_ = f;
}

inline void Q3TextDocument::takeFlow()
{
    flow_ = 0;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

inline QColor Q3TextFormat::color() const
{
    return col;
}

inline QFont Q3TextFormat::font() const
{
    return fn;
}

inline bool Q3TextFormat::isMisspelled() const
{
    return missp;
}

inline Q3TextFormat::VerticalAlignment Q3TextFormat::vAlign() const
{
    return ha;
}

inline bool Q3TextFormat::operator==(const Q3TextFormat &f) const
{
    return k == f.k;
}

inline Q3TextFormatCollection *Q3TextFormat::parent() const
{
    return collection;
}

inline void Q3TextFormat::addRef()
{
    ref++;
}

inline void Q3TextFormat::removeRef()
{
    ref--;
    if (!collection)
        return;
    if (this == collection->defFormat)
        return;
    if (ref == 0)
        collection->remove(this);
}

inline const QString &Q3TextFormat::key() const
{
    return k;
}

inline bool Q3TextFormat::useLinkColor() const
{
    return linkColor;
}

inline Q3TextStringChar *Q3TextParagraph::at(int i) const
{
    return &str->at(i);
}

inline bool Q3TextParagraph::isValid() const
{
    return invalid == -1;
}

inline bool Q3TextParagraph::hasChanged() const
{
    return changed;
}

inline void Q3TextParagraph::setBackgroundColor(const QColor & c)
{
    delete bgcol;
    bgcol = new QColor(c);
    setChanged(true);
}

inline void Q3TextParagraph::clearBackgroundColor()
{
    delete bgcol; bgcol = 0; setChanged(true);
}

inline void Q3TextParagraph::append(const QString &s, bool reallyAtEnd)
{
    if (reallyAtEnd) {
        insert(str->length(), s);
    } else {
        int str_end = str->length() - 1;
        insert(str_end > 0 ? str_end : 0, s);
    }
}

inline Q3TextParagraph *Q3TextParagraph::prev() const
{
    return p;
}

inline Q3TextParagraph *Q3TextParagraph::next() const
{
    return n;
}

inline bool Q3TextParagraph::hasAnySelection() const
{
    return mSelections ? !selections().isEmpty() : false;
}

inline void Q3TextParagraph::setEndState(int s)
{
    if (s == state)
        return;
    state = s;
}

inline int Q3TextParagraph::endState() const
{
    return state;
}

inline void Q3TextParagraph::setParagId(int i)
{
    id = i;
}

inline int Q3TextParagraph::paragId() const
{
    if (id == -1)
        qWarning("invalid parag id!!!!!!!! (%p)", (void*)this);
    return id;
}

inline bool Q3TextParagraph::firstPreProcess() const
{
    return firstPProcess;
}

inline void Q3TextParagraph::setFirstPreProcess(bool b)
{
    firstPProcess = b;
}

inline QMap<int, QTextLineStart*> &Q3TextParagraph::lineStartList()
{
    return lineStarts;
}

inline Q3TextString *Q3TextParagraph::string() const
{
    return str;
}

inline Q3TextParagraphPseudoDocument *Q3TextParagraph::pseudoDocument() const
{
    if (hasdoc)
        return 0;
    return (Q3TextParagraphPseudoDocument*) docOrPseudo;
}


#ifndef QT_NO_TEXTCUSTOMITEM
inline Q3TextTableCell *Q3TextParagraph::tableCell() const
{
    return hasdoc ? document()->tableCell () : 0;
}
#endif

inline Q3TextCommandHistory *Q3TextParagraph::commands() const
{
    return hasdoc ? document()->commands() : pseudoDocument()->commandHistory;
}


inline int Q3TextParagraph::alignment() const
{
    return align;
}

#ifndef QT_NO_TEXTCUSTOMITEM
inline void Q3TextParagraph::registerFloatingItem(Q3TextCustomItem *i)
{
    floatingItems().append(i);
}

inline void Q3TextParagraph::unregisterFloatingItem(Q3TextCustomItem *i)
{
    floatingItems().removeAll(i);
}
#endif

inline QBrush *Q3TextParagraph::background() const
{
#ifndef QT_NO_TEXTCUSTOMITEM
    return tableCell() ? tableCell()->backGround() : 0;
#else
    return 0;
#endif
}

inline int Q3TextParagraph::documentWidth() const
{
    return hasdoc ? document()->width() : pseudoDocument()->docRect.width();
}

inline int Q3TextParagraph::documentVisibleWidth() const
{
    return hasdoc ? document()->visibleWidth() : pseudoDocument()->docRect.width();
}

inline int Q3TextParagraph::documentX() const
{
    return hasdoc ? document()->x() : pseudoDocument()->docRect.x();
}

inline int Q3TextParagraph::documentY() const
{
    return hasdoc ? document()->y() : pseudoDocument()->docRect.y();
}

inline void Q3TextParagraph::setExtraData(Q3TextParagraphData *data)
{
    eData = data;
}

inline Q3TextParagraphData *Q3TextParagraph::extraData() const
{
    return eData;
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

inline void Q3TextFormatCollection::setDefaultFormat(Q3TextFormat *f)
{
    defFormat = f;
}

inline Q3TextFormat *Q3TextFormatCollection::defaultFormat() const
{
    return defFormat;
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

inline Q3TextFormat *Q3TextStringChar::format() const
{
    return (type == Regular) ? p.format : p.custom->format;
}


#ifndef QT_NO_TEXTCUSTOMITEM
inline Q3TextCustomItem *Q3TextStringChar::customItem() const
{
    return isCustom() ? p.custom->custom : 0;
}
#endif

inline int Q3TextStringChar::height() const
{
#ifndef QT_NO_TEXTCUSTOMITEM
    return !isCustom() ? format()->height() : (customItem()->placement() == Q3TextCustomItem::PlaceInline ? customItem()->height : 0);
#else
    return format()->height();
#endif
}

inline int Q3TextStringChar::ascent() const
{
#ifndef QT_NO_TEXTCUSTOMITEM
    return !isCustom() ? format()->ascent() : (customItem()->placement() == Q3TextCustomItem::PlaceInline ? customItem()->ascent() : 0);
#else
    return format()->ascent();
#endif
}

inline int Q3TextStringChar::descent() const
{
#ifndef QT_NO_TEXTCUSTOMITEM
    return !isCustom() ? format()->descent() : 0;
#else
    return format()->descent();
#endif
}

#endif // QT_NO_RICHTEXT

QT_END_NAMESPACE

#endif // Q3RICHTEXT_P_H