summaryrefslogtreecommitdiff
path: root/native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c
blob: 09ebe85262749c0076d76e8c5e49479885ccf519 (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
/* gtkwindowpeer.c -- Native implementation of GtkWindowPeer
   Copyright (C) 1998, 1999, 2002, 2004, 2005, 2006
   Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


#include "gtkpeer.h"
#include "gnu_java_awt_peer_gtk_GtkWindowPeer.h"
#include <gdk/gdkprivate.h>
#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#include <gdk/gdkkeysyms.h>

#define AWT_WINDOW_CLOSING 201
#define AWT_WINDOW_CLOSED 202
#define AWT_WINDOW_ICONIFIED 203
#define AWT_WINDOW_DEICONIFIED 204
#define AWT_WINDOW_ACTIVATED 205
#define AWT_WINDOW_DEACTIVATED 206
#define AWT_WINDOW_GAINED_FOCUS 207
#define AWT_WINDOW_LOST_FOCUS 208
#define AWT_WINDOW_STATE_CHANGED 209

#define AWT_FRAME_NORMAL 0
#define AWT_FRAME_ICONIFIED 1
#define AWT_FRAME_MAXIMIZED_BOTH 6

/* Virtual Keys */
/* This list should be kept in the same order as the VK_ field
   declarations in KeyEvent.java. */
#define VK_ENTER '\n'
#define VK_BACK_SPACE '\b'
#define VK_TAB '\t'
#define VK_CANCEL 3
#define VK_CLEAR 12
#define VK_SHIFT 16
#define VK_CONTROL 17
#define VK_ALT 18
#define VK_PAUSE 19
#define VK_CAPS_LOCK 20
#define VK_ESCAPE 27
#define VK_SPACE ' '
#define VK_PAGE_UP 33
#define VK_PAGE_DOWN 34
#define VK_END 35
#define VK_HOME 36
#define VK_LEFT 37
#define VK_UP 38
#define VK_RIGHT 39
#define VK_DOWN 40
#define VK_COMMA ','
#define VK_MINUS '-'
#define VK_PERIOD '.'
#define VK_SLASH '/'
#define VK_0 '0'
#define VK_1 '1'
#define VK_2 '2'
#define VK_3 '3'
#define VK_4 '4'
#define VK_5 '5'
#define VK_6 '6'
#define VK_7 '7'
#define VK_8 '8'
#define VK_9 '9'
#define VK_SEMICOLON ';'
#define VK_EQUALS '='
#define VK_A 'A'
#define VK_B 'B'
#define VK_C 'C'
#define VK_D 'D'
#define VK_E 'E'
#define VK_F 'F'
#define VK_G 'G'
#define VK_H 'H'
#define VK_I 'I'
#define VK_J 'J'
#define VK_K 'K'
#define VK_L 'L'
#define VK_M 'M'
#define VK_N 'N'
#define VK_O 'O'
#define VK_P 'P'
#define VK_Q 'Q'
#define VK_R 'R'
#define VK_S 'S'
#define VK_T 'T'
#define VK_U 'U'
#define VK_V 'V'
#define VK_W 'W'
#define VK_X 'X'
#define VK_Y 'Y'
#define VK_Z 'Z'
#define VK_OPEN_BRACKET '['
#define VK_BACK_SLASH '\\'
#define VK_CLOSE_BRACKET ']'
/* See gtkpeer.h */
/* #define VK_NUMPAD0 96 */
/* #define VK_NUMPAD1 97 */
/* #define VK_NUMPAD2 98 */
/* #define VK_NUMPAD3 99 */
/* #define VK_NUMPAD4 100 */
/* #define VK_NUMPAD5 101 */
/* #define VK_NUMPAD6 102 */
/* #define VK_NUMPAD7 103 */
/* #define VK_NUMPAD8 104 */
/* #define VK_NUMPAD9 105 */
#define VK_MULTIPLY 106
#define VK_ADD 107
#define VK_SEPARATER 108
#define VK_SEPARATOR 108
#define VK_SUBTRACT 109
/* See gtkpeer.h */
/* #define VK_DECIMAL 110 */
#define VK_DIVIDE 111
#define VK_DELETE 127
#define VK_NUM_LOCK 144
#define VK_SCROLL_LOCK 145
#define VK_F1 112
#define VK_F2 113
#define VK_F3 114
#define VK_F4 115
#define VK_F5 116
#define VK_F6 117
#define VK_F7 118
#define VK_F8 119
#define VK_F9 120
#define VK_F10 121
#define VK_F11 122
#define VK_F12 123
#define VK_F13 61440
#define VK_F14 61441
#define VK_F15 61442
#define VK_F16 61443
#define VK_F17 61444
#define VK_F18 61445
#define VK_F19 61446
#define VK_F20 61447
#define VK_F21 61448
#define VK_F22 61449
#define VK_F23 61450
#define VK_F24 61451
#define VK_PRINTSCREEN 154
#define VK_INSERT 155
#define VK_HELP 156
#define VK_META 157
#define VK_BACK_QUOTE 192
#define VK_QUOTE 222
#define VK_KP_UP 224
#define VK_KP_DOWN 225
#define VK_KP_LEFT 226
#define VK_KP_RIGHT 227
#define VK_DEAD_GRAVE 128
#define VK_DEAD_ACUTE 129
#define VK_DEAD_CIRCUMFLEX 130
#define VK_DEAD_TILDE 131
#define VK_DEAD_MACRON 132
#define VK_DEAD_BREVE 133
#define VK_DEAD_ABOVEDOT 134
#define VK_DEAD_DIAERESIS 135
#define VK_DEAD_ABOVERING 136
#define VK_DEAD_DOUBLEACUTE 137
#define VK_DEAD_CARON 138
#define VK_DEAD_CEDILLA 139
#define VK_DEAD_OGONEK 140
#define VK_DEAD_IOTA 141
#define VK_DEAD_VOICED_SOUND 142
#define VK_DEAD_SEMIVOICED_SOUND 143
#define VK_AMPERSAND 150
#define VK_ASTERISK 151
#define VK_QUOTEDBL 152
#define VK_LESS 153
#define VK_GREATER 160
#define VK_BRACELEFT 161
#define VK_BRACERIGHT 162
#define VK_AT 512
#define VK_COLON 513
#define VK_CIRCUMFLEX 514
#define VK_DOLLAR 515
#define VK_EURO_SIGN 516
#define VK_EXCLAMATION_MARK 517
#define VK_INVERTED_EXCLAMATION_MARK 518
#define VK_LEFT_PARENTHESIS 519
#define VK_NUMBER_SIGN 520
#define VK_PLUS 521
#define VK_RIGHT_PARENTHESIS 522
#define VK_UNDERSCORE 523
#define VK_FINAL 24
#define VK_CONVERT 28
#define VK_NONCONVERT 29
#define VK_ACCEPT 30
#define VK_MODECHANGE 31
#define VK_KANA 21
#define VK_KANJI 25
#define VK_ALPHANUMERIC 240
#define VK_KATAKANA 241
#define VK_HIRAGANA 242
#define VK_FULL_WIDTH 243
#define VK_HALF_WIDTH 244
#define VK_ROMAN_CHARACTERS 245
#define VK_ALL_CANDIDATES 256
#define VK_PREVIOUS_CANDIDATE 257
#define VK_CODE_INPUT 258
#define VK_JAPANESE_KATAKANA 259
#define VK_JAPANESE_HIRAGANA 260
#define VK_JAPANESE_ROMAN 261
#define VK_KANA_LOCK 262
#define VK_INPUT_METHOD_ON_OFF 263
#define VK_CUT 65489
#define VK_COPY 65485
#define VK_PASTE 65487
#define VK_UNDO 65483
#define VK_AGAIN 65481
#define VK_FIND 65488
#define VK_PROPS 65482
#define VK_STOP 65480
#define VK_COMPOSE 65312
#define VK_ALT_GRAPH 65406
#define VK_UNDEFINED 0
#define VK_BEGIN 65368
#define VK_CONTEXT_MENU 525
#define VK_WINDOWS 524

 
#define AWT_KEY_CHAR_UNDEFINED 0

#define AWT_FRAME_STATE_NORMAL 0
#define AWT_FRAME_STATE_ICONIFIED 1
#define AWT_FRAME_STATE_MAXIMIZED_HORIZ 2
#define AWT_FRAME_STATE_MAXIMIZED_VERT 4
#define AWT_FRAME_STATE_MAXIMIZED_BOTH 6

static jmethodID postKeyEventID;
static jmethodID postWindowEventID;
static jmethodID postConfigureEventID;
static jmethodID postInsetsChangedEventID;
static jmethodID windowGetWidthID;
static jmethodID windowGetHeightID;

void
cp_gtk_window_init_jni (void)
{
  jclass gtkwindowpeer;

  gtkwindowpeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
                                           "gnu/java/awt/peer/gtk/GtkWindowPeer");

  postKeyEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkwindowpeer,
                                              "postKeyEvent", "(IJIICI)V");

  postWindowEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkwindowpeer,
                                                 "postWindowEvent",
                                                 "(ILjava/awt/Window;I)V");

  postConfigureEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkwindowpeer, 
                                                    "postConfigureEvent", "(IIII)V");

  postInsetsChangedEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkwindowpeer,
                                                        "postInsetsChangedEvent",
                                                        "(IIII)V");

  windowGetWidthID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkwindowpeer,
                                                "getWidth", "()I");

  windowGetHeightID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkwindowpeer,
                                                 "getHeight", "()I");

  gtkwindowpeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
                                           "gnu/java/awt/peer/gtk/GtkWindowPeer");
}

/* Get the first keyval in the keymap for this event's keycode.  The
   first keyval corresponds roughly to Java's notion of a virtual key.
   Returns the uppercase version of the first keyval or -1 if no
   keyval was found for the given hardware keycode. */
static gint
get_first_keyval_from_keymap (GdkEventKey *event)
{
  guint keyval;
  guint *keyvals;
  gint n_entries;

  if (!gdk_keymap_get_entries_for_keycode (NULL,
                                           event->hardware_keycode,
                                           NULL,
                                           &keyvals,
                                           &n_entries))
    {
      /* No keyval found for hardware keycode */
      return -1;
    }
  keyval = keyvals[0];
  g_free (keyvals);

  return gdk_keyval_to_upper (keyval);
}

/* Return the AWT key code for the given keysym or -1 if no keyval was
   found for the given hardware keycode. */
#ifdef __GNUC__
__inline
#endif
static jint
keysym_to_awt_keycode (GdkEventKey *event)
{
  gint ukeyval;
  guint state;

  ukeyval = get_first_keyval_from_keymap (event);

  if (ukeyval < 0)
    return -1;

  state = event->state;

  /* VK_A through VK_Z */
  if (ukeyval >= GDK_KEY_A && ukeyval <= GDK_KEY_Z)
    return ukeyval;

  /* VK_0 through VK_9 */
  if (ukeyval >= GDK_KEY_0 && ukeyval <= GDK_KEY_9)
    return ukeyval;

  switch (ukeyval)
    {
    case GDK_KEY_Return:
    case GDK_KEY_KP_Enter:
      return VK_ENTER;
    case GDK_KEY_BackSpace:
      return VK_BACK_SPACE;
    case GDK_KEY_Tab:
      return VK_TAB;
    case GDK_KEY_Cancel:
      return VK_CANCEL;
    case GDK_KEY_Clear:
      return VK_CLEAR;
    case GDK_KEY_Shift_L:
    case GDK_KEY_Shift_R:
      return VK_SHIFT;
    case GDK_KEY_Control_L:
    case GDK_KEY_Control_R:
      return VK_CONTROL;
    case GDK_KEY_Alt_L:
    case GDK_KEY_Alt_R:
      return VK_ALT;
    case GDK_KEY_Pause:
      return VK_PAUSE;
    case GDK_KEY_Caps_Lock:
      return VK_CAPS_LOCK;
    case GDK_KEY_Escape:
      return VK_ESCAPE;
    case GDK_KEY_space:
      return VK_SPACE;
    case GDK_KEY_KP_Page_Up:
      /* For keys on the numeric keypad, the JVM produces one of two
         virtual keys, depending on the num lock state. */
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD9;
      else
        return VK_PAGE_UP;
    case GDK_KEY_Page_Up:
      return VK_PAGE_UP;
    case GDK_KEY_KP_Page_Down:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD3;
      else
        return VK_PAGE_DOWN;
    case GDK_KEY_Page_Down:
      return VK_PAGE_DOWN;
    case GDK_KEY_KP_End:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD1;
      else
        return VK_END;
    case GDK_KEY_End:
      return VK_END;
    case GDK_KEY_KP_Home:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD7;
      else
        return VK_HOME;
    case GDK_KEY_Home:
      return VK_HOME;
    case GDK_KEY_KP_Begin:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD5;
      else
        return VK_UNDEFINED;
    case GDK_KEY_Left:
      return VK_LEFT;
    case GDK_KEY_Up:
      return VK_UP;
    case GDK_KEY_Right:
      return VK_RIGHT;
    case GDK_KEY_Down:
      return VK_DOWN;
    case GDK_KEY_comma:
      return VK_COMMA;
    case GDK_KEY_minus:
      return VK_MINUS;
    case GDK_KEY_period:
      return VK_PERIOD;
    case GDK_KEY_slash:
      return VK_SLASH;
      /*
      return VK_0;
      return VK_1;
      return VK_2;
      return VK_3;
      return VK_4;
      return VK_5;
      return VK_6;
      return VK_7;
      return VK_8;
      return VK_9;
      */
    case GDK_KEY_semicolon:
      return VK_SEMICOLON;
    case GDK_KEY_equal:
      return VK_EQUALS;
      /*
      return VK_A;
      return VK_B;
      return VK_C;
      return VK_D;
      return VK_E;
      return VK_F;
      return VK_G;
      return VK_H;
      return VK_I;
      return VK_J;
      return VK_K;
      return VK_L;
      return VK_M;
      return VK_N;
      return VK_O;
      return VK_P;
      return VK_Q;
      return VK_R;
      return VK_S;
      return VK_T;
      return VK_U;
      return VK_V;
      return VK_W;
      return VK_X;
      return VK_Y;
      return VK_Z;
      */
    case GDK_KEY_bracketleft:
      return VK_OPEN_BRACKET;
    case GDK_KEY_backslash:
      return VK_BACK_SLASH;
    case GDK_KEY_bracketright:
      return VK_CLOSE_BRACKET;
    case GDK_KEY_KP_0:
      return VK_NUMPAD0;
    case GDK_KEY_KP_1:
      return VK_NUMPAD1;
    case GDK_KEY_KP_2:
      return VK_NUMPAD2;
    case GDK_KEY_KP_3:
      return VK_NUMPAD3;
    case GDK_KEY_KP_4:
      return VK_NUMPAD4;
    case GDK_KEY_KP_5:
      return VK_NUMPAD5;
    case GDK_KEY_KP_6:
      return VK_NUMPAD6;
    case GDK_KEY_KP_7:
      return VK_NUMPAD7;
    case GDK_KEY_KP_8:
      return VK_NUMPAD8;
    case GDK_KEY_KP_9:
      return VK_NUMPAD9;
    case GDK_KEY_KP_Multiply:
      return VK_MULTIPLY;
    case GDK_KEY_KP_Add:
      return VK_ADD;
      /*
      return VK_SEPARATER;
      */
    case GDK_KEY_KP_Separator:
      return VK_SEPARATOR;
    case GDK_KEY_KP_Subtract:
      return VK_SUBTRACT;
    case GDK_KEY_KP_Decimal:
      return VK_DECIMAL;
    case GDK_KEY_KP_Divide:
      return VK_DIVIDE;
    case GDK_KEY_KP_Delete:
      if (state & GDK_MOD2_MASK)
        return VK_DECIMAL;
      else
        return VK_DELETE;
    case GDK_KEY_Delete:
      return VK_DELETE;
    case GDK_KEY_Num_Lock:
      return VK_NUM_LOCK;
    case GDK_KEY_Scroll_Lock:
      return VK_SCROLL_LOCK;
    case GDK_KEY_F1:
      return VK_F1;
    case GDK_KEY_F2:
      return VK_F2;
    case GDK_KEY_F3:
      return VK_F3;
    case GDK_KEY_F4:
      return VK_F4;
    case GDK_KEY_F5:
      return VK_F5;
    case GDK_KEY_F6:
      return VK_F6;
    case GDK_KEY_F7:
      return VK_F7;
    case GDK_KEY_F8:
      return VK_F8;
    case GDK_KEY_F9:
      return VK_F9;
    case GDK_KEY_F10:
      return VK_F10;
    case GDK_KEY_F11:
      return VK_F11;
    case GDK_KEY_F12:
      return VK_F12;
    case GDK_KEY_F13:
      return VK_F13;
    case GDK_KEY_F14:
      return VK_F14;
    case GDK_KEY_F15:
      return VK_F15;
    case GDK_KEY_F16:
      return VK_F16;
    case GDK_KEY_F17:
      return VK_F17;
    case GDK_KEY_F18:
      return VK_F18;
    case GDK_KEY_F19:
      return VK_F19;
    case GDK_KEY_F20:
      return VK_F20;
    case GDK_KEY_F21:
      return VK_F21;
    case GDK_KEY_F22:
      return VK_F22;
    case GDK_KEY_F23:
      return VK_F23;
    case GDK_KEY_F24:
      return VK_F24;
    case GDK_KEY_Print:
      return VK_PRINTSCREEN;
    case GDK_KEY_KP_Insert:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD0;
      else
        return VK_INSERT;
    case GDK_KEY_Insert:
      return VK_INSERT;
    case GDK_KEY_Help:
      return VK_HELP;
    case GDK_KEY_Meta_L:
    case GDK_KEY_Meta_R:
      return VK_META;
    case GDK_KEY_grave:
      return VK_BACK_QUOTE;
    case GDK_KEY_apostrophe:
      return VK_QUOTE;
    case GDK_KEY_KP_Up:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD8;
      else
        return VK_KP_UP;
    case GDK_KEY_KP_Down:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD2;
      else
        return VK_KP_DOWN;
    case GDK_KEY_KP_Left:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD4;
      else
        return VK_KP_LEFT;
    case GDK_KEY_KP_Right:
      if (state & GDK_MOD2_MASK)
        return VK_NUMPAD6;
      else
        return VK_KP_RIGHT;
    case GDK_KEY_dead_grave:
      return VK_DEAD_GRAVE;
    case GDK_KEY_dead_acute:
      return VK_DEAD_ACUTE;
    case GDK_KEY_dead_circumflex:
      return VK_DEAD_CIRCUMFLEX;
    case GDK_KEY_dead_tilde:
      return VK_DEAD_TILDE;
    case GDK_KEY_dead_macron:
      return VK_DEAD_MACRON;
    case GDK_KEY_dead_breve:
      return VK_DEAD_BREVE;
    case GDK_KEY_dead_abovedot:
      return VK_DEAD_ABOVEDOT;
    case GDK_KEY_dead_diaeresis:
      return VK_DEAD_DIAERESIS;
    case GDK_KEY_dead_abovering:
      return VK_DEAD_ABOVERING;
    case GDK_KEY_dead_doubleacute:
      return VK_DEAD_DOUBLEACUTE;
    case GDK_KEY_dead_caron:
      return VK_DEAD_CARON;
    case GDK_KEY_dead_cedilla:
      return VK_DEAD_CEDILLA;
    case GDK_KEY_dead_ogonek:
      return VK_DEAD_OGONEK;
    case GDK_KEY_dead_iota:
      return VK_DEAD_IOTA;
    case GDK_KEY_dead_voiced_sound:
      return VK_DEAD_VOICED_SOUND;
    case GDK_KEY_dead_semivoiced_sound:
      return VK_DEAD_SEMIVOICED_SOUND;
    case GDK_KEY_ampersand:
      return VK_AMPERSAND;
    case GDK_KEY_asterisk:
      return VK_ASTERISK;
    case GDK_KEY_quotedbl:
      return VK_QUOTEDBL;
    case GDK_KEY_less:
      return VK_LESS;
    case GDK_KEY_greater:
      return VK_GREATER;
    case GDK_KEY_braceleft:
      return VK_BRACELEFT;
    case GDK_KEY_braceright:
      return VK_BRACERIGHT;
    case GDK_KEY_at:
      return VK_AT;
    case GDK_KEY_colon:
      return VK_COLON;
    case GDK_KEY_asciicircum:
      return VK_CIRCUMFLEX;
    case GDK_KEY_dollar:
      return VK_DOLLAR;
    case GDK_KEY_EuroSign:
      return VK_EURO_SIGN;
    case GDK_KEY_exclam:
      return VK_EXCLAMATION_MARK;
    case GDK_KEY_exclamdown:
      return VK_INVERTED_EXCLAMATION_MARK;
    case GDK_KEY_parenleft:
      return VK_LEFT_PARENTHESIS;
    case GDK_KEY_numbersign:
      return VK_NUMBER_SIGN;
    case GDK_KEY_plus:
      return VK_PLUS;
    case GDK_KEY_parenright:
      return VK_RIGHT_PARENTHESIS;
    case GDK_KEY_underscore:
      return VK_UNDERSCORE;
      /*
      return VK_FINAL;
      return VK_CONVERT;
      return VK_NONCONVERT;
      return VK_ACCEPT;
      */
    case GDK_KEY_Mode_switch:
      return VK_MODECHANGE;
      /*
      return VK_KANA;
      */
    case GDK_KEY_Kanji:
      return VK_KANJI;
      /*
      return VK_ALPHANUMERIC;
      */
    case GDK_KEY_Katakana:
      return VK_KATAKANA;
    case GDK_KEY_Hiragana:
      return VK_HIRAGANA;
      /*
      return VK_FULL_WIDTH;
      return VK_HALF_WIDTH;
      return VK_ROMAN_CHARACTERS;
      return VK_ALL_CANDIDATES;
      */
    case GDK_KEY_PreviousCandidate:
      return VK_PREVIOUS_CANDIDATE;
    case GDK_KEY_Codeinput:
      return VK_CODE_INPUT;
      /*
      return VK_JAPANESE_KATAKANA;
      return VK_JAPANESE_HIRAGANA;
      return VK_JAPANESE_ROMAN;
      */
    case GDK_KEY_Kana_Lock:
      return VK_KANA_LOCK;
      /*
      return VK_INPUT_METHOD_ON_OFF;
      return VK_CUT;
      return VK_COPY;
      return VK_PASTE;
      */
    case GDK_KEY_Undo:
      return VK_UNDO;
    case GDK_KEY_Redo:
      return VK_AGAIN;
      /*
      return VK_FIND;
      return VK_PROPS;
      return VK_STOP;
      return VK_COMPOSE;
      */
    case GDK_KEY_ISO_Level3_Shift:
      return VK_ALT_GRAPH;
      /*
	case VK_BEGIN:
      */
    case GDK_KEY_Menu:
      return VK_CONTEXT_MENU;
    case GDK_KEY_Super_L:
    case GDK_KEY_Super_R:
      return VK_WINDOWS;

    default:
      return VK_UNDEFINED;
    }
}

/* Return the AWT key location code for the given keysym or -1 if no
   keyval was found for the given hardware keycode. */
static jint
keysym_to_awt_keylocation (GdkEventKey *event)
{
  gint ukeyval;

  ukeyval = get_first_keyval_from_keymap (event);

  if (ukeyval < 0)
    return -1;

  /* VK_A through VK_Z */
  if (ukeyval >= GDK_KEY_A && ukeyval <= GDK_KEY_Z)
    return AWT_KEY_LOCATION_STANDARD;

  /* VK_0 through VK_9 */
  if (ukeyval >= GDK_KEY_0 && ukeyval <= GDK_KEY_9)
    return AWT_KEY_LOCATION_STANDARD;

  switch (ukeyval)
    {
    case GDK_KEY_Shift_L:
    case GDK_KEY_Control_L:
    case GDK_KEY_Alt_L:
    case GDK_KEY_Meta_L:
      return AWT_KEY_LOCATION_LEFT;

    case GDK_KEY_Shift_R:
    case GDK_KEY_Control_R:
    case GDK_KEY_Alt_R:
    case GDK_KEY_Meta_R:
      return AWT_KEY_LOCATION_RIGHT;

    case GDK_KEY_Return:
    case GDK_KEY_BackSpace:
    case GDK_KEY_Tab:
    case GDK_KEY_Cancel:
    case GDK_KEY_Clear:
    case GDK_KEY_Pause:
    case GDK_KEY_Caps_Lock:
    case GDK_KEY_Escape:
    case GDK_KEY_space:
    case GDK_KEY_Page_Up:
    case GDK_KEY_Page_Down:
    case GDK_KEY_End:
    case GDK_KEY_Home:
    case GDK_KEY_Left:
    case GDK_KEY_Up:
    case GDK_KEY_Right:
    case GDK_KEY_Down:
    case GDK_KEY_comma:
    case GDK_KEY_minus:
    case GDK_KEY_period:
    case GDK_KEY_slash:
    case GDK_KEY_semicolon:
    case GDK_KEY_equal:
    case GDK_KEY_bracketleft:
    case GDK_KEY_backslash:
    case GDK_KEY_bracketright:
    case GDK_KEY_Delete:
    case GDK_KEY_Scroll_Lock:
    case GDK_KEY_F1:
    case GDK_KEY_F2:
    case GDK_KEY_F3:
    case GDK_KEY_F4:
    case GDK_KEY_F5:
    case GDK_KEY_F6:
    case GDK_KEY_F7:
    case GDK_KEY_F8:
    case GDK_KEY_F9:
    case GDK_KEY_F10:
    case GDK_KEY_F11:
    case GDK_KEY_F12:
    case GDK_KEY_F13:
    case GDK_KEY_F14:
    case GDK_KEY_F15:
    case GDK_KEY_F16:
    case GDK_KEY_F17:
    case GDK_KEY_F18:
    case GDK_KEY_F19:
    case GDK_KEY_F20:
    case GDK_KEY_F21:
    case GDK_KEY_F22:
    case GDK_KEY_F23:
    case GDK_KEY_F24:
    case GDK_KEY_Print:
    case GDK_KEY_Insert:
    case GDK_KEY_Help:
    case GDK_KEY_grave:
    case GDK_KEY_apostrophe:
    case GDK_KEY_dead_grave:
    case GDK_KEY_dead_acute:
    case GDK_KEY_dead_circumflex:
    case GDK_KEY_dead_tilde:
    case GDK_KEY_dead_macron:
    case GDK_KEY_dead_breve:
    case GDK_KEY_dead_abovedot:
    case GDK_KEY_dead_diaeresis:
    case GDK_KEY_dead_abovering:
    case GDK_KEY_dead_doubleacute:
    case GDK_KEY_dead_caron:
    case GDK_KEY_dead_cedilla:
    case GDK_KEY_dead_ogonek:
    case GDK_KEY_dead_iota:
    case GDK_KEY_dead_voiced_sound:
    case GDK_KEY_dead_semivoiced_sound:
    case GDK_KEY_ampersand:
    case GDK_KEY_asterisk:
    case GDK_KEY_quotedbl:
    case GDK_KEY_less:
    case GDK_KEY_greater:
    case GDK_KEY_braceleft:
    case GDK_KEY_braceright:
    case GDK_KEY_at:
    case GDK_KEY_colon:
    case GDK_KEY_asciicircum:
    case GDK_KEY_dollar:
    case GDK_KEY_EuroSign:
    case GDK_KEY_exclam:
    case GDK_KEY_exclamdown:
    case GDK_KEY_parenleft:
    case GDK_KEY_numbersign:
    case GDK_KEY_plus:
    case GDK_KEY_parenright:
    case GDK_KEY_underscore:
    case GDK_KEY_Mode_switch:
    case GDK_KEY_Kanji:
    case GDK_KEY_Katakana:
    case GDK_KEY_Hiragana:
    case GDK_KEY_PreviousCandidate:
    case GDK_KEY_Codeinput:
    case GDK_KEY_Kana_Lock:
      return AWT_KEY_LOCATION_STANDARD;

    case GDK_KEY_KP_Enter:
    case GDK_KEY_KP_Page_Up:
    case GDK_KEY_KP_Page_Down:
    case GDK_KEY_KP_End:
    case GDK_KEY_KP_Home:
    case GDK_KEY_KP_Begin:
    case GDK_KEY_KP_0:
    case GDK_KEY_KP_1:
    case GDK_KEY_KP_2:
    case GDK_KEY_KP_3:
    case GDK_KEY_KP_4:
    case GDK_KEY_KP_5:
    case GDK_KEY_KP_6:
    case GDK_KEY_KP_7:
    case GDK_KEY_KP_8:
    case GDK_KEY_KP_9:
    case GDK_KEY_KP_Multiply:
    case GDK_KEY_KP_Add:
    case GDK_KEY_KP_Separator:
    case GDK_KEY_KP_Subtract:
    case GDK_KEY_KP_Decimal:
    case GDK_KEY_KP_Divide:
    case GDK_KEY_KP_Delete:
    case GDK_KEY_Num_Lock:
    case GDK_KEY_KP_Insert:
    case GDK_KEY_KP_Up:
    case GDK_KEY_KP_Down:
    case GDK_KEY_KP_Left:
    case GDK_KEY_KP_Right:
      return AWT_KEY_LOCATION_NUMPAD;

    default:
      return AWT_KEY_LOCATION_UNKNOWN;
    }
}

static jchar
keyevent_to_awt_keychar (GdkEventKey *event)
{
  if (event->length > 0)
    {
      /* Translate GDK carriage return to Java linefeed. */
      if (event->string[0] == 13)
        return VK_ENTER;
      else
        return event->string[0];
    }
  else
    {
      switch (event->keyval)
        {
        case GDK_KEY_BackSpace:
          return VK_BACK_SPACE;
        case GDK_KEY_Tab:
          return VK_TAB;
        case GDK_KEY_Delete:
        case GDK_KEY_KP_Delete:
          return VK_DELETE;
        default:
          return AWT_KEY_CHAR_UNDEFINED;
        }
    }
}

/* Modifier key events need special treatment.  In Sun's peer
   implementation, when a modifier key is pressed, the KEY_PRESSED
   event has that modifier in its modifiers list.  The corresponding
   KEY_RELEASED event's modifier list does not contain the modifier.
   For example, pressing and releasing the shift key will produce a
   key press event with modifiers=Shift, and a key release event with
   no modifiers.  GDK's key events behave in the exact opposite way,
   so this translation code is needed. */
static jint
keyevent_state_to_awt_mods (GdkEventKey *event)
{
  jint result = 0;
  guint state;

  if (event->type == GDK_KEY_PRESS)
    {
      state = event->state;

      if (event->keyval == GDK_KEY_Shift_L
          || event->keyval == GDK_KEY_Shift_R)
        result |= AWT_SHIFT_DOWN_MASK | AWT_SHIFT_MASK;
      else
        {
          if (state & GDK_SHIFT_MASK)
            result |= AWT_SHIFT_DOWN_MASK | AWT_SHIFT_MASK;
        }

      if (event->keyval == GDK_KEY_Control_L
          || event->keyval == GDK_KEY_Control_R)
        result |= AWT_CTRL_DOWN_MASK | AWT_CTRL_MASK;
      else
        {
          if (state & GDK_CONTROL_MASK)
            result |= AWT_CTRL_DOWN_MASK | AWT_CTRL_MASK;
        }

      if (event->keyval == GDK_KEY_Alt_L
          || event->keyval == GDK_KEY_Alt_R)
        result |= AWT_ALT_DOWN_MASK | AWT_ALT_MASK;
      else
        {
          if (state & GDK_MOD1_MASK)
            result |= AWT_ALT_DOWN_MASK | AWT_ALT_MASK;
        }
    }
  else if (event->type == GDK_KEY_RELEASE)
    {
      state = event->state;

      if (event->keyval != GDK_KEY_Shift_L
          && event->keyval != GDK_KEY_Shift_R)
        {
          if (state & GDK_SHIFT_MASK)
            result |= AWT_SHIFT_DOWN_MASK | AWT_SHIFT_MASK;
        }
      if (event->keyval != GDK_KEY_Control_L
          && event->keyval != GDK_KEY_Control_R)
        {
          if (state & GDK_CONTROL_MASK)
            result |= AWT_CTRL_DOWN_MASK | AWT_CTRL_MASK;
        }

      if (event->keyval != GDK_KEY_Alt_L
          && event->keyval != GDK_KEY_Alt_R)
        {
          if (state & GDK_MOD1_MASK)
            result |= AWT_ALT_DOWN_MASK | AWT_ALT_MASK;
        }
    }

  return result;
}

static gboolean window_configure_cb (GtkWidget *widget,
                                     GdkEventConfigure *event,
                                     jobject peer);

/* FIXME: we're currently seeing the double-activation that occurs
   with metacity and GTK.  See
   http://bugzilla.gnome.org/show_bug.cgi?id=140977 for details. */

static void window_get_frame_extents (GtkWidget *window,
                                      int *top, int *left,
                                      int *bottom, int *right);

static void request_frame_extents (GtkWidget *window);

static Bool property_notify_predicate (Display *display,
                                       XEvent  *xevent,
                                       XPointer arg);

static gboolean window_delete_cb (GtkWidget *widget, GdkEvent *event,
			      jobject peer);
static void window_destroy_cb (GtkWidget *widget, GdkEvent *event,
			       jobject peer);
static void window_focus_state_change_cb (GtkWidget *widget,
                                          GParamSpec *pspec,
                                          jobject peer);
static gboolean window_focus_in_cb (GtkWidget * widget,
                                    GdkEventFocus *event,
                                    jobject peer);
static gboolean window_focus_out_cb (GtkWidget * widget,
                                     GdkEventFocus *event,
                                     jobject peer);
static gboolean window_window_state_cb (GtkWidget *widget,
					GdkEvent *event,
					jobject peer);
static gboolean window_property_changed_cb (GtkWidget *widget,
					    GdkEventProperty *event,
					    jobject peer);
static void realize_cb (GtkWidget *widget, jobject peer);

static gboolean
window_configure_cb (GtkWidget *widget __attribute__((unused)),
                     GdkEventConfigure *event,
                     jobject peer)
{
  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
                                postConfigureEventID,
                                (jint) event->x,
                                (jint) event->y,
                                (jint) event->width,
                                (jint) event->height);

  return FALSE;
}

static gboolean
key_press_cb (GtkWidget *widget __attribute__((unused)),
              GdkEventKey *event,
              jobject peer)
{
  jint keycode;
  jint keylocation;

  keycode = keysym_to_awt_keycode (event);
  keylocation = keysym_to_awt_keylocation (event);

  /* Return immediately if an error occurs translating a hardware
     keycode to a keyval. */
  if (keycode < 0 || keylocation < 0)
    return TRUE;

  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
                                postKeyEventID,
                                (jint) AWT_KEY_PRESSED,
                                (jlong) event->time,
                                keyevent_state_to_awt_mods (event),
                                keycode,
                                keyevent_to_awt_keychar (event),
                                keylocation);

  /* FIXME: generation of key typed events needs to be moved
     to GtkComponentPeer.postKeyEvent.  If the key in a key
     press event is not an "action" key
     (KeyEvent.isActionKey) and is not a modifier key, then
     it should generate a key typed event. */
  return TRUE;
}


static gboolean
key_release_cb (GtkWidget *widget __attribute__((unused)),
                GdkEventKey *event,
                jobject peer)
{
  jint keycode;
  jint keylocation;

  keycode = keysym_to_awt_keycode (event);
  keylocation = keysym_to_awt_keylocation (event);

  /* Return immediately if an error occurs translating a hardware
     keycode to a keyval. */
  if (keycode < 0 || keylocation < 0)
    return TRUE;

  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
                                postKeyEventID,
                                (jint) AWT_KEY_RELEASED,
                                (jlong) event->time,
                                keyevent_state_to_awt_mods (event),
                                keycode,
                                keyevent_to_awt_keychar (event),
                                keylocation);

  return TRUE;
}

/* Union used for type punning. */
union extents_union
{
  guchar **gu_extents;
  unsigned long **extents;
};

union atom_list_union
{
  guchar **gu_extents;
  Atom **atom_list;
};

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_create
  (JNIEnv *env, jobject obj, jint type, jboolean decorated, jobject parent)
{
  GtkWidget *window_widget;
  GtkWindow *window;
  void *window_parent;
  GtkWidget *fixed;

  gdk_threads_enter ();
  
  gtkpeer_set_global_ref (env, obj);

  window_widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  window = GTK_WINDOW (window_widget);

  /* Keep this window in front of its parent, if it has one. */
  if (parent)
    {
      window_parent = gtkpeer_get_widget (env, parent);
      gtk_window_set_transient_for (window, GTK_WINDOW(window_parent));
    }

  gtk_window_set_decorated (window, decorated);

  gtk_window_set_type_hint (window, type);

  gtk_window_group_add_window (cp_gtk_global_window_group, window);

  fixed = gtk_fixed_new ();

  gtk_container_add (GTK_CONTAINER (window_widget), fixed);

  gtk_widget_show (fixed);

  gtkpeer_set_widget (env, obj, window_widget);

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_gtkWindowSetTitle
  (JNIEnv *env, jobject obj, jstring title)
{
  const char *c_title;
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);

  c_title = (*env)->GetStringUTFChars (env, title, NULL);

  gtk_window_set_title (GTK_WINDOW (ptr), c_title);

  (*env)->ReleaseStringUTFChars (env, title, c_title);

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_gtkWindowSetResizable
  (JNIEnv *env, jobject obj, jboolean resizable)
{
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);
  gtk_window_set_resizable (GTK_WINDOW (ptr), resizable);
  //g_object_set (G_OBJECT (ptr), "allow-shrink", resizable, NULL);
  g_object_set (G_OBJECT (ptr), "resizable", resizable, NULL);
  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_gtkWindowSetModal
  (JNIEnv *env, jobject obj, jboolean modal)
{
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);

  gtk_window_set_modal (GTK_WINDOW (ptr), modal);

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_gtkWindowSetAlwaysOnTop
  (JNIEnv *env, jobject obj, jboolean alwaysOnTop)
{
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);

  gtk_window_set_keep_above (GTK_WINDOW (ptr), alwaysOnTop);

  gdk_threads_leave ();
}

JNIEXPORT jboolean JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_gtkWindowHasFocus
(JNIEnv *env, jobject obj)
{
  void *ptr;
  jboolean retval;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);

  retval = gtk_window_has_toplevel_focus (GTK_WINDOW (ptr));

  gdk_threads_leave ();
  return retval;
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_setVisibleNative
  (JNIEnv *env, jobject obj, jboolean visible)
{
  gdk_threads_enter ();

  Java_gnu_java_awt_peer_gtk_GtkWindowPeer_setVisibleNativeUnlocked
    (env, obj, visible);

  gdk_flush ();

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_setVisibleNativeUnlocked
  (JNIEnv *env, jobject obj, jboolean visible)
{
  void *ptr;

  ptr = gtkpeer_get_widget (env, obj);

  if (visible)
    gtk_widget_show (GTK_WIDGET (ptr));
  else
    gtk_widget_hide (GTK_WIDGET (ptr));
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_connectSignals
  (JNIEnv *env, jobject obj)
{
  void *ptr;
  jobject gref;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);
  gref = (jobject) gtkpeer_get_global_ref (env, obj);

  g_signal_connect (G_OBJECT (ptr), "delete-event",
		    G_CALLBACK (window_delete_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "destroy-event",
		    G_CALLBACK (window_destroy_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "notify::has-toplevel-focus",
  		    G_CALLBACK (window_focus_state_change_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "focus-in-event",
                    G_CALLBACK (window_focus_in_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "focus-out-event",
                    G_CALLBACK (window_focus_out_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "window-state-event",
		    G_CALLBACK (window_window_state_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "property-notify-event",
		    G_CALLBACK (window_property_changed_cb), gref);

  g_signal_connect_after (G_OBJECT (ptr), "realize",
                          G_CALLBACK (realize_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "key-press-event",
                    G_CALLBACK (key_press_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "key-release-event",
                    G_CALLBACK (key_release_cb), gref);

  g_signal_connect_after (G_OBJECT (ptr), "window-state-event",
                          G_CALLBACK (window_window_state_cb), gref);

  g_signal_connect (G_OBJECT (ptr), "configure-event",
                    G_CALLBACK (window_configure_cb), gref);

  cp_gtk_component_connect_expose_signals (ptr, gref);
  cp_gtk_component_connect_mouse_signals (ptr, gref);

  /* FIXME: override focus signals here to prevent child fixed repaint? */

  gdk_threads_leave ();
}

/* Realize the window here so that its frame extents are known now.
   That way Window.pack can operate with the accurate insets returned
   by the window manager rather than the default estimates. */
JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_realize (JNIEnv *env, jobject obj)
{
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);

  gtk_widget_realize (GTK_WIDGET (ptr));

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_toBack (JNIEnv *env, 
    jobject obj)
{
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);
    
  gdk_window_lower (gtk_widget_get_window(GTK_WIDGET (ptr)));
  gdk_flush ();

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_toFront (JNIEnv *env, 
    jobject obj)
{
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);
    
  gdk_window_raise (gtk_widget_get_window(GTK_WIDGET (ptr)));
  gdk_flush ();

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_setSize
  (JNIEnv *env, jobject obj, jint width, jint height)
{
  void *ptr;

  gdk_threads_enter ();

  ptr = gtkpeer_get_widget (env, obj);

  /* Avoid GTK runtime assertion failures. */
  width = (width < 1) ? 1 : width;
  height = (height < 1) ? 1 : height;

  gtk_widget_set_size_request (GTK_WIDGET(ptr), width, height);

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_nativeSetBounds
  (JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
  gdk_threads_enter ();

  Java_gnu_java_awt_peer_gtk_GtkWindowPeer_nativeSetBoundsUnlocked
    (env, obj, x, y, width, height);

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_nativeSetLocationUnlocked
  (JNIEnv *env, jobject obj, jint x, jint y)
{
  void *ptr;

  ptr = gtkpeer_get_widget (env, obj);

  gtk_window_move (GTK_WINDOW(ptr), x, y);

  if (gtk_widget_get_window(GTK_WIDGET (ptr)) != NULL)
    gdk_window_move (gtk_widget_get_window(GTK_WIDGET (ptr)), x, y);
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_nativeSetLocation
  (JNIEnv *env, jobject obj, jint x, jint y)
{
  gdk_threads_enter ();

  Java_gnu_java_awt_peer_gtk_GtkWindowPeer_nativeSetLocationUnlocked
    (env, obj, x, y);

  gdk_threads_leave ();
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkWindowPeer_nativeSetBoundsUnlocked
  (JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
  void *ptr;
  gint current_width;
  gint current_height;

  ptr = gtkpeer_get_widget (env, obj);

  /* Avoid GTK runtime assertion failures. */
  width = (width < 1) ? 1 : width;
  height = (height < 1) ? 1 : height;

  gtk_window_move (GTK_WINDOW(ptr), x, y);
  /* The call to GDK_KEY_window_move is needed in addition to the call to
     gtk_window_move.  If GDK_KEY_window_move isn't called, then the
     following set of operations doesn't give the expected results:

     1. show a window
     2. manually move it to another position on the screen
     3. hide the window
     4. reposition the window with Component.setLocation
     5. show the window

     Instead of being at the position set by setLocation, the window
     is reshown at the position to which it was moved manually. */
  if (gtk_widget_get_window(GTK_WIDGET (ptr)) != NULL)
    gdk_window_move (gtk_widget_get_window(GTK_WIDGET (ptr)), x, y);

  /* Only request resizing if the actual width or height change, otherwise
   * we get unnecessary flickers because resizing causes GTK to clear the
   * window content, even if the actual size doesn't change. */
  gtk_window_get_size(GTK_WINDOW(ptr), &current_width, &current_height);
  if (current_width != width || current_height != height)
    {
      /* Need to change the widget's request size. */
      gtk_widget_set_size_request (GTK_WIDGET(ptr), width, height);
      /* Also need to call gtk_window_resize.  If the resize is requested
	 by the program and the window's "resizable" property is true then
	 the size request will not be honoured. */
      gtk_window_resize (GTK_WINDOW (ptr), width, height);
    }
}

static void
window_get_frame_extents (GtkWidget *window,
                          int *top, int *left, int *bottom, int *right)
{
  unsigned long *extents = NULL;
  union extents_union gu_ex;

  /* Guess frame extents in case _NET_FRAME_EXTENTS is not
     supported. */
  if (!gtk_window_get_decorated (GTK_WINDOW (window)))
    {
      *top = 0;
      *left = 0;
      *bottom = 0;
      *right = 0;

      return;
    }

  *top = 23;
  *left = 6;
  *bottom = 6;
  *right = 6;

  /* Request that the window manager set window's
     _NET_FRAME_EXTENTS property. */
  request_frame_extents (window);

  /* Attempt to retrieve window's frame extents. */
  gu_ex.extents = &extents;
  if (gdk_property_get (gtk_widget_get_window(window),
                        gdk_atom_intern ("_NET_FRAME_EXTENTS", FALSE),
                        gdk_atom_intern ("CARDINAL", FALSE),
                        0,
                        sizeof (unsigned long) * 4,
                        FALSE,
                        NULL,
                        NULL,
                        NULL,
                        gu_ex.gu_extents))
    {
      *left = extents [0];
      *right = extents [1];
      *top = extents [2];
      *bottom = extents [3];
    }
}

static Atom extents_atom = 0;

/* Requests that the window manager set window's
   _NET_FRAME_EXTENTS property. */
static void
request_frame_extents (GtkWidget *window)
{
  const char *request_str = "_NET_REQUEST_FRAME_EXTENTS";
  GdkAtom request_extents = gdk_atom_intern (request_str, FALSE);

  /* Check if the current window manager supports
     _NET_REQUEST_FRAME_EXTENTS. */
  //if (gdk_net_wm_supports (request_extents))
    if (gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default(), request_extents))
    {
      GdkDisplay *display = gtk_widget_get_display (window);
      Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);

      GdkWindow *root_window = gdk_get_default_root_window ();
      Window xroot_window = GDK_WINDOW_XID (root_window);

      Atom extents_request_atom =
	gdk_x11_get_xatom_by_name_for_display (display, request_str);

      XEvent xevent;
      XEvent notify_xevent;

      //unsigned long window_id = GDK_WINDOW_XID (GDK_DRAWABLE(gtk_widget_get_window(window)));
      unsigned long window_id = GDK_WINDOW_XID (gtk_widget_get_window(window));
      if (!extents_atom)
	{
	  const char *extents_str = "_NET_FRAME_EXTENTS";
	  extents_atom =
	    gdk_x11_get_xatom_by_name_for_display (display, extents_str);
	}

      xevent.xclient.type = ClientMessage;
      xevent.xclient.message_type = extents_request_atom;
      xevent.xclient.display = xdisplay;
      xevent.xclient.window = window_id;
      xevent.xclient.format = 32;
      xevent.xclient.data.l[0] = 0;
      xevent.xclient.data.l[1] = 0;
      xevent.xclient.data.l[2] = 0;
      xevent.xclient.data.l[3] = 0;
      xevent.xclient.data.l[4] = 0;

      XSendEvent (xdisplay, xroot_window, False,
		  (SubstructureRedirectMask | SubstructureNotifyMask),
                  &xevent);

      XIfEvent(xdisplay, &notify_xevent,
	       property_notify_predicate, (XPointer) &window_id);
    }
}

static Bool
property_notify_predicate (Display *xdisplay __attribute__((unused)),
                           XEvent  *event,
                           XPointer window_id)
{
  unsigned long *window = (unsigned long *) window_id;

  if (event->xany.type == PropertyNotify
      && event->xany.window == *window
      && event->xproperty.atom == extents_atom)
    return True;
  else
    return False;
}

static gboolean
window_delete_cb (GtkWidget *widget __attribute__((unused)),
		  GdkEvent *event __attribute__((unused)),
		  jobject peer)
{
  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
			      postWindowEventID,
			      (jint) AWT_WINDOW_CLOSING,
			      (jobject) NULL, (jint) 0);

  /* Prevents that the Window dissappears ("destroy"
     not being signalled). This is necessary because it
     should be up to a WindowListener implementation
     how the AWT Frame responds to close requests. */
  return TRUE;
}

static void
window_destroy_cb (GtkWidget *widget __attribute__((unused)),
		   GdkEvent *event __attribute__((unused)),
		   jobject peer)
{
  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
			      postWindowEventID,
			      (jint) AWT_WINDOW_CLOSED,
			      (jobject) NULL, (jint) 0);
}

static void
window_focus_state_change_cb (GtkWidget *widget,
			      GParamSpec *pspec __attribute__((unused)),
			      jobject peer)
{
  if (gtk_window_has_toplevel_focus(GTK_WINDOW (widget)))
    (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
                                postWindowEventID,
                                (jint) AWT_WINDOW_ACTIVATED,
                                (jobject) NULL, (jint) 0);
  else
    (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
                                postWindowEventID,
                                (jint) AWT_WINDOW_DEACTIVATED,
                                (jobject) NULL, (jint) 0);
}

static gboolean
window_focus_in_cb (GtkWidget * widget  __attribute__((unused)),
		    GdkEventFocus *event  __attribute__((unused)),
		    jobject peer)
{
  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
                              postWindowEventID,
                              (jint) AWT_WINDOW_GAINED_FOCUS,
                              (jobject) NULL, (jint) 0);

  return FALSE;
}

static gboolean
window_focus_out_cb (GtkWidget * widget __attribute__((unused)),
		     GdkEventFocus *event __attribute__((unused)),
		     jobject peer)
{
  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
                              postWindowEventID,
                              (jint) AWT_WINDOW_LOST_FOCUS,
                              (jobject) NULL, (jint) 0);

  return FALSE;
}

static gboolean
window_window_state_cb (GtkWidget *widget __attribute__((unused)),
			GdkEvent *event,
			jobject peer)
{
  jint new_java_state = 0;
  /* Put together the new state and let the java side figure out what
   * to post */
  GdkWindowState new_state = event->window_state.new_window_state;
  /* The window can be either iconfified, maximized, iconified + maximized
   * or normal. */
  if ((new_state & GDK_WINDOW_STATE_ICONIFIED) != 0)
    new_java_state |= AWT_FRAME_ICONIFIED;
  if ((new_state & GDK_WINDOW_STATE_MAXIMIZED) != 0)
    new_java_state |= AWT_FRAME_MAXIMIZED_BOTH;
  if ((new_state & (GDK_WINDOW_STATE_MAXIMIZED | GDK_WINDOW_STATE_ICONIFIED))
      == 0)
    new_java_state = AWT_FRAME_NORMAL;

  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
			      postWindowEventID,
			      (jint) AWT_WINDOW_STATE_CHANGED,
			      (jobject) NULL, new_java_state);

  return TRUE;
}

static gboolean
window_property_changed_cb (GtkWidget *widget __attribute__((unused)),
                            GdkEventProperty *event,
                            jobject peer)
{
  unsigned long *extents;
  union extents_union gu_ex;

  gu_ex.extents = &extents;
  if (gdk_atom_intern ("_NET_FRAME_EXTENTS", FALSE) == event->atom
      && gdk_property_get (event->window,
                           gdk_atom_intern ("_NET_FRAME_EXTENTS", FALSE),
                           gdk_atom_intern ("CARDINAL", FALSE),
                           0,
                           sizeof (unsigned long) * 4,
                           FALSE,
                           NULL,
                           NULL,
                           NULL,
                           gu_ex.gu_extents))
    {
      (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
				    postInsetsChangedEventID,
				    (jint) extents[2],  /* top */
				    (jint) extents[0],  /* left */
				    (jint) extents[3],  /* bottom */
				    (jint) extents[1]); /* right */
    }
  

  return FALSE;
}

static void
realize_cb (GtkWidget *widget, jobject peer)
{
  jint top = 0;
  jint left = 0;
  jint bottom = 0;
  jint right = 0;
  jint width = 0;
  jint height = 0;

  width = (*cp_gtk_gdk_env())->CallIntMethod (cp_gtk_gdk_env(), peer, windowGetWidthID);
  height = (*cp_gtk_gdk_env())->CallIntMethod (cp_gtk_gdk_env(), peer, windowGetHeightID);

  window_get_frame_extents (widget, &top, &left, &bottom, &right);

  (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
				postInsetsChangedEventID,
				top, left, bottom, right);

  gtk_window_set_default_size (GTK_WINDOW (widget),
			       MAX (1, width - left - right),
			       MAX (1, height - top - bottom));

  /* set the size like we do in nativeSetBounds */
  gtk_widget_set_size_request (widget,
			       MAX (1, width - left - right),
			       MAX (1, height - top - bottom));

  gtk_window_resize (GTK_WINDOW (widget),
		     MAX (1, width - left - right),
		     MAX (1, height - top - bottom));
}

/*
 * This method returns a GDK keyval that corresponds to one of the
 * keysyms in the X keymap table.  The return value is only used to
 * determine the keyval's corresponding hardware keycode, and doesn't
 * reflect an accurate translation of a Java virtual key value to a
 * GDK keyval.
 */
#ifdef __GNUC__
__inline
#endif
guint
cp_gtk_awt_keycode_to_keysym (jint keyCode, jint keyLocation)
{
  /* GDK_KEY_A through GDK_KEY_Z */
  if (keyCode >= VK_A && keyCode <= VK_Z)
    return gdk_keyval_to_lower (keyCode);

  /* GDK_KEY_0 through GDK_KEY_9 */
  if (keyCode >= VK_0 && keyCode <= VK_9)
    return keyCode;

  switch (keyCode)
    {
    case VK_ENTER:
      return keyLocation == AWT_KEY_LOCATION_NUMPAD ? GDK_KEY_KP_Enter : GDK_KEY_Return;
    case VK_BACK_SPACE:
      return GDK_KEY_BackSpace;
    case VK_TAB:
      return GDK_KEY_Tab;
    case VK_CANCEL:
      return GDK_KEY_Cancel;
    case VK_CLEAR:
      return GDK_KEY_Clear;
    case VK_SHIFT:
      return keyLocation == AWT_KEY_LOCATION_LEFT ? GDK_KEY_Shift_L : GDK_KEY_Shift_R;
    case VK_CONTROL:
      return keyLocation == AWT_KEY_LOCATION_LEFT ? GDK_KEY_Control_L : GDK_KEY_Control_R;
    case VK_ALT:
      return keyLocation == AWT_KEY_LOCATION_LEFT ? GDK_KEY_Alt_L : GDK_KEY_Alt_R;
    case VK_PAUSE:
      return GDK_KEY_Pause;
    case VK_CAPS_LOCK:
      return GDK_KEY_Caps_Lock;
    case VK_ESCAPE:
      return GDK_KEY_Escape;
    case VK_SPACE:
      return GDK_KEY_space;
    case VK_PAGE_UP:
      return keyLocation == AWT_KEY_LOCATION_NUMPAD ? GDK_KEY_KP_Page_Up : GDK_KEY_Page_Up;
    case VK_PAGE_DOWN:
      return keyLocation == AWT_KEY_LOCATION_NUMPAD ? GDK_KEY_KP_Page_Down : GDK_KEY_Page_Down;
    case VK_END:
      return keyLocation == AWT_KEY_LOCATION_NUMPAD ? GDK_KEY_KP_End : GDK_KEY_End;
    case VK_HOME:
      return keyLocation == AWT_KEY_LOCATION_NUMPAD ? GDK_KEY_KP_Home : GDK_KEY_Home;
    case VK_LEFT:
      return GDK_KEY_Left;
    case VK_UP:
      return GDK_KEY_Up;
    case VK_RIGHT:
      return GDK_KEY_Right;
    case VK_DOWN:
      return GDK_KEY_Down;
    case VK_COMMA:
      return GDK_KEY_comma;
    case VK_MINUS:
      return GDK_KEY_minus;
    case VK_PERIOD:
      return GDK_KEY_period;
    case VK_SLASH:
      return GDK_KEY_slash;
      /*
    case VK_0:
    case VK_1:
    case VK_2:
    case VK_3:
    case VK_4:
    case VK_5:
    case VK_6:
    case VK_7:
    case VK_8:
    case VK_9:
      */
    case VK_SEMICOLON:
      return GDK_KEY_semicolon;
    case VK_EQUALS:
      return GDK_KEY_equal;
      /*
    case VK_A:
    case VK_B:
    case VK_C:
    case VK_D:
    case VK_E:
    case VK_F:
    case VK_G:
    case VK_H:
    case VK_I:
    case VK_J:
    case VK_K:
    case VK_L:
    case VK_M:
    case VK_N:
    case VK_O:
    case VK_P:
    case VK_Q:
    case VK_R:
    case VK_S:
    case VK_T:
    case VK_U:
    case VK_V:
    case VK_W:
    case VK_X:
    case VK_Y:
    case VK_Z:
      */
    case VK_OPEN_BRACKET:
      return GDK_KEY_bracketleft;
    case VK_BACK_SLASH:
      return GDK_KEY_backslash;
    case VK_CLOSE_BRACKET:
      return GDK_KEY_bracketright;
    case VK_NUMPAD0:
      return GDK_KEY_KP_0;
    case VK_NUMPAD1:
      return GDK_KEY_KP_1;
    case VK_NUMPAD2:
      return GDK_KEY_KP_2;
    case VK_NUMPAD3:
      return GDK_KEY_KP_3;
    case VK_NUMPAD4:
      return GDK_KEY_KP_4;
    case VK_NUMPAD5:
      return GDK_KEY_KP_5;
    case VK_NUMPAD6:
      return GDK_KEY_KP_6;
    case VK_NUMPAD7:
      return GDK_KEY_KP_7;
    case VK_NUMPAD8:
      return GDK_KEY_KP_8;
    case VK_NUMPAD9:
      return GDK_KEY_KP_9;
    case VK_MULTIPLY:
      return GDK_KEY_KP_Multiply;
    case VK_ADD:
      return GDK_KEY_KP_Add;
      /*
    case VK_SEPARATER:
      */
    case VK_SEPARATOR:
      return GDK_KEY_KP_Separator;
    case VK_SUBTRACT:
      return GDK_KEY_KP_Subtract;
    case VK_DECIMAL:
      return GDK_KEY_KP_Decimal;
    case VK_DIVIDE:
      return GDK_KEY_KP_Divide;
    case VK_DELETE:
      return keyLocation == AWT_KEY_LOCATION_NUMPAD ? GDK_KEY_KP_Delete : GDK_KEY_Delete;
    case VK_NUM_LOCK:
      return GDK_KEY_Num_Lock;
    case VK_SCROLL_LOCK:
      return GDK_KEY_Scroll_Lock;
    case VK_F1:
      return GDK_KEY_F1;
    case VK_F2:
      return GDK_KEY_F2;
    case VK_F3:
      return GDK_KEY_F3;
    case VK_F4:
      return GDK_KEY_F4;
    case VK_F5:
      return GDK_KEY_F5;
    case VK_F6:
      return GDK_KEY_F6;
    case VK_F7:
      return GDK_KEY_F7;
    case VK_F8:
      return GDK_KEY_F8;
    case VK_F9:
      return GDK_KEY_F9;
    case VK_F10:
      return GDK_KEY_F10;
    case VK_F11:
      return GDK_KEY_F11;
    case VK_F12:
      return GDK_KEY_F12;
    case VK_F13:
      return GDK_KEY_F13;
    case VK_F14:
      return GDK_KEY_F14;
    case VK_F15:
      return GDK_KEY_F15;
    case VK_F16:
      return GDK_KEY_F16;
    case VK_F17:
      return GDK_KEY_F17;
    case VK_F18:
      return GDK_KEY_F18;
    case VK_F19:
      return GDK_KEY_F19;
    case VK_F20:
      return GDK_KEY_F20;
    case VK_F21:
      return GDK_KEY_F21;
    case VK_F22:
      return GDK_KEY_F22;
    case VK_F23:
      return GDK_KEY_F23;
    case VK_F24:
      return GDK_KEY_F24;
    case VK_PRINTSCREEN:
      return GDK_KEY_Print;
    case VK_INSERT:
      return keyLocation == AWT_KEY_LOCATION_NUMPAD ? GDK_KEY_KP_Insert : GDK_KEY_Insert;
    case VK_HELP:
      return GDK_KEY_Help;
    case VK_META:
      return keyLocation == AWT_KEY_LOCATION_LEFT ? GDK_KEY_Meta_L : GDK_KEY_Meta_R;
    case VK_BACK_QUOTE:
      return GDK_KEY_grave;
    case VK_QUOTE:
      return GDK_KEY_apostrophe;
    case VK_KP_UP:
      return GDK_KEY_KP_Up;
    case VK_KP_DOWN:
      return GDK_KEY_KP_Down;
    case VK_KP_LEFT:
      return GDK_KEY_KP_Left;
    case VK_KP_RIGHT:
      return GDK_KEY_KP_Right;
    case VK_DEAD_GRAVE:
      return GDK_KEY_dead_grave;
    case VK_DEAD_ACUTE:
      return GDK_KEY_dead_acute;
    case VK_DEAD_CIRCUMFLEX:
      return GDK_KEY_dead_circumflex;
    case VK_DEAD_TILDE:
      return GDK_KEY_dead_tilde;
    case VK_DEAD_MACRON:
      return GDK_KEY_dead_macron;
    case VK_DEAD_BREVE:
      return GDK_KEY_dead_breve;
    case VK_DEAD_ABOVEDOT:
      return GDK_KEY_dead_abovedot;
    case VK_DEAD_DIAERESIS:
      return GDK_KEY_dead_diaeresis;
    case VK_DEAD_ABOVERING:
      return GDK_KEY_dead_abovering;
    case VK_DEAD_DOUBLEACUTE:
      return GDK_KEY_dead_doubleacute;
    case VK_DEAD_CARON:
      return GDK_KEY_dead_caron;
    case VK_DEAD_CEDILLA:
      return GDK_KEY_dead_cedilla;
    case VK_DEAD_OGONEK:
      return GDK_KEY_dead_ogonek;
    case VK_DEAD_IOTA:
      return GDK_KEY_dead_iota;
    case VK_DEAD_VOICED_SOUND:
      return GDK_KEY_dead_voiced_sound;
    case VK_DEAD_SEMIVOICED_SOUND:
      return GDK_KEY_dead_semivoiced_sound;
    case VK_AMPERSAND:
      return GDK_KEY_ampersand;
    case VK_ASTERISK:
      return GDK_KEY_asterisk;
    case VK_QUOTEDBL:
      return GDK_KEY_quotedbl;
    case VK_LESS:
      return GDK_KEY_less;
    case VK_GREATER:
      return GDK_KEY_greater;
    case VK_BRACELEFT:
      return GDK_KEY_braceleft;
    case VK_BRACERIGHT:
      return GDK_KEY_braceright;
    case VK_AT:
      return GDK_KEY_at;
    case VK_COLON:
      return GDK_KEY_colon;
    case VK_CIRCUMFLEX:
      return GDK_KEY_asciicircum;
    case VK_DOLLAR:
      return GDK_KEY_dollar;
    case VK_EURO_SIGN:
      return GDK_KEY_EuroSign;
    case VK_EXCLAMATION_MARK:
      return GDK_KEY_exclam;
    case VK_INVERTED_EXCLAMATION_MARK:
      return GDK_KEY_exclamdown;
    case VK_LEFT_PARENTHESIS:
      return GDK_KEY_parenleft;
    case VK_NUMBER_SIGN:
      return GDK_KEY_numbersign;
    case VK_PLUS:
      return GDK_KEY_plus;
    case VK_RIGHT_PARENTHESIS:
      return GDK_KEY_parenright;
    case VK_UNDERSCORE:
      return GDK_KEY_underscore;
      /*
    case VK_FINAL:
    case VK_CONVERT:
    case VK_NONCONVERT:
    case VK_ACCEPT:
      */
    case VK_MODECHANGE:
      return GDK_KEY_Mode_switch;
      /*
    case VK_KANA:
      */
    case VK_KANJI:
      return GDK_KEY_Kanji;
      /*
    case VK_ALPHANUMERIC:
      */
    case VK_KATAKANA:
      return GDK_KEY_Katakana;
    case VK_HIRAGANA:
      return GDK_KEY_Hiragana;
      /*
    case VK_FULL_WIDTH:
    case VK_HALF_WIDTH:
    case VK_ROMAN_CHARACTERS:
    case VK_ALL_CANDIDATES:
      */
    case VK_PREVIOUS_CANDIDATE:
      return GDK_KEY_PreviousCandidate;
    case VK_CODE_INPUT:
      return GDK_KEY_Codeinput;
      /*
    case VK_JAPANESE_KATAKANA:
    case VK_JAPANESE_HIRAGANA:
    case VK_JAPANESE_ROMAN:
      */
    case VK_KANA_LOCK:
      return GDK_KEY_Kana_Lock;
      /*
    case VK_INPUT_METHOD_ON_OFF:
    case VK_CUT:
    case VK_COPY:
    case VK_PASTE:
      */
    case VK_UNDO:
      return GDK_KEY_Undo;
    case VK_AGAIN:
      return GDK_KEY_Redo;
      /*
    case VK_FIND:
    case VK_PROPS:
    case VK_STOP:
    case VK_COMPOSE:
      */
    case VK_ALT_GRAPH:
      return GDK_KEY_ISO_Level3_Shift;
      /*
	case VK_BEGIN:
      */
    case VK_CONTEXT_MENU:
      return GDK_KEY_Menu;
    case VK_WINDOWS:
      return GDK_KEY_Super_R;

    default:
      return GDK_KEY_VoidSymbol;
    }
}