summaryrefslogtreecommitdiffstats
path: root/src/qt_qt_wrapper.cpp
blob: 067cf248e8b500455205de0d07c3ca89142d982b (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
#include <tqstyle.h>
#include <tqpainter.h>
#include <tqpushbutton.h>
#include <tqimage.h>
#include <tqstylefactory.h>
#include <tqtabbar.h>
#include <tqcheckbox.h>
#include <tqradiobutton.h>
#include <tqscrollbar.h>
#include <tqmenubar.h>
#include <tqcombobox.h>
#include <tqprogressbar.h>
#include <tqslider.h>
#include <tqtoolbutton.h>
#include <tqapplication.h>
#include <tqdir.h>
#include <tqregexp.h>
#include <tqbitmap.h>
#include <tqeventloop.h>

#include <kiconloader.h>
#include <tdeapplication.h>
#include <tdecmdlineargs.h>
#include <tdeaboutdata.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>

#undef signals
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#include <cstdlib>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

#ifdef USE_FREEBSD
#include <kvm.h>
#include <paths.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/file.h>
#endif

#ifdef USE_SOLARIS
#include <procfs.h>
#endif

#include <gtk/gtkgc.h>

#include "qt_qt_wrapper.h"
#include "qt_style.h"

#define RC_CACHE_VERSION TQString("1")

bool gtkQtEnable = false;
bool tqAppOwner = false;
gboolean tde_showIconsOnPushButtons = false;

TQStringList appDirList;
typedef TQMap<TQString, TQString> IconMap;
IconMap iconMap[4];
extern int errno;

TQScrollBar* scrollBar = 0;
TQWidget* meepWidget = 0;
TQWidget* meepWidgetP = 0;
TQSlider* meepSlider = 0;
TQTabBar* meepTabBar = 0;
GdkGC* altBackGC = 0;
TQWidget* smw = 0;

GtkRcStyle* gtkRcStyle = 0;

TQStringList kdeSearchPaths;
TQString iconTheme;
TQStringList iconThemeDirs;
TQColor alternateBackgroundColour;
int showIconsOnButtons;
int toolbarStyle;

const TQPixmap* backgroundTile;
GdkPixmap* backgroundTileGdk;
TQPixmap* menuBackgroundPixmap;
GdkPixmap* menuBackgroundPixmapGdk;

TQPixmap* fillPixmap;

int scrollBarSpacingLeft = 0;
int scrollBarSpacingRight = 0;

int isBaghira;
int isKeramik;
int isAlloy;
int isDomino;
int isPolyester;
int eclipseFix;
int openOfficeFix;
int mozillaFix;
int gtkQtDebug;

Atom kipcCommAtom;
Atom desktopWindowAtom;

void setFillPixmap(GdkPixbuf* buf)
{
	if (!gtkQtEnable)
		return;
	
	// This code isn't very robust.  It doesn't handle depths other than 24 bits.
	// It sure is fast though!
	int depth = gdk_pixbuf_get_n_channels(buf) * gdk_pixbuf_get_bits_per_sample(buf);
	int width = gdk_pixbuf_get_width(buf);
	int height = gdk_pixbuf_get_height(buf);
	int excess = gdk_pixbuf_get_rowstride(buf) - (width*3);
	
	if (depth != 24)
		return;
	
	TQImage fillImage(width, height, 32);
	
	uchar* source = gdk_pixbuf_get_pixels(buf);
	uchar* dest = fillImage.bits();
	
	for (int y=0 ; y<height ; y++)
	{
		for (int x=0 ; x<width ; x++)
		{
			// TODO: Make good on other endiannesses
			dest[0] = source[2];
			dest[1] = source[1];
			dest[2] = source[0];
			dest[3] = '\0';
		
			dest += 4;
			source += 3;
		}
		source += excess;
	}
	
	if (fillPixmap)
		delete fillPixmap;
	fillPixmap = 0;
	fillPixmap = new TQPixmap();
	fillPixmap->convertFromImage(fillImage);
	return;
}

		
/* Now to get rid of a ton of un-needed new's across the board.  `new' and `delete' are 
 * non-trivial operations.  You normally just don't notice it; until you're painting a window
 * with 50 widgets, with each paint operation requiring 3-4 news and 3-4 delete's.  The cost
 * of indirection is `not insubstantial'. */

 
static int dummy_x_errhandler( Display *dpy, XErrorEvent *err )
{
	return 0;
}
static int dummy_xio_errhandler( Display * )
{
	return 0;
}

void createTQApp()
{
	int argc = 1;
	char** argv;
	// Supply it with fake data to keep TDEApplication happy
	argv = (char**) malloc(sizeof(char*));
	argv[0] = (char*) malloc(sizeof(char) * 19);
	strncpy(argv[0], "gtk-qt-application", 19);

	TDEAboutData aboutData("gtk-qt-engine", I18N_NOOP("gtk-qt-engine"), "v0.1",
		"GTK Qt theme engine", TDEAboutData::License_GPL,
		"(c) 2011, Timothy Pearson",
		"message goes here", 0 /* TODO: Website */, "[email protected]");
	TDECmdLineArgs::init(argc, argv, &aboutData);
	
	TDEApplication::disableAutoDcopRegistration();
	
	TQString cmdLine;
	
#ifdef USE_FREEBSD
/* 
   procfs(5) is deprecated on FreeBSD.
   We use the kvm(3) library to get argv[0] of the current pid.
*/
	int cnt = 0;
	int ret = 0;
	kvm_t *kd;
	struct kinfo_proc *pbase;
	char ** arg;
	const char *msg = "";

	kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
	if (kd == NULL ) 
	{
		msg = "kvm_open failed\n";
		ret = -1;
	} 
	else
	{
		pbase = kvm_getprocs(kd, KERN_PROC_PID, getpid(), &cnt);
		if (( pbase == NULL ) || ( cnt != 1 )) 
		{ 
			msg = "kvm_getprocs failed\n";
			ret = -1;
		}
		else
		{
			arg = kvm_getargv(kd, pbase, 1024);
			if (arg == NULL) 
			{
				msg = "kvm_getargv failed\n";
				ret = -1;
			}       
			else
			{
				cmdLine += arg[0];
			}
		}
		kvm_close(kd);
	}
	if (ret == -1) 
	{
		printf("Gtk-Qt theme engine warning:\n");
		printf(msg);
		printf("  This may cause problems for the GNOME window manager\n");
	}       
#endif // USE_FREEBSD
	
#ifdef USE_SOLARIS
	int pid=getpid();
	char filen[256];
	psinfo_t pfi;
	uintptr_t addr;
	uintptr_t addr2;
	int  i,count,readl, ret=0;
	const char *msg;
	
	sprintf(filen, "/proc/%d/psinfo",pid);
	int fd=open(filen, O_RDONLY);
	if (fd == -1)
	{
		msg = "Open of psinfo failed\n";
		ret = -1;
	}
	else
	{
		readl=read(fd, (void *)&pfi, sizeof(psinfo_t));
		if (readl < 0)
		{
			msg = "Read on as failed\n";
			close(fd);
			ret = -1;
		}
		else
		{
			addr=pfi.pr_argv;
			count=pfi.pr_argc;
		}
		close(fd);
	}
	/* if read of psinfo was success */
	if (!ret)
	{
		sprintf(filen, "/proc/%d/as",pid);
		fd=open(filen, O_RDONLY);
		if (fd == -1)
		{
			msg = "Open of as failed\n";
			ret = -1;
		}
		else
		{
			for (i=0;i<count;i++)
			{
				lseek(fd, addr, SEEK_SET);
				if (pfi.pr_dmodel == PR_MODEL_ILP32)
				{
					addr=addr+4;
					readl=read(fd, (void *)&addr2, 4);
				}
				else
				{
					addr=addr+8;
					readl=read(fd, (void *)&addr2,8);
				}
				if (readl < 0)
				{
					msg = "Read on as failed\n";
					close(fd);
					ret = -1;
					break;
				}
				if (addr2 != 0)
				{
					cmdLine += (char *)addr2;
					cmdLine += " ";
				}
				else
					break;
			}
			close(fd);
		}
	}

	if (ret == -1)
	{
		printf("Gtk-Qt theme engine warning:\n");
		printf(msg);
		printf("  This may cause problems for the GNOME window manager\n");
	}
#else // USE_SOLARIS
#ifndef USE_FREEBSD
	TQCString cmdlinePath;
	cmdlinePath.sprintf("/proc/%d/cmdline", getpid());
	int fd = open(cmdlinePath, O_RDONLY);
	if (fd == -1)
	{
		printf("Gtk-Qt theme engine warning:\n");
		printf("  Could not open %s\n", (const char*)cmdlinePath);
		printf("  This may cause problems for the GNOME window manager\n");
	}
	else
	{
		while (1)
		{
			char data[80];
			int len = read(fd, data, 80);
			if (len == 0)
				break;
			cmdLine += data;
		}
		close(fd);
	}
#endif // USE_FREEBSD
#endif // USE_SOLARIS

	mozillaFix = (cmdLine.contains("mozilla") || cmdLine.contains("firefox") || cmdLine.contains("thunderbird"));

	openOfficeFix = (cmdLine.endsWith("soffice.bin"))
	              | (cmdLine.endsWith("swriter.bin"))
	              | (cmdLine.endsWith("scalc.bin"))
	              | (cmdLine.endsWith("sdraw.bin"))
	              | (cmdLine.endsWith("spadmin.bin"))
	              | (cmdLine.endsWith("simpress.bin"));

	eclipseFix = cmdLine.contains("eclipse");

	gtkQtDebug = (getenv("GTK_TQT_ENGINE_DEBUG") != NULL) ? 1 : 0;

	if (gtkQtDebug)
		printf("createTQApp()\n");

	char* sessionEnv = getenv("SESSION_MANAGER");
	if (TQString(sessionEnv).endsWith(TQString::number(getpid())) || cmdLine.contains("nspluginviewer") || cmdLine.contains("gnome-wm") || cmdLine.contains("metacity") || cmdLine.contains("xfwm4") || (getenv("GTK_TQT_ENGINE_DISABLE") != NULL))
	{
		printf("Not initializing the Gtk-Qt theme engine\n");
	}
	else
	{
		int (*original_x_errhandler)( Display *dpy, XErrorEvent * );
		int (*original_xio_errhandler)( Display *dpy );
		original_x_errhandler = XSetErrorHandler( dummy_x_errhandler );
		original_xio_errhandler = XSetIOErrorHandler( dummy_xio_errhandler );

#ifndef USE_SOLARIS
		unsetenv("SESSION_MANAGER");
#else
		putenv("SESSION_MANAGER=");
#endif

		if (!tqApp)
		{
			new TDEApplication(gdk_x11_get_default_xdisplay(), true, 0, 0, true);
			tqAppOwner = true;
		}

		initKdeSettings();

#ifndef USE_SOLARIS
		setenv("SESSION_MANAGER", sessionEnv, 1);
#else
		char *tempEnv=(char *)malloc(strlen(sessionEnv)+strlen("SESSION_MANAGER")+2);
		sprintf(tempEnv, "SESSION_MANAGER=%s", sessionEnv);
		putenv(tempEnv);
#endif
		
		XSetErrorHandler( original_x_errhandler );
		XSetIOErrorHandler( original_xio_errhandler );
		
		gtkQtEnable = true;
	}
	
	free(argv[0]);
	free(argv);

	// Propagate glib events to GTK as needed
	if (tqApp) {
		TQEventLoop* loop = tqApp->eventLoop();
		if (loop) {
			loop->setSingleToolkitEventHandling(false);
		}
	}
	
	if (!gtkQtEnable)
		return;
	
	isBaghira   = (TQString(tqApp->style().name()).lower() == "baghira");
	isKeramik   = (TQString(tqApp->style().name()).lower() == "keramik");
	isAlloy     = (TQString(tqApp->style().name()).lower() == "alloy");
	isDomino    = (TQString(tqApp->style().name()).lower() == "domino");
	isPolyester = (TQString(tqApp->style().name()).lower() == "polyester");
	
	if (isDomino)
	{
		TQScrollBar sbar(NULL);
		sbar.setOrientation(Qt::Horizontal);
		sbar.setValue(1);
		sbar.resize(200,25);
		
		TQRect rect = tqApp->style().querySubControlMetrics(TQStyle::CC_ScrollBar, &sbar, TQStyle::SC_ScrollBarGroove);
		scrollBarSpacingLeft = rect.x();
		scrollBarSpacingRight = 200 - rect.x() - rect.width();
	}
	
	// Set Gtk fonts and icons
	/*setGnomeFonts();
	setGnomeIcons();*/
	
	if (!cmdLine.contains("xfce-mcs-manager"))
	{
		// Get KDE related atoms from the X server
		kipcCommAtom = XInternAtom ( gdk_x11_get_default_xdisplay() , "KIPC_COMM_ATOM" , false );
		desktopWindowAtom = XInternAtom ( gdk_x11_get_default_xdisplay() , "KDE_DESKTOP_WINDOW" , false );
		
		// Create a new window, and set the KDE_DESKTOP_WINDOW property on it
		// This window will then receive events from KDE when the style changes
		smw = new TQWidget(0,0);
		long data = 1;
		XChangeProperty(gdk_x11_get_default_xdisplay(), smw->winId(),
			desktopWindowAtom, desktopWindowAtom,
			32, PropModeReplace, (unsigned char *)&data, 1);
		
		// This filter will intercept those events
		gdk_window_add_filter( NULL, gdkEventFilter, 0);
	}
	
	meepWidgetP = new TQWidget(0);
	meepWidget = new TQWidget(meepWidgetP);
	meepSlider = new TQSlider(meepWidget);
	meepWidget->polish();
	
	meepTabBar = new TQTabBar(meepWidget);
	
	menuBackgroundPixmap = NULL;
	backgroundTile = meepWidget->paletteBackgroundPixmap();
	if (backgroundTile != NULL)
		backgroundTileGdk = gdk_pixmap_foreign_new(backgroundTile->handle());
}

void destroyTQApp()
{
	if (!gtkQtEnable)
		return;
	delete meepWidget;
	delete meepWidgetP;
	delete menuBackgroundPixmap;
	delete smw;
	if (tqAppOwner)
	{
		delete tqApp;
		tqApp = 0;
	}
	if (altBackGC != 0)
		gtk_gc_release(altBackGC);
}

GdkFilterReturn gdkEventFilter(GdkXEvent *xevent, GdkEvent *gevent, gpointer data)
{
	XEvent* event = (XEvent*) xevent;

	// Is the event a KIPC message?
	if ((event->type == ClientMessage) && (event->xclient.message_type == kipcCommAtom))
	{
		// This data variable contains the type of KIPC message
		// As defined in tdelibs/tdecore/kipc.h, 2 = StyleChanged
		if (event->xclient.data.l[0] != 2)
			return GDK_FILTER_REMOVE;
		
		if (gtkQtDebug)
			printf("StyleChanged IPC message\n");
		
		// Find out the new widget style
		TQString styleName = kdeConfigValue("General", "widgetStyle", "");
		TQStyle* style = TQStyleFactory::create(styleName);
		if (!style)
			return GDK_FILTER_REMOVE;
		
		// Tell the TQApplication about this new style
		tqApp->setStyle(style);
		
		// Now we need to update GTK's properties
		setRcProperties(gtkRcStyle, 1); // Rewrite our cache file
		gtk_rc_reparse_all(); // Tell GTK to parse the cache file
		
		return GDK_FILTER_REMOVE;
	}
	return GDK_FILTER_CONTINUE;
}

TQString kdeConfigValue(const TQString& section, const TQString& name, const TQString& def)
{
	TDEConfig currentConfig;
	currentConfig.setGroup(section);
	return currentConfig.readEntry(name, def);
}

bool kdeBoolConfigValue(const TQString& section, const TQString& name, bool def)
{
	TDEConfig currentConfig;
	currentConfig.setGroup(section);
	return currentConfig.readBoolEntry(name, def);
}

TQString kdeFindDir(const TQString& suffix, const TQString& file1, const TQString& file2)
{
	for ( TQStringList::Iterator it = kdeSearchPaths.begin(); it != kdeSearchPaths.end(); ++it )
	{
		if ((TQFile::exists((*it) + suffix + file1)) || (TQFile::exists((*it) + suffix + file2)))
			return (*it) + suffix;
	}
	return TQString();
}

TQString runCommand(const TQString& command)
{
	FILE* p = popen(command.latin1(), "r");
	if ((p == NULL) || (p < 0))
		return TQString();
	
	TQString ret;
	while (!feof(p))
	{
		char buffer[256];
		int n = fread(buffer, 1, 255, p);
		buffer[n] = '\0';
		ret += buffer;
	}
	pclose(p);
	
	return ret.stripWhiteSpace();
}

void initKdeSettings()
{
	kdeSearchPaths.clear();
	
	TQString kdeHome = getenv("TDEHOME");
	TQString kdeDirs = getenv("TDEDIRS");
	TQString kdeDir = getenv("TDEDIR");
	
	if (!kdeHome.isEmpty())
		kdeSearchPaths.append(kdeHome);
	kdeSearchPaths.append(runCommand("tde-config --localprefix"));
	
	if (!kdeDirs.isEmpty())
		kdeSearchPaths += TQStringList::split(':', kdeDirs);
	if (!kdeDir.isEmpty())
		kdeSearchPaths.append(kdeDir);
	kdeSearchPaths.append(runCommand("tde-config --prefix"));
	
	iconTheme = kdeConfigValue("Icons", "Theme", "crystalsvg");
	tde_showIconsOnPushButtons = kdeBoolConfigValue("KDE", "ShowIconsOnPushButtons", false);

	TQStringList back = TQStringList::split(',', kdeConfigValue("General", "alternateBackground", "238,246,255"));
	alternateBackgroundColour.setRgb(back[0].toInt(), back[1].toInt(), back[2].toInt());

	showIconsOnButtons = (kdeConfigValue("KDE", "ShowIconsOnPushButtons", "true").lower() == "true");
	
	
	TQString tmp = kdeConfigValue("Toolbar style", "IconText", "true").lower();
	if (tmp == "icononly")
		toolbarStyle = 0;
	else if (tmp == "icontextright")
		toolbarStyle = 3;
	else if (tmp == "textonly")
		toolbarStyle = 1;
	else if (tmp == "icontextbottom")
		toolbarStyle = 2;
	else // Should never happen, but just in case we fallback to KDE's default "icononly"
		toolbarStyle = 0;
}

TQStyle::SFlags stateToSFlags(GtkStateType state)
{
	switch (state)
	{
		case GTK_STATE_ACTIVE:
			return TQStyle::Style_Enabled | TQStyle::Style_Down;
		case GTK_STATE_PRELIGHT:
			return TQStyle::Style_Enabled | TQStyle::Style_MouseOver | TQStyle::Style_Raised;
		case GTK_STATE_SELECTED:
			return TQStyle::Style_Enabled | TQStyle::Style_HasFocus | TQStyle::Style_Raised;
		case GTK_STATE_INSENSITIVE:
			return TQStyle::Style_Default | TQStyle::Style_Raised;
		default:
			return TQStyle::Style_Enabled | TQStyle::Style_Raised;
	}
}

TQColor gdkColorToTQColor(GdkColor* c)
{
	return TQColor(c->red / 256, c->green / 256, c->blue / 256);
}


// The drawing functions follow the same pattern:
//  * Set the appropriate flags
//  * Ask QT to paint the widget to a pixmap
//  * Create a GdkPixmap that points to our TQPixmap
//  * Paint the pixmap on the window


void drawButton(GdkWindow* window, GtkStyle* style, GtkStateType state, int defaultButton, int x, int y, int w, int h, GtkButton* gwidget)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

#ifdef USE_NATIVE_GTK_BUTTON_DRAWING
	gwidget = NULL;
#endif

	if (gtkQtDebug) {
		GtkWidget* parent;
		parent = gtk_widget_get_parent(GTK_WIDGET(gwidget));
		printf("drawButton Parent 1: %s\n",gtk_widget_get_name(parent));
		parent = gtk_widget_get_parent(GTK_WIDGET(parent));
		printf("drawButton Parent 2: %s\n",gtk_widget_get_name(parent));
		parent = gtk_widget_get_parent(GTK_WIDGET(parent));
		printf("drawButton Parent 3: %s\n",gtk_widget_get_name(parent));
		parent = gtk_widget_get_parent(GTK_WIDGET(parent));
		printf("drawButton Parent 4: %s\n",gtk_widget_get_name(parent));
	}

	if (gwidget) {
		TQString     gwLabel(gtk_button_get_label(gwidget));
		if (gtk_button_get_use_stock(gwidget)) {
			GtkStockItem stockData;
			gtk_stock_lookup(gwLabel.ascii(), &stockData);
			gwLabel = TQString(stockData.label);
		}
		gwLabel.replace("&", "&&");
		gwLabel.replace("_", "&");

		TQPixmap     buttonicon;
		TQBitmap     buttonicon_mask;

		GtkWidget* giconwidget = gtk_button_get_image(gwidget);
		if (giconwidget) {
			GtkIconSize giconSize;
			gint iconWidth;
			gint iconHeight;

			GtkImageType giconStorageType = gtk_image_get_storage_type(GTK_IMAGE(giconwidget));
			if (giconStorageType == GTK_IMAGE_STOCK) {
				gchar *stock_id;
				gtk_image_get_stock(GTK_IMAGE(giconwidget), &stock_id, &giconSize);
				GdkPixbuf* pixbuf = gtk_widget_render_icon(GTK_WIDGET(gwidget), stock_id, giconSize, NULL);
				iconWidth=gdk_pixbuf_get_width(pixbuf);
				iconHeight=gdk_pixbuf_get_height(pixbuf);
				buttonicon = TQPixmap(iconWidth, iconHeight);
				buttonicon_mask = TQBitmap(iconWidth, iconHeight);
				buttonicon.fill(tqApp->palette().active().background());
				buttonicon_mask.fill(TQt::color0);
				GdkPixmap* pix = gdk_pixmap_foreign_new(buttonicon.handle());
				GdkBitmap* bmp = gdk_pixmap_foreign_new(buttonicon_mask.handle());
				gdk_drawable_set_colormap(pix, gdk_drawable_get_colormap(window));
				gdk_drawable_set_colormap(bmp, gdk_drawable_get_colormap(window));
				gdk_draw_pixbuf(pix, NULL, pixbuf, 0, 0, 0, 0, -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
				gdk_pixbuf_render_threshold_alpha(pixbuf, bmp, 0, 0, 0, 0, -1, -1, 128);
				buttonicon.setMask(buttonicon_mask);
				g_object_unref(pixbuf);
				g_object_unref(bmp);
				g_object_unref(pix);
			}
			else {
				// FIXME
				// Implement pixmap loading here for non-stock icons
			}
		}

		TQIconSet    buttonIconSet(buttonicon);
		TQPixmap     pixmap(w, h);
		TQPainter    painter(&pixmap);
		TQPushButton button(meepWidget);	
		button.setBackgroundOrigin(TQWidget::ParentOrigin);
		button.setGeometry(x, y, w, h);
		if (style->rc_style->bg[GTK_STATE_NORMAL].pixel != 0)
			button.setPaletteBackgroundColor(gdkColorToTQColor(&style->rc_style->bg[GTK_STATE_NORMAL]));
		TQPoint p = button.backgroundOffset();
		TQPoint pos = button.pos();
	
		TQStyle::SFlags sflags = stateToSFlags(state);
		
		if (defaultButton)
			sflags |= TQStyle::Style_ButtonDefault;
		button.setDefault(defaultButton);
	
		painter.fillRect(0, 0, w, h, tqApp->palette().active().background());

		button.setText(gwLabel);
		if (tde_showIconsOnPushButtons)
			button.setIconSet(buttonIconSet);
		button.setDown(sflags&TQStyle::Style_Down);
		button.setOn(sflags&TQStyle::Style_On);

		// This emulates ::drawButton() in the Qt qbutton.cpp file
		tqApp->style().drawControl(TQStyle::CE_PushButton, &painter, &button, 
		                          TQRect(0,0,w,h), button.palette().active(), sflags);
		tqApp->style().drawControl(TQStyle::CE_PushButtonLabel, &painter, &button, 
					tqApp->style().subRect(TQStyle::SR_PushButtonContents, &button), 
					button.colorGroup(), sflags);
	
		GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
		gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
		g_object_unref(pix);
	}
	else {
		TQPixmap     pixmap(w, h);
		TQPainter    painter(&pixmap);
		TQPushButton button(meepWidget);	
		button.setBackgroundOrigin(TQWidget::ParentOrigin);
		button.setGeometry(x, y, w, h);
		if (style->rc_style->bg[GTK_STATE_NORMAL].pixel != 0)
			button.setPaletteBackgroundColor(gdkColorToTQColor(&style->rc_style->bg[GTK_STATE_NORMAL]));
		TQPoint p = button.backgroundOffset();
		TQPoint pos = button.pos();
	
		TQStyle::SFlags sflags = stateToSFlags(state);
		
		if (defaultButton)
			sflags |= TQStyle::Style_ButtonDefault;
		button.setDefault(defaultButton);
	
		painter.fillRect(0, 0, w, h, tqApp->palette().active().background());
	
		tqApp->style().drawControl(TQStyle::CE_PushButton, &painter, &button, 
		                          TQRect(0,0,w,h), button.palette().active(), sflags);
	
		GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
		gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
		g_object_unref(pix);
	}
}

// Thanks Peter Hartshorn <[email protected]>
void drawToolbar(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	int w1, h1;
	TQStyle::SFlags sflags = stateToSFlags(state) | TQStyle::Style_Raised;


	// Keramik hack...
	// Keramik only draws the toolbar border, and not the gradient
	// so we also draw a separator, but make sure the line is off the
	// widget
	
	if (w > h)
	{
		sflags |= TQStyle::Style_Horizontal;
		w1 = w * 3;
		h1 = h;
	}
	else
	{
		w1 = h;
		h1 = h * 3;
	}

	if ((w1 < 1) || (h1 < 1) ||
	    (w < 1) || (h < 1))
		return;

	TQPixmap     pixmap(w1, h1);
	TQPixmap     p(w, h);
	TQPainter    painter(&pixmap);

        if ((backgroundTile) && (!backgroundTile->isNull()))
                painter.fillRect(0, 0, w1, h1, TQBrush(TQColor(255,255,255), *backgroundTile));
        else
                painter.fillRect(0, 0, w1, h1, tqApp->palette().active().brush(TQColorGroup::Background));
	
	tqApp->style().tqdrawPrimitive(TQStyle::PE_PanelDockWindow, &painter,
			TQRect(0,0,w1,h1), tqApp->palette().active(),sflags);

	if (isKeramik)
	{
		tqApp->style().tqdrawPrimitive(TQStyle::PE_DockWindowSeparator, &painter,
				TQRect(0,0,w1,h1), tqApp->palette().active(),sflags);
	}

	bitBlt(&p, 0, 0, &pixmap, 0, 0, w, h);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawMenubar(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	int w1, h1;
	TQStyle::SFlags sflags = stateToSFlags(state);


	// Keramik hack...
	// Keramik only draws the toolbar border, and not the gradient
	// so we also draw a separator, but make sure the line is off the
	// widget
	
	if (w > h)
	{
		sflags |= TQStyle::Style_Horizontal;
		w1 = w * 3;
		h1 = h;
	}
	else
	{
		w1 = h;
		h1 = h * 3;
	}

	if ((w1 < 1) || (h1 < 1) ||
	    (w < 1) || (h < 1))
		return;

	TQPixmap     pixmap(w1, h1);
	TQPixmap     p(w, h);
	TQPainter    painter(&pixmap);

        if ((backgroundTile) && (!backgroundTile->isNull()))
                painter.fillRect(0, 0, w1, h1, TQBrush(TQColor(255,255,255), *backgroundTile));
        else
                painter.fillRect(0, 0, w1, h1, tqApp->palette().active().brush(TQColorGroup::Background));

	tqApp->style().tqdrawPrimitive(TQStyle::PE_PanelMenuBar, &painter,
			TQRect(0,0,w1,h1), tqApp->palette().active(),sflags);

	bitBlt(&p, 0, 0, &pixmap, 0, 0, w, h);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawTab(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);
	
	// GTK doesn't tell us if our tab is on the left, right, or middle of the tabbar
	// So, let's always assume it's in the middle - it looks pretty
	TQTab* tab = new TQTab;
	meepTabBar->insertTab(tab,1);

	TQStyle::SFlags sflags = stateToSFlags(state);

	if (state != GTK_STATE_ACTIVE)
		sflags = TQStyle::Style_Selected;

	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().drawControl(TQStyle::CE_TabBarTab, &painter, meepTabBar, TQRect(0,0,w,h), tqApp->palette().active(), sflags, TQStyleOption(tab));
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
	
	meepTabBar->removeTab(tab);
}

void drawVLine(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int ySource, int yDest)
{
	if (!gtkQtEnable)
		return;
	
	int width = style->xthickness;
	int height = abs(ySource-yDest);
	
	if (width < 2) width = 2;

	if ((width < 1) || (height < 1))
		return;

	TQPixmap pixmap(width, height);
	TQPainter painter(&pixmap);

	painter.fillRect(2, 0, width - 2, height, tqApp->palette().active().brush(TQColorGroup::Background));
	painter.setPen( tqApp->palette().active().mid() );
	painter.drawLine( 0, 0, 0, height );
	painter.setPen( tqApp->palette().active().light() );
	painter.drawLine( 1, 0, 1, height );

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, ySource, width, height);
	g_object_unref(pix);
}

void drawHLine(GdkWindow* window, GtkStyle* style, GtkStateType state, int y, int xSource, int xDest)
{
	if (!gtkQtEnable)
		return;
	
	int width = abs(xSource-xDest);
	int height = style->ythickness;

	if ((width < 1) || (height < 1))
		return;

	TQPixmap pixmap(width, height);
	TQPainter painter(&pixmap);

	painter.fillRect(0, 2, width, height-2, tqApp->palette().active().brush(TQColorGroup::Background));
	painter.setPen(tqApp->palette().active().mid() );
	painter.drawLine(0, 0, width, 0);
	painter.setPen(tqApp->palette().active().light());
	painter.drawLine(0, 1, width, 1);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, xSource, y, width, height);
	g_object_unref(pix);
}

void drawLineEdit(GdkWindow* window, GtkStyle* style, GtkStateType state, int hasFocus, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w, h);
	TQPainter painter(&pixmap);

	TQStyle::SFlags sflags = stateToSFlags(state);
	if (hasFocus)
		sflags |= TQStyle::Style_HasFocus;
	
	painter.fillRect(0, 0, w, h, tqApp->palette().active().base());
	tqApp->style().tqdrawPrimitive(TQStyle::PE_PanelLineEdit, &painter, TQRect(0, 0, w, h), tqApp->palette().active(), sflags, TQStyleOption(1,1));

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawFrame(GdkWindow* window, GtkStyle* style, GtkStateType state, GtkShadowType shadow_type, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1)) // Caused crash in gaim file transfers window
		return;

	TQPixmap pixmap(w, h);
	TQPainter painter(&pixmap);

	TQStyle::SFlags sflags = stateToSFlags(state);
	if ((shadow_type == GTK_SHADOW_IN) || (shadow_type == GTK_SHADOW_ETCHED_IN))
		sflags |= TQStyle::Style_Sunken;

	if ((backgroundTile) && (!backgroundTile->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
		painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));

	tqApp->style().tqdrawPrimitive(TQStyle::PE_Panel, &painter, TQRect(0, 0, w, h), tqApp->palette().active(), sflags, TQStyleOption(2,2) );
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawComboBox(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);
	TQComboBox cb(false, 0);
	cb.resize(w,h);
	
	TQStyle::SFlags sflags = stateToSFlags(state);
	TQStyle::SCFlags scflags = TQStyle::SC_ComboBoxArrow | TQStyle::SC_ComboBoxFrame | TQStyle::SC_ComboBoxListBoxPopup;
	TQStyle::SCFlags activeflags = TQStyle::SC_None;

	if (state == GTK_STATE_PRELIGHT)
		activeflags = TQStyle::Style_MouseOver;

	painter.fillRect(0,0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().drawComplexControl(TQStyle::CC_ComboBox, &painter, &cb, TQRect(0, 0, w, h), tqApp->palette().active(), sflags, scflags, activeflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawCheckBox(GdkWindow* window, GtkStyle* style, GtkStateType state, int checked, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	int realH = tqApp->style().pixelMetric(TQStyle::PM_IndicatorHeight);
	int realW = tqApp->style().pixelMetric(TQStyle::PM_IndicatorWidth);

	if ((realW < 1) || (realH < 1))
		return;

	TQPixmap pixmap(realW, realH);
	TQPainter painter(&pixmap);
	TQCheckBox checkbox(0);

	TQStyle::SFlags sflags = stateToSFlags(state);
	sflags |= (checked ? TQStyle::Style_On : TQStyle::Style_Off);

	painter.fillRect(0, 0, realW, realH, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().drawControl(TQStyle::CE_CheckBox, &painter, &checkbox, TQRect(0, 0, realW, realH), tqApp->palette().active(), sflags);

	// Qt checkboxes are usually bigger than GTK wants.
	// We cheat, and draw them over the expected area.
	int xOffset = (realW - w) / 2;
	int yOffset = (realH - h) / 2;

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x - xOffset, y - yOffset, realW, realH);
	g_object_unref(pix);
}

void drawMenuCheck(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	TQCheckBox checkbox(0);

	/* A previous version of the function followed the sizehints exclusively
	   Now follow w and h provided by GTK, but if the checkmark is too big we might have to scale it */
	/*
	int w1 = checkbox.sizeHint().width();
	int h1 = checkbox.sizeHint().height(); */

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);

	TQStyle::SFlags sflags = stateToSFlags(state);
	sflags |= TQStyle::Style_On;

	if (fillPixmap && (!fillPixmap->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *fillPixmap));
	else if ((backgroundTile) && (!backgroundTile->isNull()))
                painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *backgroundTile));
        else
	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().tqdrawPrimitive(TQStyle::PE_CheckMark, &painter, TQRect(0, 0, w, h), tqApp->palette().active(), sflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawRadioButton(GdkWindow* window, GtkStyle* style, GtkStateType state, int checked, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	int realH = tqApp->style().pixelMetric(TQStyle::PM_IndicatorHeight);
	int realW = tqApp->style().pixelMetric(TQStyle::PM_IndicatorWidth);

	if ((realW < 1) || (realH < 1))
		return;

	TQPixmap pixmap(realH, realW);
	TQPainter painter(&pixmap);
	TQRadioButton radio(0);

	TQStyle::SFlags sflags = stateToSFlags(state);
	sflags |= checked ? TQStyle::Style_On : TQStyle::Style_Off;

	if (fillPixmap && (!fillPixmap->isNull()))
		painter.fillRect(0, 0, realW, realH, TQBrush(TQColor(255,255,255), *fillPixmap));
	else if ((backgroundTile) && (!backgroundTile->isNull()))
                painter.fillRect(0, 0, realW, realH, TQBrush(TQColor(255,255,255), *backgroundTile));
        else
                painter.fillRect(0, 0, realW, realH, tqApp->palette().active().brush(TQColorGroup::Background));

	tqApp->style().drawControl(TQStyle::CE_RadioButton, &painter, &radio, TQRect(0,0,realH,realW), tqApp->palette().active(), sflags);

	// Qt checkboxes are usually bigger than GTK wants.
	// We cheat, and draw them over the expected area.
	int xOffset = (realW - w) / 2;
	int yOffset = (realH - h) / 2;
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x - xOffset, y - yOffset, realW, realH);
	g_object_unref(pix);
}


void drawScrollBarSlider(GdkWindow* window, GtkStyle* style, GtkStateType state, int orientation, GtkAdjustment* adj, int x, int y, int w, int h, int offset, int totalExtent)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

	int wCorrected = w;
	int hCorrected = h;
	if (isDomino)
	{
		if (orientation == GTK_ORIENTATION_HORIZONTAL)
		    wCorrected = w + 14;
		else
		    hCorrected = h + 14;
	}
	TQPixmap pixmap(wCorrected, hCorrected);
	TQPainter painter(&pixmap);
	
	TQStyle::SFlags sflags = stateToSFlags(state);
	if (orientation == GTK_ORIENTATION_HORIZONTAL)
		sflags |= TQStyle::Style_Horizontal;
	
	tqApp->style().tqdrawPrimitive(TQStyle::PE_ScrollBarSlider, &painter, TQRect(0,0,wCorrected,hCorrected), tqApp->palette().active(), sflags);
	
	// The domino style doesn't draw the entire slider in PE_ScrollBarSlider
	// We have to draw PE_ScrollBarAddPage and PE_ScrollBarSubPage and piece the bits together
	if (isDomino && !mozillaFix && !eclipseFix)
	{
		TQPixmap leftPix, rightPix;
		TQRect leftRect, rightRect;
		if (orientation == GTK_ORIENTATION_HORIZONTAL)
		{
			leftRect = TQRect(0, 0, offset-scrollBarSpacingLeft, h);
			rightRect = TQRect(6, 0, totalExtent-offset-w-scrollBarSpacingRight+2, h);
			leftPix.resize(6 + leftRect.width(), h);
			rightPix.resize(6 + rightRect.width(), h);
		}
		else
		{
			leftRect = TQRect(0, 0, w, offset-scrollBarSpacingLeft);
			rightRect = TQRect(0, 6, w, totalExtent-offset-h-scrollBarSpacingRight+2);
			leftPix.resize(w, 6 + leftRect.height());
			rightPix.resize(w, 6 + rightRect.height());
		}
		
		TQPainter dominoPainter(&leftPix);
		tqApp->style().tqdrawPrimitive(TQStyle::PE_ScrollBarSubPage, &dominoPainter, leftRect, tqApp->palette().active(), sflags);
		
		dominoPainter.end();
		dominoPainter.begin(&rightPix);
		tqApp->style().tqdrawPrimitive(TQStyle::PE_ScrollBarAddPage, &dominoPainter, rightRect, tqApp->palette().active(), sflags);
		
		if (orientation == GTK_ORIENTATION_HORIZONTAL)
		{
			bitBlt(TQT_TQPAINTDEVICE(&pixmap), 1, 0, TQT_TQPAINTDEVICE(&leftPix), leftRect.width(), 0, 6, h, Qt::CopyROP, true);
			bitBlt(TQT_TQPAINTDEVICE(&pixmap), w-7, 0, TQT_TQPAINTDEVICE(&rightPix), 0, 0, 7, h, Qt::CopyROP, true);
		}
		else
		{
			bitBlt(TQT_TQPAINTDEVICE(&pixmap), 0, 1, TQT_TQPAINTDEVICE(&leftPix), 0, leftRect.height(), w, 6, Qt::CopyROP, true);
			bitBlt(TQT_TQPAINTDEVICE(&pixmap), 0, h-7, TQT_TQPAINTDEVICE(&rightPix), 0, 0, w, 7, Qt::CopyROP, true);
		}
	}
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	
	if (isDomino)
	{
		int endsSkip = mozillaFix ? 7 : 1;
		if (orientation == GTK_ORIENTATION_HORIZONTAL)
			gdk_draw_drawable(window, style->bg_gc[state], pix, endsSkip, 0, x, y, w-1, h);
		else
			gdk_draw_drawable(window, style->bg_gc[state], pix, 0, endsSkip, x, y, w, h-1);
	}
	else
		gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawScrollBar(GdkWindow* window, GtkStyle* style, GtkStateType state, int orientation, GtkAdjustment* adj, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

	if (scrollBar != 0)
		delete scrollBar;
	scrollBar = new TQScrollBar(NULL);
	
	scrollBar->resize(w,h);

	// Patch from Chad Kitching <[email protected]>
	// Patch from Peter Hartshorn <[email protected]>

	// another check for mozilla is step_increment and page_increment
	// are set to zero for mozilla, and have values set by all other
	// gtk applications I've tested this with.
	
	// Why oh why couldn't mozilla use native widgets instead of
	// handling everything in cross platform.
	
	scrollBar->setOrientation(orientation ? Qt::Vertical : Qt::Horizontal);
	
	
	TQStyle::SFlags sflags = stateToSFlags(state);
	if (sflags |= TQStyle::Style_Down) sflags = TQStyle::Style_Enabled;
	if (orientation == GTK_ORIENTATION_HORIZONTAL) sflags |= TQStyle::Style_Horizontal;
	
	TQPixmap pixmap(w,h);
	
	scrollBar->setMinValue(0);
	scrollBar->setMaxValue(65535);
	scrollBar->setValue(32767);
	scrollBar->setPageStep(1);

	int offset = 0;
	int thumbSize = 0;


	if (orientation == GTK_ORIENTATION_VERTICAL) {
		TQRect r;
		r = tqApp->style().querySubControlMetrics(TQStyle::CC_ScrollBar,
				scrollBar, TQStyle::SC_ScrollBarSlider);
		offset = r.y();
		thumbSize = r.height();
                if (thumbSize < 0)
                  thumbSize = -thumbSize;
		
		if (!r.isValid()) // Fix a crash bug in Eclipse where it was trying to draw tiny scrollbars.
			return;

		TQPixmap tmpPixmap(w, h + thumbSize);
		TQPainter painter2(&tmpPixmap);
		scrollBar->resize(w, h + thumbSize);

		painter2.fillRect(0, 0, w, h + thumbSize,
				tqApp->palette().active().brush(TQColorGroup::Background));
		tqApp->style().drawComplexControl(TQStyle::CC_ScrollBar,
				&painter2, scrollBar, TQRect(0, 0, w, h+thumbSize),
				tqApp->palette().active(), sflags);

		bitBlt(&pixmap, 0, 0, &tmpPixmap, 0, 0, w, offset, Qt::CopyROP);
		bitBlt(&pixmap, 0, offset, &tmpPixmap, 0, offset + thumbSize,
				w, h - offset, Qt::CopyROP);
	} else {
		TQRect r;
		r = tqApp->style().querySubControlMetrics(TQStyle::CC_ScrollBar,
				scrollBar, TQStyle::SC_ScrollBarSlider);
		offset = r.x();
		thumbSize = r.width();
		if (thumbSize < 0)
		  thumbSize = -thumbSize;

		if (!r.isValid()) // Fix a crash bug in Eclipse when it was trying to draw tiny scrollbars.
			return;
		
		TQPixmap tmpPixmap(w + thumbSize, h);
		TQPainter painter2(&tmpPixmap);
		scrollBar->resize(w + thumbSize, h);

		painter2.fillRect(0, 0, w + thumbSize, h,
				tqApp->palette().active().brush(TQColorGroup::Background));
		tqApp->style().drawComplexControl(TQStyle::CC_ScrollBar,
				&painter2, scrollBar, TQRect(0, 0, w+thumbSize, h),
				tqApp->palette().active(), sflags);

		bitBlt(&pixmap, 0, 0, &tmpPixmap, 0, 0, offset, h, Qt::CopyROP);
		bitBlt(&pixmap, offset, 0, &tmpPixmap, offset + thumbSize, 0,
				w - offset, h, Qt::CopyROP);
	}
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawToolButton(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQToolButton button(NULL);
	button.resize(w, h);
	
	/* 
	int realW = button.sizeHint().width();
	int realH = button.sizeHint().height();	 */
	
	TQStyle::SFlags sflags = stateToSFlags(state);
	TQStyle::SCFlags activeflags = TQStyle::SC_None;
	if (state == GTK_STATE_ACTIVE)
	{
		sflags |= TQStyle::Style_AutoRaise;
		activeflags = TQStyle::SC_ToolButton;
	}
	else
		sflags |= TQStyle::Style_AutoRaise | TQStyle::Style_Raised;
	
	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);
	
	if ((backgroundTile) && (!backgroundTile->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
		painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().drawComplexControl(TQStyle::CC_ToolButton, &painter, &button, TQRect(0, 0, w, h), tqApp->palette().active(), sflags, TQStyle::SC_ToolButton, activeflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawMenuBarItem(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w, h);
	TQPainter painter(&pixmap);
	TQMenuItem mi;
	TQMenuBar mb(0);
	
	TQStyle::SFlags sflags = TQStyle::Style_Down | TQStyle::Style_Enabled | TQStyle::Style_Active | TQStyle::Style_HasFocus;

	tqApp->style().drawControl(TQStyle::CE_MenuBarItem, &painter, &mb, TQRect(0, 0, w, h), tqApp->palette().active(), sflags, TQStyleOption(&mi));
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawMenuItem(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);
	TQPopupMenu pm;
	TQMenuData md;
	TQMenuItem* mi = md.findItem(md.insertItem(""));

	TQStyleOption opt(mi, 16, 16);
	TQStyle::SFlags sflags = TQStyle::Style_Active | TQStyle::Style_Enabled;

	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().drawControl(TQStyle::CE_PopupMenuItem, &painter, &pm, TQRect(0,0,w,h), tqApp->palette().active(), sflags, opt);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawSplitter(GdkWindow* window, GtkStyle* style, GtkStateType state, int orientation, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);

	TQStyle::SFlags sflags = stateToSFlags(state);
	// No idea why this works...
	if (orientation != GTK_ORIENTATION_HORIZONTAL) sflags |= TQStyle::Style_Horizontal;

	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().tqdrawPrimitive(TQStyle::PE_Splitter, &painter, TQRect(0,0,w,h), tqApp->palette().active(), sflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawTabFrame(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h, GtkPositionType pos)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

	TQStyle::SFlags sflags = stateToSFlags(state);
		
	TQPixmap pixmap(w, h);
	TQPainter painter(&pixmap);
	TQStyleOption opt(2, 2); // line width

	if ((backgroundTile) && (!backgroundTile->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
		painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().tqdrawPrimitive(TQStyle::PE_PanelTabWidget, &painter, TQRect(0,0,w,h), tqApp->palette().active(), sflags, opt);
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
	
	// Drawing tab base
        int th = tqApp->style().pixelMetric(TQStyle::PM_TabBarBaseHeight, meepTabBar);
	int tw = w;
	
	if ((tw < 1) || (th < 1))
		return;

        TQPixmap pixmap1(tw,th);
        TQPainter painter1(&pixmap1);
	if ((backgroundTile) && (!backgroundTile->isNull()))
		painter1.fillRect(0, 0, tw, th, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
		painter1.fillRect(0, 0, tw, th, tqApp->palette().active().brush(TQColorGroup::Background));
        

        tqApp->style().tqdrawPrimitive(TQStyle::PE_TabBarBase, &painter1, TQRect(0, 0, tw, th), tqApp->palette().active(), sflags, TQStyleOption(1,1));
	if (pos == GTK_POS_BOTTOM)
	{
		TQWMatrix m;
		m.scale(1, -1);
		pixmap1 = pixmap1.xForm(m);
		
		GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap1.handle());
		gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y+h+tqApp->style().pixelMetric(TQStyle::PM_TabBarBaseOverlap, meepTabBar), tw, th);
		g_object_unref(pix);
	}
	else
	{
		GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap1.handle());
		gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y-tqApp->style().pixelMetric(TQStyle::PM_TabBarBaseOverlap, meepTabBar), tw, th);
		g_object_unref(pix);
	}
}

void drawMenu(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w, h);
	TQPainter painter(&pixmap);
	TQStyle::SFlags sflags = stateToSFlags(state);

	if ((backgroundTile) && (!backgroundTile->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
		painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().tqdrawPrimitive(TQStyle::PE_PanelPopup, &painter, TQRect(0,0,w,h), tqApp->palette().active(), sflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

// Note: In GTK, the drawing of a progress bar is in two parts:
// First the progress "container" is drawn
// Second the actually percent bar is drawn
// Mozilla requires this to be done in two steps as it first
// asks gtk to draw an EMPTY progress bar, then it asks to draw
// the contents, or percent bar, over the empty progress bar.
// So, although this function is not required for any gtk application
// except mozilla based apps, doing it this way is following the gtk
// theme structure more.
//
// See also drawScrollbar/drawScrollbarSlider pair.
//
// Peter Hartshorn ([email protected])

void drawProgressChunk(GdkWindow * window, GtkStyle * style, GtkStateType state, int x, int y, int w, int h)
{
	// This is only for Mozilla/Firefox
	if (!mozillaFix || !gtkQtEnable)
		return;
	
	if ((w<=1) || (h<=1))
		return; // Trying to draw something that small caused a segfault
	
	// Dirty hack: When using the Alloy style, tweak the position and size of progress bar "filling"
	int w2 = isAlloy ? w+4 : w;
	int h2 = isAlloy ? h+4 : h;

	TQProgressBar bar(100, NULL);
	bar.resize(w2, h2);
	bar.setProgress(100);
	bar.setCenterIndicator(false);
	bar.setPercentageVisible(false);
	bar.setFrameStyle(TQFrame::NoFrame);
	
	if ((w2 < 1) || (h2 < 1))
		return;

	TQPixmap pixmap(w2, h2);
	TQPainter painter(&pixmap);

	TQStyle::SFlags sflags = stateToSFlags(state);
	
	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));

	tqApp->style().drawControl(TQStyle::CE_ProgressBarContents, &painter, &bar, TQRect(0,0,w2,h2), tqApp->palette().active(), sflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	if (isAlloy)
		gdk_draw_drawable(window, style->bg_gc[state], pix, 4, 4, x+2, y+2, w-3, h-3);
	else
		gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawProgressBar(GdkWindow * window, GtkStyle * style, GtkStateType state, GtkProgressBarOrientation orientation, gfloat percentage, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	if ((w<=1) || (h<=1))
		return; // Trying to draw something that small caused a segdault
	
	TQProgressBar bar(100, NULL);
	if ((orientation == GTK_PROGRESS_BOTTOM_TO_TOP) || (orientation == GTK_PROGRESS_TOP_TO_BOTTOM))
		bar.resize(h, w);
	else
		bar.resize(w, h);
	bar.setProgress((int)(percentage*100.0));
	bar.setCenterIndicator(false);
	bar.setPercentageVisible(false);

	TQPixmap pixmap = TQPixmap::grabWidget(&bar);
	
	TQWMatrix matrix;
	switch (orientation)
	{
		case GTK_PROGRESS_RIGHT_TO_LEFT: matrix.rotate(180); break;
		case GTK_PROGRESS_BOTTOM_TO_TOP: matrix.rotate(270);  break;
		case GTK_PROGRESS_TOP_TO_BOTTOM: matrix.rotate(90); break;
		default: break;
	}
	
	if (orientation != GTK_PROGRESS_LEFT_TO_RIGHT)
		pixmap = pixmap.xForm(matrix);
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawSlider(GdkWindow * window, GtkStyle * style, GtkStateType state, GtkAdjustment *adj, int x, int y, int w, int h, GtkOrientation orientation, int inverted)
{
	if (!gtkQtEnable)
		return;

	meepSlider->setBackgroundOrigin(TQWidget::ParentOrigin);
	
	meepSlider->setOrientation((orientation == GTK_ORIENTATION_HORIZONTAL) ? Qt::Horizontal : Qt::Vertical);
	meepSlider->setEnabled(state != GTK_STATE_INSENSITIVE);

	meepSlider->setGeometry(x, y, w, h);
	meepSlider->setMinValue(0);
	meepSlider->setMaxValue(100);
	
	if (!inverted) // Normal sliders
		meepSlider->setValue((int)((adj->value-adj->lower)/(adj->upper-adj->lower)*100));
	else // Inverted sliders... where max is at the left/top and min is at the right/bottom
		meepSlider->setValue(100-(int)((adj->value-adj->lower)/(adj->upper-adj->lower)*100));

	TQPixmap pixmap = TQPixmap::grabWidget(meepSlider);
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawSpinButton(GdkWindow * window, GtkStyle * style, GtkStateType state, int direction, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w, h);
	TQPainter painter(&pixmap);

	TQStyle::SFlags sflags = stateToSFlags(state);
	
	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().tqdrawPrimitive((direction ? TQStyle::PE_SpinWidgetDown : TQStyle::PE_SpinWidgetUp), &painter, TQRect(0,0,w,h), tqApp->palette().active(), sflags);
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawListHeader(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);

	TQStyle::SFlags sflags = stateToSFlags(state) | TQStyle::Style_Horizontal;

	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().tqdrawPrimitive(TQStyle::PE_HeaderSection, &painter, TQRect(0,0,w,h), tqApp->palette().active(), sflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}


void drawListViewItem(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
        if (!gtkQtEnable)
                return;

	if ((w < 1) || (h < 1))
		return;

        TQPixmap     pixmap(w, h);
        TQPainter    painter(&pixmap);

	/* Get the brush corresponding to highlight color */
	TQBrush brush = tqApp->palette().brush(TQPalette::Active, TQColorGroup::Highlight);
	painter.setBrush(brush);
	painter.setPen(Qt::NoPen);
	painter.drawRect(0, 0, w, h);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawSquareButton(GdkWindow* window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;

	TQPixmap     pixmap(20, 20);
	TQPainter    painter(&pixmap);
	TQPushButton button(0);
		
	TQStyle::SFlags sflags = stateToSFlags(state);
	if (fillPixmap && (!fillPixmap->isNull()))
		painter.fillRect(0, 0, 20, 20, TQBrush(TQColor(255,255,255), *fillPixmap));
	else if ((backgroundTile) && (!backgroundTile->isNull()))
		painter.fillRect(0, 0, 20, 20, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
		painter.fillRect(0, 0, 20, 20, tqApp->palette().active().brush(TQColorGroup::Background));
	
	tqApp->style().drawControl(TQStyle::CE_PushButton, &painter, &button,
					TQRect(0,0,20,20), tqApp->palette().active(), sflags);
	
	TQImage image = pixmap.convertToImage().smoothScale(w,h);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void initDrawTabNG(int count)
{
	if (!gtkQtEnable)
		return;
	
	delete meepTabBar;
	meepTabBar = 0;
	meepTabBar = new TQTabBar(meepWidget);
	
	for ( int i = 0; i < count; i++ )
		meepTabBar->addTab(new TQTab);
	
	return;
}

void drawTabNG(GdkWindow *window, GtkStyle* style, GtkStateType state, int x, int y, int w, int h, GtkNotebook *notebook)
{
	if (!gtkQtEnable)
		return;
	
	GtkPositionType tpos = gtk_notebook_get_tab_pos(notebook);
	
	// Find tab position
	int sdiff = 10000, pos = -1, diff = 1;
	for ( unsigned int i = 0; i < g_list_length(notebook->children); i++ )
	{
		GtkWidget *tab_label=gtk_notebook_get_tab_label(notebook,gtk_notebook_get_nth_page(notebook,i));
		if (tab_label) diff = tab_label->allocation.x - x;
		if ((diff > 0) && (diff < sdiff))
		{
			sdiff = diff; pos = i;
		}
	}
	
	TQTab *tab = meepTabBar->tabAt(pos);
	
	if (!tab)
	{
		// This happens in Firefox.  Just draw a normal tab instead
		if (state == GTK_STATE_ACTIVE)
			drawTab(window, style, state, x, y - 2, w, h + 2);
		else
			drawTab(window, style, state, x, y, w, h);
		return;
	}
	
	TQStyle::SFlags sflags = stateToSFlags(state);
	
	if (state != GTK_STATE_ACTIVE)
	{
		sflags = TQStyle::Style_Selected;
		if (tpos == GTK_POS_TOP)
			y += 3;
		h -= 3;
	}
	
	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);
	
	if ((backgroundTile) && (!backgroundTile->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
	
	painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));
	tqApp->style().drawControl(TQStyle::CE_TabBarTab, &painter, (TQTabBar *)meepTabBar, TQRect(0,0,w,h), tqApp->palette().active(), sflags, TQStyleOption(tab));
	
	painter.end(); // So the pixmap assignment below won't give an error
	// Account for tab position -- if its in the bottom flip the image
	if (tpos == GTK_POS_BOTTOM)
	{
		TQWMatrix m;
		m.scale(1, -1);
		pixmap = pixmap.xForm(m);
	}
	
	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawArrow(GdkWindow* window, GtkStyle* style, GtkStateType state, GtkArrowType direction, int x, int y, int w, int h)
{
	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

	TQStyle::SFlags sflags = stateToSFlags(state);
	if (state == GTK_STATE_INSENSITIVE)
		sflags |= TQStyle::Style_Off;
	else if (state == GTK_STATE_PRELIGHT)
		sflags |= TQStyle::Style_On;

	TQStyle::PrimitiveElement element;
	switch(direction)
	{
		case GTK_ARROW_UP:    element = TQStyle::PE_ArrowUp;    break;
		case GTK_ARROW_DOWN:  element = TQStyle::PE_ArrowDown;  break;
		case GTK_ARROW_LEFT:  element = TQStyle::PE_ArrowLeft;  break;
		case GTK_ARROW_RIGHT: element = TQStyle::PE_ArrowRight; break;
		case GTK_ARROW_NONE:  return;
	}

	 
	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);

	if (fillPixmap && (!fillPixmap->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *fillPixmap));
	else if ((backgroundTile) && (!backgroundTile->isNull()))
		painter.fillRect(0, 0, w, h, TQBrush(TQColor(255,255,255), *backgroundTile));
	else
		painter.fillRect(0, 0, w, h, tqApp->palette().active().brush(TQColorGroup::Background));

	tqApp->style().tqdrawPrimitive(element, &painter, TQRect(0,0,w,h), tqApp->palette().active(), sflags);

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[state], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

void drawFocusRect(GdkWindow * window, GtkStyle * style, int x, int y, int w, int h)
{
//	if (!gtkQtEnable)
		return;
	
	if ((w < 1) || (h < 1))
		return;

	TQPixmap pixmap(w,h);
	TQPainter painter(&pixmap);
	TQColor bg(tqApp->palette().active().background());

	painter.fillRect(0,0,w,h,bg);
	tqApp->style().tqdrawPrimitive(TQStyle::PE_FocusRect, &painter, TQRect(0,0,w,h), tqApp->palette().active(), TQStyle::Style_Default, TQStyleOption(bg));

	GdkPixmap* pix = gdk_pixmap_foreign_new(pixmap.handle());
	gdk_draw_drawable(window, style->bg_gc[GTK_STATE_NORMAL], pix, 0, 0, x, y, w, h);
	g_object_unref(pix);
}

GdkGC* alternateBackgroundGc(GtkStyle* style)
{
	// Alternate background color for listviews
	if (altBackGC != 0)
		return altBackGC;
	
	GdkColor altBackColor;
	altBackColor.red = alternateBackgroundColour.red() * 257;
	altBackColor.green = alternateBackgroundColour.green() * 257;
	altBackColor.blue = alternateBackgroundColour.blue() * 257;
	
	gdk_colormap_alloc_color(style->colormap, &altBackColor, FALSE, TRUE);
	
	GdkGCValues gc_values;
	GdkGCValuesMask gc_values_mask;
	gc_values_mask = GDK_GC_FOREGROUND;
	gc_values.foreground = altBackColor;
	
	altBackGC = (GdkGC*) gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
	
	return altBackGC;
}


// Thanks Martin Dvorak of metatheme
TQString parse_rc_string(const TQString& defs, const TQString& pattern, bool widgetClass = true)
{
	static int dynamic_counter = 0;
	++dynamic_counter;
	
	return "style \"gtk-qt-dynamic-" + TQString::number(dynamic_counter) + "\" { " + defs + " } " + (widgetClass ? "widget_class" : "widget") + " \"" + pattern + "\" style \"gtk-qt-dynamic-" + TQString::number(dynamic_counter) + "\"\n";
}

TQString doIconMapping(const TQString& stockName, const TQString& path, int sizes)
{
	TQString fullPath;
	bool has16 = false, has22 = false, has32 = false;

	for( TQStringList::ConstIterator it = iconThemeDirs.begin();
			it != iconThemeDirs.end();
			++it )
	{
		fullPath = *it + "16x16/" + path;
		if (access(fullPath.latin1(), R_OK) == 0)
			has16 = true;
		fullPath = *it + "22x22/" + path;
		if (access(fullPath.latin1(), R_OK) == 0)
			has22 = true;
		fullPath = *it + "32x32/" + path;
		if (access(fullPath.latin1(), R_OK) == 0)
			has32 = true;
	}

	if (!has16 && !has22 && !has32) return "";
	
	// sizes is an addition of 1=16, 2=22 and 4=32
	TQString ret = "stock[\"" + stockName + "\"]={\n";
	
	if (has22)
		ret += "\t{ \"22x22/" + path +"\", *, *, \"gtk-large-toolbar\" },\n";

	if (has32)
	{
		ret += "\t{ \"32x32/" + path +"\", *, *, \"gtk-dnd\" },\n";
		ret += "\t{ \"32x32/" + path +"\", *, *, \"gtk-dialog\" },\n";
	}

	if (has16)
	{
		ret += "\t{ \"16x16/" + path +"\", *, *, \"gtk-button\" },\n";
		ret += "\t{ \"16x16/" + path +"\", *, *, \"gtk-menu\" },\n";
		ret += "\t{ \"16x16/" + path +"\", *, *, \"gtk-small-toolbar\" },\n";
	}
	
	if (has22)
		ret += "\t{ \"22x22/" + path +"\" }\n";
	else if (has32)
		ret += "\t{ \"32x32/" + path +"\" }\n";
	else
		ret += "\t{ \"16x16/" + path +"\" }\n";
	
	ret += "}\n";
	return ret;
}

TQString colorString(TQColor color)
{
	TQString ret = "{";
	ret += TQString::number(color.red() * 257) + ", ";
	ret += TQString::number(color.green() * 257) + ", ";
	ret += TQString::number(color.blue() * 257) + "}";
	
	return ret;
}

void setColour(TQString name, TQColor color)
{
	gtk_rc_parse_string(parse_rc_string(name + " = " + colorString(color), "*").latin1());
}

static TQStringList iconInheritsDirs( const TQString& icondir )
{
	TQFile index;
	index.setName( icondir + "index.theme" );
	if( !index.open( IO_ReadOnly ))
	{
		index.setName( icondir + "index.desktop" );
		if( !index.open( IO_ReadOnly ))
			return TQStringList();
	}
	char buf[ 1024 ];
	TQRegExp reg( "^\\s*Inherits=([^\\n]*)" );
	for(;;)
	{
		if( index.readLine( buf, 1023 ) <= 0 )
			break;
		if( reg.search( buf, 0 ) >= 0 )
			return TQStringList::split(",", reg.cap(1));
	}
	return TQStringList();
}

void setRcProperties(GtkRcStyle* rc_style, int forceRecreate)
{
	if (!gtkQtEnable)
		return;
	
	if (gtkQtDebug)
		printf("setRcProperties()\n");
	
	gtkRcStyle = rc_style;
	
	// Set colors
	// Normal
	setColour("fg[NORMAL]",     tqApp->palette().active().text());
	setColour("bg[NORMAL]",     tqApp->palette().active().background());
	setColour("text[NORMAL]",   tqApp->palette().active().text());
	setColour("base[NORMAL]",   tqApp->palette().active().base());
	
	// Active (on)
	setColour("fg[ACTIVE]",     tqApp->palette().active().text());
	setColour("bg[ACTIVE]",     tqApp->palette().active().background());
	setColour("text[ACTIVE]",   tqApp->palette().active().text());
	setColour("base[ACTIVE]",   tqApp->palette().active().base());
	
	// Mouseover
	setColour("fg[PRELIGHT]",   tqApp->palette().active().text()); // menu items - change?
	setColour("bg[PRELIGHT]",   tqApp->palette().active().highlight());
	setColour("text[PRELIGHT]", tqApp->palette().active().text());
	setColour("base[PRELIGHT]", tqApp->palette().active().base());
	
	// Selected
	setColour("fg[SELECTED]",   tqApp->palette().active().highlightedText());
	setColour("bg[SELECTED]",   tqApp->palette().active().highlight());
	setColour("text[SELECTED]", tqApp->palette().active().highlightedText());
	setColour("base[SELECTED]", tqApp->palette().active().highlight());
	
	// Disabled
	setColour("fg[INSENSITIVE]",    tqApp->palette().disabled().text());
	setColour("bg[INSENSITIVE]",    tqApp->palette().disabled().background());
	setColour("text[INSENSITIVE]",  tqApp->palette().disabled().text());
	setColour("base[INSENSITIVE]",  tqApp->palette().disabled().background());

	gtk_rc_parse_string(("gtk-button-images = " + TQString::number(showIconsOnButtons)).latin1());
	
	gtk_rc_parse_string(("gtk-toolbar-style = " + TQString::number(toolbarStyle)).latin1());
	
	// This function takes quite a long time to execute, and is run at the start of every app.
	// In order to speed it up, we can store the results in a file, along with the name of icon
	// theme and style.  This file can then be regenerated when the icon theme or style change.
	
	TQString cacheFilePath = TDEGlobal::dirs()->saveLocation("cache")+"/gtk_qt_engine_rc";
	TQFile cacheFile(cacheFilePath);
	TQTextStream stream;
	
	if (!forceRecreate && cacheFile.exists())
	{
		cacheFile.open(IO_ReadOnly);
		stream.setDevice(TQT_TQIODEVICE(&cacheFile));
		
		if (stream.readLine() == "# " + iconTheme + ", " + tqApp->style().name() + ", " + RC_CACHE_VERSION)
		{
			// This cache matches the current icon theme and style
			// Let's load it and return
			gtk_rc_add_default_file(cacheFilePath.latin1());
			return;
		}
		
		stream.unsetDevice();
		cacheFile.close();
	}
	
	cacheFile.open(IO_WriteOnly | IO_Truncate);
	stream.setDevice(TQT_TQIODEVICE(&cacheFile));
	
	stream << "# " << iconTheme << ", " << tqApp->style().name() << ", " << RC_CACHE_VERSION << "\n\n";
	stream << "# This file was generated by the Gtk Qt Theme Engine\n";
	stream << "# It will be recreated when you change your KDE icon theme or widget style\n\n";
	
	TQScrollBar sbar(NULL);
	sbar.setOrientation(Qt::Horizontal);
	sbar.setValue(1);
	sbar.resize(200,25);
	
	// The following code determines how many buttons are on a scrollbar
	// It works by looking at each pixel of the scrollbar's area not taken up by the groove,
	// and asking the style which subcontrol is at that location.
	TQRect rect = tqApp->style().querySubControlMetrics(TQStyle::CC_ScrollBar, &sbar, TQStyle::SC_ScrollBarGroove);
	
	bool back1 = false;
	bool forward1 = false;
	bool back2 = false;
	bool forward2 = false;
	
	TQStyle::SubControl sc = TQStyle::SC_None;
	for (TQPoint pos(0,7) ; pos.x()<rect.x() ; pos.setX(pos.x()+1))
	{
		TQStyle::SubControl sc2 = tqApp->style().querySubControl(TQStyle::CC_ScrollBar, &sbar, pos);
		if (sc != sc2)
		{
			if (sc2 == TQStyle::SC_ScrollBarAddLine) forward1 = true;
			if (sc2 == TQStyle::SC_ScrollBarSubLine) back1 = true;
			sc = sc2;
		}
	}
	sc = TQStyle::SC_None;
	for (TQPoint pos(rect.x()+rect.width(),7) ; pos.x()<200 ; pos.setX(pos.x()+1))
	{
		TQStyle::SubControl sc2 = tqApp->style().querySubControl(TQStyle::CC_ScrollBar, &sbar, pos);
		if (sc != sc2)
		{
			if (sc2 == TQStyle::SC_ScrollBarAddLine) forward2 = true;
			if (sc2 == TQStyle::SC_ScrollBarSubLine) back2 = true;
			sc = sc2;
		}
	}

	stream << parse_rc_string(TQString("GtkComboBox::appears-as-list = 1"), "*");

	stream << parse_rc_string(TQString("GtkScrollbar::has-backward-stepper = ") + (back1 ? "1" : "0"), "*");
	stream << parse_rc_string(TQString("GtkScrollbar::has-forward-stepper = ") + (forward2 ? "1" : "0"), "*");
	stream << parse_rc_string(TQString("GtkScrollbar::has-secondary-forward-stepper = ") + (forward1 ? "1" : "0"), "*");
	stream << parse_rc_string(TQString("GtkScrollbar::has-secondary-backward-stepper = ") + (back2 ? "1" : "0"), "*");

	stream << parse_rc_string("GtkScrollbar::stepper-size = " + TQString::number(tqApp->style().querySubControlMetrics(TQStyle::CC_ScrollBar, &sbar, TQStyle::SC_ScrollBarSubLine).width() - 1), "*");

	stream << parse_rc_string("GtkScrollbar::min-slider-length = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_ScrollBarSliderMin)), "*");
	stream << parse_rc_string("GtkScrollbar::slider-width = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_ScrollBarExtent)-2), "*");

	stream << parse_rc_string("GtkButton::child-displacement-x = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_ButtonShiftHorizontal)), "*");
	stream << parse_rc_string("GtkButton::child-displacement-y = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_ButtonShiftVertical)), "*");
	stream << parse_rc_string("GtkButton::default-border = { 0, 0, 0, 0 }", "*");
	stream << parse_rc_string("GtkButton::default-outside-border = {0, 0, 0, 0}", "*");
#ifdef USE_NATIVE_GTK_BUTTON_DRAWING
	stream << parse_rc_string("GtkButton::inner-border = {2, 2, 2, 2}", "*");
#else
	if (tde_showIconsOnPushButtons) {
		stream << parse_rc_string("GtkButton::inner-border = {10, 10, 2, 2}", "*");	// Allow space for the icon on either side of the text
	}
	else {
		stream << parse_rc_string("GtkButton::inner-border = {2, 2, 2, 2}", "*");
	}
#endif

	stream << parse_rc_string("GtkButtonBox::child_min_height     = 0", "*");
	stream << parse_rc_string("GtkButtonBox::child_internal_pad_x = 0", "*");
	stream << parse_rc_string("GtkButtonBox::child_internal_pad_y = 0", "*");

	TQSlider slider(NULL); // To keep BlueCurve happy
	stream << parse_rc_string("GtkScale::slider-length = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_SliderLength, &slider)), "*");

	stream << parse_rc_string("xthickness = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_DefaultFrameWidth)), "*.GtkMenu");
	stream << parse_rc_string("ythickness = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_DefaultFrameWidth)), "*.GtkMenu");
	stream << parse_rc_string("xthickness = 5", "*.GtkMenu.Gtk*MenuItem");
	stream << parse_rc_string("xthickness = 3", "*.GtkNotebook");
	stream << parse_rc_string("ythickness = 3", "*.GtkNotebook");
	stream << parse_rc_string("ythickness = 1", "*.GtkButton");
	stream << parse_rc_string("fg[NORMAL] = {0, 0, 0}", "gtk-tooltips.GtkLabel", false);

	stream << parse_rc_string("xthickness = 1", "*.GtkButton.*");
	stream << parse_rc_string("ythickness = 1", "*.GtkButton.*");

//	stream << parse_rc_string("GtkTreeView::allow-rules = 0", "*");
//	stream << parse_rc_string("GtkTreeView::tree-line-width = 1", "*");
//	stream << parse_rc_string("GtkTreeView::vertical-separator = 30", "*");
//	//stream << parse_rc_string("GtkTreeView::odd-row-color = { 0.0, 0.0, 0.0 }", "*");

	stream << parse_rc_string("GtkButton::inner-border = {0, 0, 0, 0}", "*GtkToolbar*GtkButton*");
	stream << parse_rc_string("GtkButton::inner-border = {0, 0, 0, 0}", "*GtkToolbar*GtkToggleButton*");
	stream << parse_rc_string("GtkButton::inner-border = {0, 0, 0, 0}", "*GtkNotebook*GtkButton*");
	stream << parse_rc_string("GtkButton::inner-border = {0, 0, 0, 0}", "*GtkNotebook*GtkToggleButton*");

	// Qt calls them tab boxes, GTK calls them notebooks (!??!?)  Either way they are a pain...
	stream << parse_rc_string("GtkNotebook::tab-overlap = " + TQString::number(tqApp->style().pixelMetric(TQStyle::PM_TabBarTabOverlap)), "*");

	// This one may not work...
	//insertIntProperty(rc_style, "GtkCheckButton", "indicator-size", tqApp->style().pixelMetric(TQStyle::PM_IndicatorHeight) );

	// For icons

	// Build the list of icon theme directories.
	// This function is recursive - it gets the directories of all the inherited themes as well
	addIconThemeDir(iconTheme);

	if (iconThemeDirs.isEmpty())
	{
		cacheFile.close();
		gtk_rc_add_default_file(cacheFilePath.latin1());
		return;
	}

	stream << "\npixmap_path \"" + iconThemeDirs.join( ":" ) + "\"\n\n";

	stream << "style \"KDE-icons\" {\n";
        stream << doIconMapping("gtk-about", "actions/about_kde.png");
        stream << doIconMapping("gtk-add", "actions/add.png");
        stream << doIconMapping("gtk-apply", "actions/apply.png");
	stream << doIconMapping("gtk-bold", "actions/text_bold.png");
	stream << doIconMapping("gtk-cancel", "actions/button_cancel.png");
	stream << doIconMapping("gtk-cdrom", "devices/cdrom_unmount.png");
	stream << doIconMapping("gtk-clear", "actions/editclear.png");
	stream << doIconMapping("gtk-close", "actions/fileclose.png");
	stream << doIconMapping("gtk-color-picker", "actions/colorpicker.png", 3);
	stream << doIconMapping("gtk-copy", "actions/editcopy.png");
	stream << doIconMapping("gtk-convert", "actions/gtk-convert.png");
	stream << doIconMapping("gtk-connect", "actions/connect_creating.png");
	stream << doIconMapping("gtk-cut", "actions/editcut.png");
	stream << doIconMapping("gtk-delete", "actions/editdelete.png");
	stream << doIconMapping("gtk-dialog-authentication", "status/gtk-dialog-authentication");
	stream << doIconMapping("gtk-dialog-error", "actions/messagebox_critical.png", 4);
	stream << doIconMapping("gtk-dialog-info", "actions/messagebox_info.png", 4);
	stream << doIconMapping("gtk-dialog-question", "actions/help.png");
	stream << doIconMapping("gtk-dialog-warning", "actions/messagebox_warning.png", 4);
	stream << doIconMapping("gtk-directory", "filesystems/folder.png");
	stream << doIconMapping("gtk-disconnect", "actions/connect_no.png");
	stream << doIconMapping("gtk-dnd", "mimetypes/empty.png");
	stream << doIconMapping("gtk-dnd-multiple", "mimetypes/tdemultiple.png");
	stream << doIconMapping("gtk-edit", "actions/edit.png");  //2.6 
	stream << doIconMapping("gtk-execute", "actions/exec.png");
	stream << doIconMapping("gtk-file", "mimetypes/empty.png");
	stream << doIconMapping("gtk-find", "actions/find.png");
	stream << doIconMapping("gtk-find-and-replace", "actions/find.png");	// Is there a TDE "find and replace" icon? FIXME
	stream << doIconMapping("gtk-floppy", "devices/3floppy_unmount.png");
	stream << doIconMapping("gtk-fullscreen", "actions/window-fullscreen.png");
	stream << doIconMapping("gtk-goto-bottom", "actions/bottom.png");
	stream << doIconMapping("gtk-goto-first", "actions/start.png");
	stream << doIconMapping("gtk-goto-last", "actions/finish.png");
	stream << doIconMapping("gtk-goto-top", "actions/top.png");
	stream << doIconMapping("gtk-go-back", "actions/back.png");
	stream << doIconMapping("gtk-go-down", "actions/down.png");
	stream << doIconMapping("gtk-go-forward", "actions/forward.png");
	stream << doIconMapping("gtk-go-up", "actions/up.png");
	stream << doIconMapping("gtk-harddisk", "devices/hdd_unmount.png");
	stream << doIconMapping("gtk-help", "apps/khelpcenter.png");
	stream << doIconMapping("gtk-home", "filesystems/folder_home.png");
	stream << doIconMapping("gtk-indent", "actions/indent.png");
	stream << doIconMapping("gtk-index", "actions/contents.png");
	stream << doIconMapping("gtk-info", "actions/messagebox_info.png");
	stream << doIconMapping("gtk-italic", "actions/text_italic.png");
	stream << doIconMapping("gtk-jump-to", "actions/goto.png");
	stream << doIconMapping("gtk-justify-center", "actions/text_center.png");
	stream << doIconMapping("gtk-justify-fill", "actions/text_block.png");
	stream << doIconMapping("gtk-justify-left", "actions/text_left.png");
	stream << doIconMapping("gtk-justify-right", "actions/text_right.png");
	stream << doIconMapping("gtk-leave-fullscreen", "actions/window-nofullscreen.png");
	stream << doIconMapping("gtk-media-forward", "player-fwd.png");
	stream << doIconMapping("gtk-media-next", "actions/player-end.png");
	stream << doIconMapping("gtk-media-pause", "actions/player-pause.png");
	stream << doIconMapping("gtk-media-previous", "actions/player-start.png");
	stream << doIconMapping("gtk-media-record", "actions/gtk-media-record.png");	// FIXME
	stream << doIconMapping("gtk-media-rewind", "actions/player-rew.png");
	stream << doIconMapping("gtk-media-stop", "actions/player-stop.png");
	stream << doIconMapping("gtk-missing-image", "mimetypes/unknown.png");
	stream << doIconMapping("gtk-network", "filesystems/network.png");
	stream << doIconMapping("gtk-new", "actions/filenew.png");
	stream << doIconMapping("gtk-no", "actions/gtk-no.png");
	stream << doIconMapping("gtk-ok", "actions/button_ok.png");
	stream << doIconMapping("gtk-open", "actions/fileopen.png");
	//stream << doIconMapping("gtk-orientation-landscape", "??");			// FIXME
	//stream << doIconMapping("gtk-orientation-portrait", "??");			// FIXME
	//stream << doIconMapping("gtk-orientation-reverse-landscape", "??");		// FIXME
	//stream << doIconMapping("gtk-orientation-reverse-portrait", "??");		// FIXME
	stream << doIconMapping("gtk-paste", "actions/editpaste.png");
	stream << doIconMapping("gtk-preferences", "actions/configure.png");
	stream << doIconMapping("gtk-print", "actions/fileprint.png");
	stream << doIconMapping("gtk-print-preview", "actions/filequickprint.png");
	stream << doIconMapping("gtk-properties", "actions/configure.png");
	stream << doIconMapping("gtk-quit", "actions/exit.png");
	stream << doIconMapping("gtk-redo", "actions/redo.png");
	stream << doIconMapping("gtk-refresh", "actions/reload.png");
	stream << doIconMapping("gtk-remove", "actions/remove.png");
	stream << doIconMapping("gtk-revert-to-saved", "actions/revert.png");
	stream << doIconMapping("gtk-save", "actions/filesave.png");
	stream << doIconMapping("gtk-save-as", "actions/filesaveas.png");
	stream << doIconMapping("gtk-select-all", "actions/gtk-select-all.png");	// FIXME
	stream << doIconMapping("gtk-select-color", "actions/colorize.png");
	stream << doIconMapping("gtk-select-font", "mimetypes/font.png");
	//stream << doIconMapping("gtk-sort-ascending", "??");				// FIXME
	//stream << doIconMapping("gtk-sort-descending", "??");				// FIXME
	stream << doIconMapping("gtk-spell-check", "actions/spellcheck.png");
	stream << doIconMapping("gtk-stop", "actions/stop.png");
	stream << doIconMapping("gtk-strikethrough", "actions/text_strike.png", 3);
	stream << doIconMapping("gtk-undelete", "actions/gtk-undelete.png");		// FIXME
	stream << doIconMapping("gtk-underline", "actions/text_under.png");
	stream << doIconMapping("gtk-undo", "actions/undo.png");
	stream << doIconMapping("gtk-unindent", "actions/unindent.png");
	stream << doIconMapping("gtk-yes", "actions/button_ok.png");			// Verify mapping here
	stream << doIconMapping("gtk-zoom-100", "actions/viewmag1.png");
	stream << doIconMapping("gtk-zoom-fit", "actions/viewmagfit.png");
	stream << doIconMapping("gtk-zoom-in", "actions/viewmag+.png");
	stream << doIconMapping("gtk-zoom-out", "actions/viewmag-.png");

	// Other icons that really should have Trinity equivalents in tdelibs
	stream << doIconMapping("list-add", "actions/add.png");
	stream << doIconMapping("list-remove", "actions/remove.png");

	stream << "} class \"*\" style \"KDE-icons\"";
	
	cacheFile.close();
	
	gtk_rc_add_default_file(cacheFilePath.latin1());
}

void addIconThemeDir(const TQString& theme)
{
	// Try to find this theme's directory
	TQString icondir = kdeFindDir("/share/icons/" + theme + "/", "index.theme", "index.desktop");
	if(icondir.isEmpty())
		return;
	if (iconThemeDirs.contains(icondir))
		return;

	// Add this theme to the list
	iconThemeDirs.append(icondir);

	// Do it again for any parent themes
	TQStringList parents = iconInheritsDirs(icondir);
	for ( TQStringList::Iterator it=parents.begin() ; it!=parents.end(); ++it)
		addIconThemeDir((*it).stripWhiteSpace());
}

void setMenuBackground(GtkStyle* style)
{
	if (!gtkQtEnable)
		return;
	
	if (menuBackgroundPixmap == NULL)
	{
		// Get the menu background image
		menuBackgroundPixmap = new TQPixmap(1024, 25); // Meh
		TQPainter painter(menuBackgroundPixmap);
		TQPopupMenu pm;
		TQMenuData md;
		TQMenuItem* mi = md.findItem(md.insertItem(""));
		
		tqApp->style().polish(&pm);
		
		TQStyleOption opt(mi, 16, 16);
		TQStyle::SFlags sflags = TQStyle::Style_Default;
	
		if ((backgroundTile) && (!backgroundTile->isNull()))
			painter.fillRect(0, 0, 1024, 25, TQBrush(TQColor(255,255,255), *backgroundTile));
		else
			painter.fillRect(0, 0, 1024, 25, tqApp->palette().active().brush(TQColorGroup::Background));
		tqApp->style().drawControl(TQStyle::CE_PopupMenuItem, &painter, &pm, TQRect(0,0,1024,25), tqApp->palette().active(), sflags, opt);
		
		menuBackgroundPixmapGdk = gdk_pixmap_foreign_new(menuBackgroundPixmap->handle());
	}

	TQTENGINE_STYLE(style)->menuBackground = menuBackgroundPixmapGdk;
	g_object_ref(menuBackgroundPixmapGdk);
}

// It has a 'u' damnit
void setColour(GdkColor* g, TQColor q)
{
	g->red = q.red() * 257;
	g->green = q.green() * 257;
	g->blue = q.blue() * 257;
}

void setColors(GtkStyle* style)
{
	if (!gtkQtEnable)
		return;
	
	if (gtkQtDebug)
		printf("setColors()\n");
	
	/*gtkStyle = style;*/
	
	bool useBg = ((backgroundTile) && (!backgroundTile->isNull()));

	if (useBg)
	{
		style->bg_pixmap[GTK_STATE_NORMAL] = backgroundTileGdk;
		g_object_ref(backgroundTileGdk);
	}
	
	setMenuBackground(style);
}

void getTextColor(GdkColor *color, GtkStateType state_type)
{
	if (!gtkQtEnable)
		return;
	
	if ((state_type == GTK_STATE_PRELIGHT) || (state_type == GTK_STATE_ACTIVE) || (state_type == GTK_STATE_SELECTED))
		setColour(color, tqApp->palette().active().highlightedText());
	else if (state_type == GTK_STATE_NORMAL)
		setColour(color, tqApp->palette().active().text());
	else if (state_type == GTK_STATE_INSENSITIVE)
		setColour(color, tqApp->palette().disabled().text());
}