summaryrefslogtreecommitdiffstats
path: root/src/kernel/qpainter_x11.cpp
blob: ef80cff3a6178aef23831e11c4866ad63fefb8ab (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
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
/****************************************************************************
**
** Implementation of TQPainter class for X11
**
** Created : 940112
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the kernel module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing retquirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at [email protected].
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "qplatformdefs.h"

#include "qfont.h"
#include "qpainter.h"
#include "qwidget.h"
#include "qbitmap.h"
#include "qpixmapcache.h"
#include "qtextcodec.h"
#include "qpaintdevicemetrics.h"

#include "qt_x11_p.h"

#include "qtextlayout_p.h"
#include "qfontdata_p.h"
#include "qfontengine_p.h"
#include "qtextengine_p.h"

#include <math.h>

// paintevent magic to provide Windows semantics on X11
static TQRegion* paintEventClipRegion = 0;
static TQPaintDevice* paintEventDevice = 0;

void qt_set_paintevent_clipping( TQPaintDevice* dev, const TQRegion& region)
{
    if ( !paintEventClipRegion )
        paintEventClipRegion = new TQRegion( region );
    else
        *paintEventClipRegion = region;
    paintEventDevice = dev;
}

void qt_clear_paintevent_clipping()
{
    delete paintEventClipRegion;
    paintEventClipRegion = 0;
    paintEventDevice = 0;
}

class TQWFlagWidget : public TQWidget
{
public:
    void setWState( WFlags f )          { TQWidget::setWState(f); }
    void clearWState( WFlags f )        { TQWidget::clearWState(f); }
    void setWFlags( WFlags f )          { TQWidget::setWFlags(f); }
    void clearWFlags( WFlags f )        { TQWidget::clearWFlags(f); }
};

void qt_erase_region( TQWidget* w, const TQRegion& region)
{
    TQRegion reg = region;

    if ( TQPainter::redirect(w) || (!w->isTopLevel() && w->backgroundPixmap()
                                   && w->backgroundOrigin() != TQWidget::WidgetOrigin) ) {
	TQPoint offset = w->backgroundOffset();
	int ox = offset.x();
	int oy = offset.y();

        bool unclipped = w->testWFlags( TQt::WPaintUnclipped );
        if ( unclipped )
            ((TQWFlagWidget*)w)->clearWFlags( TQt::WPaintUnclipped );
        TQPainter p( w );
        p.setClipRegion( region ); // automatically includes paintEventDevice if retquired
        if ( w->backgroundPixmap() )
            p.drawTiledPixmap( 0, 0, w->width(), w->height(),
                               *w->backgroundPixmap(), ox, oy );
        else
            p.fillRect( w->rect(), w->eraseColor() );
        if ( unclipped )
            ((TQWFlagWidget*)w)->setWFlags( TQt::WPaintUnclipped );
        return;
    }

    if ( w == paintEventDevice && paintEventClipRegion )
        reg = paintEventClipRegion->intersect( reg );

    TQMemArray<TQRect> r = reg.rects();
    for (uint i=0; i<r.size(); i++) {
        const TQRect& rr = r[(int)i];
        XClearArea( w->x11Display(), w->winId(),
                    rr.x(), rr.y(), rr.width(), rr.height(), False );
    }
}

void qt_erase_rect( TQWidget* w, const TQRect& r)
{
    if ( TQPainter::redirect(w) || w == paintEventDevice
         || w->backgroundOrigin() != TQWidget::WidgetOrigin )
        qt_erase_region( w, r );
    else
        XClearArea( w->x11Display(), w->winId(), r.x(), r.y(), r.width(), r.height(), False );

}

#ifdef QT_NO_XFTFREETYPE
static const TQt::HANDLE rendhd = 0;
#endif

// hack, so we don't have to make TQRegion::clipRectangles() public or include
// X11 headers in qregion.h
inline void *qt_getClipRects( const TQRegion &r, int &num )
{
    return r.clipRectangles( num );
}

static inline void x11SetClipRegion(Display *dpy, GC gc, GC gc2, TQt::HANDLE draw, const TQRegion &r)
{
    int num;
    XRectangle *rects = (XRectangle *)qt_getClipRects( r, num );

    if (gc)
        XSetClipRectangles( dpy, gc, 0, 0, rects, num, YXBanded );
    if (gc2)
        XSetClipRectangles( dpy, gc2, 0, 0, rects, num, YXBanded );

#ifndef QT_NO_XFTFREETYPE
    if (draw)
        XftDrawSetClipRectangles((XftDraw *) draw, 0, 0, rects, num);
#else
    Q_UNUSED(draw);
#endif // QT_NO_XFTFREETYPE
}

static inline void x11ClearClipRegion(Display *dpy, GC gc, GC gc2, TQt::HANDLE draw)
{
    if (gc)
        XSetClipMask(dpy, gc, None);
    if (gc2)
        XSetClipMask(dpy, gc2, None);

#ifndef QT_NO_XFTFREETYPE
    if (draw) {
#  ifdef QT_XFT2
	XftDrawSetClip((XftDraw *) draw, None);
#  else
	// stupid Xft1
	Picture pict = XftDrawPicture((XftDraw *) draw);
	XRenderPictureAttributes pattr;
	pattr.clip_mask = None;
	XRenderChangePicture(dpy, pict, CPClipMask, &pattr);
#  endif // QT_XFT2
    }
#else
    Q_UNUSED(draw);
#endif // QT_NO_XFTFREETYPE
}


/*****************************************************************************
  Trigonometric function for TQPainter

  We have implemented simple sine and cosine function that are called from
  TQPainter::drawPie() and TQPainter::drawChord() when drawing the outline of
  pies and chords.
  These functions are slower and less accurate than math.h sin() and cos(),
  but with still around 1/70000th sec. execution time (on a 486DX2-66) and
  8 digits accuracy, it should not be the bottleneck in drawing these shapes.
  The advantage is that you don't have to link in the math library.
 *****************************************************************************/

const double Q_PI   = 3.14159265358979323846;   // pi
const double Q_2PI  = 6.28318530717958647693;   // 2*pi
const double Q_PI2  = 1.57079632679489661923;   // pi/2


#if defined(Q_CC_GNU) && defined(Q_OS_AIX)
// AIX 4.2 gcc 2.7.2.3 gets internal error.
static int qRoundAIX( double d )
{
    return qRound(d);
}
#define qRound qRoundAIX
#endif


#if defined(Q_CC_GNU) && defined(__i386__)

inline double qcos( double a )
{
    double r;
    __asm__ (
        "fcos"
        : "=t" (r) : "0" (a) );
    return(r);
}

inline double qsin( double a )
{
    double r;
    __asm__ (
        "fsin"
        : "=t" (r) : "0" (a) );
    return(r);
}

double qsincos( double a, bool calcCos=FALSE )
{
    return calcCos ? qcos(a) : qsin(a);
}

#else

double qsincos( double a, bool calcCos=FALSE )
{
    if ( calcCos )                              // calculate cosine
        a -= Q_PI2;
    if ( a >= Q_2PI || a <= -Q_2PI ) {          // fix range: -2*pi < a < 2*pi
        int m = (int)(a/Q_2PI);
        a -= Q_2PI*m;
    }
    if ( a < 0.0 )                              // 0 <= a < 2*pi
        a += Q_2PI;
    int sign = a > Q_PI ? -1 : 1;
    if ( a >= Q_PI )
        a = Q_2PI - a;
    if ( a >= Q_PI2 )
        a = Q_PI - a;
    if ( calcCos )
        sign = -sign;
    double a2  = a*a;                           // here: 0 <= a < pi/4
    double a3  = a2*a;                          // make taylor sin sum
    double a5  = a3*a2;
    double a7  = a5*a2;
    double a9  = a7*a2;
    double a11 = a9*a2;
    return (a-a3/6+a5/120-a7/5040+a9/362880-a11/39916800)*sign;
}

inline double qsin( double a ) { return qsincos(a, FALSE); }
inline double qcos( double a ) { return qsincos(a, TRUE); }

#endif


/*****************************************************************************
  TQPainter internal GC (Graphics Context) allocator.

  The GC allocator offers two functions; alloc_gc() and free_gc() that
  reuse GC objects instead of calling XCreateGC() and XFreeGC(), which
  are a whole lot slower.
 *****************************************************************************/

struct TQGC
{
    GC   gc;
    char in_use;
    bool mono;
    int scrn;
};

const  int  gc_array_size = 256;
static TQGC  gc_array[gc_array_size];            // array of GCs
static bool gc_array_init = FALSE;


static void init_gc_array()
{
    if ( !gc_array_init ) {
        memset( gc_array, 0, gc_array_size*sizeof(TQGC) );
        gc_array_init = TRUE;
    }
}

static void cleanup_gc_array( Display *dpy )
{
    register TQGC *p = gc_array;
    int i = gc_array_size;
    if ( gc_array_init ) {
        while ( i-- ) {
            if ( p->gc )                        // destroy GC
                XFreeGC( dpy, p->gc );
            p++;
        }
        gc_array_init = FALSE;
    }
}

// #define DONT_USE_GC_ARRAY

static GC alloc_gc( Display *dpy, int scrn, Drawable hd, bool monochrome=FALSE,
                    bool privateGC = FALSE )
{
#if defined(DONT_USE_GC_ARRAY)
    privateGC = TRUE;                           // will be slower
#endif
    if ( privateGC ) {
        GC gc = XCreateGC( dpy, hd, 0, 0 );
        XSetGraphicsExposures( dpy, gc, False );
        return gc;
    }
    register TQGC *p = gc_array;
    int i = gc_array_size;
    if ( !gc_array_init )                       // not initialized
        init_gc_array();
    while ( i-- ) {
        if ( !p->gc ) {                         // create GC (once)
            p->gc = XCreateGC( dpy, hd, 0, 0 );
            p->scrn = scrn;
            XSetGraphicsExposures( dpy, p->gc, False );
            p->in_use = FALSE;
            p->mono   = monochrome;
        }
        if ( !p->in_use && p->mono == monochrome && p->scrn == scrn ) {
            p->in_use = TRUE;                   // available/compatible GC
            return p->gc;
        }
        p++;
    }
#if defined(QT_CHECK_NULL)
    qWarning( "TQPainter: Internal error; no available GC" );
#endif
    GC gc = XCreateGC( dpy, hd, 0, 0 );
    XSetGraphicsExposures( dpy, gc, False );
    return gc;
}

static void free_gc( Display *dpy, GC gc, bool privateGC = FALSE )
{
#if defined(DONT_USE_GC_ARRAY)
    privateGC = TRUE;                           // will be slower
#endif
    if ( privateGC ) {
        Q_ASSERT( dpy != 0 );
        XFreeGC( dpy, gc );
        return;
    }
    register TQGC *p = gc_array;
    int i = gc_array_size;
    if ( gc_array_init ) {
        while ( i-- ) {
            if ( p->gc == gc ) {
                p->in_use = FALSE;              // set available
                XSetClipMask( dpy, gc, None );  // make it reusable
                XSetFunction( dpy, gc, GXcopy );
                XSetFillStyle( dpy, gc, FillSolid );
                XSetTSOrigin( dpy, gc, 0, 0 );
                return;
            }
            p++;
        }
    }

    // not found in gc_array
    XFreeGC(dpy, gc);
}


/*****************************************************************************
  TQPainter internal GC (Graphics Context) cache for solid pens and
  brushes.

  The GC cache makes a significant contribution to speeding up
  drawing.  Setting new pen and brush colors will make the painter
  look for another GC with the same color instead of changing the
  color value of the GC currently in use. The cache structure is
  optimized for fast lookup.  Only solid line pens with line width 0
  and solid brushes are cached.

  In addition, stored GCs may have an implicit clipping region
  set. This prevents any drawing outside paint events. Both
  updatePen() and updateBrush() keep track of the validity of this
  clipping region by storing the clip_serial number in the cache.

*****************************************************************************/

struct TQGCC                                     // cached GC
{
    GC gc;
    uint pix;
    int count;
    int hits;
    uint clip_serial;
    int scrn;
};

const  int   gc_cache_size = 29;                // multiply by 4
static TQGCC *gc_cache_buf;
static TQGCC *gc_cache[4*gc_cache_size];
static bool  gc_cache_init = FALSE;
static uint gc_cache_clip_serial = 0;


static void init_gc_cache()
{
    if ( !gc_cache_init ) {
        gc_cache_init = TRUE;
	gc_cache_clip_serial = 0;
        TQGCC *g = gc_cache_buf = new TQGCC[4*gc_cache_size];
        memset( g, 0, 4*gc_cache_size*sizeof(TQGCC) );
        for ( int i=0; i<4*gc_cache_size; i++ )
            gc_cache[i] = g++;
    }
}


// #define GC_CACHE_STAT
#if defined(GC_CACHE_STAT)
#include "qtextstream.h"
#include "qbuffer.h"

static int g_numhits    = 0;
static int g_numcreates = 0;
static int g_numfaults  = 0;
#endif


static void cleanup_gc_cache()
{
    if ( !gc_cache_init )
        return;
#if defined(GC_CACHE_STAT)
    qDebug( "Number of cache hits = %d", g_numhits );
    qDebug( "Number of cache creates = %d", g_numcreates );
    qDebug( "Number of cache faults = %d", g_numfaults );
    for ( int i=0; i<gc_cache_size; i++ ) {
        TQCString    str;
        TQBuffer     buf( str );
        buf.open(IO_ReadWrite);
        TQTextStream s(&buf);
        s << i << ": ";
        for ( int j=0; j<4; j++ ) {
            TQGCC *g = gc_cache[i*4+j];
            s << (g->gc ? 'X' : '-') << ',' << g->hits << ','
              << g->count << '\t';
        }
        s << '\0';
        qDebug( str );
        buf.close();
    }
#endif
    delete [] gc_cache_buf;
    gc_cache_init = FALSE;
}


static bool obtain_gc( void **ref, GC *gc, uint pix, Display *dpy, int scrn,
		       TQt::HANDLE hd, uint painter_clip_serial )
{
    if ( !gc_cache_init )
        init_gc_cache();

    int   k = (pix % gc_cache_size) * 4;
    TQGCC *g = gc_cache[k];
    TQGCC *prev = 0;

#define NOMATCH (g->gc && (g->pix != pix || g->scrn != scrn || \
                 (g->clip_serial > 0 && g->clip_serial != painter_clip_serial)))

    if ( NOMATCH ) {
        prev = g;
        g = gc_cache[++k];
        if ( NOMATCH ) {
            prev = g;
            g = gc_cache[++k];
            if ( NOMATCH ) {
                prev = g;
                g = gc_cache[++k];
                if ( NOMATCH ) {
                    if ( g->count == 0 && g->scrn == scrn) {    // steal this GC
                        g->pix   = pix;
                        g->count = 1;
                        g->hits  = 1;
			g->clip_serial = 0;
                        XSetForeground( dpy, g->gc, pix );
			XSetClipMask(dpy, g->gc, None);
                        gc_cache[k]   = prev;
                        gc_cache[k-1] = g;
                        *ref = (void *)g;
                        *gc = g->gc;
                        return TRUE;
                    } else {                    // all GCs in use
#if defined(GC_CACHE_STAT)
                        g_numfaults++;
#endif
                        *ref = 0;
                        return FALSE;
                    }
                }
            }
        }
    }

#undef NOMATCH

    *ref = (void *)g;

    if ( g->gc ) {                              // reuse existing GC
#if defined(GC_CACHE_STAT)
        g_numhits++;
#endif
        *gc = g->gc;
        g->count++;
        g->hits++;
        if ( prev && g->hits > prev->hits ) {   // maintain LRU order
            gc_cache[k]   = prev;
            gc_cache[k-1] = g;
        }
        return TRUE;
    } else {                                    // create new GC
#if defined(GC_CACHE_STAT)
        g_numcreates++;
#endif
        g->gc = alloc_gc( dpy, scrn, hd, FALSE );
        g->scrn = scrn;
        g->pix = pix;
        g->count = 1;
        g->hits = 1;
        g->clip_serial = 0;
        *gc = g->gc;
        return FALSE;
    }
}

static inline void release_gc( void *ref )
{
    ((TQGCC*)ref)->count--;
}

/*****************************************************************************
  TQPainter member functions
 *****************************************************************************/

/*!
  \internal

  Internal function that initializes the painter.
*/

void TQPainter::initialize()
{
    init_gc_array();
    init_gc_cache();
}

/*!
  \internal

  Internal function that cleans up the painter.
*/

void TQPainter::cleanup()
{
    cleanup_gc_cache();
    cleanup_gc_array( TQPaintDevice::x11AppDisplay() );
    TQPointArray::cleanBuffers();
}

/*!
  \internal

  Internal function that destroys up the painter.
*/

void TQPainter::destroy()
{

}

void TQPainter::init()
{
    d = 0;
    flags = IsStartingUp;
    bg_col = white;                             // default background color
    bg_mode = TransparentMode;                  // default background mode
    rop = CopyROP;                                // default ROP
    tabstops = 0;                               // default tabbing
    tabarray = 0;
    tabarraylen = 0;
    ps_stack = 0;
    wm_stack = 0;
    gc = gc_brush = 0;
    pdev = 0;
    dpy  = 0;
    txop = txinv = 0;
    penRef = brushRef = 0;
    clip_serial = 0;
    pfont = 0;
    block_ext = FALSE;
}


/*!
    \fn const TQFont &TQPainter::font() const

    Returns the currently set painter font.

    \sa setFont(), TQFont
*/

/*!
    Sets the painter's font to \a font.

    This font is used by subsequent drawText() functions. The text
    color is the same as the pen color.

    \sa font(), drawText()
*/

void TQPainter::setFont( const TQFont &font )
{
#if defined(QT_CHECK_STATE)
    if ( !isActive() )
        qWarning( "TQPainter::setFont: Will be reset by begin()" );
#endif
    if ( cfont.d != font.d ) {
        cfont = font;
	cfont.x11SetScreen( scrn );
        setf(DirtyFont);
    }
}


void TQPainter::updateFont()
{
    if (!isActive())
        return;

    clearf(DirtyFont);
    if ( testf(ExtDev) ) {
        if (pdev->devType() == TQInternal::Printer) {
            if ( pfont ) delete pfont;
            pfont = new TQFont( cfont.d, pdev );
        }
        TQPDevCmdParam param[1];
        param[0].font = &cfont;
        if ( !pdev->cmd( TQPaintDevice::PdcSetFont, this, param ) || !hd )
            return;
    }
    setf(NoCache);
    if ( penRef )
        updatePen();                            // force a non-cached GC
}


void TQPainter::updatePen()
{
    if (!isActive())
        return;

    if ( testf(ExtDev) ) {
        TQPDevCmdParam param[1];
        param[0].pen = &cpen;
        if ( !pdev->cmd( TQPaintDevice::PdcSetPen, this, param ) || !hd )
            return;
    }

    int ps = cpen.style();
    bool cacheIt = !testf(ClipOn|MonoDev|NoCache) &&
                   (ps == NoPen || ps == SolidLine) &&
                   cpen.width() == 0 && rop == CopyROP;

    bool obtained = FALSE;
    bool internclipok = hasClipping();
    if ( cacheIt ) {
        if ( gc ) {
            if ( penRef )
                release_gc( penRef );
            else
                free_gc( dpy, gc );
        }
        obtained = obtain_gc(&penRef, &gc, cpen.color().pixel(scrn), dpy, scrn,
			     hd, clip_serial);
        if ( !obtained && !penRef )
            gc = alloc_gc( dpy, scrn, hd, FALSE );
    } else {
        if ( gc ) {
            if ( penRef ) {
                release_gc( penRef );
                penRef = 0;
                gc = alloc_gc( dpy, scrn, hd, testf(MonoDev) );
            } else {
                internclipok = TRUE;
            }
        } else {
            gc = alloc_gc( dpy, scrn, hd, testf(MonoDev), testf(UsePrivateCx) );
        }
    }

    if ( !internclipok ) {
        if ( pdev == paintEventDevice && paintEventClipRegion ) {
            if ( penRef &&((TQGCC*)penRef)->clip_serial < gc_cache_clip_serial ) {
		x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion );
                ((TQGCC*)penRef)->clip_serial = gc_cache_clip_serial;
            } else if ( !penRef ) {
		x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion );
            }
        } else if (penRef && ((TQGCC*)penRef)->clip_serial ) {
            x11ClearClipRegion(dpy, gc, 0, rendhd);
            ((TQGCC*)penRef)->clip_serial = 0;
        }
    }

    if ( obtained )
        return;

    char dashes[10];                            // custom pen dashes
    int dash_len = 0;                           // length of dash list
    int s = LineSolid;
    int cp = CapButt;
    int jn = JoinMiter;

    /*
      We are emulating Windows here.  Windows treats cpen.width() == 1
      (or 0) as a very special case.  The fudge variable unifies this
      case with the general case.
    */
    int dot = cpen.width();                     // width of a dot
    int fudge = 1;
    bool allow_zero_lw = TRUE;
    if ( dot <= 1 ) {
        dot = 3;
        fudge = 2;
    }

    switch( ps ) {
    case NoPen:
    case SolidLine:
	s = LineSolid;
	break;
    case DashLine:
	dashes[0] = fudge * 3 * dot;
	dashes[1] = fudge * dot;
	dash_len = 2;
	allow_zero_lw = FALSE;
	break;
    case DotLine:
	dashes[0] = dot;
	dashes[1] = dot;
	dash_len = 2;
	allow_zero_lw = FALSE;
	break;
    case DashDotLine:
	dashes[0] = 3 * dot;
	dashes[1] = fudge * dot;
	dashes[2] = dot;
	dashes[3] = fudge * dot;
	dash_len = 4;
	allow_zero_lw = FALSE;
	break;
    case DashDotDotLine:
	dashes[0] = 3 * dot;
	dashes[1] = dot;
	dashes[2] = dot;
	dashes[3] = dot;
	dashes[4] = dot;
	dashes[5] = dot;
	dash_len = 6;
	allow_zero_lw = FALSE;
    }
    Q_ASSERT( dash_len <= (int) sizeof(dashes) );

    switch ( cpen.capStyle() ) {
    case SquareCap:
        cp = CapProjecting;
        break;
    case RoundCap:
        cp = CapRound;
        break;
    case FlatCap:
    default:
        cp = CapButt;
        break;
    }
    switch ( cpen.joinStyle() ) {
    case BevelJoin:
        jn = JoinBevel;
        break;
    case RoundJoin:
        jn = JoinRound;
        break;
    case MiterJoin:
    default:
        jn = JoinMiter;
        break;
    }

    XSetForeground( dpy, gc, cpen.color().pixel(scrn) );
    XSetBackground( dpy, gc, bg_col.pixel(scrn) );

    if ( dash_len ) {                           // make dash list
        XSetDashes( dpy, gc, 0, dashes, dash_len );
        s = bg_mode == TransparentMode ? LineOnOffDash : LineDoubleDash;
    }
    XSetLineAttributes( dpy, gc,
			(! allow_zero_lw && cpen.width() == 0) ? 1 : cpen.width(),
			s, cp, jn );
}


void TQPainter::updateBrush()
{
    if (!isActive())
        return;

    static const uchar dense1_pat[] = { 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff };
    static const uchar dense2_pat[] = { 0x77, 0xff, 0xdd, 0xff, 0x77, 0xff, 0xdd, 0xff };
    static const uchar dense3_pat[] = { 0x55, 0xbb, 0x55, 0xee, 0x55, 0xbb, 0x55, 0xee };
    static const uchar dense4_pat[] = { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa };
    static const uchar dense5_pat[] = { 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa, 0x11 };
    static const uchar dense6_pat[] = { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00 };
    static const uchar dense7_pat[] = { 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00 };
    static const uchar hor_pat[] = {                      // horizontal pattern
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    static const uchar ver_pat[] = {                      // vertical pattern
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20 };
    static const uchar cross_pat[] = {                    // cross pattern
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0xff, 0xff, 0xff,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0xff, 0xff, 0xff, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0xff, 0xff, 0xff,
	0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20,
	0x08, 0x82, 0x20, 0xff, 0xff, 0xff, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20 };
    static const uchar bdiag_pat[] = {                    // backward diagonal pattern
	0x20, 0x20, 0x10, 0x10, 0x08, 0x08, 0x04, 0x04, 0x02, 0x02, 0x01, 0x01,
	0x80, 0x80, 0x40, 0x40, 0x20, 0x20, 0x10, 0x10, 0x08, 0x08, 0x04, 0x04,
	0x02, 0x02, 0x01, 0x01, 0x80, 0x80, 0x40, 0x40 };
    static const uchar fdiag_pat[] = {                    // forward diagonal pattern
	0x02, 0x02, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, 0x20, 0x20, 0x40, 0x40,
	0x80, 0x80, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10,
	0x20, 0x20, 0x40, 0x40, 0x80, 0x80, 0x01, 0x01 };
    static const uchar dcross_pat[] = {                   // diagonal cross pattern
	0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x14, 0x14, 0x22, 0x22, 0x41, 0x41,
	0x80, 0x80, 0x41, 0x41, 0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x14, 0x14,
	0x22, 0x22, 0x41, 0x41, 0x80, 0x80, 0x41, 0x41 };
    static const uchar * const pat_tbl[] = {
	dense1_pat, dense2_pat, dense3_pat, dense4_pat, dense5_pat,
	dense6_pat, dense7_pat,
	hor_pat, ver_pat, cross_pat, bdiag_pat, fdiag_pat, dcross_pat };

    if ( testf(ExtDev) ) {
        TQPDevCmdParam param[1];
        param[0].brush = &cbrush;
        if ( !pdev->cmd( TQPaintDevice::PdcSetBrush, this, param ) || !hd )
            return;
    }

    int  bs      = cbrush.style();
    bool cacheIt = !testf(ClipOn|MonoDev|NoCache) &&
                   (bs == NoBrush || bs == SolidPattern) &&
	           bro.x() == 0 && bro.y() == 0 && rop == CopyROP;

    bool obtained = FALSE;
    bool internclipok = hasClipping();
    if ( cacheIt ) {
        if ( gc_brush ) {
            if ( brushRef )
                release_gc( brushRef );
            else
                free_gc( dpy, gc_brush );
        }
        obtained = obtain_gc(&brushRef, &gc_brush, cbrush.color().pixel(scrn), dpy,
			     scrn, hd, clip_serial);
        if ( !obtained && !brushRef )
            gc_brush = alloc_gc( dpy, scrn, hd, FALSE );
    } else {
        if ( gc_brush ) {
            if ( brushRef ) {
                release_gc( brushRef );
                brushRef = 0;
                gc_brush = alloc_gc( dpy, scrn, hd, testf(MonoDev) );
            } else {
                internclipok = TRUE;
            }
        } else {
            gc_brush = alloc_gc( dpy, scrn, hd, testf(MonoDev), testf(UsePrivateCx));
        }
    }

    if ( !internclipok ) {
        if ( pdev == paintEventDevice && paintEventClipRegion ) {
            if ( brushRef &&((TQGCC*)brushRef)->clip_serial < gc_cache_clip_serial ) {
		x11SetClipRegion( dpy, gc_brush, 0, rendhd, *paintEventClipRegion );
                ((TQGCC*)brushRef)->clip_serial = gc_cache_clip_serial;
            } else if ( !brushRef ){
		x11SetClipRegion( dpy, gc_brush, 0, rendhd, *paintEventClipRegion );
            }
        } else if (brushRef && ((TQGCC*)brushRef)->clip_serial ) {
            x11ClearClipRegion(dpy, gc_brush, 0, rendhd);
            ((TQGCC*)brushRef)->clip_serial = 0;
        }
    }

    if ( obtained )
        return;

    const uchar *pat = 0;                             // pattern
    int d = 0;                                  // defalt pattern size: d*d
    int s  = FillSolid;
    if ( bs >= Dense1Pattern && bs <= DiagCrossPattern ) {
        pat = pat_tbl[ bs-Dense1Pattern ];
        if ( bs <= Dense7Pattern )
            d = 8;
        else if ( bs <= CrossPattern )
            d = 24;
        else
            d = 16;
    }

    XSetLineAttributes( dpy, gc_brush, 0, LineSolid, CapButt, JoinMiter );
    XSetForeground( dpy, gc_brush, cbrush.color().pixel(scrn) );
    XSetBackground( dpy, gc_brush, bg_col.pixel(scrn) );

    if ( bs == CustomPattern || pat ) {
        TQPixmap *pm;
        if ( pat ) {
            TQString key;
            key.sprintf( "$qt-brush$%d", bs );
            pm = TQPixmapCache::find( key );
            bool del = FALSE;
            if ( !pm ) {                        // not already in pm dict
                pm = new TQBitmap( d, d, pat, TRUE );
                Q_CHECK_PTR( pm );
                del = !TQPixmapCache::insert( key, pm );
            }
            if ( cbrush.data->pixmap )
                delete cbrush.data->pixmap;
            cbrush.data->pixmap = new TQPixmap( *pm );
            if (del) delete pm;
        }
        pm = cbrush.data->pixmap;
        pm->x11SetScreen( scrn );
        if ( pm->depth() == 1 ) {
            XSetStipple( dpy, gc_brush, pm->handle() );
            s = bg_mode == TransparentMode ? FillStippled : FillOpaqueStippled;
        } else {
            XSetTile( dpy, gc_brush, pm->handle() );
            s = FillTiled;
        }
    }
    XSetFillStyle( dpy, gc_brush, s );
}


/*!
    Begins painting the paint device \a pd and returns TRUE if
    successful; otherwise returns FALSE. If \a unclipped is TRUE, the
    painting will not be clipped at the paint device's boundaries,
    (although this is not supported by all platforms).

    The errors that can occur are serious problems, such as these:

    \code
	p->begin( 0 ); // impossible - paint device cannot be 0

	TQPixmap pm( 0, 0 );
	p->begin( pm ); // impossible - pm.isNull();

	p->begin( myWidget );
	p2->begin( myWidget ); // impossible - only one painter at a time
    \endcode

    Note that most of the time, you can use one of the constructors
    instead of begin(), and that end() is automatically done at
    destruction.

    \warning A paint device can only be painted by one painter at a
    time.

    \sa end(), flush()
*/

bool TQPainter::begin( const TQPaintDevice *pd, bool unclipped )
{
    if ( isActive() ) {                         // already active painting
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::begin: Painter is already active."
                  "\n\tYou must end() the painter before a second begin()" );
#endif
        return FALSE;
    }
    if ( pd == 0 ) {
#if defined(QT_CHECK_NULL)
        qWarning( "TQPainter::begin: Paint device cannot be null" );
#endif
        return FALSE;
    }

    TQPixmap::x11SetDefaultScreen( pd->x11Screen() );

    const TQWidget *copyFrom = 0;
    pdev = redirect( (TQPaintDevice*)pd );
    if ( pdev ) {				    // redirected paint device?
	if ( pd->devType() == TQInternal::Widget )
	    copyFrom = (const TQWidget *)pd;	    // copy widget settings
    } else {
	pdev = (TQPaintDevice*)pd;
    }

    if ( pdev->isExtDev() && pdev->paintingActive() ) {
        // somebody else is already painting
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::begin: Another TQPainter is already painting "
                  "this device;\n\tAn extended paint device can only be "
                  "painted by one TQPainter at a time." );
#endif
        return FALSE;
    }

    bool reinit = flags != IsStartingUp;        // 2nd or 3rd etc. time called
    flags = IsActive | DirtyFont;               // init flags
    int dt = pdev->devType();                   // get the device type

    if ( (pdev->devFlags & TQInternal::ExternalDevice) != 0 )
        setf(ExtDev);
    else if ( dt == TQInternal::Pixmap )         // device is a pixmap
        ((TQPixmap*)pdev)->detach();             // will modify it

    dpy = pdev->x11Display();                   // get display variable
    scrn = pdev->x11Screen();			// get screen variable
    hd  = pdev->handle();                       // get handle to drawable
    rendhd = pdev->rendhd;

    if ( testf(ExtDev) ) {                      // external device
        if ( !pdev->cmd( TQPaintDevice::PdcBegin, this, 0 ) ) {
            // could not begin painting
            if ( reinit )
                clearf( IsActive | DirtyFont );
            else
                flags = IsStartingUp;
            pdev = 0;
            return FALSE;
        }
        if ( tabstops )                         // update tabstops for device
            setTabStops( tabstops );
        if ( tabarray )                         // update tabarray for device
            setTabArray( tabarray );
    }

    if ( pdev->x11Depth() != pdev->x11AppDepth( scrn ) ) { // non-standard depth
        setf(NoCache);
        setf(UsePrivateCx);
    }

    pdev->painters++;                           // also tell paint device
    bro = curPt = TQPoint( 0, 0 );
    if ( reinit ) {
        bg_mode = TransparentMode;              // default background mode
        rop = CopyROP;                          // default ROP
        wxmat.reset();                          // reset world xform matrix
	xmat.reset();
	ixmat.reset();
        txop = txinv = 0;
        if ( dt != TQInternal::Widget ) {
            TQFont  defaultFont;                 // default drawing tools
            TQPen   defaultPen;
            TQBrush defaultBrush;
            cfont  = defaultFont;               // set these drawing tools
            cpen   = defaultPen;
            cbrush = defaultBrush;
            bg_col = white;                     // default background color
        }
    }
    wx = wy = vx = vy = 0;                      // default view origins

    if ( dt == TQInternal::Widget ) {                    // device is a widget
        TQWidget *w = (TQWidget*)pdev;
        cfont = w->font();                      // use widget font
        cpen = TQPen( w->foregroundColor() );    // use widget fg color
        if ( reinit ) {
            TQBrush defaultBrush;
            cbrush = defaultBrush;
        }
        bg_col = w->backgroundColor();          // use widget bg color
        ww = vw = w->width();                   // default view size
        wh = vh = w->height();
        if ( unclipped || w->testWFlags( WPaintUnclipped ) ) {  // paint direct on device
            setf( NoCache );
            setf(UsePrivateCx);
            updatePen();
            updateBrush();
            XSetSubwindowMode( dpy, gc, IncludeInferiors );
            XSetSubwindowMode( dpy, gc_brush, IncludeInferiors );
#ifndef QT_NO_XFTFREETYPE
	    if (rendhd)
                XftDrawSetSubwindowMode((XftDraw *) rendhd, IncludeInferiors);
#endif
        }
    } else if ( dt == TQInternal::Pixmap ) {             // device is a pixmap
        TQPixmap *pm = (TQPixmap*)pdev;
        if ( pm->isNull() ) {
#if defined(QT_CHECK_NULL)
            qWarning( "TQPainter::begin: Cannot paint null pixmap" );
#endif
            end();
            return FALSE;
        }
        bool mono = pm->depth() == 1;           // monochrome bitmap
        if ( mono ) {
            setf( MonoDev );
            bg_col = color0;
            cpen.setColor( color1 );
        }
        ww = vw = pm->width();                  // default view size
        wh = vh = pm->height();
    } else if ( testf(ExtDev) ) {               // external device
        ww = vw = pdev->metric( TQPaintDeviceMetrics::PdmWidth );
        wh = vh = pdev->metric( TQPaintDeviceMetrics::PdmHeight );
    }
    if ( ww == 0 )
        ww = wh = vw = vh = 1024;
    if ( copyFrom ) {                           // copy redirected widget
        cfont = copyFrom->font();
        cpen = TQPen( copyFrom->foregroundColor() );
        bg_col = copyFrom->backgroundColor();
    }
    if ( testf(ExtDev) ) {                      // external device
        setBackgroundColor( bg_col );           // default background color
        setBackgroundMode( TransparentMode );   // default background mode
        setRasterOp( CopyROP );                 // default raster operation
    }
    clip_serial = gc_cache_clip_serial++;
    updateBrush();
    updatePen();
    return TRUE;
}

/*!
    Ends painting. Any resources used while painting are released.

    Note that while you mostly don't need to call end(), the
    destructor will do it, there is at least one common case when it
    is needed, namely double buffering.

    \code
	TQPainter p( myPixmap, this )
	// ...
	p.end(); // stops drawing on myPixmap
	p.begin( this );
	p.drawPixmap( 0, 0, myPixmap );
    \endcode

    Since you can't draw a TQPixmap while it is being painted, it is
    necessary to close the active painter.

    \sa begin(), isActive()
*/

bool TQPainter::end()                            // end painting
{
    if ( !isActive() ) {
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::end: Missing begin() or begin() failed" );
#endif
        return FALSE;
    }
    killPStack();

    //#### This should not be necessary:
    if ( pdev->devType() == TQInternal::Widget  &&       // #####
         ((TQWidget*)pdev)->testWFlags(WPaintUnclipped) ) {
        if ( gc )
            XSetSubwindowMode( dpy, gc, ClipByChildren );
        if ( gc_brush )
            XSetSubwindowMode( dpy, gc_brush, ClipByChildren );
    }

    if ( gc_brush ) {                           // restore brush gc
        if ( brushRef ) {
            release_gc( brushRef );
            brushRef = 0;
        } else {
            free_gc( dpy, gc_brush, testf(UsePrivateCx) );
        }
        gc_brush = 0;

    }
    if ( gc ) {                                 // restore pen gc
        if ( penRef ) {
            release_gc( penRef );
            penRef = 0;
        } else {
            free_gc( dpy, gc, testf(UsePrivateCx) );
        }
        gc = 0;
    }

    if ( testf(ExtDev) )
        pdev->cmd( TQPaintDevice::PdcEnd, this, 0 );

#ifndef QT_NO_XFTFREETYPE
    if (rendhd) {
        // reset clipping/subwindow mode on our render picture
        XftDrawSetClip((XftDraw *) rendhd, None);
        XftDrawSetSubwindowMode((XftDraw *) rendhd, ClipByChildren);
    }
#endif // QT_NO_XFTFREETYPE

    if ( pfont ) {
	delete pfont;
	pfont = 0;
    }

    flags = 0;
    pdev->painters--;
    pdev = 0;
    dpy  = 0;
    return TRUE;
}

/*!
    Flushes any buffered drawing operations inside the region \a
    region using clipping mode \a cm.

    The flush may update the whole device if the platform does not
    support flushing to a specified region.

    \sa flush() CoordinateMode
*/

void TQPainter::flush(const TQRegion &, CoordinateMode)
{
    flush();
}


/*!
    \overload

    Flushes any buffered drawing operations.
*/

void TQPainter::flush()
{
    if ( isActive() && dpy )
        XFlush( dpy );
}


/*!
    Sets the background color of the painter to \a c.

    The background color is the color that is filled in when drawing
    opaque text, stippled lines and bitmaps. The background color has
    no effect in transparent background mode (which is the default).

    \sa backgroundColor() setBackgroundMode() BackgroundMode
*/

void TQPainter::setBackgroundColor( const TQColor &c )
{
    if ( !isActive() ) {
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::setBackgroundColor: Call begin() first" );
#endif
        return;
    }
    bg_col = c;
    if ( testf(ExtDev) ) {
        TQPDevCmdParam param[1];
        param[0].color = &bg_col;
        if ( !pdev->cmd( TQPaintDevice::PdcSetBkColor, this, param ) || !hd )
            return;
    }
    if ( !penRef )
        updatePen();                            // update pen setting
    if ( !brushRef )
        updateBrush();                          // update brush setting
}

/*!
    Sets the background mode of the painter to \a m, which must be
    either \c TransparentMode (the default) or \c OpaqueMode.

    Transparent mode draws stippled lines and text without setting the
    background pixels. Opaque mode fills these space with the current
    background color.

    Note that in order to draw a bitmap or pixmap transparently, you
    must use TQPixmap::setMask().

    \sa backgroundMode(), setBackgroundColor()
*/

void TQPainter::setBackgroundMode( BGMode m )
{
    if ( !isActive() ) {
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::setBackgroundMode: Call begin() first" );
#endif
        return;
    }
    if ( m != TransparentMode && m != OpaqueMode ) {
#if defined(QT_CHECK_RANGE)
        qWarning( "TQPainter::setBackgroundMode: Invalid mode" );
#endif
        return;
    }
    bg_mode = m;
    if ( testf(ExtDev) ) {
        TQPDevCmdParam param[1];
        param[0].ival = m;
        if ( !pdev->cmd( TQPaintDevice::PdcSetBkMode, this, param ) || !hd )
            return;
    }
    if ( !penRef )
        updatePen();                            // update pen setting
    if ( !brushRef )
        updateBrush();                          // update brush setting
}

static const short ropCodes[] = {                     // ROP translation table
    GXcopy, // CopyROP
    GXor, // OrROP
    GXxor, // XorROP
    GXandInverted, // NotAndROP EraseROP
    GXcopyInverted, // NotCopyROP
    GXorInverted, // NotOrROP
    GXequiv, // NotXorROP
    GXand, // AndROP
    GXinvert, // NotROP
    GXclear, // ClearROP
    GXset, // SetROP
    GXnoop, // NopROP
    GXandReverse, // AndNotROP
    GXorReverse, // OrNotROP
    GXnand, // NandROP
    GXnor // NorROP
};


/*!
    Sets the \link TQt::RasterOp raster operation \endlink to \a r.
    The default is \c CopyROP.

    \sa rasterOp() TQt::RasterOp
*/

void TQPainter::setRasterOp( RasterOp r )
{
    if ( !isActive() ) {
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::setRasterOp: Call begin() first" );
#endif
        return;
    }
    if ( (uint)r > LastROP ) {
#if defined(QT_CHECK_RANGE)
        qWarning( "TQPainter::setRasterOp: Invalid ROP code" );
#endif
        return;
    }
    rop = r;
    if ( testf(ExtDev) ) {
        TQPDevCmdParam param[1];
        param[0].ival = r;
        if ( !pdev->cmd( TQPaintDevice::PdcSetROP, this, param ) || !hd )
            return;
    }
    if ( penRef )
        updatePen();                            // get non-cached pen GC
    if ( brushRef )
        updateBrush();                          // get non-cached brush GC
    XSetFunction( dpy, gc, ropCodes[rop] );
    XSetFunction( dpy, gc_brush, ropCodes[rop] );
}

// ### matthias - true?

/*!
    Sets the brush origin to \a (x, y).

    The brush origin specifies the (0, 0) coordinate of the painter's
    brush. This setting only applies to pattern brushes and pixmap
    brushes.

    \sa brushOrigin()
*/

void TQPainter::setBrushOrigin( int x, int y )
{
    if ( !isActive() ) {
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::setBrushOrigin: Call begin() first" );
#endif
        return;
    }
    bro = TQPoint(x, y);
    if ( testf(ExtDev) ) {
        TQPDevCmdParam param[1];
        param[0].point = &bro;
        if ( !pdev->cmd( TQPaintDevice::PdcSetBrushOrigin, this, param ) ||
             !hd )
            return;
    }
    if ( brushRef )
        updateBrush();                          // get non-cached brush GC
    XSetTSOrigin( dpy, gc_brush, x, y );
}


/*!
    Enables clipping if \a enable is TRUE, or disables clipping if \a
    enable is FALSE.

    \sa hasClipping(), setClipRect(), setClipRegion()
*/

void TQPainter::setClipping( bool enable )
{
    if ( !isActive() ) {
#if defined(QT_CHECK_STATE)
        qWarning( "TQPainter::setClipping: Will be reset by begin()" );
#endif
        return;
    }

    if ( enable == testf(ClipOn) )
        return;

    setf( ClipOn, enable );
    if ( testf(ExtDev) ) {
	if ( block_ext )
	    return;
        TQPDevCmdParam param[1];
        param[0].ival = enable;
        if ( !pdev->cmd( TQPaintDevice::PdcSetClip, this, param ) || !hd )
            return;
    }
    if ( enable ) {
        TQRegion rgn = crgn;
        if ( pdev == paintEventDevice && paintEventClipRegion )
            rgn = rgn.intersect( *paintEventClipRegion );
        if ( penRef )
            updatePen();
        if ( brushRef )
            updateBrush();
	x11SetClipRegion( dpy, gc, gc_brush, rendhd, rgn );
    } else {
        if ( pdev == paintEventDevice && paintEventClipRegion ) {
	    x11SetClipRegion( dpy, gc, gc_brush , rendhd, *paintEventClipRegion );
        } else {
            x11ClearClipRegion(dpy, gc, gc_brush, rendhd);
        }
    }
}


/*!
    \overload

    Sets the clip region to the rectangle \a r and enables clipping.
    The clip mode is set to \a m.

    \sa  CoordinateMode
*/

void TQPainter::setClipRect( const TQRect &r, CoordinateMode m )
{
    setClipRegion( TQRegion( r ), m );
}

/*!
    Sets the clip region to \a rgn and enables clipping. The clip mode
    is set to \a m.

    Note that the clip region is given in physical device coordinates
    and \e not subject to any \link coordsys.html coordinate
    transformation.\endlink

    \sa setClipRect(), clipRegion(), setClipping() CoordinateMode
*/

void TQPainter::setClipRegion( const TQRegion &rgn, CoordinateMode m )
{
#if defined(QT_CHECK_STATE)
    if ( !isActive() )
        qWarning( "TQPainter::setClipRegion: Will be reset by begin()" );
#endif
    if ( m == CoordDevice )
	crgn = rgn;
    else
	crgn = xmat * rgn;

    if ( testf(ExtDev) ) {
	if ( block_ext )
	    return;
        TQPDevCmdParam param[2];
        param[0].rgn = &rgn;
        param[1].ival = m;
        if ( !pdev->cmd( TQPaintDevice::PdcSetClipRegion, this, param ) )
            return; // device cannot clip
    }
    clearf( ClipOn );                           // be sure to update clip rgn
    setClipping( TRUE );
}


/*!
    \internal

  Internal function for drawing a polygon.
*/

void TQPainter::drawPolyInternal( const TQPointArray &a, bool close )
{
    if ( a.size() < 2 )
        return;

    int x1, y1, x2, y2;                         // connect last to first point
    a.point( a.size()-1, &x1, &y1 );
    a.point( 0, &x2, &y2 );
    bool do_close = close && !(x1 == x2 && y1 == y2);

    if ( close && cbrush.style() != NoBrush ) { // draw filled polygon
        XFillPolygon( dpy, hd, gc_brush, (XPoint*)a.shortPoints(), a.size(),
                      Nonconvex, CoordModeOrigin );
        if ( cpen.style() == NoPen ) {          // draw fake outline
            XDrawLines( dpy, hd, gc_brush, (XPoint*)a.shortPoints(), a.size(),
                        CoordModeOrigin );
            if ( do_close )
                XDrawLine( dpy, hd, gc_brush, x1, y1, x2, y2 );
        }
    }
    if ( cpen.style() != NoPen ) {              // draw outline
        XDrawLines( dpy, hd, gc, (XPoint*)a.shortPoints(), a.size(),
                    CoordModeOrigin);
        if ( do_close )
            XDrawLine( dpy, hd, gc, x1, y1, x2, y2 );
    }
}


/*!
    Draws/plots a single point at \a (x, y) using the current pen.

    \sa TQPen
*/

void TQPainter::drawPoint( int x, int y )
{
    if ( !isActive() )
        return;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            TQPoint p( x, y );
            param[0].point = &p;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawPoint, this, param ) ||
                 !hd )
                return;
        }
        map( x, y, &x, &y );
    }
    if ( cpen.style() != NoPen )
        XDrawPoint( dpy, hd, gc, x, y );
}


/*!
    Draws/plots an array of points, \a a, using the current pen.

    If \a index is non-zero (the default is zero) only points from \a
    index are drawn. If \a npoints is negative (the default) the rest
    of the points from \a index are drawn. If \a npoints is zero or
    greater, \a npoints points are drawn.

    \warning On X11, coordinates that do not fit into 16-bit signed
    values are truncated. This limitation is expected to go away in
    TQt 4.
*/

void TQPainter::drawPoints( const TQPointArray& a, int index, int npoints )
{
    if ( npoints < 0 )
        npoints = a.size() - index;
    if ( index + npoints > (int)a.size() )
        npoints = a.size() - index;
    if ( !isActive() || npoints < 1 || index < 0 )
        return;
    TQPointArray pa = a;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            for (int i=0; i<npoints; i++) {
                TQPoint p( pa[index+i].x(), pa[index+i].y() );
                param[0].point = &p;
                if ( !pdev->cmd( TQPaintDevice::PdcDrawPoint, this, param ))
                    return;
            }
            if ( !hd ) return;
        }
        if ( txop != TxNone ) {
            pa = xForm( a, index, npoints );
            if ( pa.size() != a.size() ) {
                index = 0;
                npoints = pa.size();
            }
        }
    }
    if ( cpen.style() != NoPen )
        XDrawPoints( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )),
                     npoints, CoordModeOrigin );
}


/*! \obsolete
    Sets the current pen position to \a (x, y)

    \sa lineTo(), pos()
*/

void TQPainter::moveTo( int x, int y )
{
    if ( !isActive() )
        return;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            TQPoint p( x, y );
            param[0].point = &p;
            if ( !pdev->cmd( TQPaintDevice::PdcMoveTo, this, param ) || !hd )
                return;
        }
    }
    curPt = TQPoint( x, y );
}

/*! \obsolete
  Use drawLine() instead.

    Draws a line from the current pen position to \a (x, y) and sets
    \a (x, y) to be the new current pen position.

    \sa TQPen moveTo(), drawLine(), pos()
*/

void TQPainter::lineTo( int x, int y )
{
    if ( !isActive() )
        return;
    int cx = curPt.x(), cy = curPt.y();
    curPt = TQPoint( x, y );
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            TQPoint p( x, y );
            param[0].point = &p;
            if ( !pdev->cmd( TQPaintDevice::PdcLineTo, this, param ) || !hd )
                return;
        }
        map( x, y, &x, &y );
	map( cx,  cy,  &cx,  &cy );
    }
    if ( cpen.style() != NoPen )
        XDrawLine( dpy, hd, gc, cx, cy, x, y );
}

/*!
    Draws a line from (\a x1, \a y1) to (\a x2, \a y2) and sets the
    current pen position to (\a x2, \a y2).

    \sa pen()
*/

void TQPainter::drawLine( int x1, int y1, int x2, int y2 )
{
    if ( !isActive() )
        return;
    curPt = TQPoint( x2, y2 );
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[2];
            TQPoint p1(x1, y1), p2(x2, y2);
            param[0].point = &p1;
            param[1].point = &p2;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawLine, this, param ) || !hd )
                return;
        }
        map( x1, y1, &x1, &y1 );
        map( x2, y2, &x2, &y2 );
    }
    if ( cpen.style() != NoPen )
        XDrawLine( dpy, hd, gc, x1, y1, x2, y2 );
}



/*!
    Draws a rectangle with upper left corner at \a (x, y) and with
    width \a w and height \a h.

    \sa TQPen, drawRoundRect()
*/

void TQPainter::drawRect( int x, int y, int w, int h )
{
    if ( !isActive() )
        return;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            TQRect r( x, y, w, h );
            param[0].rect = &r;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawRect, this, param ) || !hd )
                return;
        }
        if ( txop == TxRotShear ) {             // rotate/shear polygon
	    TQPointArray pa = xmat.mapToPolygon( TQRect(x, y, w, h) );
	    pa.resize( 5 );
	    pa.setPoint( 4, pa.point( 0 ) );
            drawPolyInternal( pa );
            return;
        }
        map( x, y, w, h, &x, &y, &w, &h );
    }
    if ( w <= 0 || h <= 0 ) {
        if ( w == 0 || h == 0 )
            return;
        fix_neg_rect( &x, &y, &w, &h );
    }
    if ( cbrush.style() != NoBrush ) {
        if ( cpen.style() == NoPen ) {
            XFillRectangle( dpy, hd, gc_brush, x, y, w, h );
            return;
        }
	int lw = cpen.width();
	int lw2 = (lw+1)/2;
        if ( w > lw && h > lw )
            XFillRectangle( dpy, hd, gc_brush, x+lw2, y+lw2, w-lw-1, h-lw-1 );
    }
    if ( cpen.style() != NoPen )
        XDrawRectangle( dpy, hd, gc, x, y, w-1, h-1 );
}

/*!
    \overload

    Draws a Windows focus rectangle with upper left corner at (\a x,
    \a y) and with width \a w and height \a h.

    This function draws a stippled XOR rectangle that is used to
    indicate keyboard focus (when TQApplication::style() is \c
    WindowStyle).

    \warning This function draws nothing if the coordinate system has
    been \link rotate() rotated\endlink or \link shear()
    sheared\endlink.

    \sa drawRect(), TQApplication::style()
*/

void TQPainter::drawWinFocusRect( int x, int y, int w, int h )
{
    drawWinFocusRect( x, y, w, h, TRUE, color0 );
}

/*!
    Draws a Windows focus rectangle with upper left corner at (\a x,
    \a y) and with width \a w and height \a h using a pen color that
    contrasts with \a bgColor.

    This function draws a stippled rectangle (XOR is not used) that is
    used to indicate keyboard focus (when the TQApplication::style() is
    \c WindowStyle).

    The pen color used to draw the rectangle is either white or black
    depending on the color of \a bgColor (see TQColor::gray()).

    \warning This function draws nothing if the coordinate system has
    been \link rotate() rotated\endlink or \link shear()
    sheared\endlink.

    \sa drawRect(), TQApplication::style()
*/

void TQPainter::drawWinFocusRect( int x, int y, int w, int h,
                                 const TQColor &bgColor )
{
    drawWinFocusRect( x, y, w, h, FALSE, bgColor );
}


/*!
  \internal
*/

void TQPainter::drawWinFocusRect( int x, int y, int w, int h,
                                 bool xorPaint, const TQColor &bgColor )
{
    if ( !isActive() || txop == TxRotShear )
        return;
    static char winfocus_line[] = { 1, 1 };

    TQPen     old_pen = cpen;
    RasterOp old_rop = (RasterOp)rop;

    if ( xorPaint ) {
        if ( TQColor::numBitPlanes() <= 8 )
            setPen( color1 );
        else
            setPen( white );
        setRasterOp( XorROP );
    } else {
        if ( qGray( bgColor.rgb() ) < 128 )
            setPen( white );
        else
            setPen( black );
    }

    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            TQRect r( x, y, w, h );
            param[0].rect = &r;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawRect, this, param ) || !hd) {
                setRasterOp( old_rop );
                setPen( old_pen );
                return;
            }
        }
        map( x, y, w, h, &x, &y, &w, &h );
    }
    if ( w <= 0 || h <= 0 ) {
        if ( w == 0 || h == 0 )
            return;
        fix_neg_rect( &x, &y, &w, &h );
    }
    XSetDashes( dpy, gc, 0, winfocus_line, 2 );
    XSetLineAttributes( dpy, gc, 1, LineOnOffDash, CapButt, JoinMiter );

    XDrawRectangle( dpy, hd, gc, x, y, w-1, h-1 );
    XSetLineAttributes( dpy, gc, 0, LineSolid, CapButt, JoinMiter );
    setRasterOp( old_rop );
    setPen( old_pen );
}


/*!
    Draws a rectangle with rounded corners at \a (x, y), with width \a
    w and height \a h.

    The \a xRnd and \a yRnd arguments specify how rounded the corners
    should be. 0 is angled corners, 99 is maximum roundedness.

    The width and height include all of the drawn lines.

    \sa drawRect(), TQPen
*/

void TQPainter::drawRoundRect( int x, int y, int w, int h, int xRnd, int yRnd )
{
    if ( !isActive() )
        return;
    if ( xRnd <= 0 || yRnd <= 0 ) {
        drawRect( x, y, w, h );                 // draw normal rectangle
        return;
    }
    if ( xRnd >= 100 )                          // fix ranges
        xRnd = 99;
    if ( yRnd >= 100 )
        yRnd = 99;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[3];
            TQRect r( x, y, w, h );
            param[0].rect = &r;
            param[1].ival = xRnd;
            param[2].ival = yRnd;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawRoundRect, this, param ) ||
                 !hd )
                return;
        }
        if ( txop == TxRotShear ) {             // rotate/shear polygon
            if ( w <= 0 || h <= 0 )
                fix_neg_rect( &x, &y, &w, &h );
            w--;
            h--;
            int rxx = w*xRnd/200;
            int ryy = h*yRnd/200;
            // were there overflows?
            if ( rxx < 0 )
                rxx = w/200*xRnd;
            if ( ryy < 0 )
                ryy = h/200*yRnd;
            int rxx2 = 2*rxx;
            int ryy2 = 2*ryy;
            TQPointArray a[4];
            a[0].makeArc( x, y, rxx2, ryy2, 1*16*90, 16*90, xmat );
            a[1].makeArc( x, y+h-ryy2, rxx2, ryy2, 2*16*90, 16*90, xmat );
            a[2].makeArc( x+w-rxx2, y+h-ryy2, rxx2, ryy2, 3*16*90, 16*90, xmat );
            a[3].makeArc( x+w-rxx2, y, rxx2, ryy2, 0*16*90, 16*90, xmat );
            // ### is there a better way to join TQPointArrays?
            TQPointArray aa;
            aa.resize( a[0].size() + a[1].size() + a[2].size() + a[3].size() );
            uint j = 0;
            for ( int k=0; k<4; k++ ) {
                for ( uint i=0; i<a[k].size(); i++ ) {
                    aa.setPoint( j, a[k].point(i) );
                    j++;
                }
            }
            drawPolyInternal( aa );
            return;
        }
        map( x, y, w, h, &x, &y, &w, &h );
    }
    w--;
    h--;
    if ( w <= 0 || h <= 0 ) {
        if ( w == 0 || h == 0 )
            return;
        fix_neg_rect( &x, &y, &w, &h );
    }
    int rx = (w*xRnd)/200;
    int ry = (h*yRnd)/200;
    int rx2 = 2*rx;
    int ry2 = 2*ry;
    if ( cbrush.style() != NoBrush ) {          // draw filled round rect
        int dp, ds;
        if ( cpen.style() == NoPen ) {
            dp = 0;
            ds = 1;
        }
        else {
            dp = 1;
            ds = 0;
        }
#define SET_ARC(px, py, w, h, a1, a2) \
    a->x=px; a->y=py; a->width=w; a->height=h; a->angle1=a1; a->angle2=a2; a++
        XArc arcs[4];
        XArc *a = arcs;
        SET_ARC( x+w-rx2, y, rx2, ry2, 0, 90*64 );
        SET_ARC( x, y, rx2, ry2, 90*64, 90*64 );
        SET_ARC( x, y+h-ry2, rx2, ry2, 180*64, 90*64 );
        SET_ARC( x+w-rx2, y+h-ry2, rx2, ry2, 270*64, 90*64 );
        XFillArcs( dpy, hd, gc_brush, arcs, 4 );
#undef SET_ARC
#define SET_RCT(px, py, w, h) \
    r->x=px; r->y=py; r->width=w; r->height=h; r++
        XRectangle rects[3];
        XRectangle *r = rects;
        SET_RCT( x+rx, y+dp, w-rx2, ry );
        SET_RCT( x+dp, y+ry, w+ds, h-ry2 );
        SET_RCT( x+rx, y+h-ry, w-rx2, ry+ds );
        XFillRectangles( dpy, hd, gc_brush, rects, 3 );
#undef SET_RCT
    }
    if ( cpen.style() != NoPen ) {              // draw outline
#define SET_ARC(px, py, w, h, a1, a2) \
    a->x=px; a->y=py; a->width=w; a->height=h; a->angle1=a1; a->angle2=a2; a++
        XArc arcs[4];
        XArc *a = arcs;
        SET_ARC( x+w-rx2, y, rx2, ry2, 0, 90*64 );
        SET_ARC( x, y, rx2, ry2, 90*64, 90*64 );
        SET_ARC( x, y+h-ry2, rx2, ry2, 180*64, 90*64 );
        SET_ARC( x+w-rx2, y+h-ry2, rx2, ry2, 270*64, 90*64 );
        XDrawArcs( dpy, hd, gc, arcs, 4 );
#undef SET_ARC
#define SET_SEG(xp1, yp1, xp2, yp2) \
    s->x1=xp1; s->y1=yp1; s->x2=xp2; s->y2=yp2; s++
        XSegment segs[4];
        XSegment *s = segs;
        SET_SEG( x+rx, y, x+w-rx, y );
        SET_SEG( x+rx, y+h, x+w-rx, y+h );
        SET_SEG( x, y+ry, x, y+h-ry );
        SET_SEG( x+w, y+ry, x+w, y+h-ry );
        XDrawSegments( dpy, hd, gc, segs, 4 );
#undef SET_SET
    }
}

/*!
    Draws an ellipse with center at \a (x + w/2, y + h/2) and size \a
    (w, h).
*/

void TQPainter::drawEllipse( int x, int y, int w, int h )
{
    if ( !isActive() )
        return;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            TQRect r( x, y, w, h );
            param[0].rect = &r;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawEllipse, this, param ) ||
                 !hd )
                return;
        }
        if ( txop == TxRotShear ) {             // rotate/shear polygon
            TQPointArray a;
            a.makeArc( x, y, w, h, 0, 360*16, xmat );
            drawPolyInternal( a );
            return;
        }
        map( x, y, w, h, &x, &y, &w, &h );
    }
    if ( w <= 0 || h <= 0 ) {
        if ( w == 0 || h == 0 )
            return;
        fix_neg_rect( &x, &y, &w, &h );
    }
    if ( w == 1 && h == 1 ) {
	XDrawPoint( dpy, hd, (cpen.style() == NoPen)?gc_brush:gc, x, y );
	return;
    }
    w--;
    h--;
    if ( cbrush.style() != NoBrush ) {          // draw filled ellipse
        XFillArc( dpy, hd, gc_brush, x, y, w, h, 0, 360*64 );
        if ( cpen.style() == NoPen ) {
            XDrawArc( dpy, hd, gc_brush, x, y, w, h, 0, 360*64 );
            return;
        }
    }
    if ( cpen.style() != NoPen )                // draw outline
        XDrawArc( dpy, hd, gc, x, y, w, h, 0, 360*64 );
}


/*!
    Draws an arc defined by the rectangle \a (x, y, w, h), the start
    angle \a a and the arc length \a alen.

    The angles \a a and \a alen are 1/16th of a degree, i.e. a full
    circle equals 5760 (16*360). Positive values of \a a and \a alen
    mean counter-clockwise while negative values mean the clockwise
    direction. Zero degrees is at the 3 o'clock position.

    Example:
    \code
	TQPainter p( myWidget );
	p.drawArc( 10,10, 70,100, 100*16, 160*16 ); // draws a "(" arc
    \endcode

    \sa drawPie(), drawChord()
*/

void TQPainter::drawArc( int x, int y, int w, int h, int a, int alen )
{
    if ( !isActive() )
        return;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[3];
            TQRect r( x, y, w, h );
            param[0].rect = &r;
            param[1].ival = a;
            param[2].ival = alen;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawArc, this, param ) ||
                 !hd )
                return;
        }
        if ( txop == TxRotShear ) {             // rotate/shear
            TQPointArray pa;
            pa.makeArc( x, y, w, h, a, alen, xmat ); // arc polyline
            drawPolyInternal( pa, FALSE );
            return;
        }
        map( x, y, w, h, &x, &y, &w, &h );
    }
    w--;
    h--;
    if ( w <= 0 || h <= 0 ) {
        if ( w == 0 || h == 0 )
            return;
        fix_neg_rect( &x, &y, &w, &h );
    }
    if ( cpen.style() != NoPen )
        XDrawArc( dpy, hd, gc, x, y, w, h, a*4, alen*4 );
}


/*!
    Draws a pie defined by the rectangle \a (x, y, w, h), the start
    angle \a a and the arc length \a alen.

    The pie is filled with the current brush().

    The angles \a a and \a alen are 1/16th of a degree, i.e. a full
    circle equals 5760 (16*360). Positive values of \a a and \a alen
    mean counter-clockwise while negative values mean the clockwise
    direction. Zero degrees is at the 3 o'clock position.

    \sa drawArc(), drawChord()
*/

void TQPainter::drawPie( int x, int y, int w, int h, int a, int alen )
{
    // Make sure "a" is 0..360*16, as otherwise a*4 may overflow 16 bits.
    if ( a > (360*16) ) {
        a = a % (360*16);
    } else if ( a < 0 ) {
        a = a % (360*16);
        if ( a < 0 ) a += (360*16);
    }

    if ( !isActive() )
        return;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[3];
            TQRect r( x, y, w, h );
            param[0].rect = &r;
            param[1].ival = a;
            param[2].ival = alen;
            if ( !pdev->cmd( TQPaintDevice::PdcDrawPie, this, param ) || !hd )
                return;
        }
        if ( txop == TxRotShear ) {             // rotate/shear
            TQPointArray pa;
            pa.makeArc( x, y, w, h, a, alen, xmat ); // arc polyline
            int n = pa.size();
            int cx, cy;
            xmat.map(x+w/2, y+h/2, &cx, &cy);
            pa.resize( n+2 );
            pa.setPoint( n, cx, cy );   // add legs
            pa.setPoint( n+1, pa.at(0) );
            drawPolyInternal( pa );
            return;
        }
        map( x, y, w, h, &x, &y, &w, &h );
    }
    XSetArcMode( dpy, gc_brush, ArcPieSlice );
    w--;
    h--;
    if ( w <= 0 || h <= 0 ) {
        if ( w == 0 || h == 0 )
            return;
        fix_neg_rect( &x, &y, &w, &h );
    }

    GC g = gc;
    bool nopen = cpen.style() == NoPen;

    if ( cbrush.style() != NoBrush ) {          // draw filled pie
        XFillArc( dpy, hd, gc_brush, x, y, w, h, a*4, alen*4 );
        if ( nopen ) {
            g = gc_brush;
            nopen = FALSE;
        }
    }
    if ( !nopen ) {                             // draw pie outline
        double w2 = 0.5*w;                      // with, height in ellipsis
        double h2 = 0.5*h;
        double xc = (double)x+w2;
        double yc = (double)y+h2;
        double ra1 = Q_PI/2880.0*a;             // convert a, alen to radians
        double ra2 = ra1 + Q_PI/2880.0*alen;
        int xic = qRound(xc);
        int yic = qRound(yc);
        XDrawLine( dpy, hd, g, xic, yic,
                   qRound(xc + qcos(ra1)*w2), qRound(yc - qsin(ra1)*h2));
        XDrawLine( dpy, hd, g, xic, yic,
                   qRound(xc + qcos(ra2)*w2), qRound(yc - qsin(ra2)*h2));
        XDrawArc( dpy, hd, g, x, y, w, h, a*4, alen*4 );
    }
}


/*!
    Draws a chord defined by the rectangle \a (x, y, w, h), the start
    angle \a a and the arc length \a alen.

    The chord is filled with the current brush().

    The angles \a a and \a alen are 1/16th of a degree, i.e. a full
    circle equals 5760 (16*360). Positive values of \a a and \a alen
    mean counter-clockwise while negative values mean the clockwise
    direction. Zero degrees is at the 3 o'clock position.

    \sa drawArc(), drawPie()
*/

void TQPainter::drawChord( int x, int y, int w, int h, int a, int alen )
{
    if ( !isActive() )
        return;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[3];
            TQRect r( x, y, w, h );
            param[0].rect = &r;
            param[1].ival = a;
            param[2].ival = alen;
            if ( !pdev->cmd(TQPaintDevice::PdcDrawChord, this, param) || !hd )
                return;
        }
        if ( txop == TxRotShear ) {             // rotate/shear
            TQPointArray pa;
            pa.makeArc( x, y, w-1, h-1, a, alen, xmat ); // arc polygon
            int n = pa.size();
            pa.resize( n+1 );
            pa.setPoint( n, pa.at(0) );         // connect endpoints
            drawPolyInternal( pa );
            return;
        }
        map( x, y, w, h, &x, &y, &w, &h );
    }
    XSetArcMode( dpy, gc_brush, ArcChord );
    w--;
    h--;
    if ( w <= 0 || h <= 0 ) {
        if ( w == 0 || h == 0 )
            return;
        fix_neg_rect( &x, &y, &w, &h );
    }

    GC g = gc;
    bool nopen = cpen.style() == NoPen;

    if ( cbrush.style() != NoBrush ) {          // draw filled chord
        XFillArc( dpy, hd, gc_brush, x, y, w, h, a*4, alen*4 );
        if ( nopen ) {
            g = gc_brush;
            nopen = FALSE;
        }
    }
    if ( !nopen ) {                             // draw chord outline
        double w2 = 0.5*w;                      // with, height in ellipsis
        double h2 = 0.5*h;
        double xc = (double)x+w2;
        double yc = (double)y+h2;
        double ra1 = Q_PI/2880.0*a;             // convert a, alen to radians
        double ra2 = ra1 + Q_PI/2880.0*alen;
        XDrawLine( dpy, hd, g,
                   qRound(xc + qcos(ra1)*w2), qRound(yc - qsin(ra1)*h2),
                   qRound(xc + qcos(ra2)*w2), qRound(yc - qsin(ra2)*h2));
        XDrawArc( dpy, hd, g, x, y, w, h, a*4, alen*4 );
    }
    XSetArcMode( dpy, gc_brush, ArcPieSlice );
}


/*!
    Draws \a nlines separate lines from points defined in \a a,
    starting at \a a[index] (\a index defaults to 0). If \a nlines is
    -1 (the default) all points until the end of the array are used
    (i.e. (a.size()-index)/2 lines are drawn).

    Draws the 1st line from \a a[index] to \a a[index+1]. Draws the
    2nd line from \a a[index+2] to \a a[index+3] etc.

    \warning On X11, coordinates that do not fit into 16-bit signed
    values are truncated. This limitation is expected to go away in
    TQt 4.

    \sa drawPolyline(), drawPolygon(), TQPen
*/

void TQPainter::drawLineSegments( const TQPointArray &a, int index, int nlines )
{
    if ( nlines < 0 )
        nlines = a.size()/2 - index/2;
    if ( index + nlines*2 > (int)a.size() )
        nlines = (a.size() - index)/2;
    if ( !isActive() || nlines < 1 || index < 0 )
        return;
    TQPointArray pa = a;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            if ( 2*nlines != (int)pa.size() ) {
                pa = TQPointArray( nlines*2 );
                for ( int i=0; i<nlines*2; i++ )
                    pa.setPoint( i, a.point(index+i) );
                index = 0;
            }
            TQPDevCmdParam param[1];
            param[0].ptarr = (TQPointArray*)&pa;
            if ( !pdev->cmd(TQPaintDevice::PdcDrawLineSegments, this, param) ||
                 !hd )
                return;
        }
        if ( txop != TxNone ) {
            pa = xForm( a, index, nlines*2 );
            if ( pa.size() != a.size() ) {
                index  = 0;
                nlines = pa.size()/2;
            }
        }
    }
    if ( cpen.style() != NoPen )
        XDrawSegments( dpy, hd, gc,
                       (XSegment*)(pa.shortPoints( index, nlines*2 )), nlines );
}


/*!
    Draws the polyline defined by the \a npoints points in \a a
    starting at \a a[index]. (\a index defaults to 0.)

    If \a npoints is -1 (the default) all points until the end of the
    array are used (i.e. a.size()-index-1 line segments are drawn).

    \warning On X11, coordinates that do not fit into 16-bit signed
    values are truncated. This limitation is expected to go away in
    TQt 4.

    \sa drawLineSegments(), drawPolygon(), TQPen
*/

void TQPainter::drawPolyline( const TQPointArray &a, int index, int npoints )
{
    if ( npoints < 0 )
        npoints = a.size() - index;
    if ( index + npoints > (int)a.size() )
        npoints = a.size() - index;
    if ( !isActive() || npoints < 2 || index < 0 )
        return;
    TQPointArray pa = a;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            if ( npoints != (int)pa.size() ) {
                pa = TQPointArray( npoints );
                for ( int i=0; i<npoints; i++ )
                    pa.setPoint( i, a.point(index+i) );
                index = 0;
            }
            TQPDevCmdParam param[1];
            param[0].ptarr = (TQPointArray*)&pa;
            if ( !pdev->cmd(TQPaintDevice::PdcDrawPolyline, this, param) || !hd )
                return;
        }
        if ( txop != TxNone ) {
            pa = xForm( pa, index, npoints );
            if ( pa.size() != a.size() ) {
                index   = 0;
                npoints = pa.size();
            }
        }
    }
    if ( cpen.style() != NoPen ) {
        while(npoints>65535) {
            XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, 65535 )),
                        65535, CoordModeOrigin );
            npoints-=65535;
            index+=65535;
        }
        XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )),
                    npoints, CoordModeOrigin );
    }
}

static int global_polygon_shape = Complex;

/*!
    Draws the polygon defined by the \a npoints points in \a a
    starting at \a a[index]. (\a index defaults to 0.)

    If \a npoints is -1 (the default) all points until the end of the
    array are used (i.e. a.size()-index line segments define the
    polygon).

    The first point is always connected to the last point.

    The polygon is filled with the current brush(). If \a winding is
    TRUE, the polygon is filled using the winding fill algorithm. If
    \a winding is FALSE, the polygon is filled using the even-odd
    (alternative) fill algorithm.

    \warning On X11, coordinates that do not fit into 16-bit signed
    values are truncated. This limitation is expected to go away in
    TQt 4.

    \sa drawLineSegments(), drawPolyline(), TQPen
*/

void TQPainter::drawPolygon( const TQPointArray &a, bool winding,
                            int index, int npoints )
{
    if ( npoints < 0 )
        npoints = a.size() - index;
    if ( index + npoints > (int)a.size() )
        npoints = a.size() - index;
    if ( !isActive() || npoints < 2 || index < 0 )
        return;
    TQPointArray pa = a;
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            if ( npoints != (int)a.size() ) {
                pa = TQPointArray( npoints );
                for ( int i=0; i<npoints; i++ )
                    pa.setPoint( i, a.point(index+i) );
                index = 0;
            }
            TQPDevCmdParam param[2];
            param[0].ptarr = (TQPointArray*)&pa;
            param[1].ival = winding;
            if ( !pdev->cmd(TQPaintDevice::PdcDrawPolygon, this, param) || !hd )
                return;
        }
        if ( txop != TxNone ) {
            pa = xForm( a, index, npoints );
            if ( pa.size() != a.size() ) {
                index   = 0;
                npoints = pa.size();
            }
        }
    }
    if ( winding )                              // set to winding fill rule
        XSetFillRule( dpy, gc_brush, WindingRule );

    if ( pa[index] != pa[index+npoints-1] ){   // close open pointarray
        pa.detach();
        pa.resize( index+npoints+1 );
        pa.setPoint( index+npoints, pa[index] );
        npoints++;
    }

    if ( cbrush.style() != NoBrush ) {          // draw filled polygon
        XFillPolygon( dpy, hd, gc_brush,
                      (XPoint*)(pa.shortPoints( index, npoints )),
                      npoints, global_polygon_shape, CoordModeOrigin );
    }
    if ( cpen.style() != NoPen ) {              // draw outline
        XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )),
                    npoints, CoordModeOrigin );
    }
    if ( winding )                              // set to normal fill rule
        XSetFillRule( dpy, gc_brush, EvenOddRule );
}

/*!
    Draws the convex polygon defined by the \a npoints points in \a pa
    starting at \a pa[index] (\a index defaults to 0).

    If the supplied polygon is not convex, the results are undefined.

    On some platforms (e.g. X Window), this is faster than
    drawPolygon().

    \warning On X11, coordinates that do not fit into 16-bit signed
    values are truncated. This limitation is expected to go away in
    TQt 4.
*/
void TQPainter::drawConvexPolygon( const TQPointArray &pa,
                                  int index, int npoints )
{
    global_polygon_shape = Convex;
    drawPolygon(pa, FALSE, index, npoints);
    global_polygon_shape = Complex;
}



/*!
    Draws a cubic Bezier curve defined by the control points in \a a,
    starting at \a a[index] (\a index defaults to 0).

    Control points after \a a[index + 3] are ignored. Nothing happens
    if there aren't enough control points.

    \warning On X11, coordinates that do not fit into 16-bit signed
    values are truncated. This limitation is expected to go away in
    TQt 4.
*/

void TQPainter::drawCubicBezier( const TQPointArray &a, int index )
{
    if ( !isActive() )
        return;
    if ( a.size() - index < 4 ) {
#if defined(QT_CHECK_RANGE)
        qWarning( "TQPainter::drawCubicBezier: Cubic Bezier needs 4 control "
                  "points" );
#endif
        return;
    }
    TQPointArray pa( a );
    if ( index != 0 || a.size() > 4 ) {
        pa = TQPointArray( 4 );
        for ( int i=0; i<4; i++ )
            pa.setPoint( i, a.point(index+i) );
    }
    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) ) {
            TQPDevCmdParam param[1];
            param[0].ptarr = (TQPointArray*)&pa;
            if ( !pdev->cmd(TQPaintDevice::PdcDrawCubicBezier, this, param) ||
                 !hd )
                return;
        }
        if ( txop != TxNone )
            pa = xForm( pa );
    }
    if ( cpen.style() != NoPen ) {
        pa = pa.cubicBezier();
        XDrawLines( dpy, hd, gc, (XPoint*)pa.shortPoints(), pa.size(),
                    CoordModeOrigin );
    }
}


/*!
    Draws a pixmap at \a (x, y) by copying a part of \a pixmap into
    the paint device.

    \a (x, y) specifies the top-left point in the paint device that is
    to be drawn onto. \a (sx, sy) specifies the top-left point in \a
    pixmap that is to be drawn. The default is (0, 0).

    \a (sw, sh) specifies the size of the pixmap that is to be drawn.
    The default, (-1, -1), means all the way to the bottom right of
    the pixmap.

    Currently the mask of the pixmap or it's alpha channel are ignored
    when painting on a TQPrinter.

    \sa bitBlt(), TQPixmap::setMask()
*/

void TQPainter::drawPixmap( int x, int y, const TQPixmap &pixmap,
                           int sx, int sy, int sw, int sh )
{
    if ( !isActive() || pixmap.isNull() )
        return;

    // right/bottom
    if ( sw < 0 )
        sw = pixmap.width()  - sx;
    if ( sh < 0 )
        sh = pixmap.height() - sy;

    // Sanity-check clipping
    if ( sx < 0 ) {
        x -= sx;
        sw += sx;
        sx = 0;
    }
    if ( sw + sx > pixmap.width() )
        sw = pixmap.width() - sx;
    if ( sy < 0 ) {
        y -= sy;
        sh += sy;
        sy = 0;
    }
    if ( sh + sy > pixmap.height() )
        sh = pixmap.height() - sy;

    if ( sw <= 0 || sh <= 0 )
        return;

    if ( pdev->x11Screen() != pixmap.x11Screen() ) {
        TQPixmap* p = (TQPixmap*) &pixmap;
        p->x11SetScreen( pdev->x11Screen() );
    }

    TQPixmap::x11SetDefaultScreen( pixmap.x11Screen() );

    if ( testf(ExtDev|VxF|WxF) ) {
        if ( testf(ExtDev) || txop == TxScale || txop == TxRotShear ) {
            if ( sx != 0 || sy != 0 ||
                 sw != pixmap.width() || sh != pixmap.height() ) {
                TQPixmap tmp( sw, sh, pixmap.depth() );
                bitBlt( &tmp, 0, 0, &pixmap, sx, sy, sw, sh, CopyROP, TRUE );
                if ( pixmap.mask() ) {
                    TQBitmap mask( sw, sh );
                    bitBlt( &mask, 0, 0, pixmap.mask(), sx, sy, sw, sh,
                            CopyROP, TRUE );
                    tmp.setMask( mask );
                }
                drawPixmap( x, y, tmp );
                return;
            }
            if ( testf(ExtDev) ) {
                TQPDevCmdParam param[2];
                TQRect r(x, y, pixmap.width(), pixmap.height());
                param[0].rect  = &r;
                param[1].pixmap = &pixmap;
                if ( !pdev->cmd(TQPaintDevice::PdcDrawPixmap, this, param) || !hd )
                    return;
            }
            if ( txop == TxScale || txop == TxRotShear ) {
                TQWMatrix mat( m11(), m12(),
                              m21(), m22(),
                              dx(),  dy() );
                mat = TQPixmap::trueMatrix( mat, sw, sh );
                TQPixmap pm = pixmap.xForm( mat );
                if ( !pm.mask() && txop == TxRotShear ) {
                    TQBitmap bm_clip( sw, sh, 1 );
                    bm_clip.fill( color1 );
                    pm.setMask( bm_clip.xForm(mat) );
                }
                map( x, y, &x, &y );            // compute position of pixmap
                int dx, dy;
                mat.map( 0, 0, &dx, &dy );
                uint save_flags = flags;
                flags = IsActive | (save_flags & ClipOn);
                drawPixmap( x-dx, y-dy, pm );
                flags = save_flags;
                return;
            }
        }
        map( x, y, &x, &y );
    }

    TQBitmap *mask = (TQBitmap *)pixmap.mask();
    bool mono = pixmap.depth() == 1;

    if ( mask && !hasClipping() && pdev != paintEventDevice ) {
        if ( mono ) {                           // needs GCs pen color
            bool selfmask = pixmap.data->selfmask;
            if ( selfmask ) {
                XSetFillStyle( dpy, gc, FillStippled );
                XSetStipple( dpy, gc, pixmap.handle() );
            } else {
                XSetFillStyle( dpy, gc, FillOpaqueStippled );
                XSetStipple( dpy, gc, pixmap.handle() );
                XSetClipMask( dpy, gc, mask->handle() );
                XSetClipOrigin( dpy, gc, x-sx, y-sy );
            }
            XSetTSOrigin( dpy, gc, x-sx, y-sy );
            XFillRectangle( dpy, hd, gc, x, y, sw, sh );
            XSetTSOrigin( dpy, gc, 0, 0 );
            XSetFillStyle( dpy, gc, FillSolid );
            if ( !selfmask ) {
                if ( pdev == paintEventDevice && paintEventClipRegion ) {
                    x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion );
		} else {
                    x11ClearClipRegion(dpy, gc, 0, rendhd);
		}
            }
        } else {
            bitBlt( pdev, x, y, &pixmap, sx, sy, sw, sh, (RasterOp)rop );
        }
        return;
    }

    TQRegion rgn = crgn;

    if ( mask ) {                               // pixmap has clip mask
        // Implies that clipping is on, either explicit or implicit
        // Create a new mask that combines the mask with the clip region

        if ( pdev == paintEventDevice && paintEventClipRegion ) {
            if ( hasClipping() )
                rgn = rgn.intersect( *paintEventClipRegion );
            else
                rgn = *paintEventClipRegion;
        }

        TQBitmap *comb = new TQBitmap( sw, sh );
        comb->detach();
        GC cgc = qt_xget_temp_gc( pixmap.x11Screen(), TRUE );   // get temporary mono GC
        XSetForeground( dpy, cgc, 0 );
        XFillRectangle( dpy, comb->handle(), cgc, 0, 0, sw, sh );
        XSetBackground( dpy, cgc, 0 );
        XSetForeground( dpy, cgc, 1 );
	int num;
	XRectangle *rects = (XRectangle *)qt_getClipRects( rgn, num );
        XSetClipRectangles( dpy, cgc, -x, -y, rects, num, YXBanded );
        XSetFillStyle( dpy, cgc, FillOpaqueStippled );
        XSetStipple( dpy, cgc, mask->handle() );
        XSetTSOrigin( dpy, cgc, -sx, -sy );
        XFillRectangle( dpy, comb->handle(), cgc, 0, 0, sw, sh );
        XSetTSOrigin( dpy, cgc, 0, 0 );         // restore cgc
        XSetFillStyle( dpy, cgc, FillSolid );
        XSetClipMask( dpy, cgc, None );
        mask = comb;                            // it's deleted below

        XSetClipMask( dpy, gc, mask->handle() );
        XSetClipOrigin( dpy, gc, x, y );
    }

    if ( mono ) {
        XSetBackground( dpy, gc, bg_col.pixel(scrn) );
        XSetFillStyle( dpy, gc, FillOpaqueStippled );
        XSetStipple( dpy, gc, pixmap.handle() );
        XSetTSOrigin( dpy, gc, x-sx, y-sy );
        XFillRectangle( dpy, hd, gc, x, y, sw, sh );
        XSetTSOrigin( dpy, gc, 0, 0 );
        XSetFillStyle( dpy, gc, FillSolid );
    } else {
#if !defined(QT_NO_XFTFREETYPE) && !defined(QT_NO_XRENDER)
        Picture pict = rendhd ? XftDrawPicture((XftDraw *) rendhd) : None;
	TQPixmap *alpha = pixmap.data->alphapm;

	if ( pict && pixmap.x11RenderHandle() &&
	     alpha && alpha->x11RenderHandle()) {
	    XRenderComposite(dpy, PictOpOver, pixmap.x11RenderHandle(),
			     alpha->x11RenderHandle(), pict,
			     sx, sy, sx, sy, x, y, sw, sh);
	} else
#endif // !QT_NO_XFTFREETYPE && !QT_NO_XRENDER
        {
            XCopyArea( dpy, pixmap.handle(), hd, gc, sx, sy, sw, sh, x, y );
        }
    }

    if ( mask ) {                               // restore clipping
        XSetClipOrigin( dpy, gc, 0, 0 );
        XSetRegion( dpy, gc, rgn.handle() );
        delete mask;                            // delete comb, created above
    }
}


/* Internal, used by drawTiledPixmap */

static void drawTile( TQPainter *p, int x, int y, int w, int h,
                      const TQPixmap &pixmap, int xOffset, int yOffset )
{
    int yPos, xPos, drawH, drawW, yOff, xOff;
    yPos = y;
    yOff = yOffset;
    while( yPos < y + h ) {
        drawH = pixmap.height() - yOff;    // Cropping first row
        if ( yPos + drawH > y + h )        // Cropping last row
            drawH = y + h - yPos;
        xPos = x;
        xOff = xOffset;
        while( xPos < x + w ) {
            drawW = pixmap.width() - xOff; // Cropping first column
            if ( xPos + drawW > x + w )    // Cropping last column
                drawW = x + w - xPos;
            p->drawPixmap( xPos, yPos, pixmap, xOff, yOff, drawW, drawH );
            xPos += drawW;
            xOff = 0;
        }
        yPos += drawH;
        yOff = 0;
    }
}

#if 0 // see comment in drawTiledPixmap
/* Internal, used by drawTiledPixmap */

static void fillTile(  TQPixmap *tile, const TQPixmap &pixmap )
{
    bitBlt( tile, 0, 0, &pixmap, 0, 0, -1, -1, TQt::CopyROP, TRUE );
    int x = pixmap.width();
    while ( x < tile->width() ) {
        bitBlt( tile, x,0, tile, 0,0, x,pixmap.height(), TQt::CopyROP, TRUE );
        x *= 2;
    }
    int y = pixmap.height();
    while ( y < tile->height() ) {
        bitBlt( tile, 0,y, tile, 0,0, tile->width(),y, TQt::CopyROP, TRUE );
        y *= 2;
    }
}
#endif

/*!
    Draws a tiled \a pixmap in the specified rectangle.

    \a (x, y) specifies the top-left point in the paint device that is
    to be drawn onto; with the width and height given by \a w and \a
    h. \a (sx, sy) specifies the top-left point in \a pixmap that is
    to be drawn. The default is (0, 0).

    Calling drawTiledPixmap() is similar to calling drawPixmap()
    several times to fill (tile) an area with a pixmap, but is
    potentially much more efficient depending on the underlying window
    system.

    \sa drawPixmap()
*/

void TQPainter::drawTiledPixmap( int x, int y, int w, int h,
                                const TQPixmap &pixmap, int sx, int sy )
{
    int sw = pixmap.width();
    int sh = pixmap.height();
    if (!sw || !sh )
        return;
    if ( sx < 0 )
        sx = sw - -sx % sw;
    else
        sx = sx % sw;
    if ( sy < 0 )
        sy = sh - -sy % sh;
    else
        sy = sy % sh;
    /*
      Retquirements for optimizing tiled pixmaps:
      - not an external device
      - not scale or rotshear
      - not mono pixmap
      - no mask
    */
    TQBitmap *mask = (TQBitmap *)pixmap.mask();
    if ( !testf(ExtDev) && txop <= TxTranslate && pixmap.depth() > 1 &&
         mask == 0 ) {
        if ( txop == TxTranslate )
            map( x, y, &x, &y );

#if !defined(QT_NO_XFTFREETYPE) && !defined(QT_NO_XRENDER)
        Picture pict = rendhd ? XftDrawPicture((XftDraw *) rendhd) : None;
	TQPixmap *alpha = pixmap.data->alphapm;

        if (pict && pixmap.x11RenderHandle() && alpha && alpha->x11RenderHandle()) {
	    // this is essentially drawTile() from above, inlined for
	    // the XRenderComposite call
	    int yPos, xPos, drawH, drawW, yOff, xOff;
	    yPos = y;
	    yOff = sy;
	    while( yPos < y + h ) {
		drawH = pixmap.height() - yOff;    // Cropping first row
		if ( yPos + drawH > y + h )        // Cropping last row
		    drawH = y + h - yPos;
		xPos = x;
		xOff = sx;
		while( xPos < x + w ) {
		    drawW = pixmap.width() - xOff; // Cropping first column
		    if ( xPos + drawW > x + w )    // Cropping last column
			drawW = x + w - xPos;
		    XRenderComposite(dpy, PictOpOver, pixmap.x11RenderHandle(),
				     alpha->x11RenderHandle(), pict,
				     xOff, yOff, xOff, yOff, xPos, yPos, drawW, drawH);
		    xPos += drawW;
		    xOff = 0;
		}
		yPos += drawH;
		yOff = 0;
	    }
	    return;
	}
#endif // !QT_NO_XFTFREETYPE && !QT_NO_XRENDER

	XSetTile( dpy, gc, pixmap.handle() );
	XSetFillStyle( dpy, gc, FillTiled );
	XSetTSOrigin( dpy, gc, x-sx, y-sy );
	XFillRectangle( dpy, hd, gc, x, y, w, h );
	XSetTSOrigin( dpy, gc, 0, 0 );
	XSetFillStyle( dpy, gc, FillSolid );
	return;
    }

#if 0
    // maybe there'll be point in this again, but for the time all it
    // does is make trouble for the postscript code.
    if ( sw*sh < 8192 && sw*sh < 16*w*h ) {
        int tw = sw;
        int th = sh;
        while( th * tw < 4096 && ( th < h || tw < w ) ) {
            if ( h/th > w/tw )
                th *= 2;
            else
                tw *= 2;
        }
        TQPixmap tile( tw, th, pixmap.depth(), TQPixmap::NormalOptim );
        fillTile( &tile, pixmap );
        if ( mask ) {
            TQBitmap tilemask( tw, th, TQPixmap::NormalOptim );
            fillTile( &tilemask, *mask );
            tile.setMask( tilemask );
        }
        drawTile( this, x, y, w, h, tile, sx, sy );
    } else {
        drawTile( this, x, y, w, h, pixmap, sx, sy );
    }
#else
    // for now we'll just output the original and let the postscript
    // code make what it can of it.  qpicture will be unhappy.
    drawTile( this, x, y, w, h, pixmap, sx, sy );
#endif
}

#if 0
//
// Generate a string that describes a transformed bitmap. This string is used
// to insert and find bitmaps in the global pixmap cache.
//

static TQString gen_text_bitmap_key( const TQWMatrix &m, const TQFont &font,
                                    const TQString &str, int pos, int len )
{
    TQString fk = font.key();
    int sz = 4*2 + len*2 + fk.length()*2 + sizeof(double)*6;
    TQByteArray buf(sz);
    uchar *p = (uchar *)buf.data();
    *((double*)p)=m.m11();  p+=sizeof(double);
    *((double*)p)=m.m12();  p+=sizeof(double);
    *((double*)p)=m.m21();  p+=sizeof(double);
    *((double*)p)=m.m22();  p+=sizeof(double);
    *((double*)p)=m.dx();   p+=sizeof(double);
    *((double*)p)=m.dy();   p+=sizeof(double);
    TQChar h1( '$' );
    TQChar h2( 'q' );
    TQChar h3( 't' );
    TQChar h4( '$' );
    *((TQChar*)p)=h1;  p+=2;
    *((TQChar*)p)=h2;  p+=2;
    *((TQChar*)p)=h3;  p+=2;
    *((TQChar*)p)=h4;  p+=2;
    memcpy( (char*)p, (char*)(str.unicode()+pos), len*2 );  p += len*2;
    memcpy( (char*)p, (char*)fk.unicode(), fk.length()*2 ); p += fk.length()*2;
    return TQString( (TQChar*)buf.data(), buf.size()/2 );
}

static TQBitmap *get_text_bitmap( const TQString &key )
{
    return (TQBitmap*)TQPixmapCache::find( key );
}

static void ins_text_bitmap( const TQString &key, TQBitmap *bm )
{
    if ( !TQPixmapCache::insert(key, bm) )       // cannot insert pixmap
        delete bm;
}
#endif

void qt_draw_transformed_rect( TQPainter *p,  int x, int y, int w,  int h, bool fill )
{
    XPoint points[5];
    int xp = x,  yp = y;
    p->map( xp, yp, &xp, &yp );
    points[0].x = xp;
    points[0].y = yp;
    xp = x + w; yp = y;
    p->map( xp, yp, &xp, &yp );
    points[1].x = xp;
    points[1].y = yp;
    xp = x + w; yp = y + h;
    p->map( xp, yp, &xp, &yp );
    points[2].x = xp;
    points[2].y = yp;
    xp = x; yp = y + h;
    p->map( xp, yp, &xp, &yp );
    points[3].x = xp;
    points[3].y = yp;
    points[4] = points[0];

    if ( fill )
	XFillPolygon( p->dpy, p->hd, p->gc, points, 4, Convex, CoordModeOrigin );
    else
	XDrawLines( p->dpy, p->hd, p->gc, points, 5, CoordModeOrigin );
}

void qt_draw_background( TQPainter *p, int x, int y, int w,  int h )
{
    if (p->testf(TQPainter::ExtDev)) {
	if (p->pdev->devType() == TQInternal::Printer)
	    p->fillRect(x, y, w, h, p->bg_col);
	return;
    }
    XSetForeground( p->dpy, p->gc, p->bg_col.pixel(p->scrn) );
    qt_draw_transformed_rect( p, x, y, w, h, TRUE);
    XSetForeground( p->dpy, p->gc, p->cpen.color().pixel(p->scrn) );
}

/*!
    Draws at most \a len characters of the string \a str at position
    \a (x, y).

    \a (x, y) is the base line position. Note that the meaning of \a y
    is not the same for the two drawText() varieties.
*/
void TQPainter::drawText( int x, int y, const TQString &str, int len, TQPainter::TextDirection dir )
{
    drawText( x, y, str, 0, len, dir );
}

/*!
  Draws at most \a len characters starting at position \a pos from the
  string \a str to position \a (x, y).

  \a (x, y) is the base line position. Note that the meaning of \a y
  is not the same for the two drawText() varieties.
*/
void TQPainter::drawText( int x, int y, const TQString &str, int pos, int len, TQPainter::TextDirection dir )
{
    if ( !isActive() )
        return;
    if (len < 0)
        len = str.length() - pos;
    if ( len <= 0 || pos >= (int)str.length() ) // empty string
        return;
    if ( pos + len > (int)str.length() )
        len = str.length() - pos;

    if ( testf(DirtyFont) ) {
	updateFont();
    }

    if ( testf(ExtDev) && pdev->devType() != TQInternal::Printer ) {
	TQPDevCmdParam param[3];
	TQPoint p(x, y);
	TQString string = str.mid( pos,  len );
	param[0].point = &p;
	param[1].str = &string;
	param[2].ival = TQFont::Latin;
	if ( !pdev->cmd(TQPaintDevice::PdcDrawText2, this, param) || !hd )
	    return;
    }

    bool simple = (dir == TQPainter::Auto) && str.simpleText();
    // we can't take the complete string here as we would otherwise
    // get quadratic behaviour when drawing long strings in parts.
    // we do however need some chars around the part we paint to get arabic shaping correct.
    // ### maybe possible to remove after cursor restrictions work in TQRT
    int start;
    int end;
    if ( simple ) {
	start = pos;
	end = pos+len;
    } else {
	start = TQMAX( 0,  pos - 8 );
	end = TQMIN( (int)str.length(), pos + len + 8 );
    }
    TQConstString cstr( str.unicode() + start, end - start );
    pos -= start;

    TQTextEngine engine( cstr.string(), pfont ? pfont->d : cfont.d );
    TQTextLayout layout( &engine );

    // this is actually what beginLayout does. Inlined here, so we can
    // avoid the bidi algorithm if we don't need it.
    engine.itemize( simple ? TQTextEngine::NoBidi|TQTextEngine::SingleLine : TQTextEngine::Full|TQTextEngine::SingleLine );
    engine.currentItem = 0;
    engine.firstItemInLine = -1;

    if ( dir != Auto ) {
	int level = dir == RTL ? 1 : 0;
	for ( int i = engine.items.size(); i >= 0; i-- )
	    engine.items[i].analysis.bidiLevel = level;
    }

    if ( !simple ) {
	layout.setBoundary( pos );
	layout.setBoundary( pos + len );
    }

    // small hack to force skipping of unneeded items
    start = 0;
    while ( engine.items[start].position < pos )
	++start;
    engine.currentItem = start;
    layout.beginLine( 0xfffffff );
    end = start;
    while ( !layout.atEnd() && layout.currentItem().from() < pos + len ) {
	layout.addCurrentItem();
	end++;
    }
    TQFontMetrics fm(fontMetrics());
    int ascent = fm.ascent(), descent = fm.descent();
    int left, right;
    layout.endLine( 0, 0, TQt::SingleLine|TQt::AlignLeft, &ascent, &descent, &left, &right );

    // do _not_ call endLayout() here, as it would clean up the shaped items and we would do shaping another time
    // for painting.

    int textFlags = 0;
    if ( cfont.d->underline ) textFlags |= TQt::Underline;
    if ( cfont.d->overline ) textFlags |= TQt::Overline;
    if ( cfont.d->strikeOut ) textFlags |= TQt::StrikeOut;

    if ( bg_mode == OpaqueMode )
	qt_draw_background( this, x, y-ascent, right-left, ascent+descent+1);

    for ( int i = start; i < end; i++ ) {
	TQTextItem ti;
	ti.item = i;
	ti.engine = &engine;

	drawTextItem( x, y - ascent, ti, textFlags );
    }
    layout.d = 0;
}


/*! \internal
    Draws the text item \a ti at position \a (x, y ).

    This method ignores the painters background mode and
    color. drawText and qt_format_text have to do it themselves, as
    only they know the extents of the complete string.

    It ignores the font set on the painter as the text item has one of its own.

    The underline and strikeout parameters of the text items font are
    ignored aswell. You'll need to pass in the correct flags to get
    underlining and strikeout.
*/
void TQPainter::drawTextItem( int x,  int y, const TQTextItem &ti, int textFlags )
{
    if ( testf(ExtDev) ) {
	TQPDevCmdParam param[2];
	TQPoint p(x, y);
	param[0].point = &p;
	param[1].textItem = &ti;
	bool retval = pdev->cmd(TQPaintDevice::PdcDrawTextItem, this, param);
	if ( !retval || !hd )
	    return;
    }

    TQTextEngine *engine = ti.engine;
    TQScriptItem *si = &engine->items[ti.item];

    engine->shape( ti.item );
    TQFontEngine *fe = si->fontEngine;
    assert( fe != 0 );

    x += si->x;
    y += si->y;

    fe->draw( this, x,  y, engine, si, textFlags );
}

#if QT_VERSION >= 0x040000
#error "remove current position and associated methods"
#endif
/*!
  \obsolete
    Returns the current position of the  pen.

    \sa moveTo()
 */
TQPoint TQPainter::pos() const
{
    return curPt;
}