summaryrefslogtreecommitdiff
path: root/champlain/champlain-view.c
blob: c5b08e8881342c63fe33e6c6c5f37052c6b03ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
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
/*
 * Copyright (C) 2008-2009 Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
 *
 * 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.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * SECTION:champlain-view
 * @short_description: A #ClutterActor to display maps
 *
 * The #ChamplainView is a ClutterActor to display maps.  It supports two modes
 * of scrolling:
 * <itemizedlist>
 *   <listitem><para>Push: the normal behavior where the maps doesn't move
 *   after the user stopped scrolling;</para></listitem>
 *   <listitem><para>Kinetic: the iPhone-like behavior where the maps
 *   decelerate after the user stopped scrolling.</para></listitem>
 * </itemizedlist>
 *
 * You can use the same #ChamplainView to display many types of maps.  In
 * Champlain they are called map sources.  You can change the #map-source
 * property at anytime to replace the current displayed map.
 *
 * The maps are downloaded from Internet from open maps sources (like
 * <ulink role="online-location"
 * url="http://www.openstreetmap.org">OpenStreetMap</ulink>).  Maps are divided
 * in tiles for each zoom level.  When a tile is requested, #ChamplainView will
 * first check if it is in cache (in the user's cache dir under champlain). If
 * an error occurs during download, an error tile will be displayed.
 *
 * The button-press-event and button-release-event signals are emitted each
 * time a mouse button is pressed on the @view.  Coordinates can be converted
 * with #champlain_view_get_coords_from_event.
 */

#include "config.h"

#include "champlain-view.h"

#define DEBUG_FLAG CHAMPLAIN_DEBUG_VIEW
#include "champlain-debug.h"

#include "champlain.h"
#include "champlain-defines.h"
#include "champlain-enum-types.h"
#include "champlain-map.h"
#include "champlain-marshal.h"
#include "champlain-map-source.h"
#include "champlain-map-source-factory.h"
#include "champlain-polygon.h"
#include "champlain-private.h"
#include "champlain-tile.h"
#include "champlain-zoom-level.h"

#include <clutter/clutter.h>
#include <glib.h>
#include <glib-object.h>
#include <math.h>
#include <tidy-adjustment.h>
#include <tidy-finger-scroll.h>
#include <tidy-scrollable.h>
#include <tidy-viewport.h>

enum
{
  /* normal signals */
  ANIMATION_COMPLETED,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_LONGITUDE,
  PROP_LATITUDE,
  PROP_ZOOM_LEVEL,
  PROP_MIN_ZOOM_LEVEL,
  PROP_MAX_ZOOM_LEVEL,
  PROP_MAP_SOURCE,
  PROP_DECEL_RATE,
  PROP_SCROLL_MODE,
  PROP_KEEP_CENTER_ON_RESIZE,
  PROP_SHOW_LICENSE,
  PROP_LICENSE_EXTRA,
  PROP_ZOOM_ON_DOUBLE_CLICK,
  PROP_STATE,
  PROP_SHOW_SCALE,
  PROP_SCALE_UNIT,
  PROP_MAX_SCALE_WIDTH,
};

#define PADDING 10
static guint signals[LAST_SIGNAL] = { 0, };

#define GET_PRIVATE(obj)     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHAMPLAIN_TYPE_VIEW, ChamplainViewPrivate))
#define ZOOM_LEVEL_OUT_OF_RANGE(priv, level)    (level < priv->min_zoom_level || level > priv->max_zoom_level)

/* Between state values for go_to */
typedef struct {
  ChamplainView *view;
  ClutterAlpha *alpha;
  ClutterTimeline *timeline;
  gdouble to_latitude;
  gdouble to_longitude;
  gdouble from_latitude;
  gdouble from_longitude;
} GoToContext;

typedef struct {
  ChamplainView *view;
  ChamplainPolygon *polygon;
} PolygonRedrawContext;

struct _ChamplainViewPrivate
{
  ClutterActor *stage;

  ChamplainMapSourceFactory *factory; /* The map source factory */
  ChamplainMapSource *map_source; /* Current map tile source */
  ChamplainScrollMode scroll_mode;
  gint zoom_level; /* Holds the current zoom level number */
  gint min_zoom_level; /* Lowest allowed zoom level */
  gint max_zoom_level; /* Highest allowed zoom level */

  /* Represents the (lat, lon) at the center of the viewport */
  gdouble longitude;
  gdouble latitude;

  /* Hack to get smaller x,y coordinates as the clutter limit is G_MAXINT16 */
  ChamplainFloatPoint anchor;

  gdouble anchor_zoom_level; /* the zoom_level for which the current anchor has
                                been computed for */

  Map *map; /* Contains the current map model */

  ClutterActor *finger_scroll; /* Contains the viewport */
  ClutterActor *viewport;  /* Contains the map_layer, license and markers */
  ClutterActor *map_layer; /* Contains tiles actors (grouped by zoom level) */
  ChamplainRectangle viewport_size;

  ClutterActor *user_layers; /* Contains the markers */

  gboolean keep_center_on_resize;

  gboolean zoom_on_double_click;

  gboolean show_license;
  ClutterActor *license_actor; /* Contains the license info */
  gchar *license_text; /* Extra license text */

  ClutterActor *scale_actor;
  gboolean show_scale;
  ChamplainUnit scale_unit;
  guint max_scale_width;

  ChamplainState state; /* View's global state */

  /* champlain_view_go_to's context, kept for stop_go_to */
  GoToContext *goto_context;

  /* notify_polygon_cb's context, kept for idle cb */
  guint polygon_redraw_id;

  /* Lines and shapes */
  GList *polygons;
  ClutterActor *polygon_layer;  /* Contains the polygons */

};

G_DEFINE_TYPE (ChamplainView, champlain_view, CLUTTER_TYPE_GROUP);

static gdouble viewport_get_current_longitude (ChamplainViewPrivate *priv);
static gdouble viewport_get_current_latitude (ChamplainViewPrivate *priv);
static gdouble viewport_get_longitude_at (ChamplainViewPrivate *priv, gint x);
static gdouble viewport_get_latitude_at (ChamplainViewPrivate *priv, gint y);
static gboolean scroll_event (ClutterActor *actor, ClutterScrollEvent *event,
    ChamplainView *view);
static void marker_reposition_cb (ChamplainMarker *marker, ChamplainView *view);
static void layer_reposition_cb (ClutterActor *layer, ChamplainView *view);
static gboolean marker_reposition (gpointer data);
static void create_initial_map (ChamplainView *view);
static void resize_viewport (ChamplainView *view);
static void champlain_view_get_property (GObject *object, guint prop_id,
    GValue *value, GParamSpec *pspec);
static void champlain_view_set_property (GObject *object, guint prop_id,
    const GValue *value, GParamSpec *pspec);
static void champlain_view_dispose (GObject *object);
static void champlain_view_class_init (ChamplainViewClass *champlainViewClass);
static void champlain_view_init (ChamplainView *view);
static void viewport_pos_changed_cb (GObject *gobject, GParamSpec *arg1,
    ChamplainView *view);
static void notify_marker_reposition_cb (ChamplainMarker *marker,
    GParamSpec *arg1, ChamplainView *view);
static void layer_add_marker_cb (ClutterGroup *layer, ChamplainMarker *marker,
    ChamplainView *view);
static void connect_marker_notify_cb (ChamplainMarker *marker,
    ChamplainView *view);
static gboolean finger_scroll_button_press_cb (ClutterActor *actor,
    ClutterButtonEvent *event, ChamplainView *view);
static void update_license (ChamplainView *view);
static void license_set_position (ChamplainView *view);
static void view_load_visible_tiles (ChamplainView *view);
static void view_position_tile (ChamplainView* view, ChamplainTile* tile);
static void view_tiles_reposition (ChamplainView* view);
static void view_update_state (ChamplainView *view);
static void view_update_anchor (ChamplainView *view, gint x, gint y);
static gboolean view_set_zoom_level_at (ChamplainView *view,
    gint zoom_level,
    gint x,
    gint y);
static void tile_state_notify (GObject *gobject,
    GParamSpec *pspec,
    gpointer data);
static void view_update_polygons (ChamplainView *view);
static gboolean finger_scroll_key_press_cb (ClutterActor *actor,
    ClutterKeyEvent *event,
    ChamplainView *view);
static void champlain_view_go_to_with_duration (ChamplainView *view,
    gdouble latitude,
    gdouble longitude,
    guint duration);

#define SCALE_HEIGHT  20
#define SCALE_PADDING 10
#define SCALE_INSIDE_PADDING 10
#define SCALE_LINE_WIDTH 2

static gdouble
viewport_get_longitude_at (ChamplainViewPrivate *priv, gint x)
{
  if (!priv->map_source)
    return 0.0;

  return champlain_map_source_get_longitude (priv->map_source,
      priv->zoom_level, x);
}

static gdouble
viewport_get_current_longitude (ChamplainViewPrivate *priv)
{
  if (!priv->map)
    return 0.0;

  return viewport_get_longitude_at (priv, priv->anchor.x +
      priv->viewport_size.x + priv->viewport_size.width / 2.0);
}

static gdouble
viewport_get_latitude_at (ChamplainViewPrivate *priv, gint y)
{
  if (!priv->map_source)
    return 0.0;

  return champlain_map_source_get_latitude (priv->map_source,
      priv->zoom_level, y);
}

static gdouble
viewport_get_current_latitude (ChamplainViewPrivate *priv)
{
  if (!priv->map)
    return 0.0;

  return viewport_get_latitude_at (priv,
      priv->anchor.y + priv->viewport_size.y +
      priv->viewport_size.height / 2.0);
}

static gboolean
scroll_event (ClutterActor *actor,
    ClutterScrollEvent *event,
    ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;

  gint zoom_level = priv->zoom_level;

  if (event->direction == CLUTTER_SCROLL_UP)
    zoom_level = priv->zoom_level + 1;
  else if (event->direction == CLUTTER_SCROLL_DOWN)
    zoom_level = priv->zoom_level - 1;

  return view_set_zoom_level_at (view, zoom_level, event->x, event->y);
}

static void
marker_reposition_cb (ChamplainMarker *marker,
    ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;
  ChamplainBaseMarkerPrivate *marker_priv = CHAMPLAIN_BASE_MARKER(marker)->priv;

  gint x, y;

  if (priv->map)
    {
      x = champlain_map_source_get_x (priv->map_source, priv->zoom_level, marker_priv->lon);
      y = champlain_map_source_get_y (priv->map_source, priv->zoom_level, marker_priv->lat);

      clutter_actor_set_position (CLUTTER_ACTOR (marker),
        x - priv->anchor.x,
        y - priv->anchor.y);
    }
}

static void
notify_marker_reposition_cb (ChamplainMarker *marker,
    GParamSpec *arg1,
    ChamplainView *view)
{
  marker_reposition_cb (marker, view);
}

static void
layer_add_marker_cb (ClutterGroup *layer,
    ChamplainMarker *marker,
    ChamplainView *view)
{
  g_signal_connect (marker, "notify::longitude",
      G_CALLBACK (notify_marker_reposition_cb), view);

  g_idle_add (marker_reposition, view);
}

static void
connect_marker_notify_cb (ChamplainMarker *marker,
    ChamplainView *view)
{
  g_signal_connect (marker, "notify::longitude",
      G_CALLBACK (notify_marker_reposition_cb), view);
}

static void
layer_reposition_cb (ClutterActor *layer,
    ChamplainView *view)
{
  clutter_container_foreach (CLUTTER_CONTAINER (layer),
      CLUTTER_CALLBACK (marker_reposition_cb), view);
}

static gboolean
marker_reposition (gpointer data)
{
  ChamplainView *view = CHAMPLAIN_VIEW (data);
  ChamplainViewPrivate *priv = view->priv;
  clutter_container_foreach (CLUTTER_CONTAINER (priv->user_layers),
      CLUTTER_CALLBACK (layer_reposition_cb), view);
  return FALSE;
}

static void
create_initial_map (ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;
  ClutterActor *group;

  priv->map = map_new ();
  map_load_level (priv->map, priv->map_source, priv->zoom_level);
  group = champlain_zoom_level_get_actor (priv->map->current_level);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->map_layer), group);

  g_idle_add (marker_reposition, view);
  view_tiles_reposition (view);
  update_license (view);

  g_object_notify (G_OBJECT (view), "zoom-level");
  g_object_notify (G_OBJECT (view), "map-source");
}

static void
license_set_position (ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;

  if (!priv->license_actor)
    return;

  clutter_actor_set_position (priv->license_actor,
      priv->viewport_size.width - PADDING,
      priv->viewport_size.height - PADDING);
}

static void
draw_polygon (ChamplainView *view, ChamplainPolygon *polygon)
{
  cairo_t *cr;
  ChamplainViewPrivate *priv = view->priv;

  if (polygon->priv->visible == FALSE)
    return;

  cr = clutter_cairo_texture_create (CLUTTER_CAIRO_TEXTURE (polygon->priv->actor));

  /* Clear the drawing area */
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_rectangle (cr, 0, 0,
      view->priv->viewport_size.width,
      view->priv->viewport_size.height);
  cairo_fill (cr);

  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  GList *list = g_list_first (polygon->priv->points);
  while (list != NULL)
    {
      ChamplainPoint *point = (ChamplainPoint*) list->data;
      gfloat x, y;

      x = champlain_map_source_get_x (priv->map_source, priv->zoom_level,
          point->lon);
      y = champlain_map_source_get_y (priv->map_source, priv->zoom_level,
          point->lat);

      x -= priv->viewport_size.x + priv->anchor.x;
      y -= priv->viewport_size.y + priv->anchor.y;

      cairo_line_to (cr, x, y);

      list = list->next;
    }

  if (polygon->priv->closed_path)
    cairo_close_path (cr);

  cairo_set_source_rgba (cr,
      polygon->priv->fill_color->red / 255.0,
      polygon->priv->fill_color->green / 255.0,
      polygon->priv->fill_color->blue / 255.0,
      polygon->priv->fill_color->alpha / 255.0);

  if (polygon->priv->fill)
    cairo_fill_preserve (cr);

  cairo_set_source_rgba (cr,
      polygon->priv->stroke_color->red / 255.0,
      polygon->priv->stroke_color->green / 255.0,
      polygon->priv->stroke_color->blue / 255.0,
      polygon->priv->stroke_color->alpha / 255.0);

  cairo_set_line_width (cr, polygon->priv->stroke_width);

  if (polygon->priv->stroke)
    cairo_stroke (cr);

  if (polygon->priv->mark_points)
    {
      /* Draw points */
      GList *list = g_list_first (polygon->priv->points);
      while (list != NULL)
        {
          ChamplainPoint *point = (ChamplainPoint*) list->data;
          gfloat x, y;

          x = champlain_map_source_get_x (priv->map_source, priv->zoom_level,
              point->lon);
          y = champlain_map_source_get_y (priv->map_source, priv->zoom_level,
              point->lat);

          x -= priv->viewport_size.x + priv->anchor.x;
          y -= priv->viewport_size.y + priv->anchor.y;

          cairo_arc (cr, x, y, polygon->priv->stroke_width * 1.5, 0, 2 * M_PI);
          cairo_fill (cr);

          list = list->next;
        }
    }

  cairo_destroy (cr);
}

static gboolean
redraw_polygon_on_idle (PolygonRedrawContext *ctx)
{

  if (ctx->polygon)
    draw_polygon (ctx->view, ctx->polygon);

  ctx->view->priv->polygon_redraw_id = 0;
  // ctx is freed by g_idle_add_full
  return FALSE;
}

static void
notify_polygon_cb (ChamplainPolygon *polygon,
    GParamSpec *arg1,
    ChamplainView *view)
{
  PolygonRedrawContext *ctx;

  if (view->priv->polygon_redraw_id != 0)
    return;

  ctx = g_new0 (PolygonRedrawContext, 1);
  ctx->view = view;
  ctx->polygon = polygon;
  g_object_add_weak_pointer (G_OBJECT (polygon), (gpointer *) &ctx->polygon);

  view->priv->polygon_redraw_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
      (GSourceFunc) redraw_polygon_on_idle, ctx, g_free);
}

static void
resize_viewport (ChamplainView *view)
{
  gdouble lower, upper;
  TidyAdjustment *hadjust, *vadjust;
  GList *polygons;

  ChamplainViewPrivate *priv = view->priv;

  if (!priv->map)
    return;

  clutter_actor_set_size (priv->finger_scroll, priv->viewport_size.width,
      priv->viewport_size.height);

  tidy_scrollable_get_adjustments (TIDY_SCROLLABLE (priv->viewport), &hadjust,
      &vadjust);

  if (priv->zoom_level < 8)
    {
      lower = -priv->viewport_size.width / 2.0;
      upper = champlain_zoom_level_get_width (priv->map->current_level) *
          champlain_map_source_get_tile_size (priv->map_source) -
          priv->viewport_size.width / 2.0;
    }
  else
    {
      lower = 0;
      upper = G_MAXINT16;
    }
  g_object_set (hadjust, "lower", lower, "upper", upper,
      "page-size", 1.0, "step-increment", 1.0, "elastic", TRUE, NULL);

  if (priv->zoom_level < 8)
    {
      lower = -priv->viewport_size.height / 2.0;
      upper = champlain_zoom_level_get_height (priv->map->current_level) *
          champlain_map_source_get_tile_size (priv->map_source) -
          priv->viewport_size.height / 2.0;
    }
  else
    {
      lower = 0;
      upper = G_MAXINT16;
    }
  g_object_set (vadjust, "lower", lower, "upper", upper,
      "page-size", 1.0, "step-increment", 1.0, "elastic", TRUE, NULL);

  /* Resize polygon actors */
  if (priv->viewport_size.width == 0 ||
      priv->viewport_size.height == 0)
    return;

  polygons = priv->polygons;
  while (polygons != NULL)
    {
      ChamplainPolygon *polygon;

      polygon = CHAMPLAIN_POLYGON (polygons->data);

      if (polygon->priv->actor != NULL)
        {
          g_object_unref (polygon->priv->actor);
          clutter_container_remove_actor (CLUTTER_CONTAINER (view->priv->polygon_layer),
              polygon->priv->actor);
        }

      polygon->priv->actor = g_object_ref (clutter_cairo_texture_new (
          view->priv->viewport_size.width,
          view->priv->viewport_size.height));
      g_object_set (G_OBJECT (polygon->priv->actor), "visible",
          polygon->priv->visible, NULL);
      clutter_container_add_actor (CLUTTER_CONTAINER (view->priv->polygon_layer),
          polygon->priv->actor);
      clutter_actor_set_position (polygon->priv->actor, 0, 0);
      draw_polygon (view, polygon);
      polygons = polygons->next;
    }
}

static void
champlain_view_get_property (GObject *object,
    guint prop_id,
    GValue *value,
    GParamSpec *pspec)
{
  ChamplainView *view = CHAMPLAIN_VIEW (object);
  ChamplainViewPrivate *priv = view->priv;

  switch (prop_id)
    {
      case PROP_LONGITUDE:
        g_value_set_double (value,
            CLAMP (priv->longitude, CHAMPLAIN_MIN_LONG, CHAMPLAIN_MAX_LONG));
        break;
      case PROP_LATITUDE:
        g_value_set_double (value, 
            CLAMP (priv->latitude, CHAMPLAIN_MIN_LAT, CHAMPLAIN_MAX_LAT));
        break;
      case PROP_ZOOM_LEVEL:
        g_value_set_int (value, priv->zoom_level);
        break;
      case PROP_MIN_ZOOM_LEVEL:
        g_value_set_int (value, priv->min_zoom_level);
        break;
      case PROP_MAX_ZOOM_LEVEL:
        g_value_set_int (value, priv->max_zoom_level);
        break;
      case PROP_MAP_SOURCE:
        g_value_set_object (value, priv->map_source);
        break;
      case PROP_SCROLL_MODE:
        g_value_set_enum (value, priv->scroll_mode);
        break;
      case PROP_SHOW_SCALE:
        g_value_set_boolean (value, priv->show_scale);
        break;
      case PROP_MAX_SCALE_WIDTH:
        g_value_set_uint (value, priv->max_scale_width);
        break;
      case PROP_SCALE_UNIT:
        g_value_set_enum (value, priv->scale_unit);
        break;
      case PROP_DECEL_RATE:
        {
          gdouble decel = 0.0;
          g_object_get (priv->finger_scroll, "decel-rate", &decel, NULL);
          g_value_set_double (value, decel);
          break;
        }
      case PROP_KEEP_CENTER_ON_RESIZE:
        g_value_set_boolean (value, priv->keep_center_on_resize);
        break;
      case PROP_SHOW_LICENSE:
        g_value_set_boolean (value, priv->show_license);
        break;
      case PROP_LICENSE_EXTRA:
        g_value_set_string (value, priv->license_text);
        break;
      case PROP_ZOOM_ON_DOUBLE_CLICK:
        g_value_set_boolean (value, priv->zoom_on_double_click);
        break;
      case PROP_STATE:
        g_value_set_enum (value, priv->state);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
champlain_view_set_property (GObject *object,
    guint prop_id,
    const GValue *value,
    GParamSpec *pspec)
{
  ChamplainView *view = CHAMPLAIN_VIEW (object);
  ChamplainViewPrivate *priv = view->priv;

  switch (prop_id)
  {
    case PROP_LONGITUDE:
      champlain_view_center_on (view, priv->latitude,
          g_value_get_double (value));
      break;
    case PROP_LATITUDE:
      champlain_view_center_on (view, g_value_get_double (value),
          priv->longitude);
      break;
    case PROP_ZOOM_LEVEL:
      champlain_view_set_zoom_level (view, g_value_get_int (value));
      break;
    case PROP_MIN_ZOOM_LEVEL:
      champlain_view_set_min_zoom_level (view, g_value_get_int (value));
      break;
    case PROP_MAX_ZOOM_LEVEL:
      champlain_view_set_max_zoom_level (view, g_value_get_int (value));
      break;
    case PROP_MAP_SOURCE:
      champlain_view_set_map_source (view, g_value_get_object (value));
      break;
    case PROP_SCROLL_MODE:
      champlain_view_set_scroll_mode (view, g_value_get_enum (value));
      break;
    case PROP_SHOW_SCALE:
      champlain_view_set_show_scale (view, g_value_get_boolean (value));
      break;
    case PROP_MAX_SCALE_WIDTH:
      champlain_view_set_max_scale_width (view, g_value_get_uint (value));
      break;
    case PROP_SCALE_UNIT:
      champlain_view_set_scale_unit (view, g_value_get_enum (value));
      break;
    case PROP_DECEL_RATE:
      champlain_view_set_decel_rate (view, g_value_get_double (value));
      break;
    case PROP_KEEP_CENTER_ON_RESIZE:
      champlain_view_set_keep_center_on_resize (view, g_value_get_boolean (value));
      break;
    case PROP_SHOW_LICENSE:
      champlain_view_set_show_license (view, g_value_get_boolean (value));
      break;
    case PROP_LICENSE_EXTRA:
      champlain_view_set_license_text (view, g_value_get_string (value));
      break;
    case PROP_ZOOM_ON_DOUBLE_CLICK:
      champlain_view_set_zoom_on_double_click (view, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
champlain_view_dispose (GObject *object)
{
  ChamplainView *view = CHAMPLAIN_VIEW (object);
  ChamplainViewPrivate *priv = view->priv;
  GList *polygons;

  if (priv->factory != NULL)
    {
      g_object_unref (priv->factory);
      priv->factory = NULL;
    }

  if (priv->map_source != NULL)
    {
      g_object_unref (priv->map_source);
      priv->map_source = NULL;
    }

  if (priv->license_actor != NULL)
    {
      g_object_unref (priv->license_actor);
      priv->license_actor = NULL;
    }

  if (priv->finger_scroll != NULL)
    {
      tidy_finger_scroll_stop (TIDY_FINGER_SCROLL (priv->finger_scroll));
      g_object_unref (priv->finger_scroll);
      priv->finger_scroll = NULL;
    }

  if (priv->viewport != NULL)
    {
      tidy_viewport_stop (TIDY_VIEWPORT (priv->viewport));
      g_object_unref (priv->viewport);
      priv->viewport = NULL;
    }

  if (priv->map_layer != NULL)
    {
      g_object_unref (priv->map_layer);
      priv->map_layer = NULL;
    }

  if (priv->user_layers != NULL)
    {
      g_object_unref (priv->user_layers);
      priv->user_layers = NULL;
    }

  if (priv->stage != NULL)
    {
      g_object_unref (priv->stage);
      priv->stage = NULL;
    }

  if (priv->map != NULL)
    {
      map_free (priv->map);
      priv->map = NULL;
    }

  polygons = priv->polygons;
  while (polygons != NULL)
    {
      g_object_unref (G_OBJECT (polygons->data));
      polygons = polygons->next;
    }
  g_list_free (priv->polygons);
  priv->polygons = NULL;

  if (priv->goto_context != NULL)
    champlain_view_stop_go_to (view);

  G_OBJECT_CLASS (champlain_view_parent_class)->dispose (object);
}

static void
champlain_view_class_init (ChamplainViewClass *champlainViewClass)
{
  g_type_class_add_private (champlainViewClass, sizeof (ChamplainViewPrivate));

  GObjectClass *object_class = G_OBJECT_CLASS (champlainViewClass);
  object_class->dispose = champlain_view_dispose;
  object_class->get_property = champlain_view_get_property;
  object_class->set_property = champlain_view_set_property;

  /**
  * ChamplainView:longitude:
  *
  * The longitude coordonate of the map
  *
  * Since: 0.1
  */
  g_object_class_install_property (object_class,
      PROP_LONGITUDE,
      g_param_spec_double ("longitude",
         "Longitude",
         "The longitude coordonate of the map",
         -180.0f, 180.0f, 0.0f, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:latitude:
  *
  * The latitude coordonate of the map
  *
  * Since: 0.1
  */
  g_object_class_install_property (object_class,
      PROP_LATITUDE,
      g_param_spec_double ("latitude",
           "Latitude",
           "The latitude coordonate of the map",
           -90.0f, 90.0f, 0.0f, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:zoom-level:
  *
  * The level of zoom of the content.
  *
  * Since: 0.1
  */
  g_object_class_install_property (object_class,
      PROP_ZOOM_LEVEL,
      g_param_spec_int ("zoom-level",
           "Zoom level",
           "The level of zoom of the map",
           0, 20, 3, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:min-zoom-level:
  *
  * The lowest allowed level of zoom of the content.
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class,
      PROP_MIN_ZOOM_LEVEL,
      g_param_spec_int ("min-zoom-level",
           "Min zoom level",
           "The lowest allowed level of zoom",
           0, 20, 0, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:max-zoom-level:
  *
  * The highest allowed level of zoom of the content.
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class,
      PROP_MAX_ZOOM_LEVEL,
      g_param_spec_int ("max-zoom-level",
           "Max zoom level",
           "The highest allowed level of zoom",
           0, 20, 20, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:map-source:
  *
  * The #ChamplainMapSource being displayed
  *
  * Since: 0.2
  */
  g_object_class_install_property (object_class,
      PROP_MAP_SOURCE,
      g_param_spec_object ("map-source",
           "Map source",
           "The map source being displayed",
           CHAMPLAIN_TYPE_MAP_SOURCE,
           CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:scroll-mode:
  *
  * Determines the way the view reacts to scroll events.
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class,
      PROP_SCROLL_MODE,
      g_param_spec_enum ("scroll-mode",
           "Scroll Mode",
           "Determines the way the view reacts to scroll events.",
           CHAMPLAIN_TYPE_SCROLL_MODE,
           CHAMPLAIN_SCROLL_MODE_KINETIC,
           CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:decel-rate:
  *
  * The deceleration rate for the kinetic mode. The default value is 1.1.
  *
  * Since: 0.2
  */
  g_object_class_install_property (object_class,
       PROP_DECEL_RATE,
       g_param_spec_double ("decel-rate",
            "Deceleration rate",
            "Rate at which the view will decelerate in kinetic mode.",
            1.0001, 2.0, 1.1, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:keep-center-on-resize:
  *
  * Keep the current centered position when resizing the view.
  *
  * Since: 0.2.7
  */
  g_object_class_install_property (object_class,
       PROP_KEEP_CENTER_ON_RESIZE,
       g_param_spec_boolean ("keep-center-on-resize",
           "Keep center on resize",
           "Keep the current centered position "
           "upon resizing",
           TRUE, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:show-license:
  *
  * Show the license on the map view.  The license information should always be
  * available in a way or another in your application.  You can have it in
  * About, or on the map.
  *
  * Since: 0.2.8
  */
  g_object_class_install_property (object_class,
       PROP_SHOW_LICENSE,
       g_param_spec_boolean ("show-license",
           "Show the map data license",
           "Show the map data license on the map view",
           TRUE, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:license-text:
  *
  * Sets additional text to be displayed in the license area.  The map's
  * license will be added below it. Your text can have multiple line, just use
  * "\n" in between.
  *
  * Since: 0.4.3
  */
  g_object_class_install_property (object_class,
       PROP_LICENSE_EXTRA,
       g_param_spec_string ("license-text",
           "Additional license",
           "Additional license text",
           "",
           CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:zoom-on-double-click:
  *
  * Should the view zoom in and recenter when the user double click on the map.
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class,
       PROP_ZOOM_ON_DOUBLE_CLICK,
       g_param_spec_boolean ("zoom-on-double-click",
           "Zoom in on double click",
           "Zoom in and recenter on double click on the map",
           TRUE, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainView:state
  *
  * The view's global state. Useful to inform using if the view is busy loading
  * tiles or not.
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class,
       PROP_STATE,
       g_param_spec_enum ("state",
           "View's state",
           "View's global state",
           CHAMPLAIN_TYPE_STATE,
           CHAMPLAIN_STATE_INIT,
           G_PARAM_READABLE));

  /**
  * ChamplainView:show-scale:
  *
  * Display the map scale.
  *
  * Since: 0.4.3
  */
  g_object_class_install_property (object_class,
       PROP_SHOW_SCALE,
       g_param_spec_boolean ("show-scale",
           "Show the map scale",
           "Show the map scale "
           "on the screen",
           FALSE,
           G_PARAM_READWRITE));

  /**
  * ChamplainView:max-scale-width:
  *
  * The size of the map scale on screen in pixels.
  *
  * Since: 0.4.3
  */
  g_object_class_install_property (object_class,
       PROP_MAX_SCALE_WIDTH,
       g_param_spec_uint ("max-scale-width",
           "The width of the scale",
           "The max width of the scale"
           "on screen",
           1,
           2000,
           100,
           G_PARAM_READWRITE));

  /**
  * ChamplainView:scale-unit:
  *
  * The scale's units.
  *
  * Since: 0.4.3
  */
  g_object_class_install_property (object_class,
       PROP_SCALE_UNIT,
       g_param_spec_enum ("scale-unit",
           "The scale's unit",
           "The map scale's unit",
           CHAMPLAIN_TYPE_UNIT,
           CHAMPLAIN_UNIT_KM,
           G_PARAM_READWRITE));

  /**
  * ChamplainView::animation-completed:
  *
  * The ::animation-completed signal is emitted when any animation in the view
  * ends.  This is a detailed signal.  For example, if you want to be signaled
  * only for go-to animation, you should connect to
  * "animation-completed::go-to".
  *
  * Since: 0.4
  */
  signals[ANIMATION_COMPLETED] =
      g_signal_new ("animation-completed", G_OBJECT_CLASS_TYPE (object_class),
          G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED, 0, NULL, NULL,
          g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 0);

}

static gboolean
button_release_cb (ClutterActor *actor,
    ClutterEvent *event,
    ChamplainView *view)
{
  GList *children = NULL;
  gboolean found = FALSE;
  ChamplainViewPrivate *priv = view->priv;

  if (clutter_event_get_button (event) != 1)
    return FALSE;

  children = clutter_container_get_children (CLUTTER_CONTAINER (priv->user_layers));
  for (;children != NULL; children = g_list_next (children))
    {
      if (CHAMPLAIN_IS_SELECTION_LAYER (children->data) &&
          champlain_selection_layer_count_selected_markers (CHAMPLAIN_SELECTION_LAYER (children->data)) > 0)
        {
          champlain_selection_layer_unselect_all (CHAMPLAIN_SELECTION_LAYER (children->data));
          found = TRUE;
        }
    }

  g_list_free (children);

  return found;
}

static void
update_scale (ChamplainView *view)
{
  static gfloat previous_m_per_pixel = 0.0;

  gboolean is_small_unit = TRUE;  /* indicates if using meters */
  ClutterActor *text, *line;
  gfloat width;
  ChamplainViewPrivate *priv = view->priv;
  gfloat m_per_pixel;
  gfloat scale_width = priv->max_scale_width;
  gchar *label;
  cairo_t *cr;
  gfloat base;
  gfloat factor;
  gboolean final_unit = FALSE;

  if (!priv->map || !priv->map->current_level)
    return;

  if (priv->show_scale)
    {
      clutter_actor_show (priv->scale_actor);
    }
  else
    {
      clutter_actor_hide (priv->scale_actor);
      return;
    }

  m_per_pixel = champlain_map_source_get_meters_per_pixel (priv->map_source,
      priv->zoom_level, priv->latitude, priv->longitude);

  /* Don't redraw too often */
  if (fabs (m_per_pixel - previous_m_per_pixel) < 0.01)
    return;

  previous_m_per_pixel = m_per_pixel;

  if (priv->scale_unit == CHAMPLAIN_UNIT_MILES)
    m_per_pixel *= 3.28; /* m_per_pixel is now in ft */

  /* This loop will find the pretty value to display on the scale.
   * It will be run once for metric units, and twice for imperials
   * so that both feet and miles have pretty numbers.
   */
  do
    {
      /* Keep the previous power of 10 */
      base = floor (log (m_per_pixel * scale_width) / log (10));
      base = pow (10, base);

      /* How many times can it be fitted in our max scale width */
      g_assert (base > 0);
      g_assert (m_per_pixel * scale_width / base > 0);
      scale_width /= m_per_pixel * scale_width / base;
      g_assert (scale_width > 0);
      factor = floor (priv->max_scale_width / scale_width);
      base *= factor;
      scale_width *= factor;

      if (priv->scale_unit == CHAMPLAIN_UNIT_KM)
        {
          if (base / 1000.0 >= 1)
            {
              base /= 1000.0; /* base is now in km */
              is_small_unit = FALSE;
            }
          final_unit = TRUE; /* Don't need to recompute */
        }
      else if (priv->scale_unit == CHAMPLAIN_UNIT_MILES)
        {
          if (is_small_unit && base / 5280.0 >= 1)
            {
              m_per_pixel /= 5280.0; /* m_per_pixel is now in miles */
              is_small_unit = FALSE;
              /* we need to recompute the base because 1000 ft != 1 mile */
            }
          else
            final_unit = TRUE;
        }
    }
  while (!final_unit);

  text = clutter_container_find_child_by_name (CLUTTER_CONTAINER (priv->scale_actor), "scale-far-label");
  label = g_strdup_printf ("%g", base);
  /* Get only digits width for centering */
  clutter_text_set_text (CLUTTER_TEXT (text), label);
  g_free (label);
  clutter_actor_get_size (text, &width, NULL);
  /* actual label with unit */
  label = g_strdup_printf ("%g %s", base,
      priv->scale_unit == CHAMPLAIN_UNIT_KM ?
      (is_small_unit ? "m": "km"):
      (is_small_unit ? "ft": "miles")
      );
  clutter_text_set_text (CLUTTER_TEXT (text), label);
  g_free (label);
  clutter_actor_set_position (text, (scale_width - width / 2) + SCALE_INSIDE_PADDING, - SCALE_INSIDE_PADDING);

  text = clutter_container_find_child_by_name (CLUTTER_CONTAINER (priv->scale_actor), "scale-mid-label");
  label = g_strdup_printf ("%g", base / 2.0);
  clutter_text_set_text (CLUTTER_TEXT (text), label);
  clutter_actor_get_size (text, &width, NULL);
  clutter_actor_set_position (text, (scale_width - width) / 2 + SCALE_INSIDE_PADDING, - SCALE_INSIDE_PADDING);
  g_free (label);

  /* Draw the line */
  line = clutter_container_find_child_by_name (CLUTTER_CONTAINER (priv->scale_actor), "scale-line");
  clutter_cairo_texture_clear (CLUTTER_CAIRO_TEXTURE (line));
  cr = clutter_cairo_texture_create (CLUTTER_CAIRO_TEXTURE (line));

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
  cairo_set_line_width (cr, SCALE_LINE_WIDTH);

  /* First tick */
  cairo_move_to (cr, SCALE_INSIDE_PADDING, SCALE_HEIGHT / 4);
  cairo_line_to (cr, SCALE_INSIDE_PADDING, SCALE_HEIGHT / 2 );
  cairo_stroke (cr);

  /* Line */
  cairo_move_to (cr, SCALE_INSIDE_PADDING, SCALE_HEIGHT / 2);
  cairo_line_to (cr, scale_width + SCALE_INSIDE_PADDING, SCALE_HEIGHT / 2);
  cairo_stroke (cr);

  /* Middle tick */
  cairo_move_to (cr, scale_width / 2 + SCALE_INSIDE_PADDING, SCALE_HEIGHT / 4);
  cairo_line_to (cr, scale_width / 2 + SCALE_INSIDE_PADDING, SCALE_HEIGHT / 2);
  cairo_stroke (cr);

  /* Last tick */
  cairo_move_to (cr, scale_width + SCALE_INSIDE_PADDING, SCALE_HEIGHT / 4);
  cairo_line_to (cr, scale_width + SCALE_INSIDE_PADDING, SCALE_HEIGHT / 2);
  cairo_stroke (cr);

  cairo_destroy (cr);
}

static void
create_scale (ChamplainView *view)
{
  ClutterActor *scale, *text;
  gfloat width;
  ChamplainViewPrivate *priv = view->priv;

  if (priv->scale_actor)
    {
      g_object_unref (priv->scale_actor);
      clutter_container_remove_actor (CLUTTER_CONTAINER (priv->stage), priv->scale_actor);
    }

  priv->scale_actor = g_object_ref (clutter_group_new());
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->stage), priv->scale_actor);

  scale = clutter_cairo_texture_new (priv->max_scale_width + 2 * SCALE_INSIDE_PADDING, SCALE_HEIGHT + 2 * SCALE_INSIDE_PADDING);
  clutter_actor_set_name (scale, "scale-line");

  text = clutter_text_new_with_text ("Sans 9", "X km");
  clutter_actor_set_name (text, "scale-far-label");
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->scale_actor), text);

  text = clutter_text_new_with_text ("Sans 9", "X km");
  clutter_actor_set_name (text, "scale-mid-label");
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->scale_actor), text);

  text = clutter_text_new_with_text ("Sans 9", "0");
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->scale_actor), text);
  clutter_actor_get_size (text, &width, NULL);
  clutter_actor_set_position (text, SCALE_INSIDE_PADDING - width / 2, - SCALE_INSIDE_PADDING);

  clutter_container_add_actor (CLUTTER_CONTAINER (priv->scale_actor), scale);
  clutter_actor_set_position (priv->scale_actor, SCALE_PADDING - SCALE_INSIDE_PADDING,
    priv->viewport_size.height - SCALE_HEIGHT - SCALE_PADDING - SCALE_INSIDE_PADDING);

  clutter_actor_set_opacity (priv->scale_actor, 200);
}

static void
champlain_view_init (ChamplainView *view)
{
  ChamplainViewPrivate *priv = GET_PRIVATE (view);

  champlain_debug_set_flags (g_getenv ("CHAMPLAIN_DEBUG"));

  view->priv = priv;

  priv->factory = champlain_map_source_factory_dup_default ();
  priv->map_source = champlain_map_source_factory_create (priv->factory, CHAMPLAIN_MAP_SOURCE_OSM_MAPNIK);
  priv->zoom_level = 0;
  priv->min_zoom_level = champlain_map_source_get_min_zoom_level (priv->map_source);
  priv->max_zoom_level = champlain_map_source_get_max_zoom_level (priv->map_source);
  priv->keep_center_on_resize = TRUE;
  priv->zoom_on_double_click = TRUE;
  priv->show_license = TRUE;
  priv->license_actor = NULL;
  priv->license_text = NULL;
  priv->stage = g_object_ref (clutter_group_new ());
  priv->scroll_mode = CHAMPLAIN_SCROLL_MODE_PUSH;
  priv->viewport_size.x = 0;
  priv->viewport_size.y = 0;
  priv->viewport_size.width = 0;
  priv->viewport_size.height = 0;
  priv->anchor.x = 0;
  priv->anchor.y = 0;
  priv->anchor_zoom_level = 0;
  priv->state = CHAMPLAIN_STATE_INIT;
  priv->latitude = 0.0f;
  priv->longitude = 0.0f;
  priv->goto_context = NULL;
  priv->map = NULL;
  priv->polygon_redraw_id = 0;
  priv->show_scale = FALSE;
  priv->scale_unit = CHAMPLAIN_UNIT_KM;
  priv->max_scale_width = 100;

  /* Setup viewport */
  priv->viewport = g_object_ref (tidy_viewport_new ());
  g_object_set (G_OBJECT (priv->viewport), "sync-adjustments", FALSE, NULL);

  g_signal_connect (priv->viewport, "notify::x-origin",
      G_CALLBACK (viewport_pos_changed_cb), view);
  g_signal_connect (priv->viewport, "notify::y-origin",
      G_CALLBACK (viewport_pos_changed_cb), view);

  /* Setup scale */
  create_scale (view);

  /* Setup finger scroll */
  priv->finger_scroll = g_object_ref (tidy_finger_scroll_new (priv->scroll_mode));

  g_signal_connect (priv->finger_scroll, "scroll-event",
      G_CALLBACK (scroll_event), view);

  clutter_container_add_actor (CLUTTER_CONTAINER (priv->finger_scroll),
      priv->viewport);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->stage),
      priv->finger_scroll);
  clutter_container_add_actor (CLUTTER_CONTAINER (view), priv->stage);

  /* Map Layer */
  priv->map_layer = g_object_ref (clutter_group_new ());
  clutter_actor_show (priv->map_layer);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->viewport),
      priv->map_layer);

  g_signal_connect (priv->finger_scroll, "button-press-event",
      G_CALLBACK (finger_scroll_button_press_cb), view);
  g_signal_connect_after (priv->finger_scroll, "button-release-event",
      G_CALLBACK (button_release_cb), view);

  clutter_stage_set_key_focus (CLUTTER_STAGE (clutter_stage_get_default()),
      priv->finger_scroll);
  g_signal_connect (priv->finger_scroll, "key-press-event",
      G_CALLBACK (finger_scroll_key_press_cb), view);

  /* Setup user_layers */
  priv->user_layers = g_object_ref (clutter_group_new ());
  clutter_actor_show (priv->user_layers);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->viewport),
      priv->user_layers);
  clutter_actor_raise (priv->user_layers, priv->map_layer);

  priv->polygons = NULL;

  /* Setup polygon layer */
  priv->polygon_layer = g_object_ref (clutter_group_new ());
  clutter_actor_show (priv->polygon_layer);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->viewport),
      priv->polygon_layer);
  clutter_actor_raise (priv->polygon_layer, priv->map_layer);

  champlain_view_set_size (view, priv->viewport_size.width,
      priv->viewport_size.height);

  clutter_actor_raise_top (priv->scale_actor);
  resize_viewport (view);

  priv->state = CHAMPLAIN_STATE_DONE;
  g_object_notify (G_OBJECT (view), "state");
}

static void
viewport_pos_changed_cb (GObject *gobject,
    GParamSpec *arg1,
    ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;

  ChamplainFloatPoint rect;
  ChamplainFloatPoint old_anchor;

  tidy_viewport_get_origin (TIDY_VIEWPORT (priv->viewport), &rect.x, &rect.y,
      NULL);

  if (rect.x == priv->viewport_size.x &&
      rect.y == priv->viewport_size.y)
      return;

  old_anchor.x = priv->anchor.x;
  old_anchor.y = priv->anchor.y;

  view_update_anchor (view,
      rect.x + priv->anchor.x + priv->viewport_size.width / 2.0,
      rect.y + priv->anchor.y + priv->viewport_size.height / 2.0);

  if (priv->anchor.x - old_anchor.x != 0)
    {
      ChamplainFloatPoint diff;

      diff.x = priv->anchor.x - old_anchor.x;
      diff.y = priv->anchor.y - old_anchor.y;

      DEBUG("Relocating the viewport by %f, %f", diff.x, diff.y);
      tidy_viewport_set_origin (TIDY_VIEWPORT (priv->viewport),
          rect.x - diff.x, rect.y - diff.y, 0);
      return;
    }

  priv->viewport_size.x = rect.x;
  priv->viewport_size.y = rect.y;

  view_load_visible_tiles (view);
  view_tiles_reposition (view);
  marker_reposition (view);
  view_update_polygons (view);
  update_scale (view);

  priv->longitude = viewport_get_current_longitude (priv);
  priv->latitude = viewport_get_current_latitude (priv);

  g_object_notify (G_OBJECT (view), "longitude");
  g_object_notify (G_OBJECT (view), "latitude");
}

/**
 * champlain_view_set_size:
 * @view: a #ChamplainView
 * @width: the width in pixels
 * @height: the height in pixels
 *
 * Sets the size of the view.  This function will most probably be deprecated in
 * future versions in favor of #clutter_actor_set_size.  In the mean time, you need
 * to call both.
 *
 * Since: 0.1
 */
//FIXME: move to an handler of actor size change
void
champlain_view_set_size (ChamplainView *view,
    guint width,
    guint height)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->viewport_size.width = width;
  priv->viewport_size.height = height;

  license_set_position (view);
  clutter_actor_set_position (priv->scale_actor, SCALE_PADDING,
      priv->viewport_size.height - SCALE_HEIGHT - SCALE_PADDING);
  resize_viewport (view);

  if (priv->keep_center_on_resize)
    champlain_view_center_on (view, priv->latitude, priv->longitude);
  else
    view_load_visible_tiles (view);
}

static void
update_license (ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;
  gchar *license;

  if (!priv->license_actor)
    {
      priv->license_actor = g_object_ref (clutter_text_new ());
      clutter_text_set_font_name (CLUTTER_TEXT (priv->license_actor), "sans 8");
      clutter_text_set_line_alignment (CLUTTER_TEXT (priv->license_actor), PANGO_ALIGN_RIGHT);
      clutter_actor_set_opacity (priv->license_actor, 128);
      clutter_container_add_actor (CLUTTER_CONTAINER (priv->stage),
          priv->license_actor);
      clutter_actor_set_anchor_point_from_gravity (priv->license_actor, CLUTTER_GRAVITY_SOUTH_EAST);
      clutter_actor_raise_top (priv->license_actor);
    }

  if (priv->license_text)
    license = g_strjoin ("\n",
        priv->license_text,
        champlain_map_source_get_license (priv->map_source),
        NULL);
  else
    license = g_strdup (champlain_map_source_get_license (priv->map_source));

  clutter_text_set_text (CLUTTER_TEXT (priv->license_actor), license);

  if (priv->show_license)
    {
      clutter_actor_show (priv->license_actor);
      license_set_position (view);
    }
  else
    clutter_actor_hide (priv->license_actor);

  g_free (license);
}

static gboolean
finger_scroll_button_press_cb (ClutterActor *actor,
    ClutterButtonEvent *event,
    ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;

  if (priv->zoom_on_double_click && event->button == 1 && event->click_count == 2)
    {
      return view_set_zoom_level_at (view, priv->zoom_level + 1, event->x, event->y);
    }
  return FALSE; /* Propagate the event */
}

static void
scroll_to (ChamplainView *view,
    gint x,
    gint y)
{

  ChamplainViewPrivate *priv = view->priv;

  if (priv->scroll_mode == CHAMPLAIN_SCROLL_MODE_KINETIC)
    {
      gfloat lat, lon;

      lat = champlain_map_source_get_latitude (priv->map_source, priv->zoom_level, y);
      lon = champlain_map_source_get_longitude (priv->map_source, priv->zoom_level, x);

      champlain_view_go_to_with_duration (view, lat, lon, 300);
    }
  else if (priv->scroll_mode == CHAMPLAIN_SCROLL_MODE_PUSH)
    {
      tidy_viewport_set_origin (TIDY_VIEWPORT (priv->viewport),
        x - priv->viewport_size.width / 2.0,
        y - priv->viewport_size.height / 2.0,
        0);
    }
}

/* These functions should be exposed in the next API break */
static void
champlain_view_scroll_left (ChamplainView* view)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  gint x, y;
  ChamplainViewPrivate *priv = view->priv;

  x = champlain_map_source_get_x (priv->map_source, priv->zoom_level, priv->longitude);
  y = champlain_map_source_get_y (priv->map_source, priv->zoom_level, priv->latitude);

  x -= priv->viewport_size.width / 4.0;

  scroll_to (view, x, y);
}

static void
champlain_view_scroll_right (ChamplainView* view)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  gint x, y;
  ChamplainViewPrivate *priv = view->priv;

  x = champlain_map_source_get_x (priv->map_source, priv->zoom_level, priv->longitude);
  y = champlain_map_source_get_y (priv->map_source, priv->zoom_level, priv->latitude);

  x += priv->viewport_size.width / 4.0;

  scroll_to (view, x, y);
}

static void
champlain_view_scroll_up (ChamplainView* view)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  gint x, y;
  ChamplainViewPrivate *priv = view->priv;

  x = champlain_map_source_get_x (priv->map_source, priv->zoom_level, priv->longitude);
  y = champlain_map_source_get_y (priv->map_source, priv->zoom_level, priv->latitude);

  y -= priv->viewport_size.width / 4.0;

  scroll_to (view, x, y);
}

static void
champlain_view_scroll_down (ChamplainView* view)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  gint x, y;
  ChamplainViewPrivate *priv = view->priv;

  x = champlain_map_source_get_x (priv->map_source, priv->zoom_level, priv->longitude);
  y = champlain_map_source_get_y (priv->map_source, priv->zoom_level, priv->latitude);

  y += priv->viewport_size.width / 4.0;

  scroll_to (view, x, y);
}


static gboolean
finger_scroll_key_press_cb (ClutterActor *actor,
    ClutterKeyEvent *event,
    ChamplainView *view)
{

  switch (event->keyval)
  {
    case 65361: // Left
      champlain_view_scroll_left (view);
      return TRUE;
      break;
    case 65362: // Up
      if (event->modifier_state & CLUTTER_CONTROL_MASK)
        champlain_view_zoom_in (view);
      else
        champlain_view_scroll_up (view);
      return TRUE;
      break;
    case 65363: // Right
      champlain_view_scroll_right (view);
      return TRUE;
      break;
    case 65364: // Down
      if (event->modifier_state & CLUTTER_CONTROL_MASK)
        champlain_view_zoom_out (view);
      else
        champlain_view_scroll_down (view);
      return TRUE;
      break;
    default:
      return FALSE; /* Propagate the event */
  }
  return FALSE; /* Propagate the event */
}

/**
 * champlain_view_new:
 *
 * Returns: a new #ChamplainView ready to be used as a #ClutterActor.
 *
 * Since: 0.4
 */
ClutterActor *
champlain_view_new (void)
{
  return g_object_new (CHAMPLAIN_TYPE_VIEW, NULL);
}

static void
view_update_anchor (ChamplainView *view,
    gint x,  /* Absolute x */
    gint y)  /* Absolute y */
{
  ChamplainViewPrivate *priv = view->priv;
  gboolean need_anchor = FALSE;
  gboolean need_update = FALSE;

  if (priv->zoom_level >= 8)
    need_anchor = TRUE;

  if (priv->anchor_zoom_level != priv->zoom_level ||
      x - priv->anchor.x + priv->viewport_size.width >= G_MAXINT16 ||
      y - priv->anchor.y + priv->viewport_size.height >= G_MAXINT16 ||
      x - priv->anchor.x + priv->viewport_size.width <= 0 ||
      y - priv->anchor.y + priv->viewport_size.height <= 0 )
    need_update = TRUE;

  if (need_anchor && need_update)
    {
      gdouble max;

      priv->anchor.x = x - G_MAXINT16 / 2;
      priv->anchor.y = y - G_MAXINT16 / 2;

      if ( priv->anchor.x < 0 )
        priv->anchor.x = 0;
      if ( priv->anchor.y < 0 )
        priv->anchor.y = 0;

      max = champlain_zoom_level_get_width (priv->map->current_level) *
          champlain_map_source_get_tile_size (priv->map_source) -
          (G_MAXINT16 / 2);
      if (priv->anchor.x > max)
        priv->anchor.x = max;
      if (priv->anchor.y > max)
        priv->anchor.y = max;

      priv->anchor_zoom_level = priv->zoom_level;
      DEBUG ("New Anchor (%f, %f) at (%d, %d)", priv->anchor.x, priv->anchor.y, x, y);
    }

  if (need_anchor == FALSE)
    {
      priv->anchor.x = 0;
      priv->anchor.y = 0;
      priv->anchor_zoom_level = priv->zoom_level;
      DEBUG ("Clear Anchor at (%d, %d)", x, y);
    }
}

/**
 * champlain_view_center_on:
 * @view: a #ChamplainView
 * @latitude: the longitude to center the map at
 * @longitude: the longitude to center the map at
 *
 * Centers the map on these coordinates.
 *
 * Since: 0.1
 */
void
champlain_view_center_on (ChamplainView *view,
    gdouble latitude,
    gdouble longitude)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  gint x, y;
  ChamplainViewPrivate *priv = view->priv;

  priv->longitude = CLAMP (longitude, CHAMPLAIN_MIN_LONG, CHAMPLAIN_MAX_LONG);
  priv->latitude = CLAMP (latitude, CHAMPLAIN_MIN_LAT, CHAMPLAIN_MAX_LAT);

  if (!priv->map)
    {
      create_initial_map (view);
    }

  x = champlain_map_source_get_x (priv->map_source, priv->zoom_level, longitude);
  y = champlain_map_source_get_y (priv->map_source, priv->zoom_level, latitude);

  DEBUG ("Centering on %f, %f (%d, %d)", latitude, longitude, x, y);

  view_update_anchor (view, x, y);

  x -= priv->anchor.x;
  y -= priv->anchor.y;

  tidy_viewport_set_origin (TIDY_VIEWPORT (priv->viewport),
    x - priv->viewport_size.width / 2.0,
    y - priv->viewport_size.height / 2.0,
    0);

  g_object_notify (G_OBJECT (view), "longitude");
  g_object_notify (G_OBJECT (view), "latitude");

  view_load_visible_tiles (view);
  view_tiles_reposition (view);
  view_update_polygons (view);
  update_scale (view);
  marker_reposition (view);
}

static void
timeline_new_frame (ClutterTimeline *timeline,
    gint frame_num,
    GoToContext *ctx)
{
  gdouble alpha;
  gdouble lat;
  gdouble lon;

  alpha = clutter_alpha_get_alpha (ctx->alpha);
  lat = ctx->to_latitude - ctx->from_latitude;
  lon = ctx->to_longitude - ctx->from_longitude;

  champlain_view_center_on (ctx->view,
      ctx->from_latitude + alpha * lat,
      ctx->from_longitude + alpha * lon);
}

static void
timeline_completed (ClutterTimeline *timeline,
                    ChamplainView *view)
{
  champlain_view_stop_go_to (view);
}

/**
 * champlain_view_stop_go_to:
 * @view: a #ChamplainView
 *
 * Stop the go to animation.  The view will stay where it was when the
 * animation was stopped.
 *
 * Since: 0.4
 */
void
champlain_view_stop_go_to (ChamplainView *view)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  if (priv->goto_context == NULL)
    return;

  clutter_timeline_stop (priv->goto_context->timeline);

  g_object_unref (priv->goto_context->timeline);
  g_object_unref (priv->goto_context->alpha);

  g_signal_emit_by_name (view, "animation-completed::go-to", NULL);

  g_free (priv->goto_context);
  priv->goto_context = NULL;
}

/**
 * champlain_view_go_to:
 * @view: a #ChamplainView
 * @latitude: the longitude to center the map at
 * @longitude: the longitude to center the map at
 *
 * Move from the current position to these coordinates. All tiles in the
 * intermediate view WILL be loaded!
 *
 * Since: 0.4
 */
void
champlain_view_go_to (ChamplainView *view,
    gdouble latitude,
    gdouble longitude)
{
  guint duration;

  duration = 500 * view->priv->zoom_level / 2.0;
  champlain_view_go_to_with_duration (view, latitude, longitude, duration);
}

/* FIXME: make public after API freeze */
static void
champlain_view_go_to_with_duration (ChamplainView *view,
    gdouble latitude,
    gdouble longitude,
    guint duration) /* In ms */
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  if (duration == 0)
    {
      champlain_view_center_on (view, latitude, longitude);
      return;
    }

  GoToContext *ctx;

  ChamplainViewPrivate *priv = view->priv;

  champlain_view_stop_go_to (view);

  ctx = g_new0 (GoToContext, 1);
  ctx->from_latitude = priv->latitude;
  ctx->from_longitude = priv->longitude;
  ctx->to_latitude = latitude;
  ctx->to_longitude = longitude;
  ctx->view = view;

  /* We keep a reference for stop */
  priv->goto_context = ctx;

  /* A ClutterTimeline will be responsible for the animation,
   * at each frame, the current position will be computer and set
   * using champlain_view_center_on.  Timelines skip frames if the
   * computer is not fast enough, so we just need to set the duration.
   *
   * To have a nice animation, the duration should be longer if the zoom level
   * is higher and if the points are far away
   */
  ctx->timeline = clutter_timeline_new (duration);
  ctx->alpha = clutter_alpha_new_full (ctx->timeline, CLUTTER_EASE_IN_OUT_CIRC);

  g_signal_connect (ctx->timeline, "new-frame", G_CALLBACK (timeline_new_frame),
      ctx);
  g_signal_connect (ctx->timeline, "completed", G_CALLBACK (timeline_completed),
      view);

  clutter_timeline_start (ctx->timeline);
}

/**
 * champlain_view_zoom_in:
 * @view: a #ChamplainView
 *
 * Zoom in the map by one level.
 *
 * Since: 0.1
 */
void
champlain_view_zoom_in (ChamplainView *view)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  champlain_view_set_zoom_level (view, priv->zoom_level + 1);
}

/**
 * champlain_view_zoom_out:
 * @view: a #ChamplainView
 *
 * Zoom out the map by one level.
 *
 * Since: 0.1
 */
void
champlain_view_zoom_out (ChamplainView *view)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  champlain_view_set_zoom_level (view, priv->zoom_level - 1);
}

/**
 * champlain_view_set_zoom_level:
 * @view: a #ChamplainView
 * @zoom_level: a gint
 *
 * Changes the current zoom level
 *
 * Since: 0.4
 */
void
champlain_view_set_zoom_level (ChamplainView *view,
    gint zoom_level)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;
  gdouble longitude;
  gdouble latitude;

  if (priv->map == NULL)
    return;

  if (zoom_level == priv->zoom_level || ZOOM_LEVEL_OUT_OF_RANGE(priv, zoom_level))
    return;

  champlain_view_stop_go_to (view);

  ClutterActor *group = champlain_zoom_level_get_actor (priv->map->current_level);
  if (!map_zoom_to (priv->map, priv->map_source, zoom_level))
    return;

  DEBUG ("Zooming to %d", zoom_level);

  priv->zoom_level = zoom_level;
  /* Fix to bug 575133: keep the lat,lon as it gets set to a wrong value
   * when resizing the viewport, when passing from zoom_level 7 to 6
   * (or more precisely when anchor is set to 0).
   */
  longitude = priv->longitude;
  latitude = priv->latitude;
  resize_viewport (view);

  ClutterActor *new_group = champlain_zoom_level_get_actor (priv->map->current_level);
  clutter_container_remove_actor (CLUTTER_CONTAINER (priv->map_layer), group);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->map_layer), new_group);
  champlain_view_center_on (view, latitude, longitude);

  g_object_notify (G_OBJECT (view), "zoom-level");
}

/**
 * champlain_view_set_min_zoom_level:
 * @view: a #ChamplainView
 * @zoom_level: a gint
 *
 * Changes the lowest allowed zoom level
 *
 * Since: 0.4
 */
void
champlain_view_set_min_zoom_level (ChamplainView *view,
    gint min_zoom_level)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  if (priv->min_zoom_level == min_zoom_level ||
      min_zoom_level > priv->max_zoom_level ||
      min_zoom_level < champlain_map_source_get_min_zoom_level (priv->map_source))
    return;

  priv->min_zoom_level = min_zoom_level;

  if (priv->zoom_level < min_zoom_level)
    champlain_view_set_zoom_level (view, min_zoom_level);
}

/**
 * champlain_view_set_max_zoom_level:
 * @view: a #ChamplainView
 * @zoom_level: a gint
 *
 * Changes the highest allowed zoom level
 *
 * Since: 0.4
 */
void
champlain_view_set_max_zoom_level (ChamplainView *view,
    gint max_zoom_level)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  if (priv->max_zoom_level == max_zoom_level ||
      max_zoom_level < priv->min_zoom_level ||
      max_zoom_level > champlain_map_source_get_max_zoom_level (priv->map_source))
    return;

  priv->max_zoom_level = max_zoom_level;

  if (priv->zoom_level > max_zoom_level)
    champlain_view_set_zoom_level (view, max_zoom_level);
}

/**
 * champlain_view_add_layer:
 * @view: a #ChamplainView
 * @layer: a #ChamplainLayer
 *
 * Adds a new layer to the view
 *
 * Since: 0.2
 */
void
champlain_view_add_layer (ChamplainView *view,
    ChamplainLayer *layer)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));
  g_return_if_fail (CHAMPLAIN_IS_LAYER (layer));

  ChamplainViewPrivate *priv = view->priv;
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->user_layers),
      CLUTTER_ACTOR (layer));
  clutter_actor_raise_top (CLUTTER_ACTOR (layer));

  if (priv->map)
    g_idle_add (marker_reposition, view);

  g_signal_connect_after (layer, "actor-added",
      G_CALLBACK (layer_add_marker_cb), view);

  clutter_container_foreach (CLUTTER_CONTAINER (layer),
      CLUTTER_CALLBACK (connect_marker_notify_cb), view);
}

/**
 * champlain_view_remove_layer:
 * @view: a #ChamplainView
 * @layer: a #ChamplainLayer
 *
 * Removes the layer from the view
 *
 * Since: 0.4.1
 */
void
champlain_view_remove_layer (ChamplainView *view,
    ChamplainLayer *layer)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));
  g_return_if_fail (CHAMPLAIN_IS_LAYER (layer));

  ChamplainViewPrivate *priv = view->priv;

  g_signal_handlers_disconnect_by_func (layer,
      G_CALLBACK (layer_add_marker_cb), view);
  clutter_container_remove_actor (CLUTTER_CONTAINER (priv->user_layers),
      CLUTTER_ACTOR (layer));
}

/**
 * champlain_view_get_coords_from_event:
 * @view: a #ChamplainView
 * @event: a #ClutterEvent
 * @lat: a variable where to put the latitude of the event
 * @lon: a variable where to put the longitude of the event
 *
 * Returns: the latitude, longitude coordinates for the given ClutterEvent.
 *
 * Since: 0.2.8
 */
gboolean
champlain_view_get_coords_from_event (ChamplainView *view,
    ClutterEvent *event,
    gdouble *latitude,
    gdouble *longitude)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);
  /* Apparently there isn a more precise test */
  g_return_val_if_fail (event, FALSE);

  guint x, y;

  switch (clutter_event_type (event))
    {
      case CLUTTER_BUTTON_PRESS:
      case CLUTTER_BUTTON_RELEASE:
        {
          ClutterButtonEvent *e = (ClutterButtonEvent*) event;
          x = e->x;
          y = e->y;
        }
        break;
      case CLUTTER_SCROLL:
        {
          ClutterScrollEvent *e = (ClutterScrollEvent*) event;
          x = e->x;
          y = e->y;
        }
        break;
      case CLUTTER_MOTION:
        {
          ClutterMotionEvent *e = (ClutterMotionEvent*) event;
          x = e->x;
          y = e->y;
        }
        break;
      case CLUTTER_ENTER:
      case CLUTTER_LEAVE:
        {
          ClutterCrossingEvent *e = (ClutterCrossingEvent*) event;
          x = e->x;
          y = e->y;
        }
        break;
      default:
        return FALSE;
    }

  return champlain_view_get_coords_at (view, x, y, latitude, longitude);
}

/**
 * champlain_view_get_coords_at:
 * @view: a #ChamplainView
 * @x: the x position in the view
 * @y: the y position in the view
 * @lat: a variable where to put the latitude of the event
 * @lon: a variable where to put the longitude of the event
 *
 * Returns: the latitude, longitude coordinates for the given x, y position in
 * the view.  Use if you get coordinates from GtkEvents for example.
 *
 * Since: 0.4
 */
gboolean champlain_view_get_coords_at (ChamplainView *view,
    guint x,
    guint y,
    gdouble *latitude,
    gdouble *longitude)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);
  ChamplainViewPrivate *priv = view->priv;
  gfloat actor_x, actor_y;
  gdouble rel_x, rel_y;

  clutter_actor_get_transformed_position (priv->finger_scroll, &actor_x, &actor_y);

  rel_x = x - actor_x;
  rel_y = y - actor_y;

  if (latitude)
    *latitude = viewport_get_latitude_at (priv,
        priv->viewport_size.y + rel_y + priv->anchor.y);
  if (longitude)
    *longitude = viewport_get_longitude_at (priv,
        priv->viewport_size.x + rel_x + priv->anchor.x);

  return TRUE;
}

static void
view_load_visible_tiles (ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;
  ChamplainRectangle viewport = priv->viewport_size;
  gint size;
  ChamplainZoomLevel *level;

  viewport.x += priv->anchor.x;
  viewport.y += priv->anchor.y;

  size = champlain_map_source_get_tile_size (priv->map_source);
  level = priv->map->current_level;

  if (viewport.x < 0)
    viewport.x = 0;
  if (viewport.y < 0)
    viewport.y = 0;

  gint x_count = ceil((float)viewport.width / size) + 1;
  gint y_count = ceil((float)viewport.height / size) + 1;

  gint x_first = viewport.x / size;
  gint y_first = viewport.y / size;

  x_count += x_first;
  y_count += y_first;

  if(x_count > champlain_zoom_level_get_width (level))
    x_count = champlain_zoom_level_get_width (level);
  if(y_count > champlain_zoom_level_get_height (level))
    y_count = champlain_zoom_level_get_height (level);

  DEBUG ("Range %d, %d to %d, %d", x_first, y_first, x_count, y_count);

  int i, j;
  guint k = 0;

  // Get rid of old tiles first
  int count = champlain_zoom_level_tile_count (level);
  while (k < count)
    {
      ChamplainTile *tile = champlain_zoom_level_get_nth_tile (level, k);

      if (tile == NULL)
        {
          k++;
          continue;
        }

      gint tile_x = champlain_tile_get_x (tile);
      gint tile_y = champlain_tile_get_y (tile);

      if (tile_x < x_first || tile_x > x_count ||
          tile_y < y_first || tile_y > y_count)
      {
        ClutterActor *group, *actor;
        if (champlain_tile_get_state (tile) == CHAMPLAIN_STATE_DONE)
          {
            actor = champlain_tile_get_actor (tile);
            group = champlain_zoom_level_get_actor (level);
            if (actor != NULL)
              clutter_container_remove_actor (CLUTTER_CONTAINER (group), actor);
          }
        champlain_zoom_level_remove_tile (level, tile);
        count = champlain_zoom_level_tile_count (level);
      }
      else
        k++;
    }

  //Load new tiles if needed
  for (i = x_first; i < x_count; i++)
    {
      for (j = y_first; j < y_count; j++)
        {
          gboolean exist = FALSE;
          for (k = 0; k < champlain_zoom_level_tile_count (level) && !exist; k++)
            {
              ChamplainTile *tile = champlain_zoom_level_get_nth_tile (level, k);

              if (tile == NULL)
                continue;

              gint tile_x = champlain_tile_get_x (tile);
              gint tile_y = champlain_tile_get_y (tile);

              if ( tile_x == i && tile_y == j)
                exist = TRUE;
            }

          if(!exist)
            {
              DEBUG ("Loading tile %d, %d, %d", champlain_zoom_level_get_zoom_level (level), i, j);
              ChamplainTile *tile = champlain_tile_new ();
              g_object_set (G_OBJECT (tile), "x", i, "y", j, "zoom-level", champlain_zoom_level_get_zoom_level (level), NULL);

              g_signal_connect (tile, "notify::state", G_CALLBACK (tile_state_notify), view);
              clutter_container_add (CLUTTER_CONTAINER (champlain_zoom_level_get_actor (level)),
                  champlain_tile_get_actor (tile), NULL);

              champlain_zoom_level_add_tile (level, tile);
              champlain_map_source_fill_tile (priv->map_source, tile);

              g_object_unref (tile);
            }
        }
    }
  view_update_state (view);
}

static void
view_position_tile (ChamplainView* view,
    ChamplainTile* tile)
{
  ChamplainViewPrivate *priv = view->priv;

  ClutterActor *actor;
  gint x;
  gint y;
  guint size;

  actor = champlain_tile_get_actor (tile);

  if (actor == NULL)
    return;

  x = champlain_tile_get_x (tile);
  y = champlain_tile_get_y (tile);
  size = champlain_tile_get_size (tile);

  clutter_actor_set_position (actor,
    (x * size) - priv->anchor.x,
    (y * size) - priv->anchor.y);
}

static void
view_tiles_reposition (ChamplainView* view)
{
  ChamplainViewPrivate *priv = view->priv;
  gint i;

  for (i = 0; i < champlain_zoom_level_tile_count (priv->map->current_level); i++)
    {
      ChamplainTile *tile = champlain_zoom_level_get_nth_tile (priv->map->current_level, i);

      if (tile == NULL)
        continue;

      if (champlain_tile_get_state (tile) == CHAMPLAIN_STATE_DONE)
        view_position_tile (view, tile);
    }
}

static void
tile_state_notify (GObject *gobject,
    GParamSpec *pspec,
    gpointer data)
{
  view_position_tile (CHAMPLAIN_VIEW (data), CHAMPLAIN_TILE (gobject));
  view_update_state (CHAMPLAIN_VIEW (data));
}

static void
view_update_state (ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;
  ChamplainState new_state = CHAMPLAIN_STATE_DONE;
  gint i;

  for (i = 0; i < champlain_zoom_level_tile_count (priv->map->current_level); i++)
    {
      ChamplainTile *tile = champlain_zoom_level_get_nth_tile (priv->map->current_level, i);

      if (tile == NULL)
        continue;

      if (champlain_tile_get_state (tile) == CHAMPLAIN_STATE_LOADING ||
          champlain_tile_get_state (tile) == CHAMPLAIN_STATE_VALIDATING_CACHE)
        new_state = CHAMPLAIN_STATE_LOADING;
    }

  if (priv->state != new_state)
    {
      priv->state = new_state;
      g_object_notify (G_OBJECT (view), "state");
    }
}

/**
 * champlain_view_set_map_source:
 * @view: a #ChamplainView
 * @map_source: a #ChamplainMapSource
 *
 * Changes the currently used map source.  #g_object_unref will be called on
 * the previous one.
 *
 * Since: 0.4
 */
void
champlain_view_set_map_source (ChamplainView *view,
    ChamplainMapSource *source)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view) &&
      CHAMPLAIN_IS_MAP_SOURCE (source));

  ClutterActor *group;

  ChamplainViewPrivate *priv = view->priv;

  if (priv->map_source == source)
    return;

  g_object_unref (priv->map_source);
  priv->map_source = g_object_ref (source);

  priv->min_zoom_level = champlain_map_source_get_min_zoom_level (priv->map_source);
  priv->max_zoom_level = champlain_map_source_get_max_zoom_level (priv->map_source);

  if (priv->map == NULL)
    return;

  group = champlain_zoom_level_get_actor (priv->map->current_level);
  clutter_container_remove_actor (CLUTTER_CONTAINER (priv->map_layer), group);

  map_free (priv->map);
  priv->map = map_new ();

  /* Keep same zoom level if the new map supports it */
  if (priv->zoom_level > priv->max_zoom_level)
    {
      priv->zoom_level = priv->max_zoom_level;
      g_object_notify (G_OBJECT (view), "zoom-level");
    }
  else if (priv->zoom_level < priv->min_zoom_level)
    {
      priv->zoom_level = priv->min_zoom_level;
      g_object_notify (G_OBJECT (view), "zoom-level");
    }

  map_load_level (priv->map, priv->map_source, priv->zoom_level);
  group = champlain_zoom_level_get_actor (priv->map->current_level);

  view_load_visible_tiles (view);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->map_layer), group);

  g_object_notify (G_OBJECT (view), "map-source");

  update_license (view);
  g_idle_add (marker_reposition, view);
  view_tiles_reposition (view);
  champlain_view_center_on (view, priv->latitude, priv->longitude);
}

/**
* champlain_view_set_decel_rate:
* @view: a #ChamplainView
* @rate: a #gdouble between 1.001 and 2.0
*
* The deceleration rate for the kinetic mode.
*
* Since: 0.4
*/
void
champlain_view_set_decel_rate (ChamplainView *view,
    gdouble rate)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view) &&
      rate < 2.0 &&
      rate > 1.0001);

  ChamplainViewPrivate *priv = view->priv;

  g_object_set (priv->finger_scroll, "decel-rate", rate, NULL);
}

/**
* champlain_view_set_scroll_mode:
* @view: a #ChamplainView
* @mode: a #ChamplainScrollMode value
*
* Determines the way the view reacts to scroll events.
*
* Since: 0.4
*/
void
champlain_view_set_scroll_mode (ChamplainView *view,
    ChamplainScrollMode mode)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->scroll_mode = mode;

  g_object_set (G_OBJECT (priv->finger_scroll), "mode",
      priv->scroll_mode, NULL);
}

/**
* champlain_view_set_keep_center_on_resize:
* @view: a #ChamplainView
* @value: a #gboolean
*
* Keep the current centered position when resizing the view.
*
* Since: 0.4
*/
void
champlain_view_set_keep_center_on_resize (ChamplainView *view,
    gboolean value)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->keep_center_on_resize = value;
}

/**
* champlain_view_set_license_text:
* @view: a #ChamplainView
* @text: a license
*
* Show the additional license text on the map view.  The text will preceed the
* map's licence when displayed. Use "\n" to separate the lines.
*
* Since: 0.4.3
*/
void
champlain_view_set_license_text (ChamplainView *view,
    const gchar *text)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->license_text = g_strdup (text);
  update_license (view);
}

/**
* champlain_view_set_show_license:
* @view: a #ChamplainView
* @value: a #gboolean
*
* Show the license on the map view.  The license information should always be
* available in a way or another in your application.  You can have it in
* About, or on the map.
*
* Since: 0.4
*/
void
champlain_view_set_show_license (ChamplainView *view,
    gboolean value)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->show_license = value;
  update_license (view);
}

/**
* champlain_view_set_show_scale:
* @view: a #ChamplainView
* @value: a #gboolean
*
* Show the scale on the map view.
*
* Since: 0.4.3
*/
void
champlain_view_set_show_scale (ChamplainView *view,
    gboolean value)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->show_scale = value;
  update_scale (view);
}

/**
* champlain_view_set_max_scale_width:
* @view: a #ChamplainView
* @value: a #guint in pixels
*
* Sets the maximum width of the scale on the screen in pixels
*
* Since: 0.4.3
*/
void
champlain_view_set_max_scale_width (ChamplainView *view,
    guint value)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->max_scale_width = value;
  create_scale (view);
  update_scale (view);
}

/**
* champlain_view_set_scale_unit:
* @view: a #ChamplainView
* @unit: a #ChamplainUnit
*
* Sets the scales unit.
*
* Since: 0.4.3
*/
void
champlain_view_set_scale_unit (ChamplainView *view,
    ChamplainUnit unit)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->scale_unit = unit;
  update_scale (view);
}

/**
* champlain_view_set_zoom_on_double_click:
* @view: a #ChamplainView
* @value: a #gboolean
*
* Should the view zoom in and recenter when the user double click on the map.
*
* Since: 0.4
*/
void
champlain_view_set_zoom_on_double_click (ChamplainView *view,
    gboolean value)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));

  ChamplainViewPrivate *priv = view->priv;

  priv->zoom_on_double_click = value;
}

/**
 * champlain_view_ensure_visible:
 * @view: a #ChamplainView
 * @lat1: the latitude of position 1
 * @lon1: the longitude of position 1
 * @lat2: the latitude of position 2
 * @lon2: the longitude of position 2
 * @animate: a #gboolean
 *
 * Changes the map's zoom level and center to make sure the two given
 * positions are visible
 *
 * Since: 0.4
 */
void
champlain_view_ensure_visible (ChamplainView *view,
    gdouble lat1,
    gdouble lon1,
    gdouble lat2,
    gdouble lon2,
    gboolean animate)
{
  ChamplainViewPrivate *priv = view->priv;
  gint zoom_level = priv->zoom_level;
  gdouble width, height;
  gdouble min_lat,min_lon,max_lat,max_lon;
  gboolean good_size = FALSE;

  /*We first sort the lat,lon in order to have min and max */
  if (lat1 < lat2)
    {
      min_lat = lat1;
      max_lat = lat2;
    }
  else
    {
      max_lat = lat1;
      min_lat = lat2;
    }

  if (lon1 < lon2)
    {
      min_lon = lon1;
      max_lon = lon2;
    }
  else
    {
      max_lon = lon1;
      min_lon = lon2;
    }

  width = max_lon - min_lon;
  height = max_lat - min_lat;
  width *= 1.1;
  height *= 1.1;

  DEBUG("Zone to expose (%f, %f) to (%f, %f)", min_lat, min_lon, max_lat, max_lon);
  do
    {
      gint min_x, min_y, max_x, max_y;
      min_x = champlain_map_source_get_x (priv->map_source, zoom_level, min_lon);
      min_y = champlain_map_source_get_y (priv->map_source, zoom_level, min_lat);

      max_x = champlain_map_source_get_x (priv->map_source, zoom_level, max_lon);
      max_y = champlain_map_source_get_y (priv->map_source, zoom_level, max_lat);

      if (min_y - max_y <= priv->viewport_size.height &&
          max_x - min_x <= priv->viewport_size.width)
        good_size = TRUE;
      else
        zoom_level--;

      if (zoom_level <= priv->min_zoom_level)
        break;
    }
  while (good_size == FALSE);

  if (good_size == FALSE)
    {
      zoom_level = priv->min_zoom_level;
      min_lat = min_lon = width = height = 0;
    }

  DEBUG ("Ideal zoom level is %d", zoom_level);
  champlain_view_set_zoom_level (view, zoom_level);
  if (animate)
    champlain_view_go_to (view, min_lat + height / 2.0, min_lon + width / 2.0);
  else
    champlain_view_center_on (view, min_lat + height / 2.0, min_lon + width / 2.0);
}

/**
 * champlain_view_ensure_markers_visible:
 * @view: a #ChamplainView
 * @markers: a NULL terminated array of #ChamplainMarkers
 * @animate: a #gboolean
 *
 * Changes the map's zoom level and center to make sure those markers are
 * visible.
 *
 * FIXME: This doesn't take into account the marker's actor size yet
 *
 * Since: 0.4
 */
void
champlain_view_ensure_markers_visible (ChamplainView *view,
    ChamplainBaseMarker *markers[],
    gboolean animate)
{
  gdouble min_lat, min_lon, max_lat, max_lon;
  ChamplainBaseMarker *marker = NULL;
  gint i = 0;

  min_lat = min_lon = 200;
  max_lat = max_lon = -200;

  marker = markers[i];
  while (marker != NULL)
    {
      gdouble lat, lon;
      g_object_get (G_OBJECT (marker), "latitude", &lat, "longitude", &lon,
          NULL);

      if (lon < min_lon)
        min_lon = lon;

      if (lat < min_lat)
        min_lat = lat;

      if (lon > max_lon)
        max_lon = lon;

      if (lat > max_lat)
        max_lat = lat;

      marker = markers[i++];
    }
  champlain_view_ensure_visible (view, min_lat, min_lon, max_lat, max_lon, animate);
}

/* Sets the zoom level, leaving the (x, y) at the exact same point in the view */
static gboolean
view_set_zoom_level_at (ChamplainView *view,
    gint zoom_level,
    gint x,
    gint y)
{
  ChamplainViewPrivate *priv = view->priv;

  ClutterActor *group, *new_group;
  gdouble lon, lat;
  gint x_diff, y_diff;
  gfloat actor_x, actor_y;
  gdouble rel_x, rel_y;
  gfloat x2, y2;
  gdouble lat2, lon2;

  if (zoom_level == priv->zoom_level || ZOOM_LEVEL_OUT_OF_RANGE(priv, zoom_level))
    return FALSE;

  champlain_view_stop_go_to (view);

  group = champlain_zoom_level_get_actor (priv->map->current_level);
  clutter_actor_get_transformed_position (priv->finger_scroll, &actor_x, &actor_y);
  rel_x = x - actor_x;
  rel_y = y - actor_y;

  /* Keep the lon, lat where the mouse is */
  lon = viewport_get_longitude_at (priv,
    priv->viewport_size.x + rel_x + priv->anchor.x);
  lat = viewport_get_latitude_at (priv,
    priv->viewport_size.y + rel_y + priv->anchor.y);

  /* How far was it from the center of the viewport (in px) */
  x_diff = priv->viewport_size.width / 2 - rel_x;
  y_diff = priv->viewport_size.height / 2 - rel_y;

  if (!map_zoom_to (priv->map, priv->map_source, zoom_level))
    return FALSE;

  priv->zoom_level = zoom_level;
  new_group = champlain_zoom_level_get_actor (priv->map->current_level);

  /* Get the new x,y in the new zoom level */
  x2 = champlain_map_source_get_x (priv->map_source, priv->zoom_level, lon);
  y2 = champlain_map_source_get_y (priv->map_source, priv->zoom_level, lat);
  /* Get the new lon,lat of these new x,y minus the distance from the
   * viewport center */
  lon2 = champlain_map_source_get_longitude (priv->map_source,
      priv->zoom_level, x2 + x_diff);
  lat2 = champlain_map_source_get_latitude (priv->map_source,
      priv->zoom_level, y2 + y_diff);

  resize_viewport (view);
  clutter_container_remove_actor (CLUTTER_CONTAINER (priv->map_layer),
      group);
  clutter_container_add_actor (CLUTTER_CONTAINER (priv->map_layer),
      new_group);
  champlain_view_center_on (view, lat2, lon2);

  g_object_notify (G_OBJECT (view), "zoom-level");
  return TRUE;
}

/**
 * champlain_view_get_zoom_level:
 * @view: The view
 *
 * Returns: the view's current zoom level.
 *
 * Since: 0.4
 */
gint
champlain_view_get_zoom_level (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0);

  ChamplainViewPrivate *priv = view->priv;

  return priv->zoom_level;
}

/**
 * champlain_view_get_min_zoom_level:
 * @view: The view
 *
 * Returns: the view's minimal zoom level allowed.
 *
 * Since: 0.4
 */
gint
champlain_view_get_min_zoom_level (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0);

  ChamplainViewPrivate *priv = view->priv;

  return priv->min_zoom_level;
}

/**
 * champlain_view_get_max_zoom_level:
 * @view: The view
 *
 * Returns: the view's maximal zoom level allowed.
 *
 * Since: 0.4
 */
gint
champlain_view_get_max_zoom_level (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0);

  ChamplainViewPrivate *priv = view->priv;

  return priv->max_zoom_level;
}

/**
 * champlain_view_get_map_source:
 * @view: The view
 *
 * Returns: the view's current map source. If you need to keep a reference to the
 * map source then you have to call #g_object_ref.
 *
 * Since: 0.4
 */
ChamplainMapSource*
champlain_view_get_map_source (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), NULL);

  ChamplainViewPrivate *priv = view->priv;

  return priv->map_source;
}

/**
 * champlain_view_get_decel_rate:
 * @view: The view
 *
 * Returns: the view's deceleration rate.
 *
 * Since: 0.4
 */
gdouble
champlain_view_get_decel_rate (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), 0.0);

  ChamplainViewPrivate *priv = view->priv;
  gdouble decel = 0.0;
  g_object_get (priv->finger_scroll, "decel-rate", &decel, NULL);
  return decel;
}

/**
 * champlain_view_get_scroll_mode:
 * @view: The view
 *
 * Returns: the view's scroll mode behaviour.
 *
 * Since: 0.4
 */
ChamplainScrollMode
champlain_view_get_scroll_mode (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), CHAMPLAIN_SCROLL_MODE_PUSH);

  ChamplainViewPrivate *priv = view->priv;
  return priv->scroll_mode;
}

/**
 * champlain_view_get_keep_center_on_resize:
 * @view: The view
 *
 * Returns: TRUE if the view keeps the center or resize, FALSE otherwise.
 *
 * Since: 0.4
 */
gboolean
champlain_view_get_keep_center_on_resize (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);

  ChamplainViewPrivate *priv = view->priv;
  return priv->keep_center_on_resize;
}

/**
 * champlain_view_get_show_license:
 * @view: The view
 *
 * Returns: TRUE if the view displays the license, FALSE otherwise.
 *
 * Since: 0.4
 */
gboolean
champlain_view_get_show_license (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);

  ChamplainViewPrivate *priv = view->priv;
  return priv->show_license;
}

/**
 * champlain_view_get_license_text:
 * @view: The view
 *
 * Returns: the additional license text
 *
 * Since: 0.4.3
 */
const gchar *
champlain_view_get_license_text (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);

  ChamplainViewPrivate *priv = view->priv;
  return priv->license_text;
}


/**
 * champlain_view_get_show_scale:
 * @view: The view
 *
 * Returns: TRUE if the view displays the scale, FALSE otherwise.
 *
 * Since: 0.4.3
 */
gboolean
champlain_view_get_show_scale (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);

  ChamplainViewPrivate *priv = view->priv;
  return priv->show_scale;
}

/**
 * champlain_view_get_max_scale_width:
 * @view: The view
 *
 * Returns: The max scale width in pixels.
 *
 * Since: 0.4.3
 */
guint
champlain_view_get_max_scale_width (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);

  ChamplainViewPrivate *priv = view->priv;
  return priv->max_scale_width;
}

/**
 * champlain_view_get_scale_unit:
 * @view: The view
 *
 * Returns: The unit used by the scale
 *
 * Since: 0.4.3
 */
ChamplainUnit
champlain_view_get_scale_unit (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);

  ChamplainViewPrivate *priv = view->priv;
  return priv->scale_unit;
}

/**
 * champlain_view_get_zoom_on_double_click:
 * @view: The view
 *
 * Returns: TRUE if the view zooms on double click, FALSE otherwise.
 *
 * Since: 0.4
 */
gboolean
champlain_view_get_zoom_on_double_click (ChamplainView *view)
{
  g_return_val_if_fail (CHAMPLAIN_IS_VIEW (view), FALSE);

  ChamplainViewPrivate *priv = view->priv;
  return priv->zoom_on_double_click;
}

static void
view_update_polygons (ChamplainView *view)
{
  ChamplainViewPrivate *priv = view->priv;
  GList *polygons;
  gfloat x, y;

  if (priv->polygons == NULL)
    return;

  polygons = priv->polygons;
  while (polygons != NULL)
    {
      ChamplainPolygon *polygon;

      polygon = CHAMPLAIN_POLYGON (polygons->data);
      draw_polygon (view, polygon);
      polygons = polygons->next;
    }

  /* Position the layer in the viewport */
  x = priv->viewport_size.x;
  y = priv->viewport_size.y;

  clutter_actor_set_position (priv->polygon_layer, x, y);
}

/**
 * champlain_view_add_polygon:
 * @view: a #ChamplainView
 * @polygon: a #ChamplainPolygon
 *
 * Adds a #ChamplainPolygon to the #ChamplainView
 *
 * Since: 0.4
 */
void
champlain_view_add_polygon (ChamplainView *view,
    ChamplainPolygon *polygon)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));
  g_return_if_fail (CHAMPLAIN_IS_POLYGON (polygon));

  view->priv->polygons = g_list_append (view->priv->polygons, g_object_ref_sink (polygon));

  g_signal_connect (polygon, "notify",
      G_CALLBACK (notify_polygon_cb), view);

  if (view->priv->viewport_size.width == 0 ||
      view->priv->viewport_size.height == 0)
  {
    polygon->priv->actor = NULL;
    return;
  }

  polygon->priv->actor = g_object_ref (clutter_cairo_texture_new (
      view->priv->viewport_size.width,
      view->priv->viewport_size.height));
  g_object_set (G_OBJECT (polygon->priv->actor), "visible",
      polygon->priv->visible, NULL);
  clutter_actor_set_position (polygon->priv->actor, 0, 0);
  clutter_container_add_actor (CLUTTER_CONTAINER (view->priv->polygon_layer),
      polygon->priv->actor);
}

/**
 * champlain_view_remove_polygon:
 * @view: a #ChamplainView
 * @polygon: a #ChamplainPolygon
 *
 * Removes a #ChamplainPolygon from #ChamplainView
 *
 * Since: 0.4
 */
void
champlain_view_remove_polygon (ChamplainView *view,
    ChamplainPolygon *polygon)
{
  g_return_if_fail (CHAMPLAIN_IS_VIEW (view));
  g_return_if_fail (CHAMPLAIN_IS_POLYGON (polygon));

  view->priv->polygons = g_list_remove (view->priv->polygons, polygon);

  if (polygon->priv->actor != NULL)
    clutter_container_remove_actor (CLUTTER_CONTAINER (view->priv->polygon_layer),
        polygon->priv->actor);

  g_object_unref (polygon);
}