1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
/* cdkwindow-quartz.c
 *
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * Copyright (C) 2005-2007 Imendio AB
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <cdk/cdk.h>
#include <cdk/cdkdeviceprivate.h>
#include <cdk/cdkdisplayprivate.h>

#include "cdkwindowimpl.h"
#include "cdkwindow-quartz.h"
#include "cdkprivate-quartz.h"
#include "cdkglcontext-quartz.h"
#include "cdkquartzglcontext.h"
#include "cdkquartzscreen.h"
#include "cdkquartzcursor.h"
#include "cdkquartz-ctk-only.h"

#include <Carbon/Carbon.h>
#include <AvailabilityMacros.h>

#include <sys/time.h>
#include <cairo-quartz.h>

static gpointer parent_class;
static gpointer root_window_parent_class;

static GSList   *update_nswindows;
static gboolean  in_process_all_updates = FALSE;

static GSList *main_window_stack;

void _cdk_quartz_window_flush (CdkWindowImplQuartz *window_impl);

typedef struct
{
  gint            x, y;
  gint            width, height;
  CdkWMDecoration decor;
} FullscreenSavedGeometry;

#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
typedef enum
{
 CDK_QUARTZ_BORDERLESS_WINDOW = NSBorderlessWindowMask,
 CDK_QUARTZ_CLOSABLE_WINDOW = NSClosableWindowMask,
#if MAC_OS_X_VERSION_MIN_REQUIRED > 1060
 /* Added in 10.7. Apple's docs are wrong to say it's from earlier. */
 CDK_QUARTZ_FULLSCREEN_WINDOW = NSFullScreenWindowMask,
#endif
 CDK_QUARTZ_MINIATURIZABLE_WINDOW = NSMiniaturizableWindowMask,
 CDK_QUARTZ_RESIZABLE_WINDOW = NSResizableWindowMask,
 CDK_QUARTZ_TITLED_WINDOW = NSTitledWindowMask,
} CdkQuartzWindowMask;
#else
typedef enum
{
 CDK_QUARTZ_BORDERLESS_WINDOW = NSWindowStyleMaskBorderless,
 CDK_QUARTZ_CLOSABLE_WINDOW = NSWindowStyleMaskClosable,
 CDK_QUARTZ_FULLSCREEN_WINDOW = NSWindowStyleMaskFullScreen,
 CDK_QUARTZ_MINIATURIZABLE_WINDOW = NSWindowStyleMaskMiniaturizable,
 CDK_QUARTZ_RESIZABLE_WINDOW = NSWindowStyleMaskResizable,
 CDK_QUARTZ_TITLED_WINDOW = NSWindowStyleMaskTitled,
} CdkQuartzWindowMask;
#endif

#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
static FullscreenSavedGeometry *get_fullscreen_geometry (CdkWindow *window);
#endif

#define FULLSCREEN_DATA "fullscreen-data"

static void update_toplevel_order (void);
static void clear_toplevel_order  (void);

#define WINDOW_IS_TOPLEVEL(window)		     \
  (CDK_WINDOW_TYPE (window) != CDK_WINDOW_CHILD &&   \
   CDK_WINDOW_TYPE (window) != CDK_WINDOW_FOREIGN && \
   CDK_WINDOW_TYPE (window) != CDK_WINDOW_OFFSCREEN)

/*
 * CdkQuartzWindow
 */

struct _CdkQuartzWindow
{
  CdkWindow parent;
};

struct _CdkQuartzWindowClass
{
  CdkWindowClass parent_class;
};

G_DEFINE_TYPE (CdkQuartzWindow, cdk_quartz_window, CDK_TYPE_WINDOW);

static void
cdk_quartz_window_class_init (CdkQuartzWindowClass *quartz_window_class)
{
}

static void
cdk_quartz_window_init (CdkQuartzWindow *quartz_window)
{
}


/*
 * CdkQuartzWindowImpl
 */

NSView *
cdk_quartz_window_get_nsview (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window))
    return NULL;

  return ((CdkWindowImplQuartz *)window->impl)->view;
}

NSWindow *
cdk_quartz_window_get_nswindow (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window))
    return NULL;

  return ((CdkWindowImplQuartz *)window->impl)->toplevel;
}

static CGContextRef
cdk_window_impl_quartz_get_context (CdkWindowImplQuartz *window_impl,
				    gboolean             antialias)
{
  CGContextRef cg_context = NULL;
  CGSize scale;

  if (CDK_WINDOW_DESTROYED (window_impl->wrapper))
    return NULL;

  /* Lock focus when not called as part of a drawRect call. This
   * is needed when called from outside "real" expose events, for
   * example for synthesized expose events when realizing windows
   * and for widgets that send fake expose events like the arrow
   * buttons in spinbuttons or the position marker in rulers.
   */
  if (window_impl->in_paint_rect_count == 0)
    {
      if (![window_impl->view lockFocusIfCanDraw])
        return NULL;
    }
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101000
    cg_context = [[NSGraphicsContext currentContext] graphicsPort];<--- syntax error: =[...];
#else
  if (cdk_quartz_osx_version () < CDK_OSX_YOSEMITE)
    cg_context = [[NSGraphicsContext currentContext] graphicsPort];
  else
    cg_context = [[NSGraphicsContext currentContext] CGContext];
#endif

  if (!cg_context)
    return NULL;
  CGContextSaveGState (cg_context);
  CGContextSetAllowsAntialiasing (cg_context, antialias);

  /* Undo the default scaling transform, since we apply our own
   * in cdk_quartz_ref_cairo_surface () */
  scale = CGContextConvertSizeToDeviceSpace (cg_context,
                                             CGSizeMake (1.0, 1.0));
  CGContextScaleCTM (cg_context, 1.0 / scale.width, 1.0 / scale.height);

  return cg_context;
}

static void
cdk_window_impl_quartz_release_context (CdkWindowImplQuartz *window_impl,
                                        CGContextRef         cg_context)
{
  if (cg_context)
    {
      CGContextRestoreGState (cg_context);
      CGContextSetAllowsAntialiasing (cg_context, TRUE);
    }

  /* See comment in cdk_quartz_window_get_context(). */
  if (window_impl->in_paint_rect_count == 0)
    {
      _cdk_quartz_window_flush (window_impl);
      [window_impl->view unlockFocus];
    }
}

static void
cdk_window_impl_quartz_finalize (GObject *object)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (object);
  CdkDisplay *display = cdk_window_get_display (impl->wrapper);
  CdkSeat *seat = cdk_display_get_default_seat (display);

  cdk_seat_ungrab (seat);

  if (impl->transient_for)
    g_object_unref (impl->transient_for);

  if (impl->view)
    [[NSNotificationCenter defaultCenter] removeObserver: impl->toplevel
                                       name: @"NSViewFrameDidChangeNotification"
                                     object: impl->view];

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/* Help preventing "beam sync penalty" where CG makes all graphics code
 * block until the next vsync if we try to flush (including call display on
 * a view) too often. We do this by limiting the manual flushing done
 * outside of expose calls to less than some frequency when measured over
 * the last 4 flushes. This is a bit arbitray, but seems to make it possible
 * for some quick manual flushes (such as ctkruler or gimp’s marching ants)
 * without hitting the max flush frequency.
 *
 * If drawable NULL, no flushing is done, only registering that a flush was
 * done externally.
 *
 * Note: As of MacOS 10.14 NSWindow flushWindow is deprecated because
 * Quartz has the ability to handle deferred drawing on its own.
 */
void
_cdk_quartz_window_flush (CdkWindowImplQuartz *window_impl)
{
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
  static struct timeval prev_tv;
  static gint intervals[4];
  static gint index;
  struct timeval tv;
  gint ms;

  gettimeofday (&tv, NULL);
  ms = (tv.tv_sec - prev_tv.tv_sec) * 1000 + (tv.tv_usec - prev_tv.tv_usec) / 1000;
  intervals[index++ % 4] = ms;

  if (window_impl)
    {
      ms = intervals[0] + intervals[1] + intervals[2] + intervals[3];

      /* ~25Hz on average. */
      if (ms > 4*40)
        {
          if (window_impl)
            [window_impl->toplevel flushWindow];

          prev_tv = tv;
        }
    }
  else
    prev_tv = tv;
#endif
}

static cairo_user_data_key_t cdk_quartz_cairo_key;

typedef struct {
  CdkWindowImplQuartz  *window_impl;
  CGContextRef  cg_context;
} CdkQuartzCairoSurfaceData;

static void
cdk_quartz_cairo_surface_destroy (void *data)
{
  CdkQuartzCairoSurfaceData *surface_data = data;

  surface_data->window_impl->cairo_surface = NULL;

  cdk_quartz_window_release_context (surface_data->window_impl,
                                     surface_data->cg_context);

  g_free (surface_data);
}

static cairo_surface_t *
cdk_quartz_create_cairo_surface (CdkWindowImplQuartz *impl,
				 int                  width,
				 int                  height)
{
  CGContextRef cg_context;
  CdkQuartzCairoSurfaceData *surface_data;
  cairo_surface_t *surface;

  cg_context = cdk_quartz_window_get_context (impl, TRUE);

  surface_data = g_new (CdkQuartzCairoSurfaceData, 1);
  surface_data->window_impl = impl;
  surface_data->cg_context = cg_context;

  if (cg_context)
    surface = cairo_quartz_surface_create_for_cg_context (cg_context,
                                                          width, height);
  else
    surface = cairo_quartz_surface_create(CAIRO_FORMAT_ARGB32, width, height);

  cairo_surface_set_user_data (surface, &cdk_quartz_cairo_key,
                               surface_data,
                               cdk_quartz_cairo_surface_destroy);

  return surface;
}

static cairo_surface_t *
cdk_quartz_ref_cairo_surface (CdkWindow *window)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (CDK_WINDOW_DESTROYED (window))
    return NULL;

  if (!impl->cairo_surface)
    {
      gint scale = cdk_window_get_scale_factor (impl->wrapper);

      impl->cairo_surface = 
          cdk_quartz_create_cairo_surface (impl,
                                           cdk_window_get_width (impl->wrapper) * scale,
                                           cdk_window_get_height (impl->wrapper) * scale);

      cairo_surface_set_device_scale (impl->cairo_surface, scale, scale);
    }
  else
    cairo_surface_reference (impl->cairo_surface);

  return impl->cairo_surface;
}

static void
cdk_window_impl_quartz_init (CdkWindowImplQuartz *impl)
{
  impl->type_hint = CDK_WINDOW_TYPE_HINT_NORMAL;
}

static gboolean
cdk_window_impl_quartz_begin_paint (CdkWindow *window)
{
  return FALSE;
}

static void
cdk_quartz_window_set_needs_display_in_region (CdkWindow    *window,
                                               cairo_region_t    *region)
{
  CdkWindowImplQuartz *impl;
  int i, n_rects;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (!impl->needs_display_region)
    impl->needs_display_region = cairo_region_create ();

  cairo_region_union (impl->needs_display_region, region);

  n_rects = cairo_region_num_rectangles (region);
  for (i = 0; i < n_rects; i++)
    {
      cairo_rectangle_int_t rect;
      cairo_region_get_rectangle (region, i, &rect);
      [impl->view setNeedsDisplayInRect:NSMakeRect (rect.x, rect.y,
                                                    rect.width, rect.height)];
    }
}

void
_cdk_quartz_window_process_updates_recurse (CdkWindow *window,
                                            cairo_region_t *region)
{
  /* Make sure to only flush each toplevel at most once if we're called
   * from process_all_updates.
   */
  if (in_process_all_updates)
    {
      CdkWindow *toplevel;

      toplevel = cdk_window_get_effective_toplevel (window);
      if (toplevel && WINDOW_IS_TOPLEVEL (toplevel))
        {
          CdkWindowImplQuartz *toplevel_impl;
          NSWindow *nswindow;

          toplevel_impl = (CdkWindowImplQuartz *)toplevel->impl;
          nswindow = toplevel_impl->toplevel;
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
          /* In theory, we could skip the flush disabling, since we only
           * have one NSView.
           */
          if (nswindow && ![nswindow isFlushWindowDisabled]) 
            {
              [nswindow retain];
              [nswindow disableFlushWindow];
              update_nswindows = g_slist_prepend (update_nswindows, nswindow);
            }
#endif
        }
    }

  if (WINDOW_IS_TOPLEVEL (window))
    cdk_quartz_window_set_needs_display_in_region (window, region);
  else
    _cdk_window_process_updates_recurse (window, region);

  /* NOTE: I'm not sure if we should displayIfNeeded here. It slows down a
   * lot (since it triggers the beam syncing) and things seem to work
   * without it.
   */
}

void
_cdk_quartz_display_before_process_all_updates (CdkDisplay *display)
{
  in_process_all_updates = TRUE;

  if (cdk_quartz_osx_version () >= CDK_OSX_EL_CAPITAN)
    {
      [NSAnimationContext endGrouping];
    }
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101100
  else
    {
      NSDisableScreenUpdates ();
    }
#endif
}

void
_cdk_quartz_display_after_process_all_updates (CdkDisplay *display)
{
  GSList *tmp_list = update_nswindows;

  update_nswindows = NULL;

  while (tmp_list)
    {
      NSWindow *nswindow = tmp_list->data;

      [[nswindow contentView] displayIfNeeded];

      _cdk_quartz_window_flush (NULL);
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
      [nswindow enableFlushWindow];
      [nswindow flushWindow];
#endif
      [nswindow release];

      tmp_list = g_slist_remove_link (tmp_list, tmp_list);
    }

  in_process_all_updates = FALSE;

  if (cdk_quartz_osx_version() >= CDK_OSX_EL_CAPITAN)
    {
      [NSAnimationContext beginGrouping];
    }
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101100
  else
    {
      NSEnableScreenUpdates ();
    }
#endif
}

static const gchar *
get_default_title (void)
{
  const char *title;

  title = g_get_application_name ();
  if (!title)
    title = g_get_prgname ();

  return title;
}

static void
get_ancestor_coordinates_from_child (CdkWindow *child_window,
				     gint       child_x,
				     gint       child_y,
				     CdkWindow *ancestor_window, 
				     gint      *ancestor_x, 
				     gint      *ancestor_y)
{
  while (child_window != ancestor_window)
    {
      child_x += child_window->x;
      child_y += child_window->y;

      child_window = child_window->parent;
    }

  *ancestor_x = child_x;
  *ancestor_y = child_y;
}

void
_cdk_quartz_window_debug_highlight (CdkWindow *window, gint number)
{
  gint x, y;
  gint gx, gy;
  CdkWindow *toplevel;
  gint tx, ty;
  static NSWindow *debug_window[10];
  static NSRect old_rect[10];
  NSRect rect;
  NSColor *color;

  g_return_if_fail (number >= 0 && number <= 9);

  if (window == _cdk_root)
    return;

  if (window == NULL)
    {
      if (debug_window[number])
        [debug_window[number] close];
      debug_window[number] = NULL;

      return;
    }

  toplevel = cdk_window_get_toplevel (window);
  get_ancestor_coordinates_from_child (window, 0, 0, toplevel, &x, &y);

  cdk_window_get_origin (toplevel, &tx, &ty);
  x += tx;
  y += ty;

  _cdk_quartz_window_cdk_xy_to_xy (x, y + window->height,
                                   &gx, &gy);

  rect = NSMakeRect (gx, gy, window->width, window->height);

  if (debug_window[number] && NSEqualRects (rect, old_rect[number]))
    return;

  old_rect[number] = rect;

  if (debug_window[number])
    [debug_window[number] close];

  debug_window[number] = [[NSWindow alloc] initWithContentRect:rect
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
                                                     styleMask:(NSUInteger)CDK_QUARTZ_BORDERLESS_WINDOW
#else
                                                     styleMask:(NSWindowStyleMask)CDK_QUARTZ_BORDERLESS_WINDOW
#endif
			                               backing:NSBackingStoreBuffered
			                                 defer:NO];

  switch (number)
    {
    case 0:
      color = [NSColor redColor];
      break;
    case 1:
      color = [NSColor blueColor];
      break;
    case 2:
      color = [NSColor greenColor];
      break;
    case 3:
      color = [NSColor yellowColor];
      break;
    case 4:
      color = [NSColor brownColor];
      break;
    case 5:
      color = [NSColor purpleColor];
      break;
    default:
      color = [NSColor blackColor];
      break;
    }

  [debug_window[number] setBackgroundColor:color];
  [debug_window[number] setAlphaValue:0.4];
  [debug_window[number] setOpaque:NO];
  [debug_window[number] setReleasedWhenClosed:YES];
  [debug_window[number] setIgnoresMouseEvents:YES];
  [debug_window[number] setLevel:NSFloatingWindowLevel];

  [debug_window[number] orderFront:nil];
}

gboolean
_cdk_quartz_window_is_ancestor (CdkWindow *ancestor,
                                CdkWindow *window)
{
  if (ancestor == NULL || window == NULL)
    return FALSE;

  return (cdk_window_get_parent (window) == ancestor ||
          _cdk_quartz_window_is_ancestor (ancestor, 
                                          cdk_window_get_parent (window)));
}


/* See notes on top of cdkscreen-quartz.c */
void
_cdk_quartz_window_cdk_xy_to_xy (gint  cdk_x,
                                 gint  cdk_y,
                                 gint *ns_x,
                                 gint *ns_y)
{
  CdkQuartzScreen *screen_quartz = CDK_QUARTZ_SCREEN (_cdk_screen);

  if (ns_y)
    *ns_y = screen_quartz->orig_y - cdk_y;

  if (ns_x)
    *ns_x = cdk_x + screen_quartz->orig_x;
}

void
_cdk_quartz_window_xy_to_cdk_xy (gint  ns_x,
                                 gint  ns_y,
                                 gint *cdk_x,
                                 gint *cdk_y)
{
  CdkQuartzScreen *screen_quartz = CDK_QUARTZ_SCREEN (_cdk_screen);

  if (cdk_y)
    *cdk_y = screen_quartz->orig_y - ns_y;

  if (cdk_x)
    *cdk_x = ns_x - screen_quartz->orig_x;
}

void
_cdk_quartz_window_nspoint_to_cdk_xy (NSPoint  point,
                                      gint    *x,
                                      gint    *y)
{
  _cdk_quartz_window_xy_to_cdk_xy (point.x, point.y,
                                   x, y);
}

static CdkWindow *
find_child_window_helper (CdkWindow *window,
			  gint       x,
			  gint       y,
			  gint       x_offset,
			  gint       y_offset,
                          gboolean   get_toplevel)
{
  CdkWindowImplQuartz *impl;
  GList *l;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (window == _cdk_root)
    update_toplevel_order ();

  for (l = impl->sorted_children; l; l = l->next)
    {
      CdkWindow *child = l->data;
      CdkWindowImplQuartz *child_impl = CDK_WINDOW_IMPL_QUARTZ (child->impl);
      int temp_x, temp_y;

      if (!CDK_WINDOW_IS_MAPPED (child))
	continue;

      temp_x = x_offset + child->x;
      temp_y = y_offset + child->y;

      /* Special-case the root window. We have to include the title
       * bar in the checks, otherwise the window below the title bar
       * will be found i.e. events punch through. (If we can find a
       * better way to deal with the events in cdkevents-quartz, this
       * might not be needed.)
       */
      if (window == _cdk_root)
        {
          NSRect frame = NSMakeRect (0, 0, 100, 100);
          NSRect content;
          NSUInteger mask;
          int titlebar_height;

          mask = [child_impl->toplevel styleMask];

          /* Get the title bar height. */
          content = [NSWindow contentRectForFrameRect:frame
                                            styleMask:mask];
          titlebar_height = frame.size.height - content.size.height;

          if (titlebar_height > 0 &&
              x >= temp_x && y >= temp_y - titlebar_height &&
              x < temp_x + child->width && y < temp_y)
            {
              /* The root means "unknown" i.e. a window not managed by
               * CDK.
               */
              return (CdkWindow *)_cdk_root;
            }
        }

      if ((!get_toplevel || (get_toplevel && window == _cdk_root)) &&
          x >= temp_x && y >= temp_y &&
	  x < temp_x + child->width && y < temp_y + child->height)
	{
	  /* Look for child windows. */
	  return find_child_window_helper (l->data,
					   x, y,
					   temp_x, temp_y,
                                           get_toplevel);
	}
    }
  
  return window;
}

/* Given a CdkWindow and coordinates relative to it, returns the
 * innermost subwindow that contains the point. If the coordinates are
 * outside the passed in window, NULL is returned.
 */
CdkWindow *
_cdk_quartz_window_find_child (CdkWindow *window,
			       gint       x,
			       gint       y,
                               gboolean   get_toplevel)
{
  if (x >= 0 && y >= 0 && x < window->width && y < window->height)
    return find_child_window_helper (window, x, y, 0, 0, get_toplevel);

  return NULL;
}

/* Raises a transient window.
 */
static void
raise_transient (CdkWindowImplQuartz *impl)
{
  /* In quartz the transient-for behavior is implemented by
   * attaching the transient-for CdkNSWindows to the parent's
   * CdkNSWindow. Stacking is managed by Quartz and the order
   * is that of the parent's childWindows array. The only way
   * to change that order is to remove the child from the
   * parent and then add it back in.
   */
  CdkWindowImplQuartz *parent_impl =
        CDK_WINDOW_IMPL_QUARTZ (impl->transient_for->impl);
  [parent_impl->toplevel removeChildWindow:impl->toplevel];
  [parent_impl->toplevel addChildWindow:impl->toplevel
                         ordered:NSWindowAbove];
}

void
_cdk_quartz_window_did_become_main (CdkWindow *window)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  main_window_stack = g_slist_remove (main_window_stack, window);

  if (window->window_type != CDK_WINDOW_TEMP)
    main_window_stack = g_slist_prepend (main_window_stack, window);

  if (impl->transient_for)
    raise_transient (impl);

  clear_toplevel_order ();
}

void
_cdk_quartz_window_did_resign_main (CdkWindow *window)
{
  CdkWindow *new_window = NULL;

  if (main_window_stack)
    new_window = main_window_stack->data;
  else
    {
      GList *toplevels;

      toplevels = cdk_screen_get_toplevel_windows (cdk_screen_get_default ());
      if (toplevels)
        new_window = toplevels->data;
      g_list_free (toplevels);
    }

  if (new_window &&
      new_window != window &&
      CDK_WINDOW_IS_MAPPED (new_window) &&
      WINDOW_IS_TOPLEVEL (new_window))
    {
      CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (new_window->impl);

      [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
    }

  clear_toplevel_order ();
}

static NSScreen *
get_nsscreen_for_point (gint x, gint y)
{
  int i;
  NSArray *screens;
  NSScreen *screen = NULL;

  CDK_QUARTZ_ALLOC_POOL;

  screens = [NSScreen screens];

  for (i = 0; i < [screens count]; i++)
    {
      NSRect rect = [[screens objectAtIndex:i] frame];

      if (x >= rect.origin.x && x <= rect.origin.x + rect.size.width &&
          y >= rect.origin.y && y <= rect.origin.y + rect.size.height)
        {
          screen = [screens objectAtIndex:i];
          break;
        }
    }

  CDK_QUARTZ_RELEASE_POOL;

  return screen;
}

void
_cdk_quartz_display_create_window_impl (CdkDisplay    *display,
                                        CdkWindow     *window,
                                        CdkWindow     *real_parent,
                                        CdkScreen     *screen,
                                        CdkEventMask   event_mask,
                                        CdkWindowAttr *attributes,
                                        gint           attributes_mask)
{
  CdkWindowImplQuartz *impl;
  CdkWindowImplQuartz *parent_impl;
  CdkWindowTypeHint    type_hint = CDK_WINDOW_TYPE_HINT_NORMAL;

  CDK_QUARTZ_ALLOC_POOL;

  impl = g_object_new (CDK_TYPE_WINDOW_IMPL_QUARTZ, NULL);
  window->impl = CDK_WINDOW_IMPL (impl);
  impl->wrapper = window;

  parent_impl = CDK_WINDOW_IMPL_QUARTZ (window->parent->impl);

  switch (window->window_type)
    {
    case CDK_WINDOW_TOPLEVEL:
    case CDK_WINDOW_TEMP:
      if (CDK_WINDOW_TYPE (window->parent) != CDK_WINDOW_ROOT)
	{
	  /* The common code warns for this case */
          parent_impl = CDK_WINDOW_IMPL_QUARTZ (_cdk_root->impl);
	}
    }

  /* Maintain the z-ordered list of children. */
  if (window->parent != _cdk_root)
    parent_impl->sorted_children = g_list_prepend (parent_impl->sorted_children, window);
  else
    clear_toplevel_order ();

  cdk_window_set_cursor (window, ((attributes_mask & CDK_WA_CURSOR) ?
				  (attributes->cursor) :
				  NULL));

  impl->view = NULL;

  if (attributes_mask & CDK_WA_TYPE_HINT)
    {
      type_hint = attributes->type_hint;
      cdk_window_set_type_hint (window, type_hint);
    }

  switch (window->window_type)
    {
    case CDK_WINDOW_TOPLEVEL:
    case CDK_WINDOW_TEMP:
      {
        NSScreen *screen;
        NSRect screen_rect;
        NSRect content_rect;
        NSUInteger style_mask;
        int nx, ny;
        const char *title;

        /* initWithContentRect will place on the mainScreen by default.
         * We want to select the screen to place on ourselves.  We need
         * to find the screen the window will be on and correct the
         * content_rect coordinates to be relative to that screen.
         */
        _cdk_quartz_window_cdk_xy_to_xy (window->x, window->y, &nx, &ny);

        screen = get_nsscreen_for_point (nx, ny);
        screen_rect = [screen frame];
        nx -= screen_rect.origin.x;
        ny -= screen_rect.origin.y;

        content_rect = NSMakeRect (nx, ny - window->height,
                                   window->width,
                                   window->height);

        if (window->window_type == CDK_WINDOW_TEMP ||
            type_hint == CDK_WINDOW_TYPE_HINT_SPLASHSCREEN)
          {
            style_mask = CDK_QUARTZ_BORDERLESS_WINDOW;
          }
        else
          {
            style_mask = (CDK_QUARTZ_TITLED_WINDOW |
                          CDK_QUARTZ_CLOSABLE_WINDOW |
                          CDK_QUARTZ_MINIATURIZABLE_WINDOW |
                          CDK_QUARTZ_RESIZABLE_WINDOW);
          }

	impl->toplevel = [[CdkQuartzNSWindow alloc] initWithContentRect:content_rect 
			                                      styleMask:style_mask
			                                        backing:NSBackingStoreBuffered
			                                          defer:NO
                                                                  screen:screen];

        if (type_hint != CDK_WINDOW_TYPE_HINT_NORMAL)
          impl->toplevel.excludedFromWindowsMenu = true;

	if (attributes_mask & CDK_WA_TITLE)
	  title = attributes->title;
	else
	  title = get_default_title ();

	cdk_window_set_title (window, title);
  
	if (cdk_window_get_visual (window) == cdk_screen_get_rgba_visual (_cdk_screen))
	  {
	    [impl->toplevel setOpaque:NO];
	    [impl->toplevel setBackgroundColor:[NSColor clearColor]];
	  }

        content_rect.origin.x = 0;
        content_rect.origin.y = 0;

	impl->view = [[CdkQuartzView alloc] initWithFrame:content_rect];
	[impl->view setCdkWindow:window];
	[impl->toplevel setContentView:impl->view];
        [[NSNotificationCenter defaultCenter] addObserver: impl->toplevel
                                      selector: @selector (windowDidResize:)
                                      name: @"NSViewFrameDidChangeNotification"
                                      object: impl->view];
	[impl->view release];
      }
      break;

    case CDK_WINDOW_CHILD:
      {
	CdkWindowImplQuartz *parent_impl = CDK_WINDOW_IMPL_QUARTZ (window->parent->impl);

	if (!window->input_only)
	  {
	    NSRect frame_rect = NSMakeRect (window->x + window->parent->abs_x,
                                            window->y + window->parent->abs_y,
                                            window->width,
                                            window->height);
	
	    impl->view = [[CdkQuartzView alloc] initWithFrame:frame_rect];
	    
	    [impl->view setCdkWindow:window];

	    /* CdkWindows should be hidden by default */
	    [impl->view setHidden:YES];
	    [parent_impl->view addSubview:impl->view];
	    [impl->view release];
	  }
      }
      break;

    default:
      g_assert_not_reached ();
    }

  CDK_QUARTZ_RELEASE_POOL;
}

void
_cdk_quartz_window_update_position (CdkWindow *window)
{
  NSRect frame_rect;
  NSRect content_rect;
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  CDK_QUARTZ_ALLOC_POOL;

  frame_rect = [impl->toplevel frame];
  content_rect = [impl->toplevel contentRectForFrameRect:frame_rect];

  _cdk_quartz_window_xy_to_cdk_xy (content_rect.origin.x,
                                   content_rect.origin.y + content_rect.size.height,
                                   &window->x, &window->y);


  CDK_QUARTZ_RELEASE_POOL;
}

void
_cdk_quartz_window_init_windowing (CdkDisplay *display,
                                   CdkScreen  *screen)
{
  CdkWindowImplQuartz *impl;

  g_assert (_cdk_root == NULL);

  _cdk_root = _cdk_display_create_window (display);

  _cdk_root->impl = g_object_new (_cdk_root_window_impl_quartz_get_type (), NULL);
  _cdk_root->impl_window = _cdk_root;
  _cdk_root->visual = cdk_screen_get_system_visual (screen);

  impl = CDK_WINDOW_IMPL_QUARTZ (_cdk_root->impl);

  _cdk_quartz_screen_update_window_sizes (screen);

  _cdk_root->state = 0; /* We don't want CDK_WINDOW_STATE_WITHDRAWN here */
  _cdk_root->window_type = CDK_WINDOW_ROOT;
  _cdk_root->depth = 24;
  _cdk_root->viewable = TRUE;

  impl->wrapper = _cdk_root;
}

static void
cdk_quartz_window_destroy (CdkWindow *window,
                           gboolean   recursing,
                           gboolean   foreign_destroy)
{
  CdkWindowImplQuartz *impl;
  CdkWindow *parent;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  main_window_stack = g_slist_remove (main_window_stack, window);

  g_list_free (impl->sorted_children);
  impl->sorted_children = NULL;

  parent = window->parent;
  if (parent)
    {
      CdkWindowImplQuartz *parent_impl = CDK_WINDOW_IMPL_QUARTZ (parent->impl);

      parent_impl->sorted_children = g_list_remove (parent_impl->sorted_children, window);
    }

  if (impl->cairo_surface)
    {
      cairo_surface_finish (impl->cairo_surface);
      cairo_surface_set_user_data (impl->cairo_surface, &cdk_quartz_cairo_key,
				   NULL, NULL);
      impl->cairo_surface = NULL;
    }

  if (!recursing && !foreign_destroy)
    {
      CDK_QUARTZ_ALLOC_POOL;

      if (impl->toplevel)
	[impl->toplevel close];
      else if (impl->view)
	[impl->view removeFromSuperview];

      CDK_QUARTZ_RELEASE_POOL;
    }
}

static void
cdk_quartz_window_destroy_foreign (CdkWindow *window)
{
  /* Foreign windows aren't supported in OSX. */
}

/* FIXME: This might be possible to simplify with client-side windows. Also
 * note that already_mapped is not used yet, see the x11 backend.
*/
static void
cdk_window_quartz_show (CdkWindow *window, gboolean already_mapped)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  gboolean focus_on_map;

  CDK_QUARTZ_ALLOC_POOL;

  if (!CDK_WINDOW_IS_MAPPED (window))
    focus_on_map = window->focus_on_map;
  else
    focus_on_map = TRUE;

  if (WINDOW_IS_TOPLEVEL (window) && impl->toplevel)
    {
      gboolean make_key;

      make_key = (window->accept_focus && focus_on_map &&
                  window->window_type != CDK_WINDOW_TEMP);

      [(CdkQuartzNSWindow*)impl->toplevel showAndMakeKey:make_key];
      clear_toplevel_order ();

      _cdk_quartz_events_send_map_event (window);
    }
  else
    {
      [impl->view setHidden:NO];
    }

  [impl->view setNeedsDisplay:YES];

  cdk_synthesize_window_state (window, CDK_WINDOW_STATE_WITHDRAWN, 0);

  if (window->state & CDK_WINDOW_STATE_MAXIMIZED)
    cdk_window_maximize (window);

  if (window->state & CDK_WINDOW_STATE_ICONIFIED)
    cdk_window_iconify (window);

  if (impl->transient_for && !CDK_WINDOW_DESTROYED (impl->transient_for))
    _cdk_quartz_window_attach_to_parent (window);

  CDK_QUARTZ_RELEASE_POOL;
}

/* Temporarily unsets the parent window, if the window is a
 * transient. 
 */
void
_cdk_quartz_window_detach_from_parent (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;

  g_return_if_fail (CDK_IS_WINDOW (window));

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  
  g_return_if_fail (impl->toplevel != NULL);

  if (impl->transient_for && !CDK_WINDOW_DESTROYED (impl->transient_for))
    {
      CdkWindowImplQuartz *parent_impl;

      parent_impl = CDK_WINDOW_IMPL_QUARTZ (impl->transient_for->impl);
      [parent_impl->toplevel removeChildWindow:impl->toplevel];
      clear_toplevel_order ();
    }
}

/* Re-sets the parent window, if the window is a transient. */
void
_cdk_quartz_window_attach_to_parent (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;

  g_return_if_fail (CDK_IS_WINDOW (window));

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  
  g_return_if_fail (impl->toplevel != NULL);

  if (impl->transient_for && !CDK_WINDOW_DESTROYED (impl->transient_for))
    {
      CdkWindowImplQuartz *parent_impl;

      parent_impl = CDK_WINDOW_IMPL_QUARTZ (impl->transient_for->impl);
      [parent_impl->toplevel addChildWindow:impl->toplevel ordered:NSWindowAbove];
      clear_toplevel_order ();
    }
}

void
cdk_window_quartz_hide (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;
  CdkDisplay *display = cdk_window_get_display (window);
  CdkSeat *seat = cdk_display_get_default_seat (display);
  cdk_seat_ungrab (seat);

  /* Make sure we're not stuck in fullscreen mode. */
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  if (get_fullscreen_geometry (window))
    SetSystemUIMode (kUIModeNormal, 0);
#endif

  _cdk_window_clear_update_area (window);

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (WINDOW_IS_TOPLEVEL (window)) 
    {
     /* Update main window. */
      main_window_stack = g_slist_remove (main_window_stack, window);
      if ([NSApp mainWindow] == impl->toplevel)
        _cdk_quartz_window_did_resign_main (window);

      if (impl->transient_for)
        _cdk_quartz_window_detach_from_parent (window);

      [(CdkQuartzNSWindow*)impl->toplevel hide];
    }
  else if (impl->view)
    {
      [impl->view setHidden:YES];
    }
}

void
cdk_window_quartz_withdraw (CdkWindow *window)
{
  cdk_window_hide (window);
}

static void
move_resize_window_internal (CdkWindow *window,
			     gint       x,
			     gint       y,
			     gint       width,
			     gint       height)
{
  CdkWindowImplQuartz *impl;
  CdkRectangle old_visible;
  CdkRectangle new_visible;
  CdkRectangle scroll_rect;
  cairo_region_t *old_region;
  cairo_region_t *expose_region;
  NSSize delta;

  if (CDK_WINDOW_DESTROYED (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if ((x == -1 || (x == window->x)) &&
      (y == -1 || (y == window->y)) &&
      (width == -1 || (width == window->width)) &&
      (height == -1 || (height == window->height)))
    {
      return;
    }

  if (!impl->toplevel)
    {
      /* The previously visible area of this window in a coordinate
       * system rooted at the origin of this window.
       */
      old_visible.x = -window->x;
      old_visible.y = -window->y;

      old_visible.width = window->width;
      old_visible.height = window->height;
    }

  if (x != -1)
    {
      delta.width = x - window->x;
      window->x = x;
    }
  else
    {
      delta.width = 0;
    }

  if (y != -1)
    {
      delta.height = y - window->y;
      window->y = y;
    }
  else
    {
      delta.height = 0;
    }

  if (width != -1)
    window->width = width;

  if (height != -1)
    window->height = height;

  CDK_QUARTZ_ALLOC_POOL;

  if (impl->toplevel)
    {
      NSRect content_rect;
      NSRect frame_rect;
      gint gx, gy;

      _cdk_quartz_window_cdk_xy_to_xy (window->x, window->y + window->height,
                                       &gx, &gy);

      content_rect = NSMakeRect (gx, gy, window->width, window->height);

      frame_rect = [impl->toplevel frameRectForContentRect:content_rect];
      [impl->toplevel setFrame:frame_rect display:YES];
    }
  else 
    {
      if (!window->input_only)
        {
          NSRect nsrect;

          nsrect = NSMakeRect (window->x, window->y, window->width, window->height);

          /* The newly visible area of this window in a coordinate
           * system rooted at the origin of this window.
           */
          new_visible.x = -window->x;
          new_visible.y = -window->y;
          new_visible.width = old_visible.width;   /* parent has not changed size */
          new_visible.height = old_visible.height; /* parent has not changed size */

          expose_region = cairo_region_create_rectangle (&new_visible);
          old_region = cairo_region_create_rectangle (&old_visible);
          cairo_region_subtract (expose_region, old_region);

          /* Determine what (if any) part of the previously visible
           * part of the window can be copied without a redraw
           */
          scroll_rect = old_visible;
          scroll_rect.x -= delta.width;
          scroll_rect.y -= delta.height;
          cdk_rectangle_intersect (&scroll_rect, &old_visible, &scroll_rect);

          if (!cairo_region_is_empty (expose_region))
            {
              if (scroll_rect.width != 0 && scroll_rect.height != 0)
                {
                  [impl->view scrollRect:NSMakeRect (scroll_rect.x,
                                                     scroll_rect.y,
                                                     scroll_rect.width,
                                                     scroll_rect.height)
			              by:delta];
                }

              [impl->view setFrame:nsrect];

              cdk_quartz_window_set_needs_display_in_region (window, expose_region);
            }
          else
            {
              [impl->view setFrame:nsrect];
              [impl->view setNeedsDisplay:YES];
            }

          cairo_region_destroy (expose_region);
          cairo_region_destroy (old_region);
        }
    }

  if (window->gl_paint_context != NULL)
    [CDK_QUARTZ_GL_CONTEXT (window->gl_paint_context)->gl_context update];

  CDK_QUARTZ_RELEASE_POOL;
}

static inline void
window_quartz_move (CdkWindow *window,
                    gint       x,
                    gint       y)
{
  g_return_if_fail (CDK_IS_WINDOW (window));

  if (window->state & CDK_WINDOW_STATE_FULLSCREEN)
    return;

  move_resize_window_internal (window, x, y, -1, -1);
}

static inline void
window_quartz_resize (CdkWindow *window,
                      gint       width,
                      gint       height)
{
  g_return_if_fail (CDK_IS_WINDOW (window));

  if (window->state & CDK_WINDOW_STATE_FULLSCREEN)
    return;

  if (width < 1)
    width = 1;
  if (height < 1)
    height = 1;

  move_resize_window_internal (window, -1, -1, width, height);
}

static inline void
window_quartz_move_resize (CdkWindow *window,
                           gint       x,
                           gint       y,
                           gint       width,
                           gint       height)
{
  if (width < 1)
    width = 1;
  if (height < 1)
    height = 1;

  move_resize_window_internal (window, x, y, width, height);
}

static void
cdk_window_quartz_move_resize (CdkWindow *window,
                               gboolean   with_move,
                               gint       x,
                               gint       y,
                               gint       width,
                               gint       height)
{
  if (with_move && (width < 0 && height < 0))
    window_quartz_move (window, x, y);
  else
    {
      if (with_move)
        window_quartz_move_resize (window, x, y, width, height);
      else
        window_quartz_resize (window, width, height);
    }
}

/* FIXME: This might need fixing (reparenting didn't work before client-side
 * windows either).
 */
static gboolean
cdk_window_quartz_reparent (CdkWindow *window,
                            CdkWindow *new_parent,
                            gint       x,
                            gint       y)
{
  CdkWindow *old_parent;
  CdkWindowImplQuartz *impl, *old_parent_impl, *new_parent_impl;
  NSView *view, *new_parent_view;

  if (new_parent == _cdk_root)
    {
      /* Could be added, just needs implementing. */
      g_warning ("Reparenting to root window is not supported yet in the Mac OS X backend");
      return FALSE;
    }

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  view = impl->view;

  new_parent_impl = CDK_WINDOW_IMPL_QUARTZ (new_parent->impl);
  new_parent_view = new_parent_impl->view;

  old_parent = window->parent;
  old_parent_impl = CDK_WINDOW_IMPL_QUARTZ (old_parent->impl);

  [view retain];

  [view removeFromSuperview];
  [new_parent_view addSubview:view];

  [view release];

  window->parent = new_parent;

  if (old_parent)
    {
      old_parent_impl->sorted_children = g_list_remove (old_parent_impl->sorted_children, window);
    }

  new_parent_impl->sorted_children = g_list_prepend (new_parent_impl->sorted_children, window);

  return FALSE;
}

/* Get the toplevel ordering from NSApp and update our own list. We do
 * this on demand since the NSApp’s list is not up to date directly
 * after we get windowDidBecomeMain.
 */
static void
update_toplevel_order (void)
{
  CdkWindowImplQuartz *root_impl;
  NSEnumerator *enumerator;
  id nswindow;
  GList *toplevels = NULL;

  root_impl = CDK_WINDOW_IMPL_QUARTZ (_cdk_root->impl);

  if (root_impl->sorted_children)
    return;

  CDK_QUARTZ_ALLOC_POOL;

  enumerator = [[NSApp orderedWindows] objectEnumerator];
  while ((nswindow = [enumerator nextObject]))
    {
      CdkWindow *window;

      if (![[nswindow contentView] isKindOfClass:[CdkQuartzView class]])
        continue;

      window = [(CdkQuartzView *)[nswindow contentView] cdkWindow];
      toplevels = g_list_prepend (toplevels, window);
    }

  CDK_QUARTZ_RELEASE_POOL;

  root_impl->sorted_children = g_list_reverse (toplevels);
}

static void
clear_toplevel_order (void)
{
  CdkWindowImplQuartz *root_impl;

  root_impl = CDK_WINDOW_IMPL_QUARTZ (_cdk_root->impl);

  g_list_free (root_impl->sorted_children);
  root_impl->sorted_children = NULL;
}

static void
cdk_window_quartz_raise (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window))
    return;

  if (WINDOW_IS_TOPLEVEL (window))
    {
      CdkWindowImplQuartz *impl;

      impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

      if (impl->transient_for)
        raise_transient (impl);
      else
        [impl->toplevel orderFront:impl->toplevel];

      clear_toplevel_order ();
    }
  else
    {
      CdkWindow *parent = window->parent;

      if (parent)
        {
          CdkWindowImplQuartz *impl;

          impl = (CdkWindowImplQuartz *)parent->impl;

          impl->sorted_children = g_list_remove (impl->sorted_children, window);
          impl->sorted_children = g_list_prepend (impl->sorted_children, window);
        }
    }
}

static void
cdk_window_quartz_lower (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window))
    return;

  if (WINDOW_IS_TOPLEVEL (window))
    {
      CdkWindowImplQuartz *impl;

      impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
      [impl->toplevel orderBack:impl->toplevel];

      clear_toplevel_order ();
    }
  else
    {
      CdkWindow *parent = window->parent;

      if (parent)
        {
          CdkWindowImplQuartz *impl;

          impl = (CdkWindowImplQuartz *)parent->impl;

          impl->sorted_children = g_list_remove (impl->sorted_children, window);
          impl->sorted_children = g_list_append (impl->sorted_children, window);
        }
    }
}

static void
cdk_window_quartz_restack_toplevel (CdkWindow *window,
				    CdkWindow *sibling,
				    gboolean   above)
{
  CdkWindowImplQuartz *impl;
  gint sibling_num;

  impl = CDK_WINDOW_IMPL_QUARTZ (sibling->impl);
  sibling_num = [impl->toplevel windowNumber];

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (above)
    [impl->toplevel orderWindow:NSWindowAbove relativeTo:sibling_num];
  else
    [impl->toplevel orderWindow:NSWindowBelow relativeTo:sibling_num];
}

static void
cdk_window_quartz_set_background (CdkWindow       *window,
                                  cairo_pattern_t *pattern)
{
  /* FIXME: We could theoretically set the background color for toplevels
   * here. (Currently we draw the background before emitting expose events)
   */
}

static void
cdk_window_quartz_set_device_cursor (CdkWindow *window,
                                     CdkDevice *device,
                                     CdkCursor *cursor)
{
  NSCursor *nscursor;

  if (CDK_WINDOW_DESTROYED (window))
    return;

  nscursor = _cdk_quartz_cursor_get_ns_cursor (cursor);

  [nscursor set];
}

static void
cdk_window_quartz_get_geometry (CdkWindow *window,
                                gint      *x,
                                gint      *y,
                                gint      *width,
                                gint      *height)
{
  CdkWindowImplQuartz *impl;
  NSRect ns_rect;

  if (CDK_WINDOW_DESTROYED (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  if (window == _cdk_root)
    {
      if (x) 
        *x = 0;
      if (y) 
        *y = 0;

      if (width) 
        *width = window->width;
      if (height)
        *height = window->height;
    }
  else if (WINDOW_IS_TOPLEVEL (window))
    {
      ns_rect = [impl->toplevel contentRectForFrameRect:[impl->toplevel frame]];

      /* This doesn't work exactly as in X. There doesn't seem to be a
       * way to get the coords relative to the parent window (usually
       * the window frame), but that seems useless except for
       * borderless windows where it's relative to the root window. So
       * we return (0, 0) (should be something like (0, 22)) for
       * windows with borders and the root relative coordinates
       * otherwise.
       */
      if ([impl->toplevel styleMask] == CDK_QUARTZ_BORDERLESS_WINDOW)
        {
          _cdk_quartz_window_xy_to_cdk_xy (ns_rect.origin.x,
                                           ns_rect.origin.y + ns_rect.size.height,
                                           x, y);
        }
      else 
        {
          if (x)
            *x = 0;
          if (y)
            *y = 0;
        }

      if (width)
        *width = ns_rect.size.width;
      if (height)
        *height = ns_rect.size.height;
    }
  else
    {
      ns_rect = [impl->view frame];
      
      if (x)
        *x = ns_rect.origin.x;
      if (y)
        *y = ns_rect.origin.y;
      if (width)
        *width  = ns_rect.size.width;
      if (height)
        *height = ns_rect.size.height;
    }
}

static void
cdk_window_quartz_get_root_coords (CdkWindow *window,
                                   gint       x,
                                   gint       y,
                                   gint      *root_x,
                                   gint      *root_y)
{
  int tmp_x = 0, tmp_y = 0;
  CdkWindow *toplevel;
  NSRect content_rect;
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window)) 
    {
      if (root_x)
	*root_x = 0;
      if (root_y)
	*root_y = 0;
      
      return;
    }

  if (window == _cdk_root)
    {
      if (root_x)
        *root_x = x;
      if (root_y)
        *root_y = y;

      return;
    }
  
  toplevel = cdk_window_get_toplevel (window);
  impl = CDK_WINDOW_IMPL_QUARTZ (toplevel->impl);

  content_rect = [impl->toplevel contentRectForFrameRect:[impl->toplevel frame]];

  _cdk_quartz_window_xy_to_cdk_xy (content_rect.origin.x,
                                   content_rect.origin.y + content_rect.size.height,
                                   &tmp_x, &tmp_y);

  tmp_x += x;
  tmp_y += y;

  while (window != toplevel)
    {
      if (_cdk_window_has_impl ((CdkWindow *)window))
        {
          tmp_x += window->x;
          tmp_y += window->y;
        }

      window = window->parent;
    }

  if (root_x)
    *root_x = tmp_x;
  if (root_y)
    *root_y = tmp_y;
}

/* Returns coordinates relative to the passed in window. */
static CdkWindow *
cdk_window_quartz_get_device_state_helper (CdkWindow       *window,
                                           CdkDevice       *device,
                                           gdouble         *x,
                                           gdouble         *y,
                                           CdkModifierType *mask)
{
  NSPoint point;
  gint x_tmp, y_tmp;
  CdkWindow *toplevel;
  CdkWindow *found_window;

  g_return_val_if_fail (window == NULL || CDK_IS_WINDOW (window), NULL);

  if (CDK_WINDOW_DESTROYED (window))
    {
      *x = 0;
      *y = 0;
      *mask = 0;
      return NULL;
    }
  
  toplevel = cdk_window_get_toplevel (window);

  *mask = _cdk_quartz_events_get_current_keyboard_modifiers () |
      _cdk_quartz_events_get_current_mouse_modifiers ();

  /* Get the y coordinate, needs to be flipped. */
  if (window == _cdk_root)
    {
      point = [NSEvent mouseLocation];
      _cdk_quartz_window_nspoint_to_cdk_xy (point, &x_tmp, &y_tmp);
    }
  else
    {
      CdkWindowImplQuartz *impl;
      NSWindow *nswindow;

      impl = CDK_WINDOW_IMPL_QUARTZ (toplevel->impl);
      nswindow = impl->toplevel;

      point = [nswindow mouseLocationOutsideOfEventStream];

      x_tmp = point.x;
      y_tmp = toplevel->height - point.y;

      window = (CdkWindow *)toplevel;
    }

  found_window = _cdk_quartz_window_find_child (window, x_tmp, y_tmp,
                                                FALSE);

  /* We never return the root window. */
  if (found_window == _cdk_root)
    found_window = NULL;

  *x = x_tmp;
  *y = y_tmp;

  return found_window;
}

static gboolean
cdk_window_quartz_get_device_state (CdkWindow       *window,
                                    CdkDevice       *device,
                                    gdouble          *x,
                                    gdouble          *y,
                                    CdkModifierType *mask)
{
  return cdk_window_quartz_get_device_state_helper (window,
                                                    device,
                                                    x, y, mask) != NULL;
}

static CdkEventMask
cdk_window_quartz_get_events (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window))
    return 0;
  else
    return window->event_mask;
}

static void
cdk_window_quartz_set_events (CdkWindow       *window,
                              CdkEventMask     event_mask)
{
  /* The mask is set in the common code. */
}

static void
cdk_quartz_window_set_urgency_hint (CdkWindow *window,
                                    gboolean   urgent)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  /* FIXME: Implement */
}

static void
cdk_quartz_window_set_geometry_hints (CdkWindow         *window,
                                      const CdkGeometry *geometry,
                                      CdkWindowHints     geom_mask)
{
  CdkWindowImplQuartz *impl;

  g_return_if_fail (geometry != NULL);

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;
  
  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  if (!impl->toplevel)
    return;

  if (geom_mask & CDK_HINT_POS)
    {
      /* FIXME: Implement */
    }

  if (geom_mask & CDK_HINT_USER_POS)
    {
      /* FIXME: Implement */
    }

  if (geom_mask & CDK_HINT_USER_SIZE)
    {
      /* FIXME: Implement */
    }
  
  if (geom_mask & CDK_HINT_MIN_SIZE)
    {
      NSSize size;

      size.width = geometry->min_width;
      size.height = geometry->min_height;

      [impl->toplevel setContentMinSize:size];
    }
  
  if (geom_mask & CDK_HINT_MAX_SIZE)
    {
      NSSize size;

      size.width = geometry->max_width;
      size.height = geometry->max_height;

      [impl->toplevel setContentMaxSize:size];
    }
  
  if (geom_mask & CDK_HINT_BASE_SIZE)
    {
      /* FIXME: Implement */
    }
  
  if (geom_mask & CDK_HINT_RESIZE_INC)
    {
      NSSize size;

      size.width = geometry->width_inc;
      size.height = geometry->height_inc;

      [impl->toplevel setContentResizeIncrements:size];
    }
  
  if (geom_mask & CDK_HINT_ASPECT)
    {
      NSSize size;

      if (geometry->min_aspect != geometry->max_aspect)
        {
          g_warning ("Only equal minimum and maximum aspect ratios are supported on Mac OS. Using minimum aspect ratio...");
        }

      size.width = geometry->min_aspect;
      size.height = 1.0;

      [impl->toplevel setContentAspectRatio:size];
    }

  if (geom_mask & CDK_HINT_WIN_GRAVITY)
    {
      /* FIXME: Implement */
    }
}

static void
cdk_quartz_window_set_title (CdkWindow   *window,
                             const gchar *title)
{
  CdkWindowImplQuartz *impl;

  g_return_if_fail (title != NULL);

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (impl->toplevel)
    {
      CDK_QUARTZ_ALLOC_POOL;
      [impl->toplevel setTitle:[NSString stringWithUTF8String:title]];
      CDK_QUARTZ_RELEASE_POOL;
    }
}

static void
cdk_quartz_window_set_role (CdkWindow   *window,
                            const gchar *role)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      WINDOW_IS_TOPLEVEL (window))
    return;

  /* FIXME: Implement */
}

static void
cdk_quartz_window_set_startup_id (CdkWindow   *window,
                                  const gchar *startup_id)
{
  /* FIXME: Implement? */
}

static void
cdk_quartz_window_set_transient_for (CdkWindow *window,
                                     CdkWindow *parent)
{
  CdkWindowImplQuartz *window_impl;
  CdkWindowImplQuartz *parent_impl;

  if (CDK_WINDOW_DESTROYED (window)  || CDK_WINDOW_DESTROYED (parent) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  window_impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  if (!window_impl->toplevel)
    return;

  CDK_QUARTZ_ALLOC_POOL;

  if (window_impl->transient_for)
    {
      _cdk_quartz_window_detach_from_parent (window);

      g_object_unref (window_impl->transient_for);
      window_impl->transient_for = NULL;
    }

  parent_impl = CDK_WINDOW_IMPL_QUARTZ (parent->impl);
  if (parent_impl->toplevel)
    {
      /* We save the parent because it needs to be unset/reset when
       * hiding and showing the window. 
       */

      /* We don't set transients for tooltips, they are already
       * handled by the window level being the top one. If we do, then
       * the parent window will be brought to the top just because the
       * tooltip is, which is not what we want.
       */
      if (cdk_window_get_type_hint (window) != CDK_WINDOW_TYPE_HINT_TOOLTIP)
        {
          window_impl->transient_for = g_object_ref (parent);

          /* We only add the window if it is shown, otherwise it will
           * be shown unconditionally here. If it is not shown, the
           * window will be added in show() instead.
           */
          if (!(window->state & CDK_WINDOW_STATE_WITHDRAWN))
            _cdk_quartz_window_attach_to_parent (window);
        }
    }
  
  CDK_QUARTZ_RELEASE_POOL;
}

static void
cdk_window_quartz_shape_combine_region (CdkWindow       *window,
                                        const cairo_region_t *shape,
                                        gint             x,
                                        gint             y)
{
  /* FIXME: Implement */
}

static void
cdk_window_quartz_input_shape_combine_region (CdkWindow       *window,
                                              const cairo_region_t *shape_region,
                                              gint             offset_x,
                                              gint             offset_y)
{
  /* FIXME: Implement */
}

static void
cdk_quartz_window_set_override_redirect (CdkWindow *window,
                                         gboolean override_redirect)
{
  /* FIXME: Implement */
}

static void
cdk_quartz_window_set_accept_focus (CdkWindow *window,
                                    gboolean accept_focus)
{
  window->accept_focus = accept_focus != FALSE;
}

static void
cdk_quartz_window_set_focus_on_map (CdkWindow *window,
                                    gboolean focus_on_map)
{
  window->focus_on_map = focus_on_map != FALSE;
}

static void
cdk_quartz_window_set_icon_name (CdkWindow   *window,
                                 const gchar *name)
{
  /* FIXME: Implement */
}

static void
cdk_quartz_window_focus (CdkWindow *window,
                         guint32    timestamp)
{
  CdkWindowImplQuartz *impl;
	
  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  if (window->accept_focus && window->window_type != CDK_WINDOW_TEMP)
    {
      CDK_QUARTZ_ALLOC_POOL;
      [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
      clear_toplevel_order ();
      CDK_QUARTZ_RELEASE_POOL;
    }
}

static gint
window_type_hint_to_level (CdkWindowTypeHint hint)
{
  /*  the order in this switch statement corresponds to the actual
   *  stacking order: the first group is top, the last group is bottom
   */
  switch (hint)
    {
    case CDK_WINDOW_TYPE_HINT_POPUP_MENU:
    case CDK_WINDOW_TYPE_HINT_COMBO:
    case CDK_WINDOW_TYPE_HINT_DND:
    case CDK_WINDOW_TYPE_HINT_TOOLTIP:
      return NSPopUpMenuWindowLevel;

    case CDK_WINDOW_TYPE_HINT_NOTIFICATION:
    case CDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
      return NSStatusWindowLevel;

    case CDK_WINDOW_TYPE_HINT_MENU: /* Torn-off menu */
    case CDK_WINDOW_TYPE_HINT_DROPDOWN_MENU: /* Menu from menubar */
      return NSTornOffMenuWindowLevel;

    case CDK_WINDOW_TYPE_HINT_DOCK:
      return NSFloatingWindowLevel; /* NSDockWindowLevel is deprecated, and not replaced */

    case CDK_WINDOW_TYPE_HINT_UTILITY:
    case CDK_WINDOW_TYPE_HINT_DIALOG:  /* Dialog window */
    case CDK_WINDOW_TYPE_HINT_NORMAL:  /* Normal toplevel window */
    case CDK_WINDOW_TYPE_HINT_TOOLBAR: /* Window used to implement toolbars */
      return NSNormalWindowLevel;

    case CDK_WINDOW_TYPE_HINT_DESKTOP:
      return kCGDesktopWindowLevelKey; /* doesn't map to any real Cocoa model */

    default:
      break;
    }

  return NSNormalWindowLevel;
}

static gboolean
window_type_hint_to_shadow (CdkWindowTypeHint hint)
{
  switch (hint)
    {
    case CDK_WINDOW_TYPE_HINT_NORMAL:  /* Normal toplevel window */
    case CDK_WINDOW_TYPE_HINT_DIALOG:  /* Dialog window */
    case CDK_WINDOW_TYPE_HINT_DOCK:
    case CDK_WINDOW_TYPE_HINT_UTILITY:
    case CDK_WINDOW_TYPE_HINT_MENU: /* Torn-off menu */
    case CDK_WINDOW_TYPE_HINT_DROPDOWN_MENU: /* Menu from menubar */
    case CDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
    case CDK_WINDOW_TYPE_HINT_POPUP_MENU:
    case CDK_WINDOW_TYPE_HINT_COMBO:
    case CDK_WINDOW_TYPE_HINT_NOTIFICATION:
    case CDK_WINDOW_TYPE_HINT_TOOLTIP:
      return TRUE;

    case CDK_WINDOW_TYPE_HINT_TOOLBAR: /* Window used to implement toolbars */
    case CDK_WINDOW_TYPE_HINT_DESKTOP: /* N/A */
    case CDK_WINDOW_TYPE_HINT_DND:
      break;

    default:
      break;
    }

  return FALSE;
}

static gboolean
window_type_hint_to_hides_on_deactivate (CdkWindowTypeHint hint)
{
  switch (hint)
    {
    case CDK_WINDOW_TYPE_HINT_UTILITY:
    case CDK_WINDOW_TYPE_HINT_MENU: /* Torn-off menu */
    case CDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
    case CDK_WINDOW_TYPE_HINT_NOTIFICATION:
    case CDK_WINDOW_TYPE_HINT_TOOLTIP:
      return TRUE;

    default:
      break;
    }

  return FALSE;
}

static void
_cdk_quartz_window_update_has_shadow (CdkWindowImplQuartz *impl)
{
    gboolean has_shadow;

    /* In case there is any shadow set we have to turn off the
     * NSWindow setHasShadow as the system drawn ones wont match our
     * window boundary anymore */
    has_shadow = (window_type_hint_to_shadow (impl->type_hint) && !impl->shadow_max);

    [impl->toplevel setHasShadow: has_shadow];
}

static void
_cdk_quartz_window_set_collection_behavior (NSWindow *nswindow,
                                            CdkWindowTypeHint hint)
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 10110
#define CDK_QUARTZ_ALLOWS_TILING NSWindowCollectionBehaviorFullScreenAllowsTiling
#define CDK_QUARTZ_DISALLOWS_TILING NSWindowCollectionBehaviorFullScreenDisallowsTiling
#else
#define CDK_QUARTZ_ALLOWS_TILING 1 << 11
#define CDK_QUARTZ_DISALLOWS_TILING 1 << 12
#endif
  if (cdk_quartz_osx_version() >= CDK_OSX_LION)
    {
      /* Fullscreen Collection Behavior */
      NSWindowCollectionBehavior behavior = [nswindow collectionBehavior];
      switch (hint)
        {
        case CDK_WINDOW_TYPE_HINT_NORMAL:
        case CDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
          behavior &= ~(NSWindowCollectionBehaviorFullScreenAuxiliary &
                        CDK_QUARTZ_DISALLOWS_TILING);
          behavior |= (NSWindowCollectionBehaviorFullScreenPrimary |
                       CDK_QUARTZ_ALLOWS_TILING);

          break;
        default:
          behavior &= ~(NSWindowCollectionBehaviorFullScreenPrimary &
                        CDK_QUARTZ_ALLOWS_TILING);
          behavior |= (NSWindowCollectionBehaviorFullScreenAuxiliary |
                       CDK_QUARTZ_DISALLOWS_TILING);
          break;
        }
      [nswindow setCollectionBehavior:behavior];
    }
#undef CDK_QUARTZ_ALLOWS_TILING
#undef CDK_QUARTZ_DISALLOWS_TILING
#endif
}

static void
cdk_quartz_window_set_type_hint (CdkWindow        *window,
                                 CdkWindowTypeHint hint)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  impl->type_hint = hint;

  /* Match the documentation, only do something if we're not mapped yet. */
  if (CDK_WINDOW_IS_MAPPED (window))
    return;

  _cdk_quartz_window_update_has_shadow (impl);
  if (impl->toplevel)
    _cdk_quartz_window_set_collection_behavior (impl->toplevel, hint);
  [impl->toplevel setLevel: window_type_hint_to_level (hint)];
  [impl->toplevel setHidesOnDeactivate: window_type_hint_to_hides_on_deactivate (hint)];
}

static CdkWindowTypeHint
cdk_quartz_window_get_type_hint (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return CDK_WINDOW_TYPE_HINT_NORMAL;
  
  return CDK_WINDOW_IMPL_QUARTZ (window->impl)->type_hint;
}

static void
cdk_quartz_window_set_modal_hint (CdkWindow *window,
                                  gboolean   modal)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  /* FIXME: Implement */
}

static void
cdk_quartz_window_set_skip_taskbar_hint (CdkWindow *window,
                                         gboolean   skips_taskbar)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  /* FIXME: Implement */
}

static void
cdk_quartz_window_set_skip_pager_hint (CdkWindow *window,
                                       gboolean   skips_pager)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  /* FIXME: Implement */
}

static void
cdk_quartz_window_begin_resize_drag (CdkWindow     *window,
                                     CdkWindowEdge  edge,
                                     CdkDevice     *device,
                                     gint           button,
                                     gint           root_x,
                                     gint           root_y,
                                     guint32        timestamp)
{
  CdkWindowImplQuartz *impl;

  g_return_if_fail (CDK_IS_WINDOW (window));

  if (CDK_WINDOW_DESTROYED (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (!impl->toplevel)
    {
      g_warning ("Can't call cdk_window_begin_resize_drag on non-toplevel window");
      return;
    }

  [(CdkQuartzNSWindow *)impl->toplevel beginManualResize:edge];
}

static void
cdk_quartz_window_begin_move_drag (CdkWindow *window,
                                   CdkDevice *device,
                                   gint       button,
                                   gint       root_x,
                                   gint       root_y,
                                   guint32    timestamp)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (!impl->toplevel)
    {
      g_warning ("Can't call cdk_window_begin_move_drag on non-toplevel window");
      return;
    }

  [(CdkQuartzNSWindow *)impl->toplevel beginManualMove];
}

static void
cdk_quartz_window_set_icon_list (CdkWindow *window,
                                 GList     *pixbufs)
{
  /* FIXME: Implement */
}

static void
cdk_quartz_window_get_frame_extents (CdkWindow    *window,
                                     CdkRectangle *rect)
{
  CdkWindow *toplevel;
  CdkWindowImplQuartz *impl;
  NSRect ns_rect;

  g_return_if_fail (rect != NULL);


  rect->x = 0;
  rect->y = 0;
  rect->width = 1;
  rect->height = 1;
  
  toplevel = cdk_window_get_effective_toplevel (window);
  impl = CDK_WINDOW_IMPL_QUARTZ (toplevel->impl);

  ns_rect = [impl->toplevel frame];

  _cdk_quartz_window_xy_to_cdk_xy (ns_rect.origin.x,
                                   ns_rect.origin.y + ns_rect.size.height,
                                   &rect->x, &rect->y);

  rect->width = ns_rect.size.width;
  rect->height = ns_rect.size.height;
}

/* Fake protocol to make gcc think that it's OK to call setStyleMask
   even if it isn't. We check to make sure before actually calling
   it. */

@protocol CanSetStyleMask
- (void)setStyleMask:(int)mask;
@end

static void
cdk_quartz_window_set_decorations (CdkWindow       *window,
			    CdkWMDecoration  decorations)
{
  CdkWindowImplQuartz *impl;
  NSUInteger old_mask, new_mask;
  NSView *old_view;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (decorations == 0 || CDK_WINDOW_TYPE (window) == CDK_WINDOW_TEMP ||
      impl->type_hint == CDK_WINDOW_TYPE_HINT_SPLASHSCREEN )
    {
      new_mask = CDK_QUARTZ_BORDERLESS_WINDOW;
    }
  else
    {
      /* FIXME: Honor other CDK_DECOR_* flags. */
      new_mask = (CDK_QUARTZ_TITLED_WINDOW | CDK_QUARTZ_CLOSABLE_WINDOW |
                  CDK_QUARTZ_MINIATURIZABLE_WINDOW |
                  CDK_QUARTZ_RESIZABLE_WINDOW);
    }

  CDK_QUARTZ_ALLOC_POOL;

  old_mask = [impl->toplevel styleMask];

  if (old_mask != new_mask)
    {
      NSRect rect;

      old_view = [[impl->toplevel contentView] retain];

      rect = [impl->toplevel frame];

      /* Properly update the size of the window when the titlebar is
       * added or removed.
       */
      if (old_mask == CDK_QUARTZ_BORDERLESS_WINDOW &&
          new_mask != CDK_QUARTZ_BORDERLESS_WINDOW)
        {
          rect = [NSWindow frameRectForContentRect:rect styleMask:new_mask];

        }
      else if (old_mask != CDK_QUARTZ_BORDERLESS_WINDOW &&
               new_mask == CDK_QUARTZ_BORDERLESS_WINDOW)
        {
          rect = [NSWindow contentRectForFrameRect:rect styleMask:old_mask];
        }

      /* Note, before OS 10.6 there doesn't seem to be a way to change this
       * without recreating the toplevel. From 10.6 onward, a simple call to
       * setStyleMask takes care of most of this, except for ensuring that the
       * title is set.
       */
      if ([impl->toplevel respondsToSelector:@selector(setStyleMask:)])
        {
          NSString *title = [impl->toplevel title];

          [(id<CanSetStyleMask>)impl->toplevel setStyleMask:new_mask];

          /* It appears that unsetting and then resetting
           * CDK_QUARTZ_TITLED_WINDOW does not reset the title in the
           * title bar as might be expected.
           *
           * In theory we only need to set this if new_mask includes
           * CDK_QUARTZ_TITLED_WINDOW. This behaved extremely oddly when
           * conditionalized upon that and since it has no side effects (i.e.
           * if CDK_QUARTZ_TITLED_WINDOW is not requested, the title will not be
           * displayed) just do it unconditionally. We also must null check
           * 'title' before setting it to avoid crashing.
           */
          if (title)
            [impl->toplevel setTitle:title];
        }
      else
        {
          NSString *title = [impl->toplevel title];
          NSColor *bg = [impl->toplevel backgroundColor];
          NSScreen *screen = [impl->toplevel screen];

          /* Make sure the old window is closed, recall that releasedWhenClosed
           * is set on CdkQuartzWindows.
           */
          [impl->toplevel close];

          impl->toplevel = [[CdkQuartzNSWindow alloc] initWithContentRect:rect
                                                                styleMask:new_mask
                                                                  backing:NSBackingStoreBuffered
                                                                    defer:NO
                                                                   screen:screen];
          _cdk_quartz_window_update_has_shadow (impl);

          [impl->toplevel setLevel: window_type_hint_to_level (impl->type_hint)];
          if (title)
            [impl->toplevel setTitle:title];
          [impl->toplevel setBackgroundColor:bg];
          [impl->toplevel setHidesOnDeactivate: window_type_hint_to_hides_on_deactivate (impl->type_hint)];
          [impl->toplevel setContentView:old_view];
        }

      if (new_mask == CDK_QUARTZ_BORDERLESS_WINDOW)
        {
          [impl->toplevel setContentSize:rect.size];
        }
      else
        [impl->toplevel setFrame:rect display:YES];

      /* Invalidate the window shadow for non-opaque views that have shadow
       * enabled, to get the shadow shape updated.
       */
      if (![old_view isOpaque] && [impl->toplevel hasShadow])
        [(CdkQuartzView*)old_view setNeedsInvalidateShadow:YES];

      [old_view release];
    }

  CDK_QUARTZ_RELEASE_POOL;
}

static gboolean
cdk_quartz_window_get_decorations (CdkWindow       *window,
                                   CdkWMDecoration *decorations)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return FALSE;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (decorations)
    {
      /* Borderless is 0, so we can't check it as a bit being set. */
      if ([impl->toplevel styleMask] == CDK_QUARTZ_BORDERLESS_WINDOW)
        {
          *decorations = 0;
        }
      else
        {
          /* FIXME: Honor the other CDK_DECOR_* flags. */
          *decorations = CDK_DECOR_ALL;
        }
    }

  return TRUE;
}

static void
cdk_quartz_window_set_functions (CdkWindow    *window,
                                 CdkWMFunction functions)
{
  CdkWindowImplQuartz *impl;
  gboolean min, max, close;

  g_return_if_fail (CDK_IS_WINDOW (window));

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (functions & CDK_FUNC_ALL)
    {
      min = !(functions & CDK_FUNC_MINIMIZE);
      max = !(functions & CDK_FUNC_MAXIMIZE);
      close = !(functions & CDK_FUNC_CLOSE);
    }
  else
    {
      min = (functions & CDK_FUNC_MINIMIZE);
      max = (functions & CDK_FUNC_MAXIMIZE);
      close = (functions & CDK_FUNC_CLOSE);
    }

  if (impl->toplevel)
    {
      NSUInteger mask = [impl->toplevel styleMask];

      if (min)
        mask = mask | CDK_QUARTZ_MINIATURIZABLE_WINDOW;
      else
        mask = mask & ~CDK_QUARTZ_MINIATURIZABLE_WINDOW;

      if (max)
        mask = mask | CDK_QUARTZ_RESIZABLE_WINDOW;
      else
        mask = mask & ~CDK_QUARTZ_RESIZABLE_WINDOW;

      if (close)
        mask = mask | CDK_QUARTZ_CLOSABLE_WINDOW;
      else
        mask = mask & ~CDK_QUARTZ_CLOSABLE_WINDOW;

      [impl->toplevel setStyleMask:mask];
    }
}

static void
cdk_quartz_window_stick (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;
}

static void
cdk_quartz_window_unstick (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;
}

static void
cdk_quartz_window_maximize (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;
  gboolean maximized;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  maximized = cdk_window_get_state (window) & CDK_WINDOW_STATE_MAXIMIZED;

  if (CDK_WINDOW_IS_MAPPED (window))
    {
      CDK_QUARTZ_ALLOC_POOL;

      if (impl->toplevel && !maximized)
        [impl->toplevel zoom:nil];

      CDK_QUARTZ_RELEASE_POOL;
    }
}

static void
cdk_quartz_window_unmaximize (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;
  gboolean maximized;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  maximized = cdk_window_get_state (window) & CDK_WINDOW_STATE_MAXIMIZED;

  if (CDK_WINDOW_IS_MAPPED (window))
    {
      CDK_QUARTZ_ALLOC_POOL;

      if (impl->toplevel && maximized)
        [impl->toplevel zoom:nil];

      CDK_QUARTZ_RELEASE_POOL;
    }
}

static void
cdk_quartz_window_iconify (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (CDK_WINDOW_IS_MAPPED (window))
    {
      CDK_QUARTZ_ALLOC_POOL;

      if (impl->toplevel)
	[impl->toplevel miniaturize:nil];

      CDK_QUARTZ_RELEASE_POOL;
    }
  else
    {
      cdk_synthesize_window_state (window,
				   0,
				   CDK_WINDOW_STATE_ICONIFIED);
    }
}

static void
cdk_quartz_window_deiconify (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (CDK_WINDOW_IS_MAPPED (window))
    {
      CDK_QUARTZ_ALLOC_POOL;

      if (impl->toplevel)
	[impl->toplevel deminiaturize:nil];

      CDK_QUARTZ_RELEASE_POOL;
    }
  else
    {
      cdk_synthesize_window_state (window,
				   CDK_WINDOW_STATE_ICONIFIED,
				   0);
    }
}

static gboolean
window_is_fullscreen (CdkWindow *window)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  if (cdk_quartz_osx_version() >= CDK_OSX_LION)
    return ([impl->toplevel styleMask] & CDK_QUARTZ_FULLSCREEN_WINDOW) != 0;
  else
#endif
    return g_object_get_data (G_OBJECT (window), FULLSCREEN_DATA) != NULL;
}

static void
cdk_quartz_window_fullscreen (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  if (cdk_quartz_osx_version() >= CDK_OSX_LION)
    {
      if (!window_is_fullscreen (window))
        [impl->toplevel toggleFullScreen:nil];
    }
  else
    {
#endif
      FullscreenSavedGeometry *geometry;
      CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
      NSRect frame;

      if (CDK_WINDOW_DESTROYED (window) ||
          !WINDOW_IS_TOPLEVEL (window))
        return;

      geometry = get_fullscreen_geometry (window);
      if (!geometry)
        {
          geometry = g_new (FullscreenSavedGeometry, 1);

          geometry->x = window->x;
          geometry->y = window->y;
          geometry->width = window->width;
          geometry->height = window->height;

          if (!cdk_window_get_decorations (window, &geometry->decor))
            geometry->decor = CDK_DECOR_ALL;

          g_object_set_data_full (G_OBJECT (window),
                                  FULLSCREEN_DATA, geometry, 
                                  g_free);

          cdk_window_set_decorations (window, 0);

          frame = [[impl->toplevel screen] frame];
          move_resize_window_internal (window,
                                       0, 0, 
                                       frame.size.width, frame.size.height);
          [impl->toplevel setContentSize:frame.size];
          [impl->toplevel makeKeyAndOrderFront:impl->toplevel];

          clear_toplevel_order ();
        }

      SetSystemUIMode (kUIModeAllHidden, kUIOptionAutoShowMenuBar);

      cdk_synthesize_window_state (window, 0, CDK_WINDOW_STATE_FULLSCREEN);
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
    }
#endif
}

static void
cdk_quartz_window_unfullscreen (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  if (cdk_quartz_osx_version() >= CDK_OSX_LION)
    {
      impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

      if (window_is_fullscreen (window))
        [impl->toplevel toggleFullScreen:nil];
    }
  else
    {
#endif
      CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
      FullscreenSavedGeometry *geometry;

      if (CDK_WINDOW_DESTROYED (window) ||
          !WINDOW_IS_TOPLEVEL (window))
        return;

      geometry = get_fullscreen_geometry (window);
      if (geometry)
        {
          SetSystemUIMode (kUIModeNormal, 0);

          move_resize_window_internal (window,
                                       geometry->x,
                                       geometry->y,
                                       geometry->width,
                                       geometry->height);

          cdk_window_set_decorations (window, geometry->decor);

          g_object_set_data (G_OBJECT (window), FULLSCREEN_DATA, NULL);

          [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
          clear_toplevel_order ();

          cdk_synthesize_window_state (window, CDK_WINDOW_STATE_FULLSCREEN, 0);
        }

#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
    }
#endif
}

#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
static FullscreenSavedGeometry *
get_fullscreen_geometry (CdkWindow *window)
{
  return g_object_get_data (G_OBJECT (window), FULLSCREEN_DATA);
}
#endif

void
_cdk_quartz_window_update_fullscreen_state (CdkWindow *window)
{
  if (CDK_WINDOW_DESTROYED (window) || !WINDOW_IS_TOPLEVEL (window))
    return;

#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  if (cdk_quartz_osx_version() >= CDK_OSX_LION)
    {
      gboolean is_fullscreen = window_is_fullscreen (window);
      gboolean was_fullscreen = (cdk_window_get_state (window) &
                                 CDK_WINDOW_STATE_FULLSCREEN) != 0;

      if (is_fullscreen != was_fullscreen)
        {
          if (is_fullscreen)
            cdk_synthesize_window_state (window, 0, CDK_WINDOW_STATE_FULLSCREEN);
          else
            cdk_synthesize_window_state (window, CDK_WINDOW_STATE_FULLSCREEN, 0);
        }
    }
#endif
}

static void
cdk_quartz_window_set_keep_above (CdkWindow *window,
                                  gboolean   setting)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  gint level;

  g_return_if_fail (CDK_IS_WINDOW (window));

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  level = window_type_hint_to_level (cdk_window_get_type_hint (window));
  
  /* Adjust normal window level by one if necessary. */
  [impl->toplevel setLevel: level + (setting ? 1 : 0)];
}

static void
cdk_quartz_window_set_keep_below (CdkWindow *window,
                                  gboolean   setting)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);
  gint level;

  g_return_if_fail (CDK_IS_WINDOW (window));

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;
  
  level = window_type_hint_to_level (cdk_window_get_type_hint (window));
  
  /* Adjust normal window level by one if necessary. */
  [impl->toplevel setLevel: level - (setting ? 1 : 0)];
}

/* X11 "feature" not useful in other backends. */
static CdkWindow *
cdk_quartz_window_get_group (CdkWindow *window)
{
    return NULL;
}

/* X11 "feature" not useful in other backends. */
static void
cdk_quartz_window_set_group (CdkWindow *window,
                             CdkWindow *leader)
{
}

static void
cdk_quartz_window_destroy_notify (CdkWindow *window)
{
  CdkDisplay *display = cdk_window_get_display (window);
  CdkSeat *seat = cdk_display_get_default_seat (display);
  cdk_seat_ungrab (seat);
}

static void
cdk_quartz_window_set_opacity (CdkWindow *window,
                               gdouble    opacity)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  g_return_if_fail (CDK_IS_WINDOW (window));
  g_return_if_fail (WINDOW_IS_TOPLEVEL (window));

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  if (opacity < 0)
    opacity = 0;
  else if (opacity > 1)
    opacity = 1;

  [impl->toplevel setAlphaValue: opacity];
}

static void
cdk_quartz_window_set_shadow_width (CdkWindow *window,
                                    gint       left,
                                    gint       right,
                                    gint       top,
                                    gint       bottom)
{
  CdkWindowImplQuartz *impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  g_return_if_fail (CDK_IS_WINDOW (window));
  g_return_if_fail (WINDOW_IS_TOPLEVEL (window));

  if (CDK_WINDOW_DESTROYED (window) ||
      !WINDOW_IS_TOPLEVEL (window))
    return;

  impl->shadow_top = top;
  impl->shadow_max = MAX (MAX (left, right), MAX (top, bottom));
  _cdk_quartz_window_update_has_shadow (impl);
}

static cairo_region_t *
cdk_quartz_window_get_shape (CdkWindow *window)
{
  /* FIXME: implement */
  return NULL;
}

static cairo_region_t *
cdk_quartz_window_get_input_shape (CdkWindow *window)
{
  /* FIXME: implement */
  return NULL;
}

/* Protocol to build cleanly for OSX < 10.7 */
@protocol ScaleFactor
- (CGFloat) backingScaleFactor;
@end

static gint
cdk_quartz_window_get_scale_factor (CdkWindow *window)
{
  CdkWindowImplQuartz *impl;

  if (CDK_WINDOW_DESTROYED (window))
    return 1;

  impl = CDK_WINDOW_IMPL_QUARTZ (window->impl);

  if (impl->toplevel != NULL && cdk_quartz_osx_version() >= CDK_OSX_LION)
    return [(id <ScaleFactor>) impl->toplevel backingScaleFactor];

  return 1;
}

static void
cdk_window_impl_quartz_class_init (CdkWindowImplQuartzClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  CdkWindowImplClass *impl_class = CDK_WINDOW_IMPL_CLASS (klass);
  CdkWindowImplQuartzClass *impl_quartz_class = CDK_WINDOW_IMPL_QUARTZ_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = cdk_window_impl_quartz_finalize;

  impl_class->ref_cairo_surface = cdk_quartz_ref_cairo_surface;
  impl_class->show = cdk_window_quartz_show;
  impl_class->hide = cdk_window_quartz_hide;
  impl_class->withdraw = cdk_window_quartz_withdraw;
  impl_class->set_events = cdk_window_quartz_set_events;
  impl_class->get_events = cdk_window_quartz_get_events;
  impl_class->raise = cdk_window_quartz_raise;
  impl_class->lower = cdk_window_quartz_lower;
  impl_class->restack_toplevel = cdk_window_quartz_restack_toplevel;
  impl_class->move_resize = cdk_window_quartz_move_resize;
  impl_class->set_background = cdk_window_quartz_set_background;
  impl_class->reparent = cdk_window_quartz_reparent;
  impl_class->set_device_cursor = cdk_window_quartz_set_device_cursor;
  impl_class->get_geometry = cdk_window_quartz_get_geometry;
  impl_class->get_root_coords = cdk_window_quartz_get_root_coords;
  impl_class->get_device_state = cdk_window_quartz_get_device_state;
  impl_class->shape_combine_region = cdk_window_quartz_shape_combine_region;
  impl_class->input_shape_combine_region = cdk_window_quartz_input_shape_combine_region;
  impl_class->destroy = cdk_quartz_window_destroy;
  impl_class->destroy_foreign = cdk_quartz_window_destroy_foreign;
  impl_class->get_shape = cdk_quartz_window_get_shape;
  impl_class->get_input_shape = cdk_quartz_window_get_input_shape;
  impl_class->begin_paint = cdk_window_impl_quartz_begin_paint;
  impl_class->get_scale_factor = cdk_quartz_window_get_scale_factor;

  impl_class->focus = cdk_quartz_window_focus;
  impl_class->set_type_hint = cdk_quartz_window_set_type_hint;
  impl_class->get_type_hint = cdk_quartz_window_get_type_hint;
  impl_class->set_modal_hint = cdk_quartz_window_set_modal_hint;
  impl_class->set_skip_taskbar_hint = cdk_quartz_window_set_skip_taskbar_hint;
  impl_class->set_skip_pager_hint = cdk_quartz_window_set_skip_pager_hint;
  impl_class->set_urgency_hint = cdk_quartz_window_set_urgency_hint;
  impl_class->set_geometry_hints = cdk_quartz_window_set_geometry_hints;
  impl_class->set_title = cdk_quartz_window_set_title;
  impl_class->set_role = cdk_quartz_window_set_role;
  impl_class->set_startup_id = cdk_quartz_window_set_startup_id;
  impl_class->set_transient_for = cdk_quartz_window_set_transient_for;
  impl_class->get_frame_extents = cdk_quartz_window_get_frame_extents;
  impl_class->set_override_redirect = cdk_quartz_window_set_override_redirect;
  impl_class->set_accept_focus = cdk_quartz_window_set_accept_focus;
  impl_class->set_focus_on_map = cdk_quartz_window_set_focus_on_map;
  impl_class->set_icon_list = cdk_quartz_window_set_icon_list;
  impl_class->set_icon_name = cdk_quartz_window_set_icon_name;
  impl_class->iconify = cdk_quartz_window_iconify;
  impl_class->deiconify = cdk_quartz_window_deiconify;
  impl_class->stick = cdk_quartz_window_stick;
  impl_class->unstick = cdk_quartz_window_unstick;
  impl_class->maximize = cdk_quartz_window_maximize;
  impl_class->unmaximize = cdk_quartz_window_unmaximize;
  impl_class->fullscreen = cdk_quartz_window_fullscreen;
  impl_class->unfullscreen = cdk_quartz_window_unfullscreen;
  impl_class->set_keep_above = cdk_quartz_window_set_keep_above;
  impl_class->set_keep_below = cdk_quartz_window_set_keep_below;
  impl_class->get_group = cdk_quartz_window_get_group;
  impl_class->set_group = cdk_quartz_window_set_group;
  impl_class->set_decorations = cdk_quartz_window_set_decorations;
  impl_class->get_decorations = cdk_quartz_window_get_decorations;
  impl_class->set_functions = cdk_quartz_window_set_functions;
  impl_class->begin_resize_drag = cdk_quartz_window_begin_resize_drag;
  impl_class->begin_move_drag = cdk_quartz_window_begin_move_drag;
  impl_class->set_opacity = cdk_quartz_window_set_opacity;
  impl_class->set_shadow_width = cdk_quartz_window_set_shadow_width;
  impl_class->destroy_notify = cdk_quartz_window_destroy_notify;
  impl_class->register_dnd = _cdk_quartz_window_register_dnd;
  impl_class->drag_begin = _cdk_quartz_window_drag_begin;
  impl_class->process_updates_recurse = _cdk_quartz_window_process_updates_recurse;
  impl_class->sync_rendering = _cdk_quartz_window_sync_rendering;
  impl_class->simulate_key = _cdk_quartz_window_simulate_key;
  impl_class->simulate_button = _cdk_quartz_window_simulate_button;
  impl_class->get_property = _cdk_quartz_window_get_property;
  impl_class->change_property = _cdk_quartz_window_change_property;
  impl_class->delete_property = _cdk_quartz_window_delete_property;

  impl_class->create_gl_context = cdk_quartz_window_create_gl_context;
  impl_class->invalidate_for_new_frame = cdk_quartz_window_invalidate_for_new_frame;

  impl_quartz_class->get_context = cdk_window_impl_quartz_get_context;
  impl_quartz_class->release_context = cdk_window_impl_quartz_release_context;
}

GType
_cdk_window_impl_quartz_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      const GTypeInfo object_info =
	{
	  sizeof (CdkWindowImplQuartzClass),
	  (GBaseInitFunc) NULL,
	  (GBaseFinalizeFunc) NULL,
	  (GClassInitFunc) cdk_window_impl_quartz_class_init,
	  NULL,           /* class_finalize */
	  NULL,           /* class_data */
	  sizeof (CdkWindowImplQuartz),
	  0,              /* n_preallocs */
	  (GInstanceInitFunc) cdk_window_impl_quartz_init,
	};

      object_type = g_type_register_static (CDK_TYPE_WINDOW_IMPL,
                                            "CdkWindowImplQuartz",
                                            &object_info, 0);
    }

  return object_type;
}

CGContextRef
cdk_quartz_window_get_context (CdkWindowImplQuartz  *window,
                               gboolean             antialias)
{
  if (!CDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->get_context)
    {
      g_warning ("%s doesn't implement CdkWindowImplQuartzClass::get_context()",
                 G_OBJECT_TYPE_NAME (window));
      return NULL;
    }

  return CDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->get_context (window, antialias);
}

void
cdk_quartz_window_release_context (CdkWindowImplQuartz  *window,
                                   CGContextRef          cg_context)
{
  if (!CDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->release_context)
    {
      g_warning ("%s doesn't implement CdkWindowImplQuartzClass::release_context()",
                 G_OBJECT_TYPE_NAME (window));
      return;
    }

  CDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->release_context (window, cg_context);
}



static CGContextRef
cdk_root_window_impl_quartz_get_context (CdkWindowImplQuartz *window,
                                         gboolean             antialias)
{
  CGColorSpaceRef colorspace;
  CGContextRef cg_context;
  CdkWindowImplQuartz *window_impl = CDK_WINDOW_IMPL_QUARTZ (window);

  if (CDK_WINDOW_DESTROYED (window_impl->wrapper))
    return NULL;

  /* We do not have the notion of a root window on OS X.  We fake this
   * by creating a 1x1 bitmap and return a context to that.
   */
  colorspace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB);
  cg_context = CGBitmapContextCreate (NULL,
                                      1, 1, 8, 4, colorspace,
                                      (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
  CGColorSpaceRelease (colorspace);

  return cg_context;
}

static void
cdk_root_window_impl_quartz_release_context (CdkWindowImplQuartz *window,
                                             CGContextRef         cg_context)
{
  CGContextRelease (cg_context);
}

static void
cdk_root_window_impl_quartz_class_init (CdkRootWindowImplQuartzClass *klass)
{
  CdkWindowImplQuartzClass *window_quartz_class = CDK_WINDOW_IMPL_QUARTZ_CLASS (klass);

  root_window_parent_class = g_type_class_peek_parent (klass);

  window_quartz_class->get_context = cdk_root_window_impl_quartz_get_context;
  window_quartz_class->release_context = cdk_root_window_impl_quartz_release_context;
}

static void
cdk_root_window_impl_quartz_init (CdkRootWindowImplQuartz *impl)
{
}

GType
_cdk_root_window_impl_quartz_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      const GTypeInfo object_info =
        {
          sizeof (CdkRootWindowImplQuartzClass),
          (GBaseInitFunc) NULL,
          (GBaseFinalizeFunc) NULL,
          (GClassInitFunc) cdk_root_window_impl_quartz_class_init,
          NULL,           /* class_finalize */
          NULL,           /* class_data */
          sizeof (CdkRootWindowImplQuartz),
          0,              /* n_preallocs */
          (GInstanceInitFunc) cdk_root_window_impl_quartz_init,
        };

      object_type = g_type_register_static (CDK_TYPE_WINDOW_IMPL_QUARTZ,
                                            "CdkRootWindowQuartz",
                                            &object_info, 0);
    }

  return object_type;
}