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
|
{
fpGUI - Free Pascal GUI Library
Copyright (C) 2006 - 2008 See the file AUTHORS.txt, included in this
distribution, for details of the copyright.
See the file COPYING.modifiedLGPL, included in this distribution,
for details about redistributing fpGUI.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description:
This unit implements X11 / Xlib support for fpGUI.
}
unit fpg_x11;
{$mode objfpc}{$H+}
{.$Define DEBUG}
interface
uses
Classes,
SysUtils,
X,
Xlib,
XUtil,
ctypes,
fpg_xft_x11,
fpg_netlayer_x11,
fpg_base,
fpg_impl;
const
IconBitmapWidth = 16;
IconBitmapHeight = 16;
IconBitmapBits: packed array[1..32] of Byte = (
$00, $00, $78, $07, $08, $09, $38, $09, $08, $07, $08, $01,
$08, $01, $00, $00, $00, $00, $98, $74, $a4, $24, $84, $24,
$b4, $24, $a4, $24, $18, $73, $00, $00);
type
TfpgGContext = Xlib.TGc;
PInt = ^integer;
TXIC = record
dummy: Pointer;
end;
PXIC = ^TXIC;
TXIM = record
dummy: Pointer;
end;
PXIM = ^TXIM;
TXdbeSwapInfo = record
Window: TfpgWinHandle;
SwapAction: PChar;
end;
PXdbeSwapInfo = ^TXdbeSwapInfo;
// MWM support
TMWMHints = record
flags: culong;
functions: culong;
decorations: culong;
input_mode: longint;
status: culong;
end;
const
// Motif window hints
MWM_HINTS_FUNCTIONS = 1 shl 0;
MWM_HINTS_DECORATIONS = 1 shl 1;
MWM_HINTS_INPUT_MODE = 1 shl 2;
MWM_HINTS_STATUS = 1 shl 3;
// bit definitions for MwmHints.functions */
MWM_FUNC_ALL = 1 shl 0;
MWM_FUNC_RESIZE = 1 shl 1;
MWM_FUNC_MOVE = 1 shl 2;
MWM_FUNC_MINIMIZE = 1 shl 3;
MWM_FUNC_MAXIMIZE = 1 shl 4;
MWM_FUNC_CLOSE = 1 shl 5;
// bit definitions for MwmHints.decorations */
MWM_DECOR_ALL = 1 shl 0;
MWM_DECOR_BORDER = 1 shl 1;
MWM_DECOR_RESIZEH = 1 shl 2;
MWM_DECOR_TITLE = 1 shl 3;
MWM_DECOR_MENU = 1 shl 4;
MWM_DECOR_MINIMIZE = 1 shl 5;
MWM_DECOR_MAXIMIZE = 1 shl 6;
// bit definitions for MwmHints.inputMode */
MWM_INPUT_MODELESS = 0;
MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1;
MWM_INPUT_SYSTEM_MODAL = 2;
MWM_INPUT_FULL_APPLICATION_MODAL = 3;
PROP_MWM_HINTS_ELEMENTS = 5;
type
TXWindowStateFlag = (xwsfMapped);
TXWindowStateFlags = set of TXWindowStateFlag;
// Returns True if it 'ate' the event
TX11EventFilter = function(const AEvent: TXEvent): Boolean of object;
// forward declaration
TfpgWindowImpl = class;
TfpgFontResourceImpl = class(TfpgFontResourceBase)
private
FFontData: PXftFont;
function DoGetTextWidthClassic(const txt: string): integer;
function DoGetTextWidthWorkaround(const txt: string): integer;
protected
property Handle: PXftFont read FFontData;
public
constructor Create(const afontdesc: string);
destructor Destroy; override;
function HandleIsValid: boolean;
function GetAscent: integer; override;
function GetDescent: integer; override;
function GetHeight: integer; override;
function GetTextWidth(const txt: string): integer; override;
end;
TfpgImageImpl = class(TfpgImageBase)
private
FXimg: TXImage;
FXimgmask: TXImage;
function XImage: PXImage;
function XImageMask: PXImage;
protected
procedure DoFreeImage; override;
procedure DoInitImage(acolordepth, awidth, aheight: integer; aimgdata: Pointer); override;
procedure DoInitImageMask(awidth, aheight: integer; aimgdata: Pointer); override;
public
constructor Create;
end;
TfpgCanvasImpl = class(TfpgCanvasBase)
private
FDrawing: boolean;
FDrawWindow: TfpgWindowImpl;
FBufferPixmap: TfpgDCHandle;
FDrawHandle: TfpgDCHandle;
Fgc: TfpgGContext;
FCurFontRes: TfpgFontResourceImpl;
FClipRect: TfpgRect;
FClipRectSet: boolean;
FXftDraw: PXftDraw;
FColorTextXft: TXftColor;
FClipRegion: TRegion;
FPixHeight,
FPixWidth: Integer;
FBufferFreeTimer: TObject;
procedure BufferFreeTimer(Sender: TObject);
procedure TryFreePixmap;
protected
procedure DoSetFontRes(fntres: TfpgFontResourceBase); override;
procedure DoSetTextColor(cl: TfpgColor); override;
procedure DoSetColor(cl: TfpgColor); override;
procedure DoSetLineStyle(awidth: integer; astyle: TfpgLineStyle); override;
procedure DoGetWinRect(out r: TfpgRect); override;
procedure DoFillRectangle(x, y, w, h: TfpgCoord); override;
procedure DoXORFillRectangle(col: TfpgColor; x, y, w, h: TfpgCoord); override;
procedure DoFillTriangle(x1, y1, x2, y2, x3, y3: TfpgCoord); override;
procedure DoDrawRectangle(x, y, w, h: TfpgCoord); override;
procedure DoDrawLine(x1, y1, x2, y2: TfpgCoord); override;
procedure DoDrawImagePart(x, y: TfpgCoord; img: TfpgImageBase; xi, yi, w, h: integer); override;
procedure DoDrawString(x, y: TfpgCoord; const txt: string); override;
procedure DoSetClipRect(const ARect: TfpgRect); override;
function DoGetClipRect: TfpgRect; override;
procedure DoAddClipRect(const ARect: TfpgRect); override;
procedure DoClearClipRect; override;
procedure DoBeginDraw(awin: TfpgWindowBase; buffered: boolean); override;
procedure DoPutBufferToScreen(x, y, w, h: TfpgCoord); override;
procedure DoEndDraw; override;
function GetPixel(X, Y: integer): TfpgColor; override;
procedure SetPixel(X, Y: integer; const AValue: TfpgColor); override;
procedure DoDrawArc(x, y, w, h: TfpgCoord; a1, a2: Extended); override;
procedure DoFillArc(x, y, w, h: TfpgCoord; a1, a2: Extended); override;
procedure DoDrawPolygon(Points: PPoint; NumPts: Integer; Winding: boolean=False); override;
property DCHandle: TfpgDCHandle read FDrawHandle;
public
constructor Create; override;
destructor Destroy; override;
end;
TfpgWindowImpl = class(TfpgWindowBase)
protected
FWinFlags: TXWindowStateFlags;
FWinHandle: TfpgWinHandle;
FBackupWinHandle: TfpgWinHandle; // Used by DestroyNotify & UnmapNotify events
FModalForWin: TfpgWindowImpl;
procedure DoAllocateWindowHandle(AParent: TfpgWindowBase); override;
procedure DoReleaseWindowHandle; override;
procedure DoRemoveWindowLookup; override;
procedure DoSetWindowVisible(const AValue: Boolean); override;
function HandleIsValid: boolean; override;
procedure DoSetWindowTitle(const ATitle: string); override;
procedure DoMoveWindow(const x: TfpgCoord; const y: TfpgCoord); override;
function DoWindowToScreen(ASource: TfpgWindowBase; const AScreenPos: TPoint): TPoint; override;
procedure DoUpdateWindowPosition; override;
procedure DoSetMouseCursor; override;
property WinHandle: TfpgWinHandle read FWinHandle;
public
constructor Create(AOwner: TComponent); override;
procedure ActivateWindow; override;
procedure CaptureMouse; override;
procedure ReleaseMouse; override;
procedure SetFullscreen(AValue: Boolean); override;
end;
TfpgApplicationImpl = class(TfpgApplicationBase)
private
FComposeBuffer: String[32];
FComposeStatus: TStatus;
FEventFilter: TX11EventFilter;
function ConvertShiftState(AState: Cardinal): TShiftState;
function KeySymToKeycode(KeySym: TKeySym): Word;
function StartComposing(const Event: TXEvent): TKeySym;
protected
FDisplay: PXDisplay;
DisplayDepth: integer;
DefaultBackground: TfpgColor;
DefaultForeground: TfpgColor;
DefaultScreen: integer;
DefaultVisual: PVisual;
DefaultColorMap: TColorMap;
FRootWindow: TfpgWinHandle;
xia_clipboard: TAtom;
xia_motif_wm_hints: TAtom;
xia_wm_protocols: TAtom;
xia_wm_delete_window: TAtom;
netlayer: TNETWindowLayer;
xia_targets: TAtom;
InputMethod: PXIM;
InputContext: PXIC;
FLastKeySym: TKeySym; // Used for KeyRelease event
function DoGetFontFaceList: TStringList; override;
public
constructor Create(const AParams: string); override;
destructor Destroy; override;
function DoMessagesPending: boolean;
procedure DoWaitWindowMessage(atimeoutms: integer);
procedure DoFlush;
function GetScreenWidth: TfpgCoord; override;
function GetScreenHeight: TfpgCoord; override;
function Screen_dpi_x: integer; override;
function Screen_dpi_y: integer; override;
function Screen_dpi: integer; override;
property Display: PXDisplay read FDisplay;
property RootWindow: TfpgWinHandle read FRootWindow;
property EventFilter: TX11EventFilter read FEventFilter write FEventFilter;
end;
TfpgClipboardImpl = class(TfpgClipboardBase)
private
FWaitingForSelection: Boolean;
protected
FClipboardText: TfpgString;
function DoGetText: TfpgString; override;
procedure DoSetText(const AValue: TfpgString); override;
procedure InitClipboard; override;
end;
TfpgFileListImpl = class(TfpgFileListBase)
function EncodeModeString(FileMode: longword): TFileModeString;
constructor Create; override;
function InitializeEntry(sr: TSearchRec): TFileEntry; override;
procedure PopulateSpecialDirs(const aDirectory: TfpgString); override;
end;
function fpgColorToX(col: TfpgColor): longword;
implementation
uses
baseunix,
users, { for *nix user and group name support }
fpg_main,
fpg_widget,
fpg_popupwindow,
fpg_stringutils, // used for GetTextWidth
fpg_form, // for modal event support
cursorfont,
xatom; // used for XA_WM_NAME
var
xapplication: TfpgApplication;
// some externals
// defines:
procedure XRenderSetPictureClipRectangles(disp: PXDisplay; pic: TPicture; xorigin, yorigin: integer; rect: PXRectangle; num: integer); cdecl; external;
// redefines:
function XmbLookupString(p1: PXIC; ev: PXKeyPressedEvent; str: PChar; len: longword; ks: PKeySym; stat: PStatus): longint; cdecl; external;
function Xutf8LookupString(p1: PXIC; ev: PXKeyPressedEvent; str: PChar; len: longword; ks: PKeySym; stat: PStatus): longint; cdecl; external;
// Double buffer functions
function XdbeQueryExtension(ADisplay: PXDisplay; AMajor, AMinor: PInt): PStatus; cdecl; external;
function XdbeAllocateBackBufferName(ADisplay: PXDisplay; AWindow: TfpgWinHandle; ASwapAction: PChar): TfpgWinHandle; cdecl; external;
function XdbeSwapBuffers(ADisplay: PXDisplay; ASwapInfo: PXdbeSwapInfo; AScreenNums: integer): PStatus; cdecl; external;
function XdbeDeallocateBackBufferName(ADisplay: PXDisplay; ABuffer: TfpgWinHandle): PStatus; cdecl; external;
function XOpenIM(para1: PDisplay; para2: PXrmHashBucketRec; para3: Pchar; para4: Pchar): PXIM; cdecl; external;
function XCreateIC(para1: PXIM; para2: array of const): PXIC; cdecl; external;
function ConvertTo565Pixel(rgb: longword): word;
begin
Result := (rgb and $F8) shr 3;
Result := Result or ((rgb and $FC00) shr 5);
Result := Result or ((rgb and $F80000) shr 8);
end;
function fpgColorToX(col: TfpgColor): longword;
var
xc: TXColor;
c: TfpgColor;
begin
c := fpgColorToRGB(col);
if xapplication.DisplayDepth >= 24 then
Result := c
else if xapplication.DisplayDepth = 16 then
Result := ConvertTo565Pixel(c)
else
begin
c := col;
xc.blue := (c and $000000FF) shl 8;
xc.green := (c and $0000FF00);
xc.red := (c and $00FF0000) shr 8;
// THIS CALL IS TOO SLOW !!!!!:
XAllocColor(xapplication.display, xapplication.DefaultColorMap, @xc);
Result := xc.pixel;
end;
end;
procedure SetXftColor(col: TfpgColor; var colxft: TXftColor);
var
c: TfpgColor;
begin
c := fpgColorToRGB(col);
colxft.color.blue := (c and $000000FF) shl 8;
colxft.color.green := (c and $0000FF00);
colxft.color.red := (c and $00FF0000) shr 8;
colxft.color.alpha := (c and $7F000000) shr 15;
colxft.color.alpha := colxft.color.alpha xor $FFFF; // invert: 0 means not translucent
colxft.pixel := 0;
end;
type
PWindowLookupRec = ^WindowLookupRec;
// single direction linked list
WindowLookupRec = record
w: TfpgWindowImpl;
Next: PWindowLookupRec;
end;
var
FirstWindowLookupRec: PWindowLookupRec;
LastWindowLookupRec: PWindowLookupRec;
procedure AddWindowLookup(w: TfpgWindowImpl);
var
p: PWindowLookupRec;
begin
if w = nil then
Exit;
New(p);
p^.w := w;
p^.Next := nil;
if FirstWindowLookupRec = nil then
FirstWindowLookupRec := p
else
LastWindowLookupRec^.Next := p;
LastWindowLookupRec := p;
end;
procedure RemoveWindowLookup(w: TfpgWindowImpl);
var
prevp: PWindowLookupRec;
p: PWindowLookupRec;
px: PWindowLookupRec;
begin
p := FirstWindowLookupRec;
prevp := nil;
while p <> nil do
if p^.w = w then
begin
if prevp = nil then
FirstWindowLookupRec := p^.Next
else
prevp^.Next := p^.Next;
if LastWindowLookupRec = p then
LastWindowLookupRec := prevp;
px := p;
p := p^.Next;
Dispose(px);
end
else
begin
prevp := p;
p := p^.Next;
end;
end;
function FindWindowByHandle(wh: TfpgWinHandle): TfpgWindowImpl;
var
p: PWindowLookupRec;
begin
p := FirstWindowLookupRec;
while p <> nil do
begin
if p^.w.WinHandle = wh then
begin
Result := p^.w;
Exit;
end;
p := p^.Next;
end;
{$IFDEF DEBUG}
writeln('GFX/X11: FindWindowByHandle failed to find <', IntToHex(wh, 9), '>');
{$ENDIF}
Result := nil;
end;
function FindWindowByBackupHandle(wh: TfpgWinHandle): TfpgWindowImpl;
var
p: PWindowLookupRec;
begin
p := FirstWindowLookupRec;
while p <> nil do
begin
if p^.w.FBackupWinHandle = wh then
begin
Result := p^.w;
Exit;
end;
p := p^.Next;
end;
{$IFDEF DEBUG}
writeln('GFX/X11: FindWindowByBackupHandle failed to find <', IntToHex(wh, 9), '>');
{$ENDIF}
Result := nil;
end;
function GetXEventName(Event: longint): string;
const
EventNames: array[2..34] of string = (
'KeyPress', 'KeyRelease', 'ButtonPress', 'ButtonRelease', 'MotionNotify',
'EnterNotify', 'LeaveNotify', 'FocusIn', 'FocusOut', 'KeymapNotify',
'Expose', 'GraphicsExpose', 'NoExpose', 'VisibilityNotify', 'CreateNotify',
'DestroyNotify', 'UnmapNotify', 'MapNotify', 'MapRequest', 'ReparentNotify',
'ConfigureNotify', 'ConfigureRequest', 'GravityNotify', 'ResizeRequest',
'CirculateNotify', 'CirculateRequest', 'PropertyNotify', 'SelectionClear',
'SelectionRequest', 'SelectionNotify', 'ColormapNotify', 'ClientMessage',
'MappingNotify');
begin
if (Event >= Low(EventNames)) and (Event <= High(EventNames)) then
Result := EventNames[Event]
else
Result := '#' + IntToStr(Event);
end;
// clipboard event
procedure ProcessSelection(var ev: TXEvent);
var
s: string;
actual: TAtom;
format: integer;
count, remaining: longword;
data: PChar;
begin
if ev.xselection._property > 0 then
begin
XGetWindowProperty(xapplication.Display, ev.xselection.requestor,
ev.xselection._property, 0, 16000,
TBool(false), // delete
0, // type
@actual, @format, @count, @remaining,
@data);
s := data;
fpgClipboard.FClipboardText := s;
XFree(data);
end
else
begin
fpgClipboard.FClipboardText := '';
end;
fpgClipboard.FWaitingForSelection := false;
end;
// clipboard event
procedure ProcessSelectionRequest(var ev: TXEvent);
var
e: TXSelectionEvent;
a: TAtom;
begin
e._type := SelectionNotify;
e.requestor := ev.xselectionrequest.requestor;
e.selection := ev.xselectionrequest.selection;
e.selection := xapplication.xia_clipboard;
e.target := ev.xselectionrequest.target;
e.time := ev.xselectionrequest.time;
e._property := ev.xselectionrequest._property;
if e.target = xapplication.xia_targets then
begin
a := XA_STRING;
XChangeProperty(xapplication.Display, e.requestor, e._property,
XA_ATOM, sizeof(TAtom)*8, 0, PByte(@a), sizeof(TAtom));
end
else
begin
XChangeProperty(xapplication.Display, e.requestor, e._property, e.target,
8, 0, PByte(@fpgClipboard.FClipboardText[1]), Length(fpgClipboard.FClipboardText));
end;
XSendEvent(xapplication.Display, e.requestor, false, 0, @e );
end;
// File utils
function ExtractTargetSymLinkPath(ALink: string): string;
begin
Result := fpReadLink(ALink);
end;
function FileIsSymlink(const AFilename: string): boolean;
begin
Result := (FpReadLink(AFilename) <> '');
end;
{ TfpgApplicationImpl }
function TfpgApplicationImpl.ConvertShiftState(AState: Cardinal): TShiftState;
begin
Result := [];
if (AState and Button1Mask) <> 0 then
Include(Result, ssLeft);
if (AState and Button2Mask) <> 0 then
Include(Result, ssMiddle);
if (AState and Button3Mask) <> 0 then
Include(Result, ssRight);
if (AState and ShiftMask) <> 0 then
Include(Result, ssShift);
if (AState and LockMask) <> 0 then
Include(Result, ssCaps);
if (AState and ControlMask) <> 0 then
Include(Result, ssCtrl);
if (AState and Mod1Mask) <> 0 then
Include(Result, ssAlt);
if (AState and Mod2Mask) <> 0 then
Include(Result, ssNum);
if (AState and Mod4Mask) <> 0 then
Include(Result, ssSuper);
if (AState and Mod5Mask) <> 0 then
Include(Result, ssScroll);
if (AState and (1 shl 13)) <> 0 then
Include(Result, ssAltGr);
end;
function TfpgApplicationImpl.KeySymToKeycode(KeySym: TKeySym): Word;
const
Table_20aX: array[$20a0..$20ac] of Word = (keyEcuSign, keyColonSign,
keyCruzeiroSign, keyFFrancSign, keyLiraSign, keyMillSign, keyNairaSign,
keyPesetaSign, keyRupeeSign, keyWonSign, keyNewSheqelSign, keyDongSign,
keyEuroSign);
Table_feXX: array[$fe50..$fe60] of Word = (keyDeadGrave, keyDeadAcute,
keyDeadCircumflex, keyDeadTilde, keyDeadMacron,keyDeadBreve,
keyDeadAbovedot, keyDeadDiaeresis, keyDeadRing, keyDeadDoubleacute,
keyDeadCaron, keyDeadCedilla, keyDeadOgonek, keyDeadIota,
keyDeadVoicedSound, keyDeadSemivoicedSound, keyDeadBelowdot);
Table_ff5X: array[$ff50..$ff58] of Word = (keyHome, keyLeft, keyUp, keyRight,
keyDown, keyPrior, keyNext, keyEnd, keyBegin);
Table_ff6X: array[$ff60..$ff6b] of Word = (keySelect, keyPrintScreen,
keyExecute, keyInsert, keyNIL, keyUndo, keyRedo, keyMenu, keyFind,
keyCancel, keyHelp, keyBreak);
Table_ff9X: array[$ff91..$ff9f] of Word = (keyPF1, keyPF2, keyPF3, keyPF4,
keyP7, keyP4, keyP8, keyP6, keyP2, keyP9, keyP3, keyP1, keyP5, keyP0,
keyPDecimal);
Table_ffeX: array[$ffe1..$ffee] of Word = (keyShiftL, keyShiftR, keyCtrlL,
keyCtrlR, keyCapsLock, keyShiftLock, keyMetaL, keyMetaR, keyAltL, keyAltR,
keySuperL, keySuperR, keyHyperL, keyHyperR);
begin
case KeySym of
0..Ord('a')-1, Ord('z')+1..$bf, $f7:
Result := KeySym;
Ord('a')..Ord('z'), $c0..$f6, $f8..$ff:
Result := KeySym - 32;
$20a0..$20ac: Result := Table_20aX[KeySym];
$fe20: Result := keyTab;
$fe50..$fe60: Result := Table_feXX[KeySym];
$ff08: Result := keyBackspace;
$ff09: Result := keyTab;
$ff0a: Result := keyLinefeed;
$ff0b: Result := keyClear;
$ff0d: Result := keyReturn;
$ff13: Result := keyPause;
$ff14: Result := keyScrollLock;
$ff15: Result := keySysRq;
$ff1b: Result := keyEscape;
$ff50..$ff58: Result := Table_ff5X[KeySym];
$ff60..$ff6b: Result := Table_ff6X[KeySym];
$ff7e: Result := keyModeSwitch;
$ff7f: Result := keyNumLock;
$ff80: Result := keyPSpace;
$ff89: Result := keyPTab;
$ff8d: Result := keyPEnter;
$ff91..$ff9f: Result := Table_ff9X[KeySym];
$ffaa: Result := keyPAsterisk;
$ffab: Result := keyPPlus;
$ffac: Result := keyPSeparator;
$ffad: Result := keyPMinus;
$ffae: Result := keyPDecimal;
$ffaf: Result := keyPSlash;
$ffb0..$ffb9: Result := keyP0 + KeySym - $ffb0;
$ffbd: Result := keyPEqual;
$ffbe..$ffe0: Result := keyF1 + KeySym - $ffbe;
$ffe1..$ffee: Result := Table_ffeX[KeySym];
$ffff: Result := keyDelete;
else
Result := keyNIL;
end;
{$IFDEF Debug}
if Result = keyNIL then
WriteLn('fpGFX/X11: Unknown KeySym: $', IntToHex(KeySym, 4));
{$ENDIF}
end;
function TfpgApplicationImpl.StartComposing(const Event: TXEvent): TKeySym;
var
l: integer;
begin
// Xutf8LookupString returns the size of FComposeBuffer in bytes.
l := Xutf8LookupString(InputContext, @Event.xkey, @FComposeBuffer[1],
SizeOf(FComposeBuffer) - 1, @Result, @FComposeStatus);
SetLength(FComposeBuffer, l);
end;
function TfpgApplicationImpl.DoGetFontFaceList: TStringList;
var
pfs: PFcFontSet;
ppat: PPFcPattern;
n: integer;
s: string;
pc: PChar;
begin
pfs := XftListFonts(Display, DefaultScreen, [FC_SCALABLE, FcTypeBool, 1, 0, FC_FAMILY, 0]);
if pfs = nil then
Exit; //==>
Result := TStringList.Create;
GetMem(pc, 128);
n := 0;
ppat := pfs^.fonts;
while n < pfs^.nfont do
begin
XftNameUnparse(ppat^, pc, 127); //XftNameUnparse does not free the name string!
s := pc;
Result.Add(s);
inc(PChar(ppat), sizeof(pointer));
inc(n);
end;
FreeMem(pc);
FcFontSetDestroy(pfs);
Result.Sort;
end;
constructor TfpgApplicationImpl.Create(const AParams: string);
begin
inherited Create(AParams);
FIsInitialized := False;
FDisplay := XOpenDisplay(PChar(aparams));
if FDisplay = nil then
raise Exception.Create('fpGUI-X11: Could not open the display. Is your X11 server running?');
Terminated := False;
DefaultScreen := XDefaultScreen(Display);
FRootWindow := XRootWindow(FDisplay, DefaultScreen);
DefaultBackground := XBlackPixel(FDisplay, DefaultScreen);
DefaultForeground := XWhitePixel(FDisplay, DefaultScreen);
DefaultVisual := XDefaultVisual(FDisplay, DefaultScreen);
DisplayDepth := XDefaultDepth(FDisplay, DefaultScreen);
//Writeln('display depth: ',DisplayDepth);
DefaultColorMap := XDefaultColorMap(FDisplay, DefaultScreen);
// Initialize atoms
xia_clipboard := XInternAtom(FDisplay, 'CLIPBOARD', longbool(0));
xia_targets := XInternAtom(FDisplay, 'TARGETS', longbool(0));
xia_motif_wm_hints := XInternAtom(FDisplay, '_MOTIF_WM_HINTS', longbool(0));
xia_wm_protocols := XInternAtom(FDisplay, 'WM_PROTOCOLS', longbool(0));
xia_wm_delete_window := XInternAtom(FDisplay, 'WM_DELETE_WINDOW', longbool(0));
netlayer := TNETWindowLayer.Create(FDisplay);
// for correct keyboard handling
InputMethod := XOpenIM(FDisplay, nil, nil, nil);
if InputMethod = nil then
Exit;
InputContext := XCreateIC(InputMethod, [XNInputStyle, XIMPreeditNothing or XIMStatusNothing, 0]);
if InputContext = nil then
Exit;
FIsInitialized := True;
xapplication := TfpgApplication(self);
end;
destructor TfpgApplicationImpl.Destroy;
begin
netlayer.Free;
XCloseDisplay(FDisplay);
inherited Destroy;
end;
function TfpgApplicationImpl.DoMessagesPending: boolean;
begin
Result := (XPending(display) > 0);
end;
function GetParentWindow(wh: TfpgWinHandle; var pw, rw: TfpgWinHandle): boolean;
var
rootw: TfpgWinHandle;
parentw: TfpgWinHandle;
childs: ^TfpgWinHandle;
cnum: longword;
begin
childs := nil;
if XQueryTree(xapplication.display, wh, @rootw, @parentw, @childs, @cnum) <> 0 then
begin
pw := parentw;
rw := rootw;
Result := True;
end
else
Result := False;
if childs <> nil then
XFree(childs);
end;
function GetDecorationWindow(wh: TfpgWinHandle): TfpgWinHandle;
var
lpw: TfpgWinHandle;
pw: TfpgWinHandle;
rw: TfpgWinHandle;
bok: boolean;
begin
pw := wh;
repeat
lpw := pw;
bok := GetParentWindow(lpw, pw, rw);
until (not bok) or (pw = rw);
if bok then
Result := lpw
else
Result := 0;
end;
function X11keycodeToScanCode(akeycode: word): word;
begin
case akeycode and $ff of
$09..$5B: Result := akeycode - 8;
$6C: Result := $11C; // numpad enter
$6D: Result := $11D; // right ctrl
$70: Result := $135; // numpad /
$62: Result := $148; // up arrow
$64: Result := $14B;
$66: Result := $14D;
$68: Result := $150; // down arrow
$6A: Result := $152;
$61: Result := $147;
$63: Result := $149;
$6B: Result := $153;
$67: Result := $14F;
$69: Result := $151;
$71: Result := $138;
else
Result := akeycode;
end;
end;
procedure TfpgApplicationImpl.DoWaitWindowMessage(atimeoutms: integer);
var
ev: TXEvent;
NewEvent: TXevent;
i: integer;
r: integer;
blockmsg: boolean;
w: TfpgWindowImpl;
ew: TfpgWindowImpl;
kwg: TfpgWidget;
wh: TfpgWinHandle;
wa: TXWindowAttributes;
mcode: integer;
msgp: TfpgMessageParams;
rfds: baseunix.TFDSet;
xfd: integer;
KeySym: TKeySym;
Popup: TfpgWidget;
status: TStatus;
needToWait: boolean;
// debug purposes only
procedure PrintKeyEvent(const event: TXEvent);
var
keysym: TKeySym;
compose_status: TXComposeStatus;
length: integer;
s: string[10];
begin
case event._type of
X.KeyPress:
begin
write('*** KeyPress ');
end;
X.KeyRelease:
begin
write('*** KeyRelease ');
end;
else
begin
writeln('not a key event ');
end;
end;
length := Xutf8LookupString(InputContext, @event.xkey, @s[1], 9, @keysym, @compose_status);
SetLength(s, length);
if((length > 0) and (length <=9)) then
writeln('result of xlookupstring [' + s + ']');
writeln(Format('*** keysym [%s] ', [XKeysymToString(keysym)]));
end;
// Debug info only
procedure ReportLostWindow(const event: TXEvent);
begin
{$IFDEF DEBUG}
writeln('fpGFX/X11: ', GetXEventName(event._type), ' can''t find <',
IntToHex(event.xany.window, 9), '>');
{$ENDIF}
end;
begin
xfd := XConnectionNumber(display);
XFlush(display);
(*
repeat
if (atimeoutms >= 0) and (XPending(display) < 1) then { there are no X messages }
begin
if Assigned(FOnIdle) then
OnIdle(self);
// Some event is waiting for the given timeout.
// This Select handles only the first 256 file descriptors.
// Poll would be better but FPC has no official poll interface (if I'm right)
fpFD_ZERO(rfds);
fpFD_SET(xfd, rfds);
r := fpSelect(xfd + 1, @rfds, nil, nil, {atimeoutms} 50);
if r < 1 then
Exit; // no event received.
end;
XNextEvent(display, @ev);
until (not XFilterEvent(@ev, X.None));
*)
needToWait := True;
if XPending(display) > 0 then // We have a X message to process
begin
XNextEvent(display, @ev);
needToWait := False;
end;
if needToWait then // No X messages to process (we are idle). So do a timeout wait
begin
if Assigned(FOnIdle) then
OnIdle(self);
fpFD_ZERO(rfds);
fpFD_SET(xfd, rfds);
r := fpSelect(xfd + 1, @rfds, nil, nil, {atimeoutms} 50);
if r <> 0 then // We got a X event or the timeout happened
XNextEvent(display, @ev)
else
Exit; // nothing further to do here!
end;
// if the event filter returns true then it ate the message
if Assigned(FEventFilter) and FEventFilter(ev) then
exit; // no more processing required for that event
blockmsg := False;
fillchar(msgp, sizeof(msgp), 0);
// According to a comment in X.h, the valid event types start with 2!
if ev._type < 2 then
exit;
Popup := PopupListFirst;
{$IFDEF DEBUG}
w := FindWindowByHandle(ev.xany.window);
if not Assigned(w) then
WriteLn('Event ',GetXEventName(ev._type),'(', ev._type,') window: ', IntToHex(ev.xany.window,7))
else
WriteLn('Event ',GetXEventName(ev._type),'(', ev._type,') window: ', IntToHex(ev.xany.window,7), ' name:', w.Name);
// PrintKeyEvent(ev); { debug purposes only }
{$ENDIF}
case ev._type of
X.KeyPress,
X.KeyRelease:
begin
if ev._type = X.KeyPress then
begin
KeySym := StartComposing(ev);
{ TODO -oGG : Move some code into the case statement }
case FComposeStatus of
XLookupNone:
begin
// writeln('KeyPress - XLookupNone');
Exit;
end;
XLookupChars:
begin
// writeln('KeyPress - XLookupChars');
// do nothing
end;
XLookupKeySymVal:
begin
// writeln('KeyPress - XLookupKeySymVal');
end;
XLookupBoth:
begin
// writeln('KeyPress - XLookupBoth');
end;
end;
FLastKeySym := KeySym // save it for KeyRelease event
end
else
KeySym := FLastKeySym; // restore saved KeySym
msgp.keyboard.keycode := KeySymToKeycode(KeySym);
msgp.keyboard.shiftstate := ConvertShiftState(ev.xkey.state);
// By default X11 sends keyboard event to window under mouse cursor.
// We need to get the corrected "focused" widget instead.
kwg := FindKeyboardFocus;
if kwg <> nil then
w := kwg
else
begin
{$IFDEF DEBUG}
writeln('ERR: We couldn''t find keyboard focused window. Using event window instead!');
{$ENDIF}
w := FindWindowByHandle(ev.xkey.window);
if not Assigned(w) then
ReportLostWindow(ev);
end;
if ev._type = X.KeyPress then
begin
fpgPostMessage(nil, w, FPGM_KEYPRESS, msgp);
if (ev.xkey.state and (ControlMask or Mod1Mask)) = 0 then
begin
for i := 1 to UTF8Length(FComposeBuffer) do
begin
msgp.keyboard.keychar := UTF8Copy(FComposeBuffer, i, 1);
fpgPostMessage(nil, w, FPGM_KEYCHAR, msgp);
end;
end;
end { if }
else if ev._type = X.KeyRelease then
fpgPostMessage(nil, w, FPGM_KEYRELEASE, msgp);
end;
X.ButtonPress,
X.ButtonRelease:
begin
msgp.mouse.x := ev.xbutton.x;
msgp.mouse.y := ev.xbutton.y;
msgp.mouse.Buttons := ev.xbutton.button;
msgp.mouse.shiftstate := ConvertShiftState(ev.xbutton.state);
w := FindWindowByHandle(ev.xbutton.window);
if not Assigned(w) then
ReportLostWindow(ev);
{ This closes popup windows when you click the mouse elsewhere }
if ev._type = X.ButtonPress then
begin
if (Popup <> nil) then
begin
ew := w;
while (w <> nil) and (w.Parent <> nil) do
w := TfpgWindowImpl(w.Parent);
if (w <> nil) and (PopupListFind(w.WinHandle) = nil) and
(not PopupDontCloseWidget(TfpgWidget(ew))) then
begin
ClosePopups;
end;
end;
end;
w := FindWindowByHandle(ev.xbutton.window); // restore w
if xapplication.TopModalForm <> nil then
begin
ew := TfpgWindowImpl(WidgetParentForm(TfpgWidget(w)));
if (ew <> nil) and (xapplication.TopModalForm <> ew) and (waUnblockableMessages in ew.WindowAttributes = False) then
blockmsg := true;
end;
// Is message blocked by a modal form?
if not blockmsg then
begin
if (ev.xbutton.button >= 4) and (ev.xbutton.button <= 7) then // mouse wheel
begin
// generate scroll events:
if ev._type = X.ButtonPress then
begin
if ev.xbutton.button = Button4 then
i := -1
else
i := 1;
// Check for other mouse wheel messages in the queue
while XCheckTypedWindowEvent(display, ev.xbutton.window, X.ButtonPress, @NewEvent) do
begin
if NewEvent.xbutton.Button = 4 then
Dec(i)
else if NewEvent.xbutton.Button = 5 then
Inc(i)
else
begin
XPutBackEvent(display, @NewEvent);
break;
end;
end;
msgp.mouse.delta := i;
fpgPostMessage(nil, w, FPGM_SCROLL, msgp);
end;
end
else
begin
if ev._type = X.ButtonRelease then
begin
{$IFDEF DEBUG}
writeln('**** PostMessage MouseUp ', w.ClassName, ' - ', w.Name);
{$ENDIF}
mcode := FPGM_MOUSEUP;
end
else
begin
{$IFDEF DEBUG}
writeln('**** PostMessage MouseDown ', w.ClassName, ' - ', w.Name);
{$ENDIF}
mcode := FPGM_MOUSEDOWN;
end;
fpgPostMessage(nil, w, mcode, msgp);
end; { if/else }
end; { if not blocking }
end;
X.Expose:
begin
repeat
//
until not XCheckTypedWindowEvent(display, ev.xexpose.window, X.Expose, @ev);
if ev.xexpose.count = 0 then
begin
fpgPostMessage(nil, FindWindowByHandle(ev.xexpose.window), FPGM_PAINT);
end;
end;
X.MotionNotify:
begin
repeat
//
until not XCheckTypedWindowEvent(display, ev.xmotion.window, X.MotionNotify, @ev);
w := FindWindowByHandle(ev.xmotion.window);
if not Assigned(w) then
ReportLostWindow(ev);
if xapplication.TopModalForm <> nil then
begin
ew := TfpgWindowImpl(WidgetParentForm(TfpgWidget(w)));
if (ew <> nil) and (xapplication.TopModalForm <> ew) and (waUnblockableMessages in ew.WindowAttributes = False) then
blockmsg := true;
end;
if not blockmsg then
begin
msgp.mouse.x := ev.xmotion.x;
msgp.mouse.y := ev.xmotion.y;
msgp.mouse.Buttons := (ev.xmotion.state and $FF00) shr 8;
msgp.mouse.shiftstate := ConvertShiftState(ev.xmotion.state);
fpgPostMessage(nil, FindWindowByHandle(ev.xmotion.window), FPGM_MOUSEMOVE, msgp);
end;
end;
// message blockings for modal windows
X.ClientMessage:
begin
w := FindWindowByBackupHandle(ev.xclient.window);
if not Assigned(w) then
ReportLostWindow(ev);
// WM_PROTOCOLS message
if Assigned(w) and (ev.xclient.message_type = xia_wm_protocols) then
begin
//WriteLn(XGetAtomName(FDisplay, TAtom(ev.xclient.data.l[0])));
if (ev.xclient.data.l[0] = netlayer.NetAtom[naWM_PING]) then
begin
// always respond to pings or the wm will kill us
netlayer.WindowReplyToPING(w.FWinHandle, @ev.xclient);
end
else if ev.xclient.data.l[0] = xia_wm_delete_window then
begin
if xapplication.TopModalForm <> nil then
begin
// This is ugly!!!!!!!!!!!!!!!
ew := TfpgWindowImpl(WidgetParentForm(TfpgWidget(w)));
if (ew <> nil) and (xapplication.TopModalForm <> ew) and (waUnblockableMessages in ew.WindowAttributes = False) then
blockmsg := true;
end;
if not blockmsg then
fpgPostMessage(nil, FindWindowByHandle(ev.xclient.window), FPGM_CLOSE);
end;
end; // WM_PROTOCOLS
end;
X.ConfigureNotify:
begin
repeat
//
until not XCheckTypedWindowEvent(display, ev.xconfigure.window, ConfigureNotify, @ev);
msgp.rect.Left := ev.xconfigure.x;
msgp.rect.Top := ev.xconfigure.y;
msgp.rect.Width := ev.xconfigure.Width;
msgp.rect.Height := ev.xconfigure.Height;
w := FindWindowByBackupHandle(ev.xconfigure.window);
if not Assigned(w) then
ReportLostWindow(ev);
if w <> nil then
begin
if w.FWindowType <> wtChild then
begin
wh := GetDecorationWindow(ev.xconfigure.window);
if wh > 0 then
begin
XGetWindowAttributes(display, wh, @wa);
msgp.rect.Left := wa.x;
msgp.rect.Top := wa.y;
end;
end;
if (w.FWidth <> msgp.rect.Width) or (w.FHeight <> msgp.rect.Height) then
fpgPostMessage(nil, w, FPGM_RESIZE, msgp);
if (w.FLeft <> msgp.rect.Left) or (w.FTop <> msgp.rect.Top) then
fpgPostMessage(nil, w, FPGM_MOVE, msgp);
end;
end;
X.SelectionNotify:
begin
ProcessSelection(ev);
end;
X.SelectionRequest:
begin
ProcessSelectionRequest(ev);
end;
X.SelectionClear:
begin
{ TODO : Not sure if I am handling this correctly? }
if ev.xselectionclear.selection = xia_clipboard then
begin
fpgClipboard.FClipboardText := '';
Exit;
end;
end;
X.FocusIn:
fpgPostMessage(nil, FindWindowByHandle(ev.xfocus.window), FPGM_ACTIVATE);
X.FocusOut:
fpgPostMessage(nil, FindWindowByHandle(ev.xfocus.window), FPGM_DEACTIVATE);
X.EnterNotify:
fpgPostMessage(nil, FindWindowByHandle(ev.xcrossing.window), FPGM_MOUSEENTER);
X.LeaveNotify:
fpgPostMessage(nil, FindWindowByHandle(ev.xcrossing.window), FPGM_MOUSEEXIT);
X.MapNotify:
begin
w := FindWindowByHandle(ev.xmap.window);
if w <> nil then
Include(w.FWinFlags, xwsfMapped);
end;
X.UnmapNotify:
begin
// special case which uses a different find window method
w := FindWindowByBackupHandle(ev.xunmap.window);
if not Assigned(w) then
ReportLostWindow(ev)
else
Exclude(w.FWinFlags, xwsfMapped);
end;
X.DestroyNotify:
begin
// special case which uses a different find window method
w := FindWindowByBackupHandle(ev.xdestroywindow.window);
if not Assigned(w) then
ReportLostWindow(ev)
else
RemoveWindowLookup(TfpgWindowImpl(w));
end;
X.GraphicsExpose,
X.NoExpose:
begin
// writeln('got a GraphicsExpose or NoExpose event');
{ If this application calls XCopyArea or XCopyPlane
and the graphics_exposures member of the GC is
True and the source is a window, these events may
be generated; handle GraphicsExpose like Expose }
end;
X.ReparentNotify:
begin
// We are not interrested in this event yet
end;
else
WriteLn('fpGFX/X11: Unhandled X11 event received: ', GetXEventName(ev._type));
end;
end;
procedure TfpgApplicationImpl.DoFlush;
begin
XFlush(FDisplay);
end;
function TfpgApplicationImpl.GetScreenWidth: TfpgCoord;
var
wa: TXWindowAttributes;
begin
XGetWindowAttributes(FDisplay, RootWindow, @wa);
Result := wa.Width;
end;
function TfpgApplicationImpl.GetScreenHeight: TfpgCoord;
var
wa: TXWindowAttributes;
begin
XGetWindowAttributes(FDisplay, RootWindow, @wa);
Result := wa.Height;
end;
function TfpgApplicationImpl.Screen_dpi_x: integer;
var
mm: integer;
begin
// 25.4 is millimeters per inch
mm := 0;
mm := DisplayWidthMM(Display, DefaultScreen);
if mm > 0 then
Result := Round((GetScreenWidth * 25.4) / mm)
else
Result := 96; // seems to be a well known default. :-(
end;
function TfpgApplicationImpl.Screen_dpi_y: integer;
var
mm: integer;
begin
// 25.4 is millimeters per inch
mm := 0;
mm := DisplayHeightMM(Display, DefaultScreen);
if mm > 0 then
Result := Round((GetScreenHeight * 25.4) / mm)
else
Result := Screen_dpi_x; // same as width
end;
function TfpgApplicationImpl.Screen_dpi: integer;
begin
Result := Screen_dpi_y;
{$IFDEF DEBUG}
writeln('Display width in mm: ', DisplayWidthMM(Display, DefaultScreen));
writeln('Display height in mm: ', DisplayHeightMM(Display, DefaultScreen));
writeln('Display dpi: ', Result);
{$ENDIF}
end;
{ TfpgWindowImpl }
procedure TfpgWindowImpl.DoAllocateWindowHandle(AParent: TfpgWindowBase);
var
pwh: TfpgWinHandle;
wh: TfpgWinHandle;
lmwh: TfpgWinHandle;
attr: TXSetWindowAttributes;
mask: longword;
hints: TXSizeHints;
IconPixmap: TPixmap;
WMHints: PXWMHints;
prop: TAtom;
mwmhints: TMWMHints;
begin
if HandleIsValid then
Exit; //==>
if AParent <> nil then
pwh := TfpgWindowImpl(AParent).WinHandle
else
pwh := xapplication.RootWindow;
FillChar(attr, sizeof(attr), 0);
mask := 0;
if (FWindowType in [wtPopup]) or (waX11SkipWMHints in FWindowAttributes) then
begin
attr.Override_Redirect := TBool(True);
mask := CWOverrideRedirect;
end;
AdjustWindowStyle;
wh := XCreateWindow(xapplication.Display, pwh,
FLeft, FTop, FWidth, FHeight, 0,
CopyFromParent,
InputOutput,
xapplication.DefaultVisual,
mask, @attr);
FWinHandle := wh;
FBackupWinHandle := wh;
// so newish window manager can close unresponsive programs
if AParent = nil then // is a toplevel window
begin
IconPixMap := XCreateBitmapFromData(fpgApplication.Display, FWinHandle,
@IconBitmapBits, IconBitmapWidth, IconBitmapHeight);
WMHints := XAllocWMHints;
WMHints^.icon_pixmap := IconPixmap;
WMHints^.flags := IconPixmapHint;
// XSetWMProperties(fpgApplication.Display, FWinHandle, nil, nil, nil, 0, nil, nil, nil);
XSetWMProperties(fpgApplication.Display, FWinHandle, nil, nil, nil, 0, nil, WMHints, nil);
fpgApplication.netlayer.WindowSetPID(FWinHandle, GetProcessID);
fpgApplication.netlayer.WindowSetSupportPING(FWinHandle);
XFree(WMHints);
end;
hints.flags := 0;
if not (waAutoPos in FWindowAttributes) then
hints.flags := hints.flags or PPosition;
if waScreenCenterPos in FWindowAttributes then
begin
hints.flags := hints.flags or PPosition;
FLeft := (xapplication.ScreenWidth - FWidth) div 2;
FTop := (xapplication.ScreenHeight - FHeight) div 2;
DoMoveWindow(FLeft, FTop);
end;
if (FWindowType <> wtChild) and (waSizeable in FWindowAttributes) then
begin
hints.flags := hints.flags or PMinSize;
hints.min_width := FMinWidth;
hints.min_height := FMinHeight;
end
else
begin
hints.flags := hints.flags or PMinSize or PMaxSize;
hints.min_width := FWidth;
hints.min_height := FHeight;
hints.max_width := FWidth;
hints.max_height := FHeight;
end;
XSetWMNormalHints(xapplication.display, FWinHandle, @hints);
if FWindowType <> wtChild then
// send close event instead of quiting the whole application...
fpgApplication.netlayer.WindowAddProtocol(FWinHandle, xapplication.xia_wm_delete_window);
// for modal windows, this is necessary
if FWindowType = wtModalForm then
begin
if Parent = nil then
begin
lmwh := 0;
if fpgApplication.PrevModalForm <> nil then
lmwh := TfpgWindowImpl(fpgApplication.PrevModalForm).WinHandle
else if fpgApplication.MainForm <> nil then
lmwh := TfpgWindowImpl(fpgApplication.MainForm).WinHandle;
if lmwh <> 0 then
begin
XSetTransientForHint(xapplication.display, FWinHandle, lmwh);
fpgApplication.netlayer.WindowSetModal(FWinHandle, True);
end;
end;
end;
if (FWindowType = wtPopup) and (waStayOnTop in FWindowAttributes) then
// we have a Splash screen
fpgApplication.netlayer.WindowSetType(FWinHandle, [nwtSplash]);
// process Borderless forms
if ((FWindowType = wtWindow) or (FWindowType = wtModalForm)) and (waBorderless in FWindowAttributes) and not (waX11SkipWMHints in FWindowAttributes) then
begin
prop := X.None;
prop := XInternAtom(xapplication.display, '_MOTIF_WM_INFO', longbool(0));
if prop = X.None then
begin
writeln('Window Manager does not support MWM hints. Bypassing window manager control for borderless window.');
// Set Override Redirect here!
mwmhints.flags := 0;
end
else
begin
mwmhints.flags := MWM_HINTS_DECORATIONS;
mwmhints.decorations := 0;
if xapplication.xia_motif_wm_hints <> X.None then
begin
prop := xapplication.xia_motif_wm_hints;
XChangeProperty(xapplication.display, FWinHandle, prop, prop, 32, PropModeReplace, @mwmhints, PROP_MWM_HINTS_ELEMENTS);
end;
end;
end;
{ TODO : We could optimise this for non-focusable widgets }
XSelectInput(xapplication.Display, wh, KeyPressMask or KeyReleaseMask or
ButtonPressMask or ButtonReleaseMask or
EnterWindowMask or LeaveWindowMask or
ButtonMotionMask or PointerMotionMask or
ExposureMask or FocusChangeMask or
StructureNotifyMask);
SetWindowParameters;
AddWindowLookup(self);
end;
procedure TfpgWindowImpl.DoReleaseWindowHandle;
//var
// lCallTrace: IInterface;
begin
// lCallTrace := PrintCallTrace(Classname, 'DoReleaseWindowHandle: ' + Name);
if HandleIsValid then
begin
// PrintCallTraceDbgLn('XDestroyWindow');
XDestroyWindow(xapplication.Display, FWinHandle);
end
else
begin
// PrintCallTraceDbgLn(' RemoveWindowLookup');
RemoveWindowLookup(self);
end;
FWinHandle := 0;
end;
procedure TfpgWindowImpl.DoRemoveWindowLookup;
begin
// PrintCallTraceDbgLn('RemoveWindowLookup ' + Name + ' [' + Classname + ']');
RemoveWindowLookup(self);
end;
procedure TfpgWindowImpl.DoSetWindowVisible(const AValue: Boolean);
begin
if AValue then
begin
if not HandleIsValid then
AllocateWindowHandle;
XMapWindow(xapplication.Display, FWinHandle);
Include(FWinFlags, xwsfMapped);
// Fullscreen can only be set visible (mapped) windows.
if waFullScreen in FWindowAttributes then
fpgApplication.netlayer.WindowSetFullscreen(FWinHandle, True);
end
else
begin
if HandleIsValid and (xwsfMapped in FWinFlags) then
XUnmapWindow(xapplication.Display, FWinHandle);
end;
end;
function TfpgWindowImpl.HandleIsValid: boolean;
begin
Result := (FWinHandle > 0);
end;
procedure TfpgWindowImpl.DoMoveWindow(const x: TfpgCoord; const y: TfpgCoord);
begin
if HandleIsValid then
XMoveWindow(xapplication.display, FWinHandle, x, y);
end;
function TfpgWindowImpl.DoWindowToScreen(ASource: TfpgWindowBase; const AScreenPos: TPoint): TPoint;
var
dx: integer;
dy: integer;
cw: TfpgWinHandle;
begin
if not TfpgWindowImpl(ASource).HandleIsValid then
Exit; //==>
XTranslateCoordinates(xapplication.display, TfpgWindowImpl(ASource).WinHandle,
XDefaultRootWindow(xapplication.display), AScreenPos.X, AScreenPos.Y, @dx, @dy, @cw);
Result.X := dx;
Result.Y := dy;
end;
procedure TfpgWindowImpl.DoUpdateWindowPosition;
var
w: longword;
h: longword;
begin
if FWidth > 1 then
w := FWidth
else
w := 1;
if FHeight > 1 then
h := FHeight
else
h := 1;
if FWinHandle > 0 then
XMoveResizeWindow(xapplication.display, FWinHandle, FLeft, FTop, w, h);
end;
procedure TfpgWindowImpl.DoSetMouseCursor;
var
xc: TCursor;
shape: integer;
begin
if not HasHandle then
Exit; //==>
case FMouseCursor of
mcSizeEW: shape := XC_sb_h_double_arrow;
mcSizeNS: shape := XC_sb_v_double_arrow;
mcIBeam: shape := XC_xterm;
mcSizeNWSE: shape := XC_bottom_right_corner;
mcSizeNESW: shape := XC_bottom_left_corner;
mcSizeSWNE: shape := XC_top_right_corner;
mcSizeSENW: shape := XC_top_left_corner;
mcMove: shape := XC_fleur;
mcCross: shape := XC_crosshair;
mcHourGlass: shape := XC_watch;
mcHand: shape := XC_hand2;
else
shape := XC_left_ptr; //XC_arrow;
end;
xc := XCreateFontCursor(xapplication.Display, shape);
XDefineCursor(xapplication.Display, FWinHandle, xc);
XFreeCursor(xapplication.Display, xc);
end;
procedure TfpgWindowImpl.DoSetWindowTitle(const ATitle: string);
var
tp: TXTextProperty;
begin
if FWinHandle <= 0 then
Exit;
fpgApplication.netlayer.WindowSetName(FWinHandle, PChar(ATitle));
// Required for titles to work in IceWM. The above netlayer doesn't do the trick.
tp.value := PCUChar(ATitle);
tp.encoding := XA_WM_NAME;
tp.format := 8;
tp.nitems := UTF8Length(ATitle);
XSetWMName(xapplication.Display, FWinHandle, @tp);
XStoreName(xapplication.Display, FWinHandle, PChar(ATitle));
XSetIconName(xapplication.Display, FWinHandle, PChar(ATitle));
XSetWMIconName(xapplication.Display, FWinHandle, @tp);
end;
constructor TfpgWindowImpl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWinHandle := 0;
FBackupWinHandle := 0;
end;
procedure TfpgWindowImpl.ActivateWindow;
begin
XSetInputFocus(xapplication.Display, FWinHandle, RevertToParent, CurrentTime);
end;
procedure TfpgWindowImpl.CaptureMouse;
begin
XGrabPointer(xapplication.Display, FWinHandle,
TBool(False),
ButtonPressMask or ButtonReleaseMask or ButtonMotionMask or PointerMotionMask
or EnterWindowMask or LeaveWindowMask,
GrabModeAsync,
GrabModeAsync,
None,
0,
CurrentTime
);
end;
procedure TfpgWindowImpl.ReleaseMouse;
begin
XUngrabPointer(xapplication.display, CurrentTime);
end;
procedure TfpgWindowImpl.SetFullscreen(AValue: Boolean);
begin
inherited SetFullscreen(AValue);
fpgApplication.netlayer.WindowSetFullscreen(FWinHandle, AValue);
end;
{ TfpgFontResourceImpl }
function TfpgFontResourceImpl.DoGetTextWidthClassic(const txt: string): integer;
var
extents: TXGlyphInfo;
begin
XftTextExtentsUTF8(xapplication.display, FFontData, PChar(txt), Length(txt), extents);
Result := extents.xOff;
end;
function TfpgFontResourceImpl.DoGetTextWidthWorkaround(const txt: string): integer;
var
extents: TXGlyphInfo;
ch: string;
dpos: integer;
begin
Result := 0;
dpos := 1;
while dpos <= Length(txt) do
begin
dpos := UTF8CharAtByte(txt, dpos, ch);
XftTextExtentsUTF8(xapplication.display, FFontData, PChar(ch), Length(ch), extents);
Inc(Result, extents.xOff);
end;
end;
constructor TfpgFontResourceImpl.Create(const afontdesc: string);
begin
FFontData := XftFontOpenName(xapplication.display, xapplication.DefaultScreen, PChar(afontdesc));
end;
destructor TfpgFontResourceImpl.Destroy;
begin
if HandleIsValid then
XftFontClose(xapplication.Display, FFontData);
inherited;
end;
function TfpgFontResourceImpl.HandleIsValid: boolean;
begin
Result := (FFontData <> nil);
end;
function TfpgFontResourceImpl.GetAscent: integer;
begin
Result := FFontData^.ascent;
end;
function TfpgFontResourceImpl.GetDescent: integer;
begin
Result := FFontData^.descent;
end;
function TfpgFontResourceImpl.GetHeight: integer;
begin
// Do NOT use FFontData^.height as it isn't as accurate
Result := GetAscent + GetDescent;
end;
function TfpgFontResourceImpl.GetTextWidth(const txt: string): integer;
begin
if length(txt) < 1 then
begin
Result := 0;
Exit;
end;
// Xft uses smallint to return text extent information, so we have to
// check if the text width is small enough to fit into smallint range
if DoGetTextWidthClassic('W') * Length(txt) < High(smallint) then
Result := DoGetTextWidthClassic(txt)
else
Result := DoGetTextWidthWorkaround(txt);
end;
{ TfpgCanvasImpl }
constructor TfpgCanvasImpl.Create;
begin
inherited;
FDrawing := False;
FDrawWindow := nil;
FBufferPixmap := 0;
FDrawHandle := 0;
Fgc := nil;
FXftDraw := nil;
FClipRegion := nil;
end;
destructor TfpgCanvasImpl.Destroy;
begin
if FDrawing then
DoEndDraw;
FreeAndNil(FBufferFreeTimer);
TryFreePixmap;
inherited Destroy;
end;
procedure TfpgCanvasImpl.DoBeginDraw(awin: TfpgWindowBase; buffered: boolean);
var
x: integer;
y: integer;
rw: TXID;
d: TXID;
w: longword;
h: longword;
bw: longword;
pmw: longword;
pmh: longword;
GcValues: TXGcValues;
begin
if Assigned(TfpgWindowImpl(awin)) then
begin
// This occurs every now and again with TfpgMemo and InvertCaret painting!
// Investigate this.
if not TfpgWindowImpl(awin).HasHandle then
raise Exception.Create('Window doesn''t have a Handle');
end;
XGetGeometry(xapplication.display, TfpgWindowImpl(awin).FWinHandle, @rw, @x, @y, @w, @h, @bw, @d);
if FDrawing and buffered and (FBufferPixmap > 0) then
if FBufferPixmap > 0 then
begin
// check if the dimensions are ok
XGetGeometry(xapplication.display, FBufferPixmap, @rw, @x, @y, @pmw, @pmh, @bw, @d);
if (pmw <> w) or (pmh <> h) then
DoEndDraw;
end;
if not FDrawing then
begin
FDrawWindow := TfpgWindowImpl(awin);
if buffered then
begin
if (FBufferPixmap = 0)
or (FastDoubleBuffer = False)
or (FastDoubleBuffer and (w > FPixWidth) or (h > FPixHeight))
or ((FastDoubleBuffer = False) and ((w <> FPixWidth) or (h <> FPixHeight)))
then
begin
if FastDoubleBuffer and ((w > FPixWidth) or (h > FPixHeight)) then
begin
FPixHeight := h + 30;
FPixWidth := w + 30;
end
else begin
FPixHeight := h;
FPixWidth := w;
end;
TryFreePixmap;
FBufferPixmap := XCreatePixmap(xapplication.display, FDrawWindow.FWinHandle, FPixWidth, FPixHeight, xapplication.DisplayDepth);
end;
if FastDoubleBuffer then
begin
// Rapid paint events reuse the double buffer which resests a delay
// After the delay the double buffer is freed, letting the OS use video
// memory if needed.
// Things like scrolling and resizing are fast
// Reset the timers next trigger
if FBufferFreeTimer = nil then
begin
FBufferFreeTimer := TfpgTimer.Create(500);
TfpgTimer(FBufferFreeTimer).OnTimer := @BufferFreeTimer;
end
else
TfpgTimer(FBufferFreeTimer).Enabled := False;
TfpgTimer(FBufferFreeTimer).Enabled := True;
end;
FDrawHandle := FBufferPixmap;
end
else
begin
TryFreePixmap;
FDrawHandle := FDrawWindow.FWinHandle;
end;
Fgc := XCreateGc(xapplication.display, FDrawHandle, 0, @GcValues);
// CapNotLast is so we get the same behavior as Windows. See documentation for more details.
XSetLineAttributes(xapplication.display, Fgc, 0, LineSolid, CapNotLast, JoinMiter);
FXftDraw := XftDrawCreate(xapplication.display, FDrawHandle,
XDefaultVisual(xapplication.display, xapplication.DefaultScreen),
XDefaultColormap(xapplication.display, xapplication.DefaultScreen));
FClipRegion := XCreateRegion;
end;
FDrawing := True;
end;
procedure TfpgCanvasImpl.DoPutBufferToScreen(x, y, w, h: TfpgCoord);
var
cgc: TfpgGContext;
GcValues: TXGcValues;
begin
if FBufferPixmap > 0 then
begin
cgc := XCreateGc(xapplication.display, FBufferPixmap, 0, @GcValues);
XCopyArea(xapplication.Display, FBufferPixmap, FDrawWindow.FWinHandle, cgc, x, y, w, h, x, y);
XFreeGc(xapplication.display, cgc);
end;
end;
procedure TfpgCanvasImpl.DoEndDraw;
begin
if FDrawing then
begin
XDestroyRegion(FClipRegion);
XftDrawDestroy(FXftDraw);
XFreeGc(xapplication.display, Fgc);
if FastDoubleBuffer = False then
TryFreePixmap;
FDrawing := False;
FDrawWindow := nil;
end;
end;
function TfpgCanvasImpl.GetPixel(X, Y: integer): TfpgColor;
var
Image: PXImage;
Pixel: Cardinal;
x_Color: TXColor;
begin
Result := 0;
Image := XGetImage(xapplication.display, FDrawHandle, X, Y, 1, 1, $FFFFFFFF, ZPixmap);
if Image = nil then
raise Exception.Create('fpGFX/X11: Invalid XImage');
try
Pixel := XGetPixel(Image, 0, 0);
x_Color.pixel := Pixel;
XQueryColor(xapplication.display, xapplication.DefaultColorMap, @x_Color);
Result := TfpgColor(((x_Color.red and $00FF) shl 16) or
((x_Color.green and $00FF) shl 8) or
(x_Color.blue and $00FF));
finally
XDestroyImage(Image);
end;
end;
procedure TfpgCanvasImpl.SetPixel(X, Y: integer; const AValue: TfpgColor);
var
oldColor: TfpgColor;
begin
oldColor := Color;
SetColor(AValue);
XDrawPoint(xapplication.display, FDrawHandle, Fgc, X, Y);
SetColor(oldColor);
end;
procedure TfpgCanvasImpl.DoDrawArc(x, y, w, h: TfpgCoord; a1, a2: Extended);
begin
XDrawArc(xapplication.display, FDrawHandle, Fgc, x, y, w-1, h-1,
Trunc(64 * a1), Trunc(64 * a2));
end;
procedure TfpgCanvasImpl.DoFillArc(x, y, w, h: TfpgCoord; a1, a2: Extended);
begin
XFillArc(xapplication.display, FDrawHandle, Fgc, x, y, w, h,
Trunc(64 * a1), Trunc(64 * a2));
end;
procedure TfpgCanvasImpl.DoDrawPolygon(Points: fpg_base.PPoint; NumPts: Integer; Winding: boolean);
var
PointArray: PXPoint;
i: integer;
begin
{ convert TPoint to TXPoint }
GetMem(PointArray, SizeOf(TXPoint)*(NumPts+1)); // +1 for return line
for i := 0 to NumPts-1 do
begin
PointArray[i].x := Points[i].x;
PointArray[i].y := Points[i].y;
end;
XFillPolygon(xapplication.display, FDrawHandle, Fgc, PointArray, NumPts, CoordModeOrigin, X.Complex);
if PointArray <> nil then
FreeMem(PointArray);
end;
procedure TfpgCanvasImpl.BufferFreeTimer(Sender: TObject);
begin
{$IFDEF DEBUG}
WriteLn('fpGFX/X11: Freeing Buffer w=', FPixWidth, ' h=', FPixHeight);
{$ENDIF}
TryFreePixmap;
FreeAndNil(FBufferFreeTimer);
end;
procedure TfpgCanvasImpl.TryFreePixmap;
begin
if FBufferPixmap > 0 then
XFreePixmap(xapplication.Display, FBufferPixmap);
FBufferPixmap := 0;
end;
procedure TfpgCanvasImpl.DoSetFontRes(fntres: TfpgFontResourceBase);
begin
if fntres = nil then
Exit; //==>
FCurFontRes := TfpgFontResourceImpl(fntres);
end;
procedure TfpgCanvasImpl.DoSetTextColor(cl: TfpgColor);
begin
SetXftColor(cl, FColorTextXft);
end;
procedure TfpgCanvasImpl.DoSetColor(cl: TfpgColor);
begin
XSetForeGround(xapplication.display, Fgc, fpgColorToX(cl));
end;
procedure TfpgCanvasImpl.DoSetLineStyle(awidth: integer; astyle: TfpgLineStyle);
const
cDot: array[0..1] of Char = #1#1;
cDash: array[0..1] of Char = #4#2;
cDashDot: array[0..3] of Char = #4#1#1#1;
cDashDotDot: array[0..5] of Char = #4#1#1#1#1#1;
var
aCapStyle: Longint;
begin
aCapStyle := CapNotLast;
// Is this still needed?? I don't think so
//if (awidth > 1) and (astyle = lsSolid) then
// aCapStyle := CapButt;
if awidth = 1 then
awidth := 0; // switch to hardware algorithm
case AStyle of
lsDot:
begin
XSetLineAttributes(xapplication.display, Fgc, awidth,
LineOnOffDash, aCapStyle, JoinMiter);
XSetDashes(xapplication.display, Fgc, 0, cDot, 2);
end;
lsDash:
begin
XSetLineAttributes(xapplication.display, Fgc, awidth,
LineOnOffDash, aCapStyle, JoinMiter);
XSetDashes(xapplication.display, Fgc, 0, cDash, 2);
end;
lsDashDot:
begin
XSetLineAttributes(xapplication.display, Fgc, awidth,
LineOnOffDash, aCapStyle, JoinMiter);
XSetDashes(xapplication.display, Fgc, 0, cDashDot, 4);
end;
lsDashDotDot:
begin
XSetLineAttributes(xapplication.display, Fgc, awidth,
LineOnOffDash, aCapStyle, JoinMiter);
XSetDashes(xapplication.display, Fgc, 0, cDashDotDot, 6);
end;
else // which includes lsSolid
XSetLineAttributes(xapplication.display, Fgc, awidth,
LineSolid, aCapStyle, JoinMiter);
end; { case }
end;
procedure TfpgCanvasImpl.DoDrawString(x, y: TfpgCoord; const txt: string);
begin
if Length(txt) < 1 then
Exit; //==>
XftDrawStringUTF8(FXftDraw, FColorTextXft, FCurFontRes.Handle, x,
y + FCurFontRes.GetAscent, PChar(txt), Length(txt));
end;
procedure TfpgCanvasImpl.DoGetWinRect(out r: TfpgRect);
var
rw: TfpgWinHandle;
x: integer;
y: integer;
bw: longword;
d: longword;
begin
r.Left := 0;
r.Top := 0;
XGetGeometry(xapplication.display, FDrawWindow.FWinHandle, @rw, @x, @y,
@(r.width), @(r.height), @bw, @d);
end;
procedure TfpgCanvasImpl.DoFillRectangle(x, y, w, h: TfpgCoord);
begin
XFillRectangle(xapplication.display, FDrawHandle, Fgc, x, y, w, h);
end;
procedure TfpgCanvasImpl.DoXORFillRectangle(col: TfpgColor; x, y, w, h: TfpgCoord);
begin
XSetForeGround(xapplication.display, Fgc, fpgColorToX(fpgColorToRGB(col)));
XSetFunction(xapplication.display, Fgc, GXxor);
XFillRectangle(xapplication.display, FDrawHandle, Fgc, x, y, w, h);
XSetForeGround(xapplication.display, Fgc, 0);
XSetFunction(xapplication.display, Fgc, GXcopy);
end;
procedure TfpgCanvasImpl.DoFillTriangle(x1, y1, x2, y2, x3, y3: TfpgCoord);
var
pts: array[1..3] of TXPoint;
begin
pts[1].x := x1; pts[1].y := y1;
pts[2].x := x2; pts[2].y := y2;
pts[3].x := x3; pts[3].y := y3;
XFillPolygon(xapplication.display, FDrawHandle, Fgc, @pts, 3, CoordModeOrigin, X.Complex);
end;
procedure TfpgCanvasImpl.DoDrawRectangle(x, y, w, h: TfpgCoord);
begin
// writeln(Format('DoDrawRectangle x=%d y=%d w=%d h=%d', [x, y, w, h]));
// Same behavior as Windows. See documentation for reason.
XDrawRectangle(xapplication.display, FDrawHandle, Fgc, x, y, w-1, h-1);
end;
procedure TfpgCanvasImpl.DoDrawLine(x1, y1, x2, y2: TfpgCoord);
begin
// Same behavior as Windows. See documentation for reason.
XDrawLine(xapplication.display, FDrawHandle, Fgc, x1, y1, x2, y2);
end;
procedure TfpgCanvasImpl.DoSetClipRect(const ARect: TfpgRect);
var
r: TXRectangle;
rg: TRegion;
begin
r.x := ARect.Left;
r.y := ARect.Top;
r.Width := ARect.Width;
r.Height := ARect.Height;
rg := XCreateRegion;
XUnionRectWithRegion(@r, rg, FClipRegion);
XSetRegion(xapplication.display, Fgc, FClipRegion);
XftDrawSetClip(FXftDraw, FClipRegion);
FClipRect := ARect;
FClipRectSet := True;
XDestroyRegion(rg);
end;
function TfpgCanvasImpl.DoGetClipRect: TfpgRect;
begin
Result := FClipRect;
end;
procedure TfpgCanvasImpl.DoAddClipRect(const ARect: TfpgRect);
var
r: TXRectangle;
rg: TRegion;
begin
r.x := ARect.Left;
r.y := ARect.Top;
r.Width := ARect.Width;
r.Height := ARect.Height;
rg := XCreateRegion;
XUnionRectWithRegion(@r, rg, rg);
XIntersectRegion(FClipRegion, rg, FClipRegion);
XSetRegion(xapplication.display, Fgc, FClipRegion);
FClipRect := ARect; // Double check this, it might be wrong!!
FClipRectSet := True;
XftDrawSetClip(FXftDraw, FClipRegion);
XDestroyRegion(rg);
end;
procedure TfpgCanvasImpl.DoClearClipRect;
var
r: TfpgRect;
begin
DoGetWinRect(r);
DoSetClipRect(r);
FClipRectSet := False;
end;
procedure TfpgCanvasImpl.DoDrawImagePart(x, y: TfpgCoord; img: TfpgImageBase; xi, yi, w, h: integer);
var
msk: TPixmap;
gc2: Tgc;
drawgc: Tgc;
GcValues: TXGcValues;
begin
if img = nil then
Exit; //==>
if img.Masked then
begin
// rendering the mask
msk := XCreatePixmap(xapplication.display, XDefaultRootWindow(xapplication.display), w, h, 1);
GcValues.foreground := 1;
GcValues.background := 0;
// clear mask
gc2 := XCreateGc(xapplication.display, msk, GCForeground or GCBackground, @GcValues);
XSetForeground(xapplication.display, gc2, 0);
XFillRectangle(xapplication.display, msk, gc2, 0, 0, w, h);
XSetForeground(xapplication.display, gc2, 1);
XPutImage(xapplication.display, msk, gc2, TfpgImageImpl(img).XImageMask, xi, yi, 0, 0, w, h);
drawgc := XCreateGc(xapplication.display, FDrawHandle, 0, @GcValues);
XSetClipMask(xapplication.display, drawgc, msk);
XSetClipOrigin(xapplication.display, drawgc, x, y);
XPutImage(xapplication.display, FDrawHandle, drawgc, TfpgImage(img).XImage, xi, yi, x, y, w, h);
XFreePixmap(xapplication.display, msk);
XFreeGc(xapplication.display, drawgc);
XFreeGc(xapplication.display, gc2);
end
else
XPutImage(xapplication.display, FDrawHandle, Fgc, TfpgImage(img).XImage, xi, yi, x, y, w, h);
end;
{ TfpgImageImpl }
constructor TfpgImageImpl.Create;
begin
inherited Create;
end;
procedure TfpgImageImpl.DoFreeImage;
begin
// does nothing on X11
end;
procedure TfpgImageImpl.DoInitImage(acolordepth, awidth, aheight: integer; aimgdata: Pointer);
begin
FMasked := False;
with FXimg do
begin
Width := awidth;
Height := aheight;
xoffset := 0;
obdata := #0;
byte_order := LSBFirst;
bitmap_bit_order := MSBFirst;
bitmap_pad := 32;
bytes_per_line := 0;
if acolordepth = 1 then
begin
format := XYBitmap;
bitmap_unit := 8;
depth := 1;
bits_per_pixel := 1;
red_mask := 1;
green_mask := 0;
blue_mask := 0;
end
else
begin
format := ZPixmap;
bitmap_unit := 32;
// only truecolor 24/32 displays supported now, otherwise color conversion required!
// this must be match for the display !!!
depth := xapplication.DisplayDepth; // acolordepth;
bits_per_pixel := 32;
// Shouldn't we rather get this from XDefaultVisualOfScreen(). PVisual?
red_mask := $000000FF;
green_mask := $0000FF00;
blue_mask := $00FF0000;
end;
Data := aimgdata;
end;
XInitImage(@FXimg);
end;
procedure TfpgImageImpl.DoInitImageMask(awidth, aheight: integer; aimgdata: Pointer);
begin
FMasked := True;
with FXimgMask do
begin
Width := awidth;
Height := aheight;
xoffset := 0;
format := XYBitmap;
byte_order := LSBFirst;
bitmap_unit := 8;
bitmap_bit_order := MSBFirst;
bitmap_pad := 32;
depth := 1;
bytes_per_line := 0;
bits_per_pixel := 1;
red_mask := 1;
green_mask := 0;
blue_mask := 0;
obdata := #0;
Data := aimgdata;
end;
XInitImage(@FXimgMask);
end;
function TfpgImageImpl.XImage: PXImage;
begin
Result := @FXimg;
end;
function TfpgImageImpl.XImageMask: PXImage;
begin
Result := @FXimgMask;
end;
{ TfpgClipboardImpl }
function TfpgClipboardImpl.DoGetText: TfpgString;
begin
XConvertSelection(xapplication.Display, xapplication.xia_clipboard,
XA_STRING, xapplication.xia_clipboard, FClipboardWndHandle, 0);
FWaitingForSelection := True;
fpgDeliverMessages; // delivering the remaining messages
repeat
fpgWaitWindowMessage;
fpgDeliverMessages;
until not FWaitingForSelection;
Result := FClipboardText;
end;
procedure TfpgClipboardImpl.DoSetText(const AValue: TfpgString);
begin
FClipboardText := AValue;
XSetSelectionOwner(xapplication.Display, xapplication.xia_clipboard,
FClipboardWndHandle, 0);
end;
procedure TfpgClipboardImpl.InitClipboard;
begin
FWaitingForSelection := False;
FClipboardWndHandle := XCreateSimpleWindow(xapplication.Display,
xapplication.RootWindow, 10, 10, 10, 10, 0, 0, 0);
end;
{ TfpgFileListImpl }
function TfpgFileListImpl.EncodeModeString(FileMode: longword): TFileModeString;
const
modestring: string[9] = 'xwrxwrxwr'; // must be in reverse order
var
b: integer;
n: integer;
begin
// rights
//rwx rwx rwx
b := 1;
n := 1;
Result := '';
while n <= 9 do
begin
if (FileMode and b) = 0 then
Result := '-' + Result
else
Result := modestring[n] + Result;
inc(n);
b := b shl 1;
end;
end;
constructor TfpgFileListImpl.Create;
begin
inherited Create;
FHasFileMode := true;
end;
function TfpgFileListImpl.InitializeEntry(sr: TSearchRec): TFileEntry;
var
info: Tstat;
fullname: TfpgString;
begin
Result := inherited InitializeEntry(sr);
if Assigned(Result) then
begin
fullname := DirectoryName + Result.Name;
Result.LinkTarget := ExtractTargetSymLinkPath(fullname);
Result.IsLink := (Result.LinkTarget<>'');
Result.IsExecutable := ((sr.Mode and $40) <> 0);
Result.mode := EncodeModeString(sr.Mode);
Fpstat(PChar(fullname), info);
{Result.GroupID := info.st_gid;
Result.OwnerID := info.st_uid;}
Result.Owner := GetUserName(TUID(info.st_uid));
Result.Group := GetGroupName(TGID(info.st_uid));
end;
end;
procedure TfpgFileListImpl.PopulateSpecialDirs(const aDirectory: TfpgString);
var
ds: string;
begin
FSpecialDirs.Clear;
FSpecialDirs.Add(DirectorySeparator); // add root
ds := aDirectory;
if Copy(ds, 1, 1) <> DirectorySeparator then
ds := DirectorySeparator + ds;
inherited PopulateSpecialDirs(ds);
end;
initialization
xapplication := nil;
end.
|