This file is indexed.

/usr/share/ada/adainclude/gtkada/misc.c is in libgtkada16.1.0-dev 17.0.2017-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
/*
-----------------------------------------------------------------------
--               GtkAda - Ada95 binding for Gtk+/Gnome               --
--                                                                   --
--   Copyright (C) 1998-2000 E. Briot, J. Brobecker and A. Charlet   --
--                Copyright (C) 2000-2017, AdaCore                   --
--                                                                   --
-- This library 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 3 of the License, or (at your option) any later version.  --
--                                                                   --
-- This library 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 this library; if not, write to the             --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      --
-- Boston, MA 02111-1307, USA.                                       --
--                                                                   --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-----------------------------------------------------------------------
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <pango/pango.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>

#ifdef GDK_WINDOWING_QUARTZ
#include "misc_osx.h"
#endif

#ifdef GDK_WINDOWING_WIN32
#include <windows.h>
#include <ddeml.h>
#endif

// Workaround bug in gtk+ 3.14.5
#ifndef GTK_IS_PLUG
int GTK_IS_PLUG(void* object) { return 0; }
#endif

/********************************************************************
 *  Returns the major/minor/macro version number of Gtk+. This is
 *  needed as the windows version uses a different convention for the
 *  corresponding variables gtk_{major/minor/micro)_version than the
 *  unix version.
 ********************************************************************/

guint
ada_gtk_major_version ()
{
  return GTK_MAJOR_VERSION;
}

guint
ada_gtk_minor_version ()
{
  return GTK_MINOR_VERSION;
}

guint
ada_gtk_micro_version ()
{
  return GTK_MICRO_VERSION;
}

/********************************************************************
 **  wrappers for functions which vary on Windows
 ********************************************************************/

gchar *
ada_g_filename_from_uri (const gchar *uri,
			 gchar **hostname,
			 GError **error) {
#ifdef GDK_WINDOWING_WIN32
  return g_filename_from_uri_utf8 (uri, hostname, error);
#else
  return g_filename_from_uri (uri, hostname, error);
#endif
}

gchar *
ada_g_filename_from_utf8 (const gchar *utf8string,
		      gssize len,
		      gsize *bytes_read,
		      gsize *bytes_written,
			  GError **error)
{
#ifdef GDK_WINDOWING_WIN32
  return g_filename_from_utf8_utf8
    (utf8string, len, bytes_read, bytes_written, error);
#else
  return g_filename_from_utf8
    (utf8string, len, bytes_read, bytes_written, error);
#endif
}

gchar *
ada_g_filename_to_uri (const gchar *filename,
		       const gchar *hostname,
		       GError **error)
{
#ifdef GDK_WINDOWING_WIN32
  return g_filename_to_uri_utf8 (filename, hostname, error);
#else
  return g_filename_to_uri (filename, hostname, error);
#endif
}

gchar *
ada_g_filename_to_utf8 (const gchar *opsysstring,
			gssize len,
			gsize *bytes_read,
			gsize *bytes_written,
			GError **error)
{
#ifdef GDK_WINDOWING_WIN32
  return g_filename_to_utf8_utf8
    (opsysstring, len, bytes_read, bytes_written, error);
#else
  return g_filename_to_utf8
    (opsysstring, len, bytes_read, bytes_written, error);
#endif
}

GdkPixbuf *
ada_gdk_pixbuf_new_from_file (const char *filename,
			      GError **error)
{
#ifdef GDK_WINDOWING_WIN32
  return gdk_pixbuf_new_from_file_utf8 (filename, error);
#else
  return gdk_pixbuf_new_from_file (filename, error);
#endif
}

GdkPixbufAnimation *
ada_gdk_pixbuf_animation_new_from_file (const char *filename,
					GError **error)
{
#ifdef GDK_WINDOWING_WIN32
  return gdk_pixbuf_animation_new_from_file_utf8 (filename, error);
#else
  return gdk_pixbuf_animation_new_from_file (filename, error);
#endif
}

/********************************************************************
 ** gmodule wrappers, deactivated under Windows
 ********************************************************************/

#ifdef GDK_WINDOWING_WIN32
gboolean ada_g_module_supported (void) { return FALSE; };

gchar*
ada_g_module_build_path (const gchar *directory,
			 const gchar *module_name)
{
  return "";
}
GModule*
ada_g_module_open (const gchar *file_name,
	       GModuleFlags flags)
{
  return 0;
}

gboolean
ada_g_module_close (GModule *module)
{
  return TRUE;
}

gboolean
ada_g_module_symbol (GModule *module,
		 const gchar *symbol_name,
		 gpointer *symbol)
{
  return FALSE;
}

const gchar*
ada_g_module_name (GModule *module)
{
  return "";
}

const gchar*
ada_g_module_error (void)
{
  return "modules not supported under Windows";
}
#else
gboolean ada_g_module_supported (void)
{
  return g_module_supported();
};

gchar*
ada_g_module_build_path (const gchar *directory,
			 const gchar *module_name)
{
  return g_module_build_path (directory, module_name);
}
GModule*
ada_g_module_open (const gchar *file_name,
	       GModuleFlags flags)
{
  return g_module_open (file_name, flags);
}

gboolean
ada_g_module_close (GModule *module)
{
  return g_module_close (module);
}

gboolean
ada_g_module_symbol (GModule *module,
		 const gchar *symbol_name,
		 gpointer *symbol)
{
  return g_module_symbol (module, symbol_name, symbol);
}

const gchar*
ada_g_module_name (GModule *module)
{
  return g_module_name (module);
}

const gchar*
ada_g_module_error (void)
{
  return g_module_error ();
}
#endif

/********************************************************************
 **  var_arg wrappers.
 ********************************************************************/

gpointer
ada_g_object_new (GType object_type)
{
  return g_object_new (object_type, NULL);
}

gpointer
ada_g_dialog_new (GType object_type, GtkDialogFlags flags)
{
   return g_object_new
      (object_type,
       "use-header-bar", (flags & GTK_DIALOG_USE_HEADER_BAR) != 0,
       NULL);
}

void
ada_g_object_get_ulong (gpointer object,
		        const gchar *property_name,
		        gulong *property)
{
  g_object_get (object, property_name, property, NULL);
}


void
ada_g_object_set_string (gpointer object,
			 const gchar *property_name,
			 const gchar *property)
{
  g_object_set (object, property_name, property, NULL);
}

void
ada_g_object_set_int (gpointer object,
		      const gchar *property_name,
		      gint property)
{
  g_object_set (object, property_name, property, NULL);
}

void
ada_g_object_set_ulong (gpointer object,
		        const gchar *property_name,
		        gulong property)
{
  g_object_set (object, property_name, property, NULL);
}

void
ada_g_object_set_ptr (gpointer object,
		      const gchar *property_name,
		      void *property)
{
  g_object_set (object, property_name, property, NULL);
}

void
ada_g_object_set_float (gpointer object,
			const gchar *property_name,
			gfloat property)
{
  g_object_set (object, property_name, property, NULL);
}

void
ada_g_object_set_double (gpointer object,
			 const gchar *property_name,
			 gdouble property)
{
  g_object_set (object, property_name, property, NULL);
}

void
ada_g_signal_emit_by_name (gpointer     instance,
			   const gchar *detailed_signal)
{
  g_signal_emit_by_name (instance, detailed_signal);
}

void
ada_g_signal_emit_by_name_ptr (gpointer     instance,
			       const gchar *detailed_signal,
			       void *arg)
{
  g_signal_emit_by_name (instance, detailed_signal, arg);
}

void
ada_g_signal_emit_by_name_ptr_ptr (gpointer     instance,
			           const gchar *detailed_signal,
			           void *arg1,
			           void *arg2)
{
  g_signal_emit_by_name (instance, detailed_signal, arg1, arg2);
}

void
ada_g_signal_emit_by_name_int_ptr (gpointer     instance,
			           const gchar *detailed_signal,
			           gint arg1,
			           void *arg2)
{
  g_signal_emit_by_name (instance, detailed_signal, arg1, arg2);
}

void
ada_gtk_list_store_set_ptr (GtkListStore *list_store,
                            GtkTreeIter  *iter,
                            gint          col,
                            void         *val)
{
  gtk_list_store_set (list_store, iter, col, val, -1);
}


void
ada_gtk_list_store_set_int (GtkListStore *list_store,
                            GtkTreeIter  *iter,
                            gint          col,
                            gint          val)
{
  gtk_list_store_set (list_store, iter, col, val, -1);
}

void
ada_gtk_tree_store_set_ptr (GtkTreeStore *tree_store,
			    GtkTreeIter  *iter,
			    gint          col,
			    void         *val)
{
  gtk_tree_store_set (tree_store, iter, col, val, -1);
}

void
ada_gtk_tree_store_set_int (GtkTreeStore *tree_store,
			    GtkTreeIter  *iter,
			    gint          col,
			    gint          val)
{
  gtk_tree_store_set (tree_store, iter, col, val, -1);
}

void
ada_gtk_tree_store_set_ulong (GtkTreeStore *list_store,
                            GtkTreeIter  *iter,
                            gint          col,
                            gulong        val)
{
  gtk_tree_store_set (list_store, iter, col, val, -1);
}


GtkWidget*
ada_gtk_dialog_new_with_buttons (const gchar     *title,
                                 GtkWindow       *parent,
                                 GtkDialogFlags   flags)
{
  return gtk_dialog_new_with_buttons (title, parent, flags,
                                      NULL /* first_button_text*/, NULL);
}

gboolean
ada_gdk_pixbuf_save (GdkPixbuf  *pixbuf,
		     const char *filename,
		     const char *type,
		     GError    **error,
		     char       *key,
		     char       *value)
{
  return gdk_pixbuf_save (pixbuf, filename, type, error, key, value, NULL);
}

void
ada_g_log (const gchar    *log_domain,
	   GLogLevelFlags  log_level,
	   const gchar    *message)
{
  g_log (log_domain, log_level, "%s", message);
}

void
c_sprintf (char *s, char *format, int arg1, int arg2, int arg3)
{
  sprintf (s, format, arg1, arg2, arg3);
}

void
ada_gtk_tree_model_get (GtkTreeModel *tree_model,
                        GtkTreeIter  *iter,
			gint         column,
			void         *data)
{
   gtk_tree_model_get (tree_model, iter, column, data, -1);
}

/********************************************************************
 **  This function should only be used for debug purposes.
 ********************************************************************/

guint
ada_gtk_debug_get_ref_count (GObject* object) {
  return G_OBJECT (object)->ref_count;
}

/******************************************
 ** GSignal                              **
 ******************************************/

const gchar*
ada_gsignal_query_signal_name (GSignalQuery* query)
{
  return query->signal_name;
}

const GType*
ada_gsignal_query_params (GSignalQuery* query, guint* n_params)
{
  *n_params = query->n_params;
  return query->param_types;
}

guint
ada_gsignal_query_id (GSignalQuery* query)
{
  return query->signal_id;
}

GType
ada_gsignal_query_return_type (GSignalQuery* query)
{
  return query->return_type;
}

/*********************************************************************
 ** Creating new widgets
 ** For each new widget created by the user, we create a new
 ** class record, that has the following layout:
 **
 **  struct NewClassRecord {
 **     struct AncestorClass ancestor_class;   // the ancestor
 **     void (*handler1) (...);                // handler for first signal
 **     void (*handler2) (...);                // handler for second signal
 **     ...
 **     void (*handlern) (...);                // handler for nth signal
 **     GObjectGetPropertyFunc real_get_property;
 **                                            // pointer to the get_property
 **                                            // in user's code
 **     GObjectSetPropertyFunc real_set_property;
 **                                            // likewise for set_property
 **  };
 *********************************************************************/

typedef struct {
   GType type;            // The type. This also acts as a lock while
                          // initializing the class record.
   void (*class_init)(gpointer type);           // ada class init
   GObjectSetPropertyFunc ada_property_setter;
   GObjectGetPropertyFunc ada_property_getter;
} AdaGObjectClassRecord;
typedef AdaGObjectClassRecord* AdaGObjectClass;
//  Type must be synchronized with Ada.

GType ada_type_from_class (GObjectClass* klass)
{
  return G_TYPE_FROM_CLASS (klass);
}

/** Reuse the dummy padding pointers set by gtk+. They are never used by
 *  gtk+, and just there for binary compatibility. Simpler to use them than
 *  do pointer arithmetics to find the pointer on the class structure.
 */
#define ADA_CLASS_FROM_C_CLASS(class) (AdaGObjectClass)(class->pdummy[0])
#define SET_ADA_CLASS_FROM_C_CLASS(class, adaclass) \
   (class->pdummy[0] = (gpointer)adaclass)

AdaGObjectClass ada_gobject_class_from_object(GObject* object) {
   return ADA_CLASS_FROM_C_CLASS(G_OBJECT_GET_CLASS(object));
}


/** The class_init for the gobject types created in Ada
 * The class_data is of type AdaGObjectClass, as set by
 * ada_initialize_class_record
 **/

static void
ada_class_record_init (GObjectClass* klass, gpointer class_data)
{
   GType type = G_TYPE_FROM_CLASS (klass);
   GType parent = g_type_parent (type);
   GTypeQuery query;
   GTypeQuery parent_query;
   const AdaGObjectClass ada_klass = (AdaGObjectClass)class_data;

   g_type_query (type, &query);
   g_type_query (parent, &parent_query);

   /* Initialize the function pointers for the new signals to NULL */

   memset ((char*)(klass) + parent_query.class_size, 0,
           query.class_size - parent_query.class_size);

   /* Set a pointer to the AdaGObjectClass, so that we can retrieve it
    * later from a type.
    */
   SET_ADA_CLASS_FROM_C_CLASS(klass, ada_klass);

   if (ada_klass->class_init) {
      ada_klass->class_init (klass);
   }
}

int
ada_initialize_class_record
  (GType         ancestor,
   gint          nsignals,
   char*         signals[],
   GType         parameters[],
   gint          max_parameters,
   GType         returns[],
   gint          max_returns,
   AdaGObjectClass klass,
   gchar*        type_name)
{
   // Make this function thread-safe and ensure we only initialize the class
   // once
   if (g_once_init_enter (&klass->type)) {
       /* Note: The memory allocated in this function is never freed. No need
          to worry, since this is only allocated once per user's widget type,
          and might be used until the end of the application */

       GTypeQuery query;
       int j;

       /* We need to know the ancestor's class/instance sizes */
       g_type_query (ancestor, &query);

       /*************************
        * This code is the equivalent of type_name@@_get_type in C. In Ada, the
        * type will be accessible only once at least one instance of it has
        * been created (whereas in C the GType is created at elaboration time).
        *************************/

       GTypeInfo info;
       info.class_size = query.class_size
              + nsignals * sizeof (void*);
              //+ sizeof (AdaGObjectClass);
       info.base_init = NULL;
       info.base_finalize = NULL;
       info.class_init = (GClassInitFunc)(&ada_class_record_init);
       info.class_finalize = NULL;
       info.class_data = (gconstpointer)klass;
       info.instance_size = query.instance_size;
       info.n_preallocs = 0;
       info.instance_init = NULL;
       info.value_table = NULL;

       GType new_type = g_type_register_static (
             ancestor  /* parent_type */,
             type_name /* type_name */,
             &info     /* info */,
             0         /* flags */);

       /*************************
        * This code is generally called by g_object_new (which itself is called
        * from type_name_new() in C). Its result is to create and initialized
        * (via class_init) the class the first time an instance of it is
        * created. In Ada, we do not us a _class_init, so we initialize the
        * signals immediately after creating the class.
        *************************/

       for (j = 0; j < nsignals; j++) {
          int count = 0;
          GClosure *closure;

          while (count < max_parameters &&
                  (parameters [j * max_parameters + count] != G_TYPE_NONE))
          {
                count++;
          }

          closure = g_signal_type_cclosure_new
              (new_type, query.class_size + j * sizeof (void*)); /* offset */

          GType return_type = G_TYPE_NONE;
          GSignalAccumulator acc = NULL;

          if (j < max_returns) {
             return_type = returns[j];
             if (return_type == G_TYPE_BOOLEAN) {
                acc = g_signal_accumulator_true_handled;
             }
          }

          /* id = */ g_signal_newv
            (signals[j],                       /* signal_name */
             new_type,                         /* itype */
             G_SIGNAL_RUN_LAST,                /* signal_flags */
             closure,                          /* class_closure */
             acc,                              /* accumulator */
             NULL,                             /* accu_data */
             g_cclosure_marshal_VOID__VOID,    /* c_marshaller, unused at the
               Ada level ??? This probably makes the widget unusable from C */
             return_type,                      /* return_type */
             count,                            /* n_params */
             parameters + j * max_parameters); /* param_types */
       }

       /* Do not call g_type_class_ref here, since that would prevent us
        * from adding interfaces later on. Instead, rely on the class_init
        * function
        */
       g_once_init_leave (&klass->type, new_type); // sets klass->type
       return 1;
   }
   return 0;
}

#define ADA_GTK_OVERRIDE_METHOD(TypeName, method) \
   void \
   ada_##TypeName##_override_##method ( \
         GObjectClass* klass, gpointer handler) { \
      if (handler && GTK_IS_##TypeName (klass)) { \
          GTK_##TypeName (klass)->method = handler; \
      } \
   }

ADA_GTK_OVERRIDE_METHOD(WIDGET_CLASS, size_allocate)
ADA_GTK_OVERRIDE_METHOD(WIDGET_CLASS, draw)
ADA_GTK_OVERRIDE_METHOD(WIDGET_CLASS, realize)
ADA_GTK_OVERRIDE_METHOD(WIDGET_CLASS, get_preferred_width)
ADA_GTK_OVERRIDE_METHOD(WIDGET_CLASS, get_preferred_height)
ADA_GTK_OVERRIDE_METHOD(WIDGET_CLASS, get_preferred_width_for_height)
ADA_GTK_OVERRIDE_METHOD(WIDGET_CLASS, get_preferred_height_for_width)

void ada_inherited_WIDGET_CLASS_size_allocate (
      AdaGObjectClass klass, GtkWidget* widget, GtkAllocation* rect)
{
   GObjectClass* objklass = g_type_class_ref (klass->type);
   GObjectClass* parent_class = g_type_class_peek_parent (objklass);
   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, rect);
   g_type_class_unref (objklass);
}

void ada_inherited_WIDGET_CLASS_realize (
      AdaGObjectClass klass, GtkWidget* widget)
{
   GObjectClass* objklass = g_type_class_ref (klass->type);
   GObjectClass* parent_class = g_type_class_peek_parent (objklass);
   GTK_WIDGET_CLASS (parent_class)->realize (widget);
   g_type_class_unref (objklass);
}

void ada_inherited_WIDGET_CLASS_get_preferred_width (
      AdaGObjectClass klass, GtkWidget* widget, gint* min, gint* natural)
{
   GObjectClass* objklass = g_type_class_ref (klass->type);
   GObjectClass* parent_class = g_type_class_peek_parent (objklass);
   GTK_WIDGET_CLASS (parent_class)->get_preferred_width (widget, min, natural);
   g_type_class_unref (objklass);
}

void ada_inherited_WIDGET_CLASS_get_preferred_width_for_height (
      AdaGObjectClass klass, GtkWidget* widget, gint height, gint* min, gint* natural)
{
   GObjectClass* objklass = g_type_class_ref (klass->type);
   GObjectClass* parent_class = g_type_class_peek_parent (objklass);
   GTK_WIDGET_CLASS (parent_class)->get_preferred_width_for_height
      (widget, height, min, natural);
   g_type_class_unref (objklass);
}

void ada_inherited_WIDGET_CLASS_get_preferred_height (
      AdaGObjectClass klass, GtkWidget* widget, gint* min, gint* natural)
{
   GObjectClass* objklass = g_type_class_ref (klass->type);
   GObjectClass* parent_class = g_type_class_peek_parent (objklass);
   GTK_WIDGET_CLASS (parent_class)->get_preferred_height
     (widget, min, natural);
   g_type_class_unref (objklass);
}

void ada_inherited_WIDGET_CLASS_get_preferred_height_for_width (
      AdaGObjectClass klass, GtkWidget* widget, gint width, gint* min, gint* natural)
{
   GObjectClass* objklass = g_type_class_ref (klass->type);
   GObjectClass* parent_class = g_type_class_peek_parent (objklass);
   GTK_WIDGET_CLASS (parent_class)->get_preferred_height_for_width
     (widget, width, min, natural);
   g_type_class_unref (objklass);
}

gboolean ada_inherited_WIDGET_CLASS_draw (
      AdaGObjectClass klass, GtkWidget* widget, cairo_t *cr)
{
   GObjectClass* objklass = g_type_class_ref (klass->type);
   GObjectClass* parent_class = g_type_class_peek_parent (objklass);
   g_type_class_unref (objklass);
   return GTK_WIDGET_CLASS (parent_class)->draw (widget, cr);
}

/*****************************************************
 ** Gtk.Selection and Gtk.Dnd functions
 *****************************************************/

guint ada_gtk_dnd_context_targets_count (GdkDragContext* context)
{
  return g_list_length (gdk_drag_context_list_targets (context));
}

void ada_gtk_dnd_context_get_targets (GdkDragContext* context, GdkAtom* result)
{
  GList *glist = gdk_drag_context_list_targets (context);
  GdkAtom* tmp = result;
  while (glist != NULL)
    {
      *tmp++ = (GdkAtom)glist->data;
//      gchar *name = gdk_atom_name ((GdkAtom)glist->data);
//      *tmp++ = name;
      glist = glist->next;
    }
}

/*
 * Gnode macros
 *
 */

gboolean
ada_gnode_is_root (GNode * node)
{
  return G_NODE_IS_ROOT (node);
}

gboolean
ada_gnode_is_leaf (GNode * node)
{
  return G_NODE_IS_LEAF (node);
}

GNode*
ada_gnode_prev_sibling (GNode * node)
{
  return g_node_prev_sibling (node);
}

GNode*
ada_gnode_next_sibling (GNode * node)
{
  return g_node_next_sibling (node);
}

GNode*
ada_gnode_first_child (GNode * node)
{
  return g_node_first_child (node);
}

/**********************************************************
 **  Support for events
 **********************************************************/

#ifdef _WIN32
#define ada_gdk_invalid_gdouble_value 1.79769313486232e308
#define ada_gdk_invalid_gint_value ((2<<31) - 1)
#define ada_gdk_invalid_guint_value (guint)((2LL<<32) - 1)
#define ada_gdk_invalid_guint32_value (guint32)((2LL<<32) - 1)
#define ada_gdk_invalid_gulong_value (gulong)((2LL<<32) - 1)

#else
extern const gdouble ada_gdk_invalid_gdouble_value;
extern const gint    ada_gdk_invalid_gint_value;
extern const guint   ada_gdk_invalid_guint_value;
extern const guint32 ada_gdk_invalid_guint32_value;
extern const gulong  ada_gdk_invalid_gulong_value;
#endif

GdkAtom
ada_make_atom (gulong num)
{
  return _GDK_MAKE_ATOM (num);
}

GdkEventType
ada_gdk_event_get_event_type (GdkEvent *event) {
  return event->type;
}

guint
ada_gdk_event_get_button (GdkEvent * event)
{
  guint button;
  if (!gdk_event_get_button(event, &button)) {
    return ada_gdk_invalid_guint_value;
  }
  return button;
}

GdkModifierType
ada_gdk_event_get_state (GdkEvent * event)
{
  GdkModifierType state;
  if (!gdk_event_get_state(event, &state)) {
    return ada_gdk_invalid_guint_value;
  }
  return state;
}

guint
ada_gdk_event_get_keyval (GdkEvent * event)
{
  guint keyval;
  if (!gdk_event_get_keyval(event, &keyval)) {
    return ada_gdk_invalid_gint_value;
  }
  return keyval;
}

GdkWindow*
ada_gdk_event_get_window (GdkEvent * event)
{
  return ((GdkEventAny*)event)->window;
}

guint16
ada_gdk_event_get_keycode (GdkEvent * event)
{
  guint16 keycode;
  if (!gdk_event_get_keycode(event, &keycode)) {
    return 0;
  }
  return keycode;
}

/***************************************************
 *  Functions for Objects
 ***************************************************/

GType
ada_gobject_get_type (GObject* object)
{
  return G_OBJECT_TYPE (object);
}

/***************************************************
 *  Functions for GClosure
 ***************************************************/

void*
ada_gclosure_get_data (GClosure *closure)
{
  return closure->data;
}

void*
ada_cclosure_get_callback (GCClosure* closure) {
   return closure->callback;
}

/***************************************************
 *  Functions for GValue
 ***************************************************/

gpointer
ada_gvalue_get_pointer (GValue* value)
{
  return value->data[0].v_pointer;
}

void
ada_gvalue_nth (GValue* value, guint num, GValue* val)
{
  *val = *(value + num);
}

int
ada_c_gvalue_size ()
{
  return sizeof (GValue);
}

void
ada_gvalue_set (GValue* value, void *val)
{
  if G_VALUE_HOLDS_CHAR (value)
    g_value_set_schar (value, *(gchar*)val);
  else if G_VALUE_HOLDS_UCHAR (value)
    g_value_set_uchar (value, *(guchar*)val);
  else if G_VALUE_HOLDS_BOOLEAN (value)
    g_value_set_boolean (value, *(char*)val);
  else if G_VALUE_HOLDS_INT (value)
    g_value_set_int (value, *(gint*)val);
  else if G_VALUE_HOLDS_UINT (value)
    g_value_set_uint (value, *(guint*)val);
  else if G_VALUE_HOLDS_LONG (value)
    g_value_set_long (value, *(glong*)val);
  else if G_VALUE_HOLDS_ULONG (value)
    g_value_set_ulong (value, *(gulong*)val);
  else if G_VALUE_HOLDS_FLOAT (value)
    g_value_set_float (value, *(gfloat*)val);
  else if G_VALUE_HOLDS_DOUBLE (value)
    g_value_set_double (value, *(gdouble*)val);
  else if G_VALUE_HOLDS_POINTER (value)
    g_value_set_pointer (value, *(gpointer*)val);
  else
    fprintf (stderr, "GtkAda: Return value type not supported\n");
}

/**********************************************
 ** Functions for Box
 **********************************************/

GtkWidget*
ada_box_get_child (GtkBox* widget, gint num)
{
  GList * list;
  list = gtk_container_get_children ((GtkContainer*)widget);
  if (list && num < g_list_length (list))
    return ((GtkWidget*) (g_list_nth_data (list, num)));
  return NULL;
}

/**********************************************
 ** Functions for Glib.Glist
 **********************************************/

GList*
ada_list_next (GList* list)
{
  if (list)
    return list->next;
  else
    return NULL;
}

GList*
ada_list_prev (GList* list)
{
  if (list)
    return list->prev;
  else
    return NULL;
}

gpointer
ada_list_get_data (GList* list)
{
  if (list)
     return list->data;
  else
     return NULL;
}

/**********************************************
 ** Functions for Glib.GSlist
 **********************************************/

GSList*
ada_gslist_next (GSList* list)
{
  if (list)
    return list->next;
  else
    return NULL;
}

gpointer
ada_gslist_get_data (GSList* list)
{
  return list->data;
}

gpointer
ada_slist_get_data (GSList* list)
{
  return list->data;
}


/*
 *
 * GdkWindowAttr
 *
 */

GdkWindowAttr*
ada_gdk_window_attr_new (void)
{
  GdkWindowAttr *result;

  result = (GdkWindowAttr*) g_new (GdkWindowAttr, 1);

  if (result)
    {
      result->title = NULL;
      result->visual = NULL;
      result->cursor = NULL;
      result->wmclass_name = NULL;
      result->wmclass_class = NULL;
      /*
       * Here, we only set the pointers to NULL to avoid any dangling
       * pointer. All the other values are left as is. It is the
       * responsibility of the client to make sure they are properly
       * set before they are accessed.
       */
    }

  return result;
}

void
ada_gdk_window_attr_destroy (GdkWindowAttr *window_attr)
{
  g_return_if_fail (window_attr != NULL);

  if (window_attr->title) g_free (window_attr->title);
  if (window_attr->wmclass_name) g_free (window_attr->wmclass_name);
  if (window_attr->wmclass_class) g_free (window_attr->wmclass_class);

  g_free (window_attr);
}

void
ada_gdk_window_attr_set_title (GdkWindowAttr *window_attr,
			       gchar * title)
{
  g_return_if_fail (window_attr != NULL);

  if (window_attr->title) g_free (window_attr->title);
  window_attr->title = g_strdup (title);
}

gchar*
ada_gdk_window_attr_get_title (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, NULL);

  return window_attr->title;
}

void
ada_gdk_window_attr_set_event_mask (GdkWindowAttr *window_attr,
				    gint event_mask)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->event_mask = event_mask;
}

gint
ada_gdk_window_attr_get_event_mask (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, 0);

  return window_attr->event_mask;
}

void
ada_gdk_window_attr_set_x (GdkWindowAttr * window_attr, gint x)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->x = x;
}

gint
ada_gdk_window_attr_get_x (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, 0);

  return window_attr->x;
}

void
ada_gdk_window_attr_set_y (GdkWindowAttr * window_attr, gint y)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->y = y;
}

gint
ada_gdk_window_attr_get_y (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, 0);

  return window_attr->y;
}

void
ada_gdk_window_attr_set_width (GdkWindowAttr * window_attr, gint width)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->width = width;
}

gint
ada_gdk_window_attr_get_width (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, 0);

  return window_attr->width;
}

void
ada_gdk_window_attr_set_height (GdkWindowAttr * window_attr, gint height)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->height = height;
}

gint
ada_gdk_window_attr_get_height (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, 0);

  return window_attr->height;
}

void
ada_gdk_window_attr_set_wclass (GdkWindowAttr *window_attr,
				GdkWindowWindowClass wclass)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->wclass = wclass;
}

GdkWindowWindowClass
ada_gdk_window_attr_get_wclass (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, GDK_INPUT_OUTPUT);

  return window_attr->wclass;
}

void
ada_gdk_window_attr_set_visual (GdkWindowAttr *window_attr,
				GdkVisual *visual)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->visual = visual;
}

GdkVisual*
ada_gdk_window_attr_get_visual (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, NULL);

  return window_attr->visual;
}

void
ada_gdk_window_attr_set_window_type (GdkWindowAttr *window_attr,
				     GdkWindowType window_type)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->window_type = window_type;
}

GdkWindowType
ada_gdk_window_attr_get_window_type (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, GDK_WINDOW_ROOT);

  return window_attr->window_type;
}

void
ada_gdk_window_attr_set_cursor (GdkWindowAttr *window_attr,
				GdkCursor *cursor)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->cursor = cursor;
}

GdkCursor*
ada_gdk_window_attr_get_cursor (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, NULL);

  return window_attr->cursor;
}

void
ada_gdk_window_attr_set_wmclass_name (GdkWindowAttr *window_attr,
				      gchar *wmclass_name)
{
  g_return_if_fail (window_attr != NULL);

  if (window_attr->wmclass_name) g_free (window_attr->wmclass_name);
  window_attr->wmclass_name = g_strdup (wmclass_name);
}

gchar*
ada_gdk_window_attr_get_wmclass_name (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, NULL);

  return window_attr->wmclass_name;
}

void
ada_gdk_window_attr_set_wmclass_class (GdkWindowAttr *window_attr,
				      gchar *wmclass_class)
{
  g_return_if_fail (window_attr != NULL);

  if (window_attr->wmclass_class) g_free (window_attr->wmclass_class);
  window_attr->wmclass_class = g_strdup (wmclass_class);
}

gchar*
ada_gdk_window_attr_get_wmclass_class (GdkWindowAttr *window_attr)
{
  g_return_val_if_fail (window_attr != NULL, NULL);

  return window_attr->wmclass_class;
}

void
ada_gdk_window_attr_set_override_redirect (GdkWindowAttr *window_attr,
					   gboolean override_redirect)
{
  g_return_if_fail (window_attr != NULL);

  window_attr->override_redirect = override_redirect;
}

gboolean
ada_gdk_window_attr_get_override_redirect (GdkWindowAttr * window_attr)
{
  g_return_val_if_fail (window_attr != NULL, FALSE);

  return window_attr->override_redirect;
}

/*
 *
 * Gdk properties
 *
 */

void
ada_gdk_property_get (GdkWindow	 *window,
		      GdkAtom     property,
		      GdkAtom     type,
		      gulong      offset,
		      gulong      length,
		      gint        pdelete,
		      GdkAtom    *actual_property_type,
		      gint       *actual_format,
		      gint       *actual_length,
		      guchar    **data,
		      gint       *success)
{
  *success = gdk_property_get (window, property, type, offset, length,
			       pdelete, actual_property_type, actual_format,
			       actual_length, data);
}


/******************************************
 ** GEnumClass                           **
 ******************************************/

int
ada_c_enum_value_size ()
{
  return sizeof (GEnumValue);
}

GEnumValue*
ada_genum_nth_value (GEnumClass* klass, guint nth)
{
  return (nth < klass->n_values) ? &(klass->values[nth]) : NULL;
}

gint
ada_genum_get_value (GEnumValue* value)
{
  return value->value;
}

const gchar*
ada_genum_get_name (GEnumValue* value)
{
  return value->value_name;
}

const gchar*
ada_genum_get_nick (GEnumValue* value)
{
  return value->value_nick;
}

/******************************************
 ** GFlags                               **
 ******************************************/

GFlagsValue*
ada_gflags_nth_value (GFlagsClass* klass, guint nth)
{
  return (nth < klass->n_values) ? &(klass->values[nth]) : NULL;
}

gint
ada_gflags_get_value (GFlagsValue* value)
{
  return value->value;
}

const gchar*
ada_gflags_get_name (GFlagsValue* value)
{
  return value->value_name;
}

const gchar*
ada_gflags_get_nick (GFlagsValue* value)
{
  return value->value_nick;
}

/******************************************
 ** GParamSpec                           **
 ******************************************/

const char*
ada_gparam_get_name (GParamSpec* param)
{
  return param->name;
}

GParamFlags
ada_gparam_get_flags (GParamSpec* param)
{
  return param->flags;
}

GType
ada_gparam_get_owner_type (GParamSpec* param)
{
  return param->owner_type;
}

GType
ada_gparam_get_value_type (GParamSpec* param)
{
  return G_PARAM_SPEC_VALUE_TYPE (param);
}

void
ada_gparam_set_value_type (GParamSpec* param, GType value_type)
{
  G_PARAM_SPEC_VALUE_TYPE (param) = value_type;
}

gint8
ada_gparam_get_minimum_char (GParamSpecChar* param)
{
  return param->minimum;
}

gint8
ada_gparam_get_maximum_char (GParamSpecChar* param)
{
  return param->maximum;
}

gint8
ada_gparam_get_default_char (GParamSpecChar* param)
{
  return param->default_value;
}

guint8
ada_gparam_get_minimum_uchar (GParamSpecUChar* param)
{
  return param->minimum;
}

guint8
ada_gparam_get_maximum_uchar (GParamSpecUChar* param)
{
  return param->maximum;
}

guint8
ada_gparam_get_default_uchar (GParamSpecUChar* param)
{
  return param->default_value;
}

gboolean
ada_gparam_get_default_boolean (GParamSpecBoolean* param)
{
  return param->default_value;
}

gint
ada_gparam_get_minimum_int (GParamSpecInt* param)
{
  return param->minimum;
}

gint
ada_gparam_get_maximum_int (GParamSpecInt* param)
{
  return param->maximum;
}

gint
ada_gparam_get_default_int (GParamSpecInt* param)
{
  return param->default_value;
}

guint
ada_gparam_get_minimum_uint (GParamSpecUInt* param)
{
  return param->minimum;
}

guint
ada_gparam_get_maximum_uint (GParamSpecUInt* param)
{
  return param->maximum;
}

guint
ada_gparam_get_default_uint (GParamSpecUInt* param)
{
  return param->default_value;
}

glong
ada_gparam_get_minimum_long (GParamSpecLong* param)
{
  return param->minimum;
}

glong
ada_gparam_get_maximum_long (GParamSpecLong* param)
{
  return param->maximum;
}

glong
ada_gparam_get_default_long (GParamSpecLong* param)
{
  return param->default_value;
}

gulong
ada_gparam_get_minimum_ulong (GParamSpecULong* param)
{
  return param->minimum;
}

gulong
ada_gparam_get_maximum_ulong (GParamSpecULong* param)
{
  return param->maximum;
}

gulong
ada_gparam_get_default_ulong (GParamSpecULong* param)
{
  return param->default_value;
}

gunichar
ada_gparam_get_default_unichar (GParamSpecUnichar* param)
{
  return param->default_value;
}

gint
ada_gparam_get_default_enum (GParamSpecEnum* param)
{
  return param->default_value;
}

GEnumClass*
ada_gparam_get_enum_class_enum (GParamSpecEnum* param)
{
  return param->enum_class;
}

GFlagsClass*
ada_gparam_get_flags_flags (GParamSpecFlags* param)
{
  return param->flags_class;
}

glong
ada_gparam_get_default_flags (GParamSpecFlags* param)
{
  return param->default_value;
}

gfloat
ada_gparam_get_minimum_gfloat (GParamSpecFloat* param)
{
  return param->minimum;
}

gfloat
ada_gparam_get_maximum_gfloat (GParamSpecFloat* param)
{
  return param->maximum;
}

gfloat
ada_gparam_get_default_gfloat (GParamSpecFloat* param)
{
  return param->default_value;
}

gfloat
ada_gparam_get_epsilon_gfloat (GParamSpecFloat* param)
{
  return param->epsilon;
}

gdouble
ada_gparam_get_minimum_gdouble (GParamSpecDouble* param)
{
  return param->minimum;
}

gdouble
ada_gparam_get_maximum_gdouble (GParamSpecDouble* param)
{
  return param->maximum;
}

gdouble
ada_gparam_get_default_gdouble (GParamSpecDouble* param)
{
  return param->default_value;
}

gdouble
ada_gparam_get_epsilon_gdouble (GParamSpecDouble* param)
{
  return param->epsilon;
}

gchar*
ada_gparam_default_string (GParamSpecString* param)
{
  return param->default_value;
}

gchar*
ada_gparam_cset_first_string (GParamSpecString* param)
{
  return param->cset_first;
}

gchar*
ada_gparam_cset_nth_string (GParamSpecString* param)
{
  return param->cset_nth;
}

gchar
ada_gparam_substitutor_string (GParamSpecString* param)
{
  return param->substitutor;
}

gboolean
ada_gparam_ensure_non_null_string (GParamSpecString* param)
{
  return param->ensure_non_null != 0;
}

/******************************************
 ** New widgets
 ******************************************/

void
ada_install_property_handlers
   (GObjectClass* klass,
    GObjectSetPropertyFunc c_set_handler,
    GObjectGetPropertyFunc c_get_handler,
    GObjectSetPropertyFunc ada_set_handler,
    GObjectGetPropertyFunc ada_get_handler)
{
  G_OBJECT_CLASS (klass)->set_property = c_set_handler;
  G_OBJECT_CLASS (klass)->get_property = c_get_handler;

  AdaGObjectClass adaklass = ADA_CLASS_FROM_C_CLASS(klass);
  adaklass->ada_property_getter = ada_get_handler;
  adaklass->ada_property_setter = ada_set_handler;
}

GObjectGetPropertyFunc ada_real_get_property_handler (GObject* object) {
   return ada_gobject_class_from_object(object)->ada_property_getter;
}

GObjectSetPropertyFunc ada_real_set_property_handler (GObject* object) {
   return ada_gobject_class_from_object(object)->ada_property_setter;
}

void
ada_genum_create_enum_value
  (gint value, gchar* name, gchar* nick, GEnumValue* val)
{
  val->value = value;
  val->value_name = g_strdup (name);
  val->value_nick = g_strdup (nick);
}

/******************************************
 ** GType                                **
 ******************************************/

GType
ada_gtype_fundamental (GType type)
{
  return G_TYPE_FUNDAMENTAL (type);
}

gboolean
ada_g_type_is_interface (GType type)
{
  return G_TYPE_IS_INTERFACE (type);
}

/******************************************
 ** Handling of tree Freeze/Thaw         **
 ******************************************/

gint
ada_gtk_tree_view_freeze_sort (GtkTreeStore* tree)
{
  gint save;
  GtkSortType order;
  gtk_tree_sortable_get_sort_column_id
    (GTK_TREE_SORTABLE (tree), &save, &order);
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (tree), -2, order);
  return save;
}

void
ada_gtk_tree_view_thaw_sort (GtkTreeStore* tree, gint id)
{
  gint save;
  GtkSortType order;
  gtk_tree_sortable_get_sort_column_id
    (GTK_TREE_SORTABLE (tree), &save, &order);
  gtk_tree_sortable_set_sort_column_id
    (GTK_TREE_SORTABLE (tree), id, order);
}

/*****************************************************
 ** Glib
*****************************************************/

struct CustomGSource
{
  GSource source;
  gpointer user_data;
};

GSourceFuncs*
ada_allocate_g_source_funcs
  (gpointer prepare, gpointer check, gpointer dispatch, gpointer finalize)
{
  GSourceFuncs* result;
  result = (GSourceFuncs*) malloc (sizeof (GSourceFuncs));

  result->prepare  = prepare;
  result->check    = check;
  result->dispatch = dispatch;
  result->finalize = finalize;
  return result;
}

GSource*
ada_g_source_new (GSourceFuncs* type, gpointer user_data)
{
  struct CustomGSource* result =
    (struct CustomGSource*)g_source_new (type, sizeof (struct CustomGSource));
  result->user_data = user_data;
  return (GSource*)result;
}

gpointer
ada_g_source_get_user_data (GSource* source)
{
  return ((struct CustomGSource*)source)->user_data;
}

/***********************************************************
 ** Gtk_Text_Buffer
***********************************************************/

void
ada_gtk_text_buffer_insert_with_tags
 (GtkTextBuffer *buffer,
  GtkTextIter   *iter,
  const gchar   *text,
  gint           len,
  GtkTextTag    *tag)
{
  gtk_text_buffer_insert_with_tags
    (buffer, iter, text, len, tag, NULL);
}

GtkTextTag*
ada_gtk_text_buffer_create_tag (GtkTextBuffer* buffer, const gchar* name)
{
   return gtk_text_buffer_create_tag (buffer, name, NULL);
}

/***********************************************************
 ** Gtk_File_Chooser_Dialog
***********************************************************/

GtkWidget *
ada_gtk_file_chooser_dialog_new
  (const gchar          *title,
   GtkWindow            *parent,
   GtkFileChooserAction  action)
{
  return gtk_file_chooser_dialog_new
    (title, parent, action, NULL, (char *)NULL);
}

/***********************************************************
 ** Gtk_Recent_Chooser_Dialog
***********************************************************/

GtkWidget*
ada_gtk_recent_chooser_dialog_new
  (const gchar *title,
   GtkWindow   *parent)
{
  return gtk_recent_chooser_dialog_new (title, parent, NULL, NULL);
}

GtkWidget*
ada_gtk_recent_chooser_dialog_new_for_manager
  (const gchar      *title,
   GtkWindow        *parent,
   GtkRecentManager *manager)
{
  return gtk_recent_chooser_dialog_new_for_manager
    (title, parent, manager, NULL, NULL);
}

/**************************************************************
 **  Gtk_Bindings
**************************************************************/

void
ada_gtk_binding_entry_add_signal_NO
  (GtkBindingSet* set, guint keyval, GdkModifierType modifier,
   const gchar* signal_name)
{
  gtk_binding_entry_add_signal (set, keyval, modifier, signal_name, 0);
}

void
ada_gtk_binding_entry_add_signal_int
  (GtkBindingSet* set, guint keyval, GdkModifierType modifier,
   const gchar* signal_name, gint arg1)
{
  gtk_binding_entry_add_signal
    (set, keyval, modifier, signal_name, 1,
     G_TYPE_INT, arg1);
}

void
ada_gtk_binding_entry_add_signal_int_int
  (GtkBindingSet* set, guint keyval, GdkModifierType modifier,
   const gchar* signal_name, gint arg1, gint arg2)
{
  gtk_binding_entry_add_signal
    (set, keyval, modifier, signal_name, 2,
     G_TYPE_INT, arg1, G_TYPE_INT, arg2);
}

void
ada_gtk_binding_entry_add_signal_bool
  (GtkBindingSet* set, guint keyval, GdkModifierType modifier,
   const gchar* signal_name, gboolean arg1)
{
  gtk_binding_entry_add_signal
    (set, keyval, modifier, signal_name, 1,
     G_TYPE_BOOLEAN, arg1);
}

GdkModifierType
ada_gdk_get_default_modifier ()
{
  static GdkModifierType primary = 0;

  if (!primary)
    {
      GdkDisplay      *display = gdk_display_get_default ();
      GdkKeymap       *keymap = gdk_keymap_get_for_display (display);
      GdkModifierType real;

      g_return_val_if_fail (GDK_IS_KEYMAP (keymap), 0);

      /* Retrieve the real modifier mask */
      real = gdk_keymap_get_modifier_mask
        (keymap,
         GDK_MODIFIER_INTENT_PRIMARY_ACCELERATOR);

      primary = real;

      /* We need to translate the real modifiers into a virtual modifier
         (like Super, Meta, etc.).
         The following call adds the virtual modifiers for each real modifier
         defined in primary.
      */
      gdk_keymap_add_virtual_modifiers (keymap, &primary);

      if (primary != real) {
        /* In case the virtual and real modifiers are different, we need to
           remove the real modifier from the result, and keep only the
           virtual one.
        */
        primary &= ~real;
      }
    }

  return primary;
}

// GtkPlug is only build on X11 backends

#ifndef GDK_WINDOWING_X11
int gtk_plug_get_type() {
   return 0;
}
int gtk_socket_get_type() {
   return 0;
}
#endif

// Application handling for opening files from the explorer/finder

typedef struct {
  GtkApplication *app;    //  The app responsible for opening the file
  GFile          **files; //  The array of files to open
  gint           n_files; //  The size of the above array
} ada_gtk_open_data;

/*
 * Idle callback responsible for actually calling g_application_open with the
 * proper files to open.
 */
gboolean
ada_gtk_application_open_files (gpointer ptr)
{
  int i;
  ada_gtk_open_data *data = (ada_gtk_open_data*)ptr;

  g_application_open (G_APPLICATION (data->app),
		      data->files, data->n_files, NULL);

  for (i = 0; i < data->n_files; i++)
    g_object_unref (data->files[i]);
  g_free (data->files);
  g_free (data);

  return G_SOURCE_REMOVE;
}

#ifdef GDK_WINDOWING_QUARTZ
void ada_gtk_quartz_application_open_files
  (GFile** files, gint n_files, gpointer user_data)
{
  ada_gtk_open_data *data = malloc (sizeof (ada_gtk_open_data));

  data->app     = (GtkApplication*)user_data;
  data->files   = files;
  data->n_files = n_files;

  ada_gtk_application_open_files (data);
}
#endif

#ifdef GDK_WINDOWING_WIN32

/* The DDE API do not support user data in the DDE callback. We thus need to
   use the below global data to support the feature */
GtkApplication *ada_gtk_win32_app;
DWORD ada_gtk_win32_id_instance;

HDDEDATA CALLBACK ada_gtk_open_document_win32
  (UINT uType,
   UINT uFmt,
   HCONV hconv,
   HSZ hsz1,
   HSZ hsz2,
   HDDEDATA hData,
   ULONG_PTR dwData1,
   ULONG_PTR dwData2)
{
  switch (uType) {
    case XTYP_ADVDATA:
      return (HDDEDATA) DDE_FACK;

    case XTYP_CONNECT:
      return (HDDEDATA) TRUE;

    case XTYP_DISCONNECT:
      return (HDDEDATA) NULL;

    case XTYP_REGISTER:
    case XTYP_UNREGISTER:
      return (HDDEDATA) NULL;

    case XTYP_EXECUTE:
      {
        DWORD dataLen;
        PWSTR strData = (PWSTR) DdeAccessData (hData, &dataLen);
        DWORD j;
        PWSTR path = NULL;
        LPCWSTR openCmd = L"FileOpen:";
        DWORD utfSize;
        PSTR utfPath;

        for (j = 0; j < dataLen; j++) {
          if (strData[j] == ':') {
            path = &strData[j + 1];
            break;
          } else if (strData[j] != openCmd[j]) {
            return (HDDEDATA) DDE_FNOTPROCESSED;
          }
        }

        utfSize = WideCharToMultiByte
          (CP_UTF8,
           0,
           path,
           dataLen - 9,
           NULL,
           0, /* indicates that we want to get the size of the result */
           NULL, NULL);

        utfPath = (char*)malloc (utfSize + 1);

        WideCharToMultiByte
          (CP_UTF8,
           0,
           path,
           dataLen - 9,
           utfPath,
           utfSize,
           NULL, NULL);
        utfPath[utfSize] = '\0';

        ada_gtk_open_data *data = malloc (sizeof(ada_gtk_open_data));
        data->app     = ada_gtk_win32_app;
        data->files   = (GFile **) malloc (sizeof(GFile*));
        data->n_files = 1;
        data->files[0] = g_file_new_for_path ((const char *)utfPath);
        free (utfPath);

        g_idle_add (ada_gtk_application_open_files, data);

        DdeUnaccessData (hData); /* Release the resource */
      }

      return (HDDEDATA) DDE_FACK;
  };

  return (HDDEDATA) DDE_FNOTPROCESSED;
}

#endif /* WIN32 */

static void
ada_gtk_application_startup (GtkApplication *application) {
#if defined (GDK_WINDOWING_QUARTZ)

  init_osx_open_files_event_handler
    (ada_gtk_quartz_application_open_files, (gpointer)application);

#elif defined (GDK_WINDOWING_WIN32)

  /* On Windows, we use a DDE server to handle events from the Shell */

  HSZ hszAppName;
  gchar* appName =
    g_application_get_application_id (G_APPLICATION (application));
  gchar* shortAppName = appName;

  /* DDE server only supports a short application name. We extract the last
     part of the full application name for that. So that for example:
     com.adacore.GPS will use GPS as DDE server name */
  for (; *appName != '\0'; appName++) {
    if (*appName == '.') shortAppName = appName + 1;
  }

  ada_gtk_win32_app = application;

  /* Initialize the DDE framework */
  DdeInitializeW (&ada_gtk_win32_id_instance,
                  (PFNCALLBACK) &ada_gtk_open_document_win32,
                  APPCLASS_STANDARD |
                  CBF_FAIL_ADVISES | CBF_FAIL_POKES | CBF_FAIL_SELFCONNECTIONS,
                  0);

  /* And register the DDE Service to the name server */
  hszAppName = DdeCreateStringHandleA
    (ada_gtk_win32_id_instance, shortAppName, CP_WINANSI);
  DdeNameService
    (ada_gtk_win32_id_instance,   /* instance identifier */
     hszAppName, /*  handle to service name string */
     0, /* reserved */
     DNS_REGISTER | DNS_FILTERON);
#endif
}

static void
ada_gtk_application_shutdown (GtkApplication *application) {
#ifdef GDK_WINDOWING_WIN32
  DdeUninitialize (ada_gtk_win32_id_instance);
#endif
}

void
ada_gtk_app_activate (void){
#ifdef GDK_WINDOWING_QUARTZ
  ada_activate_app();
#endif
}

#ifdef GDK_WINDOWING_QUARTZ
/* Enable the full-screen button on OSX for the main window */
static void
ada_gtk_quartz_window_added
  (GtkApplication *application,
   GtkWindow      *window,
   gpointer       user_data)
{
  ada_gtk_osx_allow_fullscreen (window);
}
#endif /* QUARTZ */

typedef enum
  {
    GTKADA_APPLICATION_FLAGS_NONE,
    GTKADA_APPLICATION_HANDLES_OPEN =   (1 << 0),
    GTKADA_APPLICATION_OSX_FULLSCREEN = (1 << 1)
  } GtkadaApplicationFlags;

/* Called by the GtkAda.Application instance during initialisation */
void
ada_gtk_setup_application(GtkApplication *app, GtkadaApplicationFlags flags)
{
  if ((flags & GTKADA_APPLICATION_HANDLES_OPEN) != 0)
    {
      g_signal_connect
	(app, "startup", G_CALLBACK (ada_gtk_application_startup), NULL);
      g_signal_connect
	(app, "shutdown", G_CALLBACK (ada_gtk_application_shutdown), NULL);
    }
#ifdef GDK_WINDOWING_QUARTZ
  if ((flags & GTKADA_APPLICATION_OSX_FULLSCREEN) != 0)
    {
      g_signal_connect
	(app, "window-added", G_CALLBACK (ada_gtk_quartz_window_added), NULL);
    }
#endif
}

const GVariantType* ada_gvariant_type_boolean = G_VARIANT_TYPE_BOOLEAN;
const GVariantType* ada_gvariant_type_byte    = G_VARIANT_TYPE_BYTE;
const GVariantType* ada_gvariant_type_int16   = G_VARIANT_TYPE_INT16;
const GVariantType* ada_gvariant_type_uint16  = G_VARIANT_TYPE_UINT16;
const GVariantType* ada_gvariant_type_int32   = G_VARIANT_TYPE_INT32;
const GVariantType* ada_gvariant_type_uint32  = G_VARIANT_TYPE_UINT32;
const GVariantType* ada_gvariant_type_int64   = G_VARIANT_TYPE_INT64;
const GVariantType* ada_gvariant_type_uint64  = G_VARIANT_TYPE_UINT64;
const GVariantType* ada_gvariant_type_double  = G_VARIANT_TYPE_DOUBLE;
const GVariantType* ada_gvariant_type_string  = G_VARIANT_TYPE_STRING;