summaryrefslogtreecommitdiff
path: root/gtk/gtkcalendar.c
blob: 9c36354969f13c690567e4df1b5153aeda5ad0d0 (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
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * GTK Calendar Widget
 * Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson and Mattias Groenlund
 * 
 * lib_date routines
 * Copyright (c) 1995, 1996, 1997, 1998 by Steffen Beyer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include <config.h>

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE__NL_TIME_FIRST_WEEKDAY
#include <langinfo.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <glib/gprintf.h>

#undef GTK_DISABLE_DEPRECATED
#include "gtkcalendar.h"
#define GTK_DISABLE_DEPRECATED

#include "gtkdnd.h"
#include "gtkintl.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkintl.h"
#include "gdk/gdkkeysyms.h"
#include "gtkalias.h"

/***************************************************************************/
/* The following date routines are taken from the lib_date package.  Keep
 * them separate in case we want to update them if a newer lib_date comes
 * out with fixes.  */

typedef	 unsigned   int	    N_int;
typedef	 unsigned   long    N_long;
typedef	 signed	    long    Z_long;
typedef enum { false = FALSE , true = TRUE } boolean;

#define and	    &&	    /* logical (boolean) operators: lower case */
#define or	    ||

static const N_int month_length[2][13] =
{
  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};

static const N_int days_in_months[2][14] =
{
  { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
};

static Z_long  calc_days(N_int year, N_int mm, N_int dd);
static N_int   day_of_week(N_int year, N_int mm, N_int dd);
static Z_long  dates_difference(N_int year1, N_int mm1, N_int dd1,
				N_int year2, N_int mm2, N_int dd2);
static N_int   weeks_in_year(N_int year);

static boolean 
leap(N_int year)
{
  return((((year % 4) == 0) and ((year % 100) != 0)) or ((year % 400) == 0));
}

static N_int 
day_of_week(N_int year, N_int mm, N_int dd)
{
  Z_long  days;
  
  days = calc_days(year, mm, dd);
  if (days > 0L)
    {
      days--;
      days %= 7L;
      days++;
    }
  return( (N_int) days );
}

static N_int weeks_in_year(N_int year)
{
  return(52 + ((day_of_week(year,1,1)==4) or (day_of_week(year,12,31)==4)));
}

static boolean 
check_date(N_int year, N_int mm, N_int dd)
{
  if (year < 1) return(false);
  if ((mm < 1) or (mm > 12)) return(false);
  if ((dd < 1) or (dd > month_length[leap(year)][mm])) return(false);
  return(true);
}

static N_int 
week_number(N_int year, N_int mm, N_int dd)
{
  N_int first;
  
  first = day_of_week(year,1,1) - 1;
  return( (N_int) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
	  (first < 4) );
}

static Z_long 
year_to_days(N_int year)
{
  return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
}


static Z_long 
calc_days(N_int year, N_int mm, N_int dd)
{
  boolean lp;
  
  if (year < 1) return(0L);
  if ((mm < 1) or (mm > 12)) return(0L);
  if ((dd < 1) or (dd > month_length[(lp = leap(year))][mm])) return(0L);
  return( year_to_days(--year) + days_in_months[lp][mm] + dd );
}

static boolean 
week_of_year(N_int *week, N_int *year, N_int mm, N_int dd)
{
  if (check_date(*year,mm,dd))
    {
      *week = week_number(*year,mm,dd);
      if (*week == 0) 
	*week = weeks_in_year(--(*year));
      else if (*week > weeks_in_year(*year))
	{
	  *week = 1;
	  (*year)++;
	}
      return(true);
    }
  return(false);
}

static Z_long 
dates_difference(N_int year1, N_int mm1, N_int dd1,
		 N_int year2, N_int mm2, N_int dd2)
{
  return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
}

/*** END OF lib_date routines ********************************************/

/* Spacing around day/week headers and main area, inside those windows */
#define CALENDAR_MARGIN		 0
/* Spacing around day/week headers and main area, outside those windows */
#define INNER_BORDER		 4
/* Separation between day headers and main area */
#define CALENDAR_YSEP		 4
/* Separation between week headers and main area */
#define CALENDAR_XSEP		 4

#define DAY_XSEP		 0 /* not really good for small calendar */
#define DAY_YSEP		 0 /* not really good for small calendar */

/* Color usage */
#define HEADER_FG_COLOR(widget)		 (& (widget)->style->fg[GTK_WIDGET_STATE (widget)])
#define HEADER_BG_COLOR(widget)		 (& (widget)->style->bg[GTK_WIDGET_STATE (widget)])
#define SELECTED_BG_COLOR(widget)	 (& (widget)->style->base[GTK_WIDGET_HAS_FOCUS (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
#define SELECTED_FG_COLOR(widget)	 (& (widget)->style->text[GTK_WIDGET_HAS_FOCUS (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
#define NORMAL_DAY_COLOR(widget)	 (& (widget)->style->fg[GTK_WIDGET_STATE (widget)])
#define PREV_MONTH_COLOR(widget)	 (& (widget)->style->mid[GTK_WIDGET_STATE (widget)])
#define NEXT_MONTH_COLOR(widget)	 (& (widget)->style->mid[GTK_WIDGET_STATE (widget)])
#define MARKED_COLOR(widget)		 (& (widget)->style->fg[GTK_WIDGET_STATE (widget)])
#define BACKGROUND_COLOR(widget)	 (& (widget)->style->base[GTK_WIDGET_STATE (widget)])
#define HIGHLIGHT_BACK_COLOR(widget)	 (& (widget)->style->mid[GTK_WIDGET_STATE (widget)])

enum {
  ARROW_YEAR_LEFT,
  ARROW_YEAR_RIGHT,
  ARROW_MONTH_LEFT,
  ARROW_MONTH_RIGHT
};

enum {
  MONTH_PREV,
  MONTH_CURRENT,
  MONTH_NEXT
};

enum {
  MONTH_CHANGED_SIGNAL,
  DAY_SELECTED_SIGNAL,
  DAY_SELECTED_DOUBLE_CLICK_SIGNAL,
  PREV_MONTH_SIGNAL,
  NEXT_MONTH_SIGNAL,
  PREV_YEAR_SIGNAL,
  NEXT_YEAR_SIGNAL,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_YEAR,
  PROP_MONTH,
  PROP_DAY,
  PROP_SHOW_HEADING,
  PROP_SHOW_DAY_NAMES,
  PROP_NO_MONTH_CHANGE,
  PROP_SHOW_WEEK_NUMBERS,
  PROP_LAST
};

static guint gtk_calendar_signals[LAST_SIGNAL] = { 0 };

struct _GtkCalendarPrivate
{
  GdkWindow *header_win;
  GdkWindow *day_name_win;
  GdkWindow *main_win;
  GdkWindow *week_win;
  GdkWindow *arrow_win[4];

  guint header_h;
  guint day_name_h;
  guint main_h;

  guint	     arrow_state[4];
  guint	     arrow_width;
  guint	     max_month_width;
  guint	     max_year_width;
  
  guint day_width;
  guint week_width;

  guint min_day_width;
  guint max_day_char_width;
  guint max_day_char_ascent;
  guint max_day_char_descent;
  guint max_label_char_ascent;
  guint max_label_char_descent;
  guint max_week_char_width;
  
  /* flags */
  guint year_before : 1;

  guint need_timer  : 1;

  guint in_drag : 1;
  guint drag_highlight : 1;

  guint32 timer;
  gint click_child;

  gint week_start;

  gint drag_start_x;
  gint drag_start_y;
};

#define GTK_CALENDAR_GET_PRIVATE(widget)  (GTK_CALENDAR (widget)->priv)

static void gtk_calendar_finalize     (GObject      *calendar);
static void gtk_calendar_destroy      (GtkObject    *calendar);
static void gtk_calendar_set_property (GObject      *object,
				       guint         prop_id,
				       const GValue *value,
				       GParamSpec   *pspec);
static void gtk_calendar_get_property (GObject      *object,
				       guint         prop_id,
				       GValue       *value,
				       GParamSpec   *pspec);

static void     gtk_calendar_realize        (GtkWidget        *widget);
static void     gtk_calendar_unrealize      (GtkWidget        *widget);
static void     gtk_calendar_size_request   (GtkWidget        *widget,
					     GtkRequisition   *requisition);
static void     gtk_calendar_size_allocate  (GtkWidget        *widget,
					     GtkAllocation    *allocation);
static gboolean gtk_calendar_expose         (GtkWidget        *widget,
					     GdkEventExpose   *event);
static gboolean gtk_calendar_button_press   (GtkWidget        *widget,
					     GdkEventButton   *event);
static gboolean gtk_calendar_button_release (GtkWidget        *widget,
					     GdkEventButton   *event);
static gboolean gtk_calendar_motion_notify  (GtkWidget        *widget,
					     GdkEventMotion   *event);
static gboolean gtk_calendar_enter_notify   (GtkWidget        *widget,
					     GdkEventCrossing *event);
static gboolean gtk_calendar_leave_notify   (GtkWidget        *widget,
					     GdkEventCrossing *event);
static gboolean gtk_calendar_scroll         (GtkWidget        *widget,
					     GdkEventScroll   *event);
static gboolean gtk_calendar_key_press      (GtkWidget        *widget,
					     GdkEventKey      *event);
static gboolean gtk_calendar_focus_out      (GtkWidget        *widget,
					     GdkEventFocus    *event);
static void     gtk_calendar_grab_notify    (GtkWidget        *widget,
					     gboolean          was_grabbed);
static void     gtk_calendar_state_changed  (GtkWidget        *widget,
					     GtkStateType      previous_state);
static void     gtk_calendar_style_set      (GtkWidget        *widget,
					     GtkStyle         *previous_style);

static void     gtk_calendar_drag_data_get      (GtkWidget        *widget,
						 GdkDragContext   *context,
						 GtkSelectionData *selection_data,
						 guint             info,
						 guint             time);
static void     gtk_calendar_drag_data_received (GtkWidget        *widget,
						 GdkDragContext   *context,
						 gint              x,
						 gint              y,
						 GtkSelectionData *selection_data,
						 guint             info,
						 guint             time);
static gboolean gtk_calendar_drag_motion        (GtkWidget        *widget,
						 GdkDragContext   *context,
						 gint              x,
						 gint              y,
						 guint             time);
static void     gtk_calendar_drag_leave         (GtkWidget        *widget,
						 GdkDragContext   *context,
						 guint             time);
static gboolean gtk_calendar_drag_drop          (GtkWidget        *widget,
						 GdkDragContext   *context,
						 gint              x,
						 gint              y,
						 guint             time);

static void calendar_start_spinning (GtkCalendar *calendar,
				     gint         click_child);
static void calendar_stop_spinning  (GtkCalendar *calendar);

static void calendar_invalidate_day     (GtkCalendar *widget,
					 gint       row,
					 gint       col);
static void calendar_invalidate_day_num (GtkCalendar *widget,
					 gint       day);
static void calendar_invalidate_arrow   (GtkCalendar *widget,
					 guint      arrow);

static void calendar_compute_days      (GtkCalendar *calendar);
     
static char    *default_abbreviated_dayname[7];
static char    *default_monthname[12];

G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)

static void
gtk_calendar_class_init (GtkCalendarClass *class)
{
  GObjectClass   *gobject_class;
  GtkObjectClass   *object_class;
  GtkWidgetClass *widget_class;

  gobject_class = (GObjectClass*)  class;
  object_class = (GtkObjectClass*)  class;
  widget_class = (GtkWidgetClass*) class;
  
  gobject_class->set_property = gtk_calendar_set_property;
  gobject_class->get_property = gtk_calendar_get_property;
  gobject_class->finalize = gtk_calendar_finalize;

  object_class->destroy = gtk_calendar_destroy;

  widget_class->realize = gtk_calendar_realize;
  widget_class->unrealize = gtk_calendar_unrealize;
  widget_class->expose_event = gtk_calendar_expose;
  widget_class->size_request = gtk_calendar_size_request;
  widget_class->size_allocate = gtk_calendar_size_allocate;
  widget_class->button_press_event = gtk_calendar_button_press;
  widget_class->button_release_event = gtk_calendar_button_release;
  widget_class->motion_notify_event = gtk_calendar_motion_notify;
  widget_class->enter_notify_event = gtk_calendar_enter_notify;
  widget_class->leave_notify_event = gtk_calendar_leave_notify;
  widget_class->key_press_event = gtk_calendar_key_press;
  widget_class->scroll_event = gtk_calendar_scroll;
  widget_class->style_set = gtk_calendar_style_set;
  widget_class->state_changed = gtk_calendar_state_changed;
  widget_class->grab_notify = gtk_calendar_grab_notify;
  widget_class->focus_out_event = gtk_calendar_focus_out;

  widget_class->drag_data_get = gtk_calendar_drag_data_get;
  widget_class->drag_motion = gtk_calendar_drag_motion;
  widget_class->drag_leave = gtk_calendar_drag_leave;
  widget_class->drag_drop = gtk_calendar_drag_drop;
  widget_class->drag_data_received = gtk_calendar_drag_data_received;
  
  g_object_class_install_property (gobject_class,
                                   PROP_YEAR,
                                   g_param_spec_int ("year",
						     P_("Year"),
						     P_("The selected year"),
						     0, G_MAXINT, 0,
						     GTK_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_MONTH,
                                   g_param_spec_int ("month",
						     P_("Month"),
						     P_("The selected month (as a number between 0 and 11)"),
						     0, 11, 0,
						     GTK_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_DAY,
                                   g_param_spec_int ("day",
						     P_("Day"),
						     P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
						     0, 31, 0,
						     GTK_PARAM_READWRITE));

/**
 * GtkCalendar:show-heading:
 *
 * Determines whether a heading is displayed.
 *
 * Since: 2.4
 */
  g_object_class_install_property (gobject_class,
                                   PROP_SHOW_HEADING,
                                   g_param_spec_boolean ("show-heading",
							 P_("Show Heading"),
							 P_("If TRUE, a heading is displayed"),
							 TRUE,
							 GTK_PARAM_READWRITE));

/**
 * GtkCalendar:show-day-names:
 *
 * Determines whether day names are displayed.
 *
 * Since: 2.4
 */
  g_object_class_install_property (gobject_class,
                                   PROP_SHOW_DAY_NAMES,
                                   g_param_spec_boolean ("show-day-names",
							 P_("Show Day Names"),
							 P_("If TRUE, day names are displayed"),
							 TRUE,
							 GTK_PARAM_READWRITE));
/**
 * GtkCalendar:no-month-change:
 *
 * Determines whether the selected month can be changed.
 *
 * Since: 2.4
 */
  g_object_class_install_property (gobject_class,
                                   PROP_NO_MONTH_CHANGE,
                                   g_param_spec_boolean ("no-month-change",
							 P_("No Month Change"),
							 P_("If TRUE, the selected month cannot be changed"),
							 FALSE,
							 GTK_PARAM_READWRITE));

/**
 * GtkCalendar:show-week-numbers:
 *
 * Determines whether week numbers are displayed.
 *
 * Since: 2.4
 */
  g_object_class_install_property (gobject_class,
                                   PROP_SHOW_WEEK_NUMBERS,
                                   g_param_spec_boolean ("show-week-numbers",
							 P_("Show Week Numbers"),
							 P_("If TRUE, week numbers are displayed"),
							 FALSE,
							 GTK_PARAM_READWRITE));

  gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
    g_signal_new ("month_changed",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
    g_signal_new ("day_selected",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
    g_signal_new ("day_selected_double_click",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  gtk_calendar_signals[PREV_MONTH_SIGNAL] =
    g_signal_new ("prev_month",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
    g_signal_new ("next_month",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCalendarClass, next_month),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  gtk_calendar_signals[PREV_YEAR_SIGNAL] =
    g_signal_new ("prev_year",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
    g_signal_new ("next_year",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkCalendarClass, next_year),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  
  g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
}

static void
gtk_calendar_init (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  time_t secs;
  struct tm *tm;
  gint i;
  char buffer[255];
  time_t tmp_time;
  GtkCalendarPrivate *priv;
  gchar *year_before;
  gchar *week_start;
  
  priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
						       GTK_TYPE_CALENDAR,
						       GtkCalendarPrivate);

  GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
  
  if (!default_abbreviated_dayname[0])
    for (i=0; i<7; i++)
      {
	tmp_time= (i+3)*86400;
	strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
	default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
      }
  
  if (!default_monthname[0])
    for (i=0; i<12; i++)
      {
	tmp_time=i*2764800;
	strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
	default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
      }
  
  /* Set defaults */
  secs = time (NULL);
  tm = localtime (&secs);
  calendar->month = tm->tm_mon;
  calendar->year  = 1900 + tm->tm_year;

  calendar_compute_days (calendar);

  for (i=0;i<31;i++)
    calendar->marked_date[i] = FALSE;
  calendar->selected_day = tm->tm_mday;
  
  calendar->display_flags = ( GTK_CALENDAR_SHOW_HEADING | 
			      GTK_CALENDAR_SHOW_DAY_NAMES );
  
  calendar->highlight_row = -1;
  calendar->highlight_col = -1;
  
  calendar->focus_row = -1;
  calendar->focus_col = -1;

  priv->max_year_width = 0;
  priv->max_month_width = 0;
  priv->max_day_char_width = 0;
  priv->max_week_char_width = 0;

  priv->max_day_char_ascent = 0;
  priv->max_day_char_descent = 0;
  priv->max_label_char_ascent = 0;
  priv->max_label_char_descent = 0;

  priv->arrow_width = 10;

  priv->need_timer = 0;
  priv->timer = 0;
  priv->click_child = -1;

  priv->in_drag = 0;
  priv->drag_highlight = 0;

  gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
  gtk_drag_dest_add_text_targets (widget);

  priv->year_before = 0;

  /* Translate to calendar:YM if you want years to be displayed
   * before months; otherwise translate to calendar:MY.
   * Do *not* translate it to anything else, if it
   * it isn't calendar:YM or calendar:MY it will not work.
   *
   * Note that this flipping is in top the text direction flipping,
   * so if you have a default text direction of RTL and YM, then
   * the year will appear on the right.
   */
  year_before = _("calendar:MY");
  if (strcmp (year_before, "calendar:YM") == 0)
    priv->year_before = 1;
  else if (strcmp (year_before, "calendar:MY") != 0)
    g_warning ("Whoever translated calendar:MY did so wrongly.\n");

#ifdef HAVE__NL_TIME_FIRST_WEEKDAY
  week_start = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
  priv->week_start = *((unsigned char *) week_start) % 7;
#else
  /* Translate to calendar:week_start:0 if you want Sunday to be the
   * first day of the week to calendar:week_start:1 if you want Monday
   * to be the first day of the week, and so on.
   */  
  week_start = _("calendar:week_start:0");

  if (strncmp (week_start, "calendar:week_start:", 20) == 0)
    priv->week_start = *(week_start + 20) - '0';
  else 
    priv->week_start = -1;
  
  if (priv->week_start < 0 || priv->week_start > 6)
    {
      g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
      priv->week_start = 0;
    }
#endif
}


/****************************************
 *          Utility Functions           *
 ****************************************/

static void
calendar_set_month_next (GtkCalendar *calendar)
{
  gint month_len;
  
  g_return_if_fail (GTK_IS_WIDGET (calendar));
  
  if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
    return;
  
  
  if (calendar->month == 11)
    {
      calendar->month = 0;
      calendar->year++;
    } 
  else 
    calendar->month++;
  
  calendar_compute_days (calendar);
  g_signal_emit (calendar,
		 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
		 0);
  g_signal_emit (calendar,
		 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
		 0);
  
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
  
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    gtk_calendar_select_day (calendar, calendar->selected_day);

  gtk_widget_queue_draw (GTK_WIDGET (calendar));
}

static void
calendar_set_year_prev (GtkCalendar *calendar)
{
  gint month_len;
  
  g_return_if_fail (GTK_IS_WIDGET (calendar));
  
  calendar->year--;
  calendar_compute_days (calendar);
  g_signal_emit (calendar,
		 gtk_calendar_signals[PREV_YEAR_SIGNAL],
		 0);
  g_signal_emit (calendar,
		 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
		 0);
  
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
  
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    gtk_calendar_select_day (calendar, calendar->selected_day);
  
  gtk_widget_queue_draw (GTK_WIDGET (calendar));
}

static void
calendar_set_year_next (GtkCalendar *calendar)
{
  gint month_len;
  GtkWidget *widget;
  
  g_return_if_fail (GTK_IS_WIDGET (calendar));
  
  widget = GTK_WIDGET (calendar);
  
  calendar->year++;
  calendar_compute_days (calendar);
  g_signal_emit (calendar,
		 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
		 0);
  g_signal_emit (calendar,
		 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
		 0);
  
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
  
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    gtk_calendar_select_day (calendar, calendar->selected_day);
  
  gtk_widget_queue_draw (GTK_WIDGET (calendar));
}

static void
calendar_compute_days (GtkCalendar *calendar)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (GTK_WIDGET (calendar));
  gint month;
  gint year;
  gint ndays_in_month;
  gint ndays_in_prev_month;
  gint first_day;
  gint row;
  gint col;
  gint day;

  g_return_if_fail (GTK_IS_CALENDAR (calendar));

  year = calendar->year;
  month = calendar->month + 1;
  
  ndays_in_month = month_length[leap (year)][month];
  
  first_day = day_of_week (year, month, 1);
  first_day = (first_day + 7 - priv->week_start) % 7;
  
  /* Compute days of previous month */
  if (month > 1)
    ndays_in_prev_month = month_length[leap (year)][month-1];
  else
    ndays_in_prev_month = month_length[leap (year)][12];
  day = ndays_in_prev_month - first_day + 1;
  
  row = 0;
  if (first_day > 0)
    {
      for (col = 0; col < first_day; col++)
	{
	  calendar->day[row][col] = day;
	  calendar->day_month[row][col] = MONTH_PREV;
	  day++;
	}
    }
  
  /* Compute days of current month */
  col = first_day;
  for (day = 1; day <= ndays_in_month; day++)
    {
      calendar->day[row][col] = day;
      calendar->day_month[row][col] = MONTH_CURRENT;
      
      col++;
      if (col == 7)
	{
	  row++;
	  col = 0;
	}
    }
  
  /* Compute days of next month */
  day = 1;
  for (; row <= 5; row++)
    {
      for (; col <= 6; col++)
	{
	  calendar->day[row][col] = day;
	  calendar->day_month[row][col] = MONTH_NEXT;
	  day++;
	}
      col = 0;
    }
}

static void
calendar_select_and_focus_day (GtkCalendar *calendar,
			       guint        day)
{
  gint old_focus_row = calendar->focus_row;
  gint old_focus_col = calendar->focus_col;
  gint row;
  gint col;
  
  for (row = 0; row < 6; row ++)
    for (col = 0; col < 7; col++)
      {
	if (calendar->day_month[row][col] == MONTH_CURRENT 
	    && calendar->day[row][col] == day)
	  {
	    calendar->focus_row = row;
	    calendar->focus_col = col;
	  }
      }

  if (old_focus_row != -1 && old_focus_col != -1)
    calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
  
  gtk_calendar_select_day (calendar, day);
}


/****************************************
 *     Layout computation utilities     *
 ****************************************/

static gint
calendar_row_height (GtkCalendar *calendar)
{
  return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
	  - ((calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
	     ? CALENDAR_YSEP : CALENDAR_MARGIN)) / 6;
}


/* calendar_left_x_for_column: returns the x coordinate
 * for the left of the column */
static gint
calendar_left_x_for_column (GtkCalendar *calendar,
			    gint	 column)
{
  gint width;
  gint x_left;
  
  if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
    column = 6 - column;

  width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
  if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
    x_left = CALENDAR_XSEP + (width + DAY_XSEP) * column;
  else
    x_left = CALENDAR_MARGIN + (width + DAY_XSEP) * column;
  
  return x_left;
}

/* column_from_x: returns the column 0-6 that the
 * x pixel of the xwindow is in */
static gint
calendar_column_from_x (GtkCalendar *calendar,
			gint	     event_x)
{
  gint c, column;
  gint x_left, x_right;
  
  column = -1;
  
  for (c = 0; c < 7; c++)
    {
      x_left = calendar_left_x_for_column (calendar, c);
      x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
      
      if (event_x >= x_left && event_x < x_right)
	{
	  column = c;
	  break;
	}
    }
  
  return column;
}

/* calendar_top_y_for_row: returns the y coordinate
 * for the top of the row */
static gint
calendar_top_y_for_row (GtkCalendar *calendar,
			gint	     row)
{
  
  return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h 
	  - (CALENDAR_MARGIN + (6 - row)
	     * calendar_row_height (calendar)));
}

/* row_from_y: returns the row 0-5 that the
 * y pixel of the xwindow is in */
static gint
calendar_row_from_y (GtkCalendar *calendar,
		     gint	  event_y)
{
  gint r, row;
  gint height;
  gint y_top, y_bottom;
  
  height = calendar_row_height (calendar);
  row = -1;
  
  for (r = 0; r < 6; r++)
    {
      y_top = calendar_top_y_for_row (calendar, r);
      y_bottom = y_top + height;
      
      if (event_y >= y_top && event_y < y_bottom)
	{
	  row = r;
	  break;
	}
    }
  
  return row;
}

static void
calendar_arrow_rectangle (GtkCalendar  *calendar,
			  guint	        arrow,
			  GdkRectangle *rect)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  gboolean year_left;

  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
    year_left = priv->year_before;
  else
    year_left = !priv->year_before;
    
  rect->y = 3;
  rect->width = priv->arrow_width;
  rect->height = priv->header_h - 7;
  
  switch (arrow)
    {
    case ARROW_MONTH_LEFT:
      if (year_left) 
	rect->x = (widget->allocation.width - 2 * widget->style->xthickness
		   - (3 + 2*priv->arrow_width 
		      + priv->max_month_width));
      else
	rect->x = 3;
      break;
    case ARROW_MONTH_RIGHT:
      if (year_left) 
	rect->x = (widget->allocation.width - 2 * widget->style->xthickness 
		   - 3 - priv->arrow_width);
      else
	rect->x = (priv->arrow_width 
		   + priv->max_month_width);
      break;
    case ARROW_YEAR_LEFT:
      if (year_left) 
	rect->x = 3;
      else
	rect->x = (widget->allocation.width - 2 * widget->style->xthickness
		   - (3 + 2*priv->arrow_width 
		      + priv->max_year_width));
      break;
    case ARROW_YEAR_RIGHT:
      if (year_left) 
	rect->x = (priv->arrow_width 
		   + priv->max_year_width);
      else
	rect->x = (widget->allocation.width - 2 * widget->style->xthickness 
		   - 3 - priv->arrow_width);
      break;
    }
}

static void
calendar_day_rectangle (GtkCalendar  *calendar,
			gint          row,
			gint          col,
			GdkRectangle *rect)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);

  rect->x = calendar_left_x_for_column (calendar, col);
  rect->y = calendar_top_y_for_row (calendar, row);
  rect->height = calendar_row_height (calendar);
  rect->width = priv->day_width;
}

static void
calendar_set_month_prev (GtkCalendar *calendar)
{
  gint month_len;
  
  if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
    return;
  
  if (calendar->month == 0)
    {
      calendar->month = 11;
      calendar->year--;
    } 
  else 
    calendar->month--;
  
  month_len = month_length[leap (calendar->year)][calendar->month + 1];
  
  calendar_compute_days (calendar);
  
  g_signal_emit (calendar,
		 gtk_calendar_signals[PREV_MONTH_SIGNAL],
		 0);
  g_signal_emit (calendar,
		 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
		 0);
  
  if (month_len < calendar->selected_day)
    {
      calendar->selected_day = 0;
      gtk_calendar_select_day (calendar, month_len);
    }
  else
    {
      if (calendar->selected_day < 0)
	calendar->selected_day = calendar->selected_day + 1 + month_length[leap (calendar->year)][calendar->month + 1];
      gtk_calendar_select_day (calendar, calendar->selected_day);
    }

  gtk_widget_queue_draw (GTK_WIDGET (calendar));
}


/****************************************
 *           Basic object methods       *
 ****************************************/

static void
gtk_calendar_finalize (GObject *object)
{
  (* G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize) (object);
}

static void
gtk_calendar_destroy (GtkObject *object)
{
  calendar_stop_spinning (GTK_CALENDAR (object));
  
  GTK_OBJECT_CLASS (gtk_calendar_parent_class)->destroy (object);
}


static void
calendar_set_display_option (GtkCalendar              *calendar,
			     GtkCalendarDisplayOptions flag,
			     gboolean                  setting)
{
  GtkCalendarDisplayOptions flags;
  if (setting) 
    flags = calendar->display_flags | flag;
  else
    flags = calendar->display_flags & ~flag; 
  gtk_calendar_display_options (calendar, flags);
}

static gboolean
calendar_get_display_option (GtkCalendar              *calendar,
			     GtkCalendarDisplayOptions flag)
{
  return (calendar->display_flags & flag) != 0;
}

static void 
gtk_calendar_set_property (GObject      *object,
			   guint         prop_id,
			   const GValue *value,
			   GParamSpec   *pspec)
{
  GtkCalendar *calendar;

  calendar = GTK_CALENDAR (object);

  switch (prop_id) 
    {
    case PROP_YEAR:
      gtk_calendar_select_month (calendar,
				 calendar->month,
				 g_value_get_int (value));
      break;
    case PROP_MONTH:
      gtk_calendar_select_month (calendar,
				 g_value_get_int (value),
				 calendar->year);
      break;
    case PROP_DAY:
      gtk_calendar_select_day (calendar,
			       g_value_get_int (value));
      break;
    case PROP_SHOW_HEADING:
      calendar_set_display_option (calendar,
				   GTK_CALENDAR_SHOW_HEADING,
				   g_value_get_boolean (value));
      break;
    case PROP_SHOW_DAY_NAMES:
      calendar_set_display_option (calendar,
				   GTK_CALENDAR_SHOW_DAY_NAMES,
				   g_value_get_boolean (value));
      break;
    case PROP_NO_MONTH_CHANGE:
      calendar_set_display_option (calendar,
				   GTK_CALENDAR_NO_MONTH_CHANGE,
				   g_value_get_boolean (value));
      break;
    case PROP_SHOW_WEEK_NUMBERS:
      calendar_set_display_option (calendar,
				   GTK_CALENDAR_SHOW_WEEK_NUMBERS,
				   g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void 
gtk_calendar_get_property (GObject      *object,
			   guint         prop_id,
			   GValue       *value,
			   GParamSpec   *pspec)
{
  GtkCalendar *calendar;

  calendar = GTK_CALENDAR (object);

  switch (prop_id) 
    {
    case PROP_YEAR:
      g_value_set_int (value, calendar->year);
      break;
    case PROP_MONTH:
      g_value_set_int (value, calendar->month);
      break;
    case PROP_DAY:
      g_value_set_int (value, calendar->selected_day);
      break;
    case PROP_SHOW_HEADING:
      g_value_set_boolean (value, calendar_get_display_option (calendar,
							       GTK_CALENDAR_SHOW_HEADING));
      break;
    case PROP_SHOW_DAY_NAMES:
      g_value_set_boolean (value, calendar_get_display_option (calendar,
							       GTK_CALENDAR_SHOW_DAY_NAMES));
      break;
    case PROP_NO_MONTH_CHANGE:
      g_value_set_boolean (value, calendar_get_display_option (calendar,
							       GTK_CALENDAR_NO_MONTH_CHANGE));
      break;
    case PROP_SHOW_WEEK_NUMBERS:
      g_value_set_boolean (value, calendar_get_display_option (calendar,
							       GTK_CALENDAR_SHOW_WEEK_NUMBERS));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


/****************************************
 *             Realization              *
 ****************************************/

static void
calendar_realize_arrows (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  GdkWindowAttr attributes;
  gint attributes_mask;
  gint i;
  
  /* Arrow windows ------------------------------------- */
  if (! (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
      && (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING))
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
			       | GDK_BUTTON_PRESS_MASK	| GDK_BUTTON_RELEASE_MASK
			       | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
      for (i = 0; i < 4; i++)
	{
	  GdkRectangle rect;
	  calendar_arrow_rectangle (calendar, i, &rect);
	  
	  attributes.x = rect.x;
	  attributes.y = rect.y;
	  attributes.width = rect.width;
	  attributes.height = rect.width;
	  priv->arrow_win[i] = gdk_window_new (priv->header_win,
					       &attributes, 
					       attributes_mask);
	  if (GTK_WIDGET_IS_SENSITIVE (widget))
	    priv->arrow_state[i] = GTK_STATE_NORMAL;
	  else 
	    priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
	  gdk_window_set_background (priv->arrow_win[i],
				     HEADER_BG_COLOR (GTK_WIDGET (calendar)));
	  gdk_window_show (priv->arrow_win[i]);
	  gdk_window_set_user_data (priv->arrow_win[i], widget);
	}
    }
  else
    {
      for (i = 0; i < 4; i++)
	priv->arrow_win[i] = NULL;
    }
}

static void
calendar_realize_header (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  GdkWindowAttr attributes;
  gint attributes_mask;
  
  /* Header window ------------------------------------- */
  if (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING)
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
      attributes.x = widget->style->xthickness;
      attributes.y = widget->style->ythickness;
      attributes.width = widget->allocation.width - 2 * attributes.x;
      attributes.height = priv->header_h - 2 * attributes.y;
      priv->header_win = gdk_window_new (widget->window,
					 &attributes, attributes_mask);
      
      gdk_window_set_background (priv->header_win,
				 HEADER_BG_COLOR (GTK_WIDGET (calendar)));
      gdk_window_show (priv->header_win);
      gdk_window_set_user_data (priv->header_win, widget);
      
    }
  else
    {
      priv->header_win = NULL;
    }
  calendar_realize_arrows (calendar);
}

static void
calendar_realize_day_names (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  GdkWindowAttr attributes;
  gint attributes_mask;
  
  /* Day names	window --------------------------------- */
  if ( calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
      attributes.x = (widget->style->xthickness + INNER_BORDER);
      attributes.y = priv->header_h + (widget->style->ythickness 
					   + INNER_BORDER);
      attributes.width = (widget->allocation.width 
			  - (widget->style->xthickness + INNER_BORDER) 
			  * 2);
      attributes.height = priv->day_name_h;
      priv->day_name_win = gdk_window_new (widget->window,
					   &attributes, 
					   attributes_mask);
      gdk_window_set_background (priv->day_name_win, 
				 BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
      gdk_window_show (priv->day_name_win);
      gdk_window_set_user_data (priv->day_name_win, widget);
    }
  else
    {
      priv->day_name_win = NULL;
    }
}

static void
calendar_realize_week_numbers (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  GdkWindowAttr attributes;
  gint attributes_mask;
  
  /* Week number window -------------------------------- */
  if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
    {
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.visual = gtk_widget_get_visual (widget);
      attributes.colormap = gtk_widget_get_colormap (widget);
      attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
      
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
      attributes.x = widget->style->xthickness + INNER_BORDER;
      attributes.y = (priv->header_h + priv->day_name_h 
		      + (widget->style->ythickness + INNER_BORDER));
      attributes.width = priv->week_width;
      attributes.height = priv->main_h;
      priv->week_win = gdk_window_new (widget->window,
				       &attributes, attributes_mask);
      gdk_window_set_background (priv->week_win,  
				 BACKGROUND_COLOR (GTK_WIDGET (calendar)));
      gdk_window_show (priv->week_win);
      gdk_window_set_user_data (priv->week_win, widget);
    } 
  else
    {
      priv->week_win = NULL;
    }
}

static void
gtk_calendar_realize (GtkWidget *widget)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  GdkWindowAttr attributes;
  gint attributes_mask;

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
  
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.event_mask =  (gtk_widget_get_events (widget) 
			    | GDK_EXPOSURE_MASK |GDK_KEY_PRESS_MASK | GDK_SCROLL_MASK);
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
  widget->window = gdk_window_new (widget->parent->window,
				   &attributes, attributes_mask);
  
  widget->style = gtk_style_attach (widget->style, widget->window);
  
  /* Header window ------------------------------------- */
  calendar_realize_header (calendar);
  /* Day names	window --------------------------------- */
  calendar_realize_day_names (calendar);
  /* Week number window -------------------------------- */
  calendar_realize_week_numbers (calendar);
  /* Main Window --------------------------------------	 */
  attributes.event_mask =  (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
			    | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
			    | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
  
  attributes.x = priv->week_width + (widget->style->ythickness + INNER_BORDER);
  attributes.y = (priv->header_h + priv->day_name_h 
		  + (widget->style->ythickness + INNER_BORDER));
  attributes.width = (widget->allocation.width - attributes.x 
		      - (widget->style->xthickness + INNER_BORDER));
  attributes.height = priv->main_h;
  priv->main_win = gdk_window_new (widget->window,
				   &attributes, attributes_mask);
  gdk_window_set_background (priv->main_win, 
			     BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
  gdk_window_show (priv->main_win);
  gdk_window_set_user_data (priv->main_win, widget);
  gdk_window_set_background (widget->window, BACKGROUND_COLOR (widget));
  gdk_window_show (widget->window);
  gdk_window_set_user_data (widget->window, widget);
}

static void
gtk_calendar_unrealize (GtkWidget *widget)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  gint i;
  
  if (priv->header_win)
    {
      for (i = 0; i < 4; i++)
	{
	  if (priv->arrow_win[i])
	    {
	      gdk_window_set_user_data (priv->arrow_win[i], NULL);
	      gdk_window_destroy (priv->arrow_win[i]);
	      priv->arrow_win[i] = NULL;
	    }
	}
      gdk_window_set_user_data (priv->header_win, NULL);
      gdk_window_destroy (priv->header_win);
      priv->header_win = NULL;
    }
  
  if (priv->week_win)
    {
      gdk_window_set_user_data (priv->week_win, NULL);
      gdk_window_destroy (priv->week_win);
      priv->week_win = NULL;      
    }
  
  if (priv->main_win)
    {
      gdk_window_set_user_data (priv->main_win, NULL);
      gdk_window_destroy (priv->main_win);
      priv->main_win = NULL;      
    }
  if (priv->day_name_win)
    {
      gdk_window_set_user_data (priv->day_name_win, NULL);
      gdk_window_destroy (priv->day_name_win);
      priv->day_name_win = NULL;      
    }
  
  if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize) (widget);
}


/****************************************
 *       Size Request and Allocate      *
 ****************************************/

static void
gtk_calendar_size_request (GtkWidget	  *widget,
			   GtkRequisition *requisition)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  PangoLayout *layout;
  PangoRectangle logical_rect;

  gint height;
  gint i;
  gchar buffer[255];
  gint calendar_margin = CALENDAR_MARGIN;
  gint header_width, main_width;
  gint max_header_height = 0;
  gint focus_width;
  gint focus_padding;
  
  gtk_widget_style_get (GTK_WIDGET (widget),
			"focus-line-width", &focus_width,
			"focus-padding", &focus_padding,
			NULL);

  layout = gtk_widget_create_pango_layout (widget, NULL);
  
  /*
   * Calculate the requisition	width for the widget.
   */
  
  /* Header width */
  
  if (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING)
    {
      priv->max_month_width = 0;
      for (i = 0; i < 12; i++)
	{
	  pango_layout_set_text (layout, default_monthname[i], -1);
	  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
	  priv->max_month_width = MAX (priv->max_month_width,
					       logical_rect.width + 8);
	  max_header_height = MAX (max_header_height, logical_rect.height); 
	}
      priv->max_year_width = 0;
      for (i=0; i<10; i++)
	{
	  g_snprintf (buffer, sizeof (buffer), "%d%d%d%d", i,i,i,i);
	  pango_layout_set_text (layout, buffer, -1);	  
	  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
	  priv->max_year_width = MAX (priv->max_year_width,
					      logical_rect.width + 8);
	  max_header_height = MAX (max_header_height, logical_rect.height); 
	}
    } 
  else 
    {
      priv->max_month_width = 0;
      priv->max_year_width = 0;
    }
  
  if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
    header_width = (priv->max_month_width 
		    + priv->max_year_width
		    + 3 * 3);
  else
    header_width = (priv->max_month_width 
		    + priv->max_year_width
		    + 4 * priv->arrow_width + 3 * 3);

  /* Mainwindow labels width */
  
  priv->max_day_char_width = 0;
  priv->min_day_width = 0;
  priv->max_label_char_ascent = 0;
  priv->max_label_char_descent = 0;

  for (i = 0; i < 9; i++)
    {
      g_snprintf (buffer, sizeof (buffer), "%d%d", i, i);
      pango_layout_set_text (layout, buffer, -1);	  
      pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
      priv->min_day_width = MAX (priv->min_day_width,
					 logical_rect.width);

      priv->max_day_char_ascent = MAX (priv->max_label_char_ascent,
					       PANGO_ASCENT (logical_rect));
      priv->max_day_char_descent = MAX (priv->max_label_char_descent, 
						PANGO_DESCENT (logical_rect));
    }
  /* We add one to max_day_char_width to be able to make the marked day "bold" */
  priv->max_day_char_width = priv->min_day_width / 2 + 1;
  
  if (calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
    for (i = 0; i < 7; i++)
      {
	pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
	pango_layout_line_get_pixel_extents (pango_layout_get_lines (layout)->data, NULL, &logical_rect);

	priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
	priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
						   PANGO_ASCENT (logical_rect));
	priv->max_label_char_descent = MAX (priv->max_label_char_descent, 
						    PANGO_DESCENT (logical_rect));
      }
  
  priv->max_week_char_width = 0;
  if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
    for (i = 0; i < 9; i++)
      {
	g_snprintf (buffer, sizeof (buffer), "%d%d", i, i);
	pango_layout_set_text (layout, buffer, -1);	  
	pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
	priv->max_week_char_width = MAX (priv->max_week_char_width,
						 logical_rect.width / 2);
      }
  
  main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
		+ (priv->max_week_char_width
		   ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + CALENDAR_XSEP * 2
		   : 0));
  
  
  requisition->width = MAX (header_width, main_width + INNER_BORDER * 2) + widget->style->xthickness * 2;
  
  /*
   * Calculate the requisition height for the widget.
   */
  
  if (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING)
    {
      priv->header_h = (max_header_height + CALENDAR_YSEP * 2);
    }
  else
    {
      priv->header_h = 0;
    }
  
  if (calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
    {
      priv->day_name_h = (priv->max_label_char_ascent
				  + priv->max_label_char_descent
				  + 2 * (focus_padding + focus_width) + calendar_margin);
      calendar_margin = CALENDAR_YSEP;
    } 
  else
    {
      priv->day_name_h = 0;
    }

  priv->main_h = (CALENDAR_MARGIN + calendar_margin
			  + 6 * (priv->max_day_char_ascent
				 + priv->max_day_char_descent 
				 + 2 * (focus_padding + focus_width))
			  + DAY_YSEP * 5);
  
  height = (priv->header_h + priv->day_name_h 
	    + priv->main_h);
  
  requisition->height = height + (widget->style->ythickness + INNER_BORDER) * 2;

  g_object_unref (layout);
}

static void
gtk_calendar_size_allocate (GtkWidget	  *widget,
			    GtkAllocation *allocation)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  gint xthickness = widget->style->xthickness;
  gint ythickness = widget->style->xthickness;
  gboolean year_left;
  guint i;
  
  widget->allocation = *allocation;
  
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
    year_left = priv->year_before;
  else
    year_left = !priv->year_before;
  
  if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
    {
      priv->day_width = (priv->min_day_width
			 * ((allocation->width - (xthickness + INNER_BORDER) * 2
			     - (CALENDAR_MARGIN * 2) -  (DAY_XSEP * 6) - CALENDAR_XSEP * 2))
			 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
      priv->week_width = ((allocation->width - (xthickness + INNER_BORDER) * 2
			   - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - CALENDAR_XSEP * 2 )
			  - priv->day_width * 7 + CALENDAR_MARGIN + CALENDAR_XSEP);
    } 
  else 
    {
      priv->day_width = (allocation->width
			 - (xthickness + INNER_BORDER) * 2
			 - (CALENDAR_MARGIN * 2)
			 - (DAY_XSEP * 6))/7;
      priv->week_width = 0;
    }
  
  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
      if (priv->header_win)
	gdk_window_move_resize (priv->header_win,
				xthickness, ythickness,
				allocation->width - 2 * xthickness, priv->header_h);

      for (i = 0 ; i < 4 ; i++)
	{
	  if (priv->arrow_win[i])
	    {
	      GdkRectangle rect;
	      calendar_arrow_rectangle (calendar, i, &rect);
	  
	      gdk_window_move_resize (priv->arrow_win[i],
				      rect.x, rect.y, rect.width, rect.height);
	    }
	}
      
      if (priv->day_name_win)
	gdk_window_move_resize (priv->day_name_win,
				xthickness + INNER_BORDER,
				priv->header_h + (widget->style->ythickness + INNER_BORDER),
				allocation->width - (xthickness + INNER_BORDER) * 2,
				priv->day_name_h);
      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
	{
	  if (priv->week_win)
	    gdk_window_move_resize (priv->week_win,
				    (xthickness + INNER_BORDER),
				    priv->header_h + priv->day_name_h
				    + (widget->style->ythickness + INNER_BORDER),
				    priv->week_width,
				    priv->main_h);
	  gdk_window_move_resize (priv->main_win,
				  priv->week_width + (xthickness + INNER_BORDER),
				  priv->header_h + priv->day_name_h
				  + (widget->style->ythickness + INNER_BORDER),
				  allocation->width 
				  - priv->week_width 
				  - (xthickness + INNER_BORDER) * 2,
				  priv->main_h);
	}
      else 
	{
	  gdk_window_move_resize (priv->main_win,
				  (xthickness + INNER_BORDER),
				  priv->header_h + priv->day_name_h
				  + (widget->style->ythickness + INNER_BORDER),
				  allocation->width 
				  - priv->week_width 
				  - (xthickness + INNER_BORDER) * 2,
				  priv->main_h);
	  if (priv->week_win)
	    gdk_window_move_resize (priv->week_win,
				    allocation->width 
				    - priv->week_width 
				    - (xthickness + INNER_BORDER),
				    priv->header_h + priv->day_name_h
				    + (widget->style->ythickness + INNER_BORDER),
				    priv->week_width,
				    priv->main_h);
	}
    }
}


/****************************************
 *              Repainting              *
 ****************************************/

static void
calendar_paint_header (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  cairo_t *cr;
  char buffer[255];
  int x, y;
  gint header_width, cal_height;
  gint max_month_width;
  gint max_year_width;
  PangoLayout *layout;
  PangoRectangle logical_rect;
  gboolean year_left;

  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
    year_left = priv->year_before;
  else
    year_left = !priv->year_before;

  cr = gdk_cairo_create (priv->header_win);
  
  header_width = widget->allocation.width - 2 * widget->style->xthickness;
  cal_height = widget->allocation.height;
  
  max_month_width = priv->max_month_width;
  max_year_width = priv->max_year_width;
  
  gtk_paint_shadow (widget->style, priv->header_win,
		    GTK_STATE_NORMAL, GTK_SHADOW_OUT,
		    NULL, widget, "calendar",
		    0, 0, header_width, priv->header_h);
  
  
  g_snprintf (buffer, sizeof (buffer), "%d", calendar->year);
  layout = gtk_widget_create_pango_layout (widget, buffer);
  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
  
  /* Draw title */
  y = (priv->header_h - logical_rect.height) / 2;
  
  /* Draw year and its arrows */
  
  if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
    if (year_left)
      x = 3 + (max_year_width - logical_rect.width)/2;
    else
      x = header_width - (3 + max_year_width
			  - (max_year_width - logical_rect.width)/2);
  else
    if (year_left)
      x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
    else
      x = header_width - (3 + priv->arrow_width + max_year_width
			  - (max_year_width - logical_rect.width)/2);
  

  gdk_cairo_set_source_color (cr, HEADER_FG_COLOR (GTK_WIDGET (calendar)));
  cairo_move_to (cr, x, y);
  pango_cairo_show_layout (cr, layout);
  
  /* Draw month */
  g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[calendar->month]);
  pango_layout_set_text (layout, buffer, -1);
  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);

  if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
    if (year_left)
      x = header_width - (3 + max_month_width
			  - (max_month_width - logical_rect.width)/2);      
    else
    x = 3 + (max_month_width - logical_rect.width) / 2;
  else
    if (year_left)
      x = header_width - (3 + priv->arrow_width + max_month_width
			  - (max_month_width - logical_rect.width)/2);
    else
    x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;

  cairo_move_to (cr, x, y);
  pango_cairo_show_layout (cr, layout);

  g_object_unref (layout);
  cairo_destroy (cr);
}

static void
calendar_paint_day_names (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  cairo_t *cr;
  char buffer[255];
  int day,i;
  int day_width, cal_width;
  gint cal_height;
  int day_wid_sep;
  PangoLayout *layout;
  PangoRectangle logical_rect;
  gint focus_padding;
  gint focus_width;
  
  cr = gdk_cairo_create (priv->day_name_win);
  
  gtk_widget_style_get (GTK_WIDGET (widget),
			"focus-line-width", &focus_width,
			"focus-padding", &focus_padding,
			NULL);
  
  day_width = priv->day_width;
  cal_width = widget->allocation.width;
  cal_height = widget->allocation.height;
  day_wid_sep = day_width + DAY_XSEP;
  
  /*
   * Draw rectangles as inverted background for the labels.
   */

  gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
  cairo_rectangle (cr,
		   CALENDAR_MARGIN, CALENDAR_MARGIN,
		   cal_width-CALENDAR_MARGIN * 2,
		   priv->day_name_h - CALENDAR_MARGIN);
  cairo_fill (cr);
  
  if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
    {
      cairo_rectangle (cr, 
		       CALENDAR_MARGIN,
		       priv->day_name_h - CALENDAR_YSEP,
		       priv->week_width - CALENDAR_YSEP - CALENDAR_MARGIN,
		       CALENDAR_YSEP);
      cairo_fill (cr);
    }
  
  /*
   * Write the labels
   */

  layout = gtk_widget_create_pango_layout (widget, NULL);

  gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
  for (i = 0; i < 7; i++)
    {
      if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
	day = 6 - i;
      else
	day = i;
      day = (day + priv->week_start) % 7;
      g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);

      pango_layout_set_text (layout, buffer, -1);
      pango_layout_get_pixel_extents (layout, NULL, &logical_rect);

      cairo_move_to (cr, 
		     (CALENDAR_MARGIN +
		      + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
			 (priv->week_width + (priv->week_width ? CALENDAR_XSEP : 0))
			 : 0)
		      + day_wid_sep * i
		      + (day_width - logical_rect.width)/2),
		     CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y);
      pango_cairo_show_layout (cr, layout);
    }
  
  g_object_unref (layout);
  cairo_destroy (cr);
}

static void
calendar_paint_week_numbers (GtkCalendar *calendar)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  cairo_t *cr;
  gint row, week = 0, year;
  gint x_loc;
  char buffer[3];
  gint y_loc, day_height;
  PangoLayout *layout;
  PangoRectangle logical_rect;
  gint focus_padding;
  gint focus_width;
  
  cr = gdk_cairo_create (priv->week_win);
  
  gtk_widget_style_get (GTK_WIDGET (widget),
			"focus-line-width", &focus_width,
			"focus-padding", &focus_padding,
			NULL);
  
  /*
   * Draw a rectangle as inverted background for the labels.
   */

  gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
  if (priv->day_name_win)
    cairo_rectangle (cr, 
		     CALENDAR_MARGIN,
		     0,
		     priv->week_width - CALENDAR_MARGIN,
		     priv->main_h - CALENDAR_MARGIN);
  else
    cairo_rectangle (cr,
		     CALENDAR_MARGIN,
		     CALENDAR_MARGIN,
		     priv->week_width - CALENDAR_MARGIN,
		     priv->main_h - 2 * CALENDAR_MARGIN);
  cairo_fill (cr);
  
  /*
   * Write the labels
   */
  
  layout = gtk_widget_create_pango_layout (widget, NULL);
  
  gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
  day_height = calendar_row_height (calendar);
  for (row = 0; row < 6; row++)
    {
      gboolean result;
      
      year = calendar->year;
      if (calendar->day[row][6] < 15 && row > 3 && calendar->month == 11)
	year++;

      result = week_of_year (&week, &year,		
			     ((calendar->day[row][6] < 15 && row > 3 ? 1 : 0)
			      + calendar->month) % 12 + 1, calendar->day[row][6]);
      g_return_if_fail (result);

      g_snprintf (buffer, sizeof (buffer), "%d", week);
      pango_layout_set_text (layout, buffer, -1);
      pango_layout_get_pixel_extents (layout, NULL, &logical_rect);

      y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;

      x_loc = (priv->week_width
	       - logical_rect.width
	       - CALENDAR_XSEP - focus_padding - focus_width);

      cairo_move_to (cr, x_loc, y_loc);
      pango_cairo_show_layout (cr, layout);
    }
  
  g_object_unref (layout);
  cairo_destroy (cr);
}

static void
calendar_invalidate_day_num (GtkCalendar *calendar,
			     gint         day)
{
  gint r, c, row, col;
  
  row = -1;
  col = -1;
  for (r = 0; r < 6; r++)
    for (c = 0; c < 7; c++)
      if (calendar->day_month[r][c] == MONTH_CURRENT &&
	  calendar->day[r][c] == day)
	{
	  row = r;
	  col = c;
	}
  
  g_return_if_fail (row != -1);
  g_return_if_fail (col != -1);
  
  calendar_invalidate_day (calendar, row, col);
}

static void
calendar_invalidate_day (GtkCalendar *calendar,
			 gint         row,
			 gint         col)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);

  if (priv->main_win)
    {
      GdkRectangle day_rect;
      
      calendar_day_rectangle (calendar, row, col, &day_rect);
      gdk_window_invalidate_rect (priv->main_win, &day_rect, FALSE);
    }
}

static void
calendar_paint_day (GtkCalendar *calendar,
		    gint	     row,
		    gint	     col)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  cairo_t *cr;
  GdkColor *text_color;
  gchar buffer[255];
  gint day;
  gint x_loc, y_loc;
  GdkRectangle day_rect;

  PangoLayout *layout;
  PangoRectangle logical_rect;
  
  g_return_if_fail (row < 6);
  g_return_if_fail (col < 7);

  cr = gdk_cairo_create (priv->main_win);

  day = calendar->day[row][col];

  calendar_day_rectangle (calendar, row, col, &day_rect);
  
  if (calendar->day_month[row][col] == MONTH_PREV)
    {
      text_color = PREV_MONTH_COLOR (widget);
    } 
  else if (calendar->day_month[row][col] == MONTH_NEXT)
    {
      text_color =  NEXT_MONTH_COLOR (widget);
    } 
  else 
    {
      /*
      if (calendar->highlight_row == row && calendar->highlight_col == col)
	{
	  cairo_set_source_color (cr, HIGHLIGHT_BG_COLOR (widget));
	  gdk_cairo_rectangle (cr, &day_rect);
	  cairo_fill (cr);
	}
      */
      if (calendar->selected_day == day)
	{
	  gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
	  gdk_cairo_rectangle (cr, &day_rect);
	  cairo_fill (cr);
	}
      if (calendar->selected_day == day)
	text_color = SELECTED_FG_COLOR (widget);
      else if (calendar->marked_date[day-1])
	text_color = MARKED_COLOR (widget);
      else
	text_color = NORMAL_DAY_COLOR (widget);
    }

  g_snprintf (buffer, sizeof (buffer), "%d", day);
  layout = gtk_widget_create_pango_layout (widget, buffer);
  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
  
  x_loc = day_rect.x + day_rect.width / 2 + priv->max_day_char_width;
  x_loc -= logical_rect.width;
  y_loc = day_rect.y + (day_rect.height - logical_rect.height) / 2;
  
  gdk_cairo_set_source_color (cr, text_color);
  cairo_move_to (cr, x_loc, y_loc);
  pango_cairo_show_layout (cr, layout);
    
  if (calendar->marked_date[day-1]
      && calendar->day_month[row][col] == MONTH_CURRENT)
    {
      cairo_move_to (cr, x_loc - 1, y_loc);
      pango_cairo_show_layout (cr, layout);
    }

  if (GTK_WIDGET_HAS_FOCUS (calendar) 
      && calendar->focus_row == row && calendar->focus_col == col)
    {
      GtkStateType state;

      if (calendar->selected_day == day)
	state = GTK_WIDGET_HAS_FOCUS (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
      else
	state = GTK_STATE_NORMAL;
      
      gtk_paint_focus (widget->style, 
		       priv->main_win,
		       (calendar->selected_day == day) 
		          ? GTK_STATE_SELECTED : GTK_STATE_NORMAL, 
		       NULL, widget, "calendar-day",
		       day_rect.x,     day_rect.y, 
		       day_rect.width, day_rect.height);
    }

  g_object_unref (layout);
  cairo_destroy (cr);
}

static void
calendar_paint_main (GtkCalendar *calendar)
{
  gint row, col;
  
  for (col = 0; col < 7; col++)
    for (row = 0; row < 6; row++)
      calendar_paint_day (calendar, row, col);
}

static void
calendar_invalidate_arrow (GtkCalendar *calendar,
			   guint        arrow)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  GdkWindow *window;
  
  window = priv->arrow_win[arrow];
  if (window)
    gdk_window_invalidate_rect (window, NULL, FALSE);
}

static void
calendar_paint_arrow (GtkCalendar *calendar,
		      guint	       arrow)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  GdkWindow *window;
  
  window = priv->arrow_win[arrow];
  if (window)
    {
      cairo_t *cr = gdk_cairo_create (window);
      gint width, height;
      gint state;
	
      state = priv->arrow_state[arrow];

      gdk_cairo_set_source_color (cr, &widget->style->bg[state]);
      cairo_paint (cr);
      
      gdk_drawable_get_size (window, &width, &height);
      if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
	gtk_paint_arrow (widget->style, window, state, 
			 GTK_SHADOW_OUT, NULL, widget, "calendar",
			 GTK_ARROW_LEFT, TRUE, 
			 width/2 - 3, height/2 - 4, 8, 8);
      else 
	gtk_paint_arrow (widget->style, window, state, 
			 GTK_SHADOW_OUT, NULL, widget, "calendar",
			 GTK_ARROW_RIGHT, TRUE, 
			 width/2 - 2, height/2 - 4, 8, 8);
    }
}

static gboolean
gtk_calendar_expose (GtkWidget	    *widget,
		     GdkEventExpose *event)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  int i;

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      if (event->window == priv->main_win)
	calendar_paint_main (calendar);
      
      if (event->window == priv->header_win)
	calendar_paint_header (calendar);

      for (i = 0; i < 4; i++)
	if (event->window == priv->arrow_win[i])
	  calendar_paint_arrow (calendar, i);
      
      if (event->window == priv->day_name_win)
	calendar_paint_day_names (calendar);
      
      if (event->window == priv->week_win)
	calendar_paint_week_numbers (calendar);
      if (event->window == widget->window)
	{
	  gtk_paint_shadow (widget->style, widget->window, GTK_WIDGET_STATE (widget),
			    GTK_SHADOW_IN, NULL, widget, "calendar",
			    0, 0, widget->allocation.width, widget->allocation.height);
	}
    }
  
  return FALSE;
}


/****************************************
 *           Mouse handling             *
 ****************************************/

#define CALENDAR_INITIAL_TIMER_DELAY    200
#define CALENDAR_TIMER_DELAY            20

static void
calendar_arrow_action (GtkCalendar *calendar,
		       guint        arrow)
{
  switch (arrow)
    {
    case ARROW_YEAR_LEFT:
      calendar_set_year_prev (calendar);
      break;
    case ARROW_YEAR_RIGHT:
      calendar_set_year_next (calendar);
      break;
    case ARROW_MONTH_LEFT:
      calendar_set_month_prev (calendar);
      break;
    case ARROW_MONTH_RIGHT:
      calendar_set_month_next (calendar);
      break;
    default:;
      /* do nothing */
    }
}

static gboolean
calendar_timer (gpointer data)
{
  GtkCalendar *calendar = data;
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  gboolean retval = FALSE;
  
  GDK_THREADS_ENTER ();

  if (priv->timer)
    {
      calendar_arrow_action (calendar, priv->click_child);

      if (priv->need_timer)
	{
	  priv->need_timer = FALSE;
	  priv->timer = g_timeout_add (CALENDAR_TIMER_DELAY, 
					       (GSourceFunc) calendar_timer, 
					       (gpointer) calendar);
	}
      else 
	retval = TRUE;
    }

  GDK_THREADS_LEAVE ();

  return retval;
}

static void
calendar_start_spinning (GtkCalendar *calendar,
			 gint         click_child)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);

  priv->click_child = click_child;
  
  if (!priv->timer)
    {
      priv->need_timer = TRUE;
      priv->timer = g_timeout_add (CALENDAR_INITIAL_TIMER_DELAY, 
				   calendar_timer,
				   calendar);
    }
}

static void
calendar_stop_spinning (GtkCalendar *calendar)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);

  if (priv->timer)
    {
      g_source_remove (priv->timer);
      priv->timer = 0;
      priv->need_timer = FALSE;
    }
}

static void
calendar_main_button_press (GtkCalendar	   *calendar,
			    GdkEventButton *event)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  gint x, y;
  gint row, col;
  gint day_month;
  gint day;
  
  x = (gint) (event->x);
  y = (gint) (event->y);
  
  row = calendar_row_from_y (calendar, y);
  col = calendar_column_from_x (calendar, x);

  /* If row or column isn't found, just return. */
  if (row == -1 || col == -1)
    return;
  
  day_month = calendar->day_month[row][col];

  if (event->type == GDK_BUTTON_PRESS)
    {
      day = calendar->day[row][col];
      
      if (day_month == MONTH_PREV)
	calendar_set_month_prev (calendar);
      else if (day_month == MONTH_NEXT)
	calendar_set_month_next (calendar);
      
      if (!GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
	  
      if (event->button == 1) 
	{
	  priv->in_drag = 1;
	  priv->drag_start_x = x;
	  priv->drag_start_y = y;
	}

      calendar_select_and_focus_day (calendar, day);
    }
  else if (event->type == GDK_2BUTTON_PRESS)
    {
      priv->in_drag = 0;
      if (day_month == MONTH_CURRENT)
	g_signal_emit (calendar,
		       gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
		       0);
    }
}

static gboolean
gtk_calendar_button_press (GtkWidget	  *widget,
			   GdkEventButton *event)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  gint arrow = -1;
  
  if (event->window == priv->main_win)
    calendar_main_button_press (calendar, event);

  if (!GTK_WIDGET_HAS_FOCUS (widget))
    gtk_widget_grab_focus (widget);

  for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
    {
      if (event->window == priv->arrow_win[arrow])
	{
	  
	  /* only call the action on single click, not double */
	  if (event->type == GDK_BUTTON_PRESS)
	    {
	      if (event->button == 1)
		calendar_start_spinning (calendar, arrow);

	      calendar_arrow_action (calendar, arrow);	      
	    }

	  return TRUE;
	}
    }

  return FALSE;
}

static gboolean
gtk_calendar_button_release (GtkWidget	  *widget,
			     GdkEventButton *event)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);

  if (event->button == 1) 
    {
      calendar_stop_spinning (calendar);

      if (priv->in_drag)
	priv->in_drag = 0;
    }

  return TRUE;
}

static gboolean
gtk_calendar_motion_notify (GtkWidget	   *widget,
			    GdkEventMotion *event)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  gint event_x, event_y;
  gint row, col;
  gint old_row, old_col;
  
  event_x = (gint) (event->x);
  event_y = (gint) (event->y);
  
  if (event->window == priv->main_win)
    {
      
      if (priv->in_drag) 
	{
	  if (gtk_drag_check_threshold (widget,
					priv->drag_start_x, priv->drag_start_y,
					event->x, event->y))
	    {
	      GdkDragContext *context;
	      GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
	      gtk_target_list_add_text_targets (target_list, 0);
	      context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
					1, (GdkEvent *)event);

	  
	      priv->in_drag = 0;
	      
	      gtk_target_list_unref (target_list);
	      gtk_drag_set_icon_default (context);
	    }
	}
      else 
	{
	  row = calendar_row_from_y (calendar, event_y);
	  col = calendar_column_from_x (calendar, event_x);
	  
	  if (row != calendar->highlight_row || calendar->highlight_col != col)
	    {
	      old_row = calendar->highlight_row;
	      old_col = calendar->highlight_col;
	      if (old_row > -1 && old_col > -1)
		{
		  calendar->highlight_row = -1;
		  calendar->highlight_col = -1;
		  calendar_invalidate_day (calendar, old_row, old_col);
		}
	      
	      calendar->highlight_row = row;
	      calendar->highlight_col = col;
	      
	      if (row > -1 && col > -1)
		calendar_invalidate_day (calendar, row, col);
	    }
	}
    }
  return TRUE;
}

static gboolean
gtk_calendar_enter_notify (GtkWidget	    *widget,
			   GdkEventCrossing *event)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  
  if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
    {
      priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_PRELIGHT;
      calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
    }
  
  if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
    {
      priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_PRELIGHT;
      calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
    }
  
  if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
    {
      priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_PRELIGHT;
      calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
    }
  
  if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
    {
      priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_PRELIGHT;
      calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
    }
  
  return TRUE;
}

static gboolean
gtk_calendar_leave_notify (GtkWidget	    *widget,
			   GdkEventCrossing *event)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  gint row;
  gint col;
  
  if (event->window == priv->main_win)
    {
      row = calendar->highlight_row;
      col = calendar->highlight_col;
      calendar->highlight_row = -1;
      calendar->highlight_col = -1;
      if (row > -1 && col > -1)
	calendar_invalidate_day (calendar, row, col);
    }
  
  if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
    {
      priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_NORMAL;
      calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
    }
  
  if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
    {
      priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_NORMAL;
      calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
    }
  
  if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
    {
      priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_NORMAL;
      calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
    }
  
  if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
    {
      priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_NORMAL;
      calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
    }
  
  return TRUE;
}

static gboolean
gtk_calendar_scroll (GtkWidget      *widget,
		     GdkEventScroll *event)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);

  if (event->direction == GDK_SCROLL_UP) 
    {
      if (!GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
      calendar_set_month_prev (calendar);
    }
  else if (event->direction == GDK_SCROLL_DOWN) 
    {
      if (!GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
      calendar_set_month_next (calendar);
    }
  else
    return FALSE;

  return TRUE;
}


/****************************************
 *             Key handling              *
 ****************************************/

static void 
move_focus (GtkCalendar *calendar, 
	    gint         direction)
{
  GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));

  if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
      (text_dir == GTK_TEXT_DIR_RTL && direction == 1)) 
    {
      if (calendar->focus_col > 0)
	  calendar->focus_col--;
      else if (calendar->focus_row > 0)
	{
	  calendar->focus_col = 6;
	  calendar->focus_row--;
	}
    }
  else 
    {
      if (calendar->focus_col < 6)
	calendar->focus_col++;
      else if (calendar->focus_row < 5)
	{
	  calendar->focus_col = 0;
	  calendar->focus_row++;
	}
    }
}

static gboolean
gtk_calendar_key_press (GtkWidget   *widget,
			GdkEventKey *event)
{
  GtkCalendar *calendar;
  gint return_val;
  gint old_focus_row;
  gint old_focus_col;
  gint row, col, day;
  
  calendar = GTK_CALENDAR (widget);
  return_val = FALSE;
  
  old_focus_row = calendar->focus_row;
  old_focus_col = calendar->focus_col;

  switch (event->keyval)
    {
    case GDK_KP_Left:
    case GDK_Left:
      return_val = TRUE;
      if (event->state & GDK_CONTROL_MASK)
	calendar_set_month_prev (calendar);
      else
	{
	  move_focus (calendar, -1);
	  calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
	  calendar_invalidate_day (calendar, calendar->focus_row,
				   calendar->focus_col);
	}
      break;
    case GDK_KP_Right:
    case GDK_Right:
      return_val = TRUE;
      if (event->state & GDK_CONTROL_MASK)
	calendar_set_month_next (calendar);
      else
	{
	  move_focus (calendar, 1);
	  calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
	  calendar_invalidate_day (calendar, calendar->focus_row,
				   calendar->focus_col);
	}
      break;
    case GDK_KP_Up:
    case GDK_Up:
      return_val = TRUE;
      if (event->state & GDK_CONTROL_MASK)
	calendar_set_year_prev (calendar);
      else
	{
	  if (calendar->focus_row > 0)
	    calendar->focus_row--;
	  calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
	  calendar_invalidate_day (calendar, calendar->focus_row,
				   calendar->focus_col);
	}
      break;
    case GDK_KP_Down:
    case GDK_Down:
      return_val = TRUE;
      if (event->state & GDK_CONTROL_MASK)
	calendar_set_year_next (calendar);
      else
	{
	  if (calendar->focus_row < 5)
	    calendar->focus_row++;
	  calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
	  calendar_invalidate_day (calendar, calendar->focus_row,
				   calendar->focus_col);
	}
      break;
    case GDK_KP_Space:
    case GDK_space:
      row = calendar->focus_row;
      col = calendar->focus_col;
      day = calendar->day[row][col];
      
      if (row > -1 && col > -1)
	{
	  return_val = TRUE;

	  if (calendar->day_month[row][col] == MONTH_PREV)
	    calendar_set_month_prev (calendar);
	  else if (calendar->day_month[row][col] == MONTH_NEXT)
	    calendar_set_month_next (calendar);

	  calendar_select_and_focus_day (calendar, day);
	}
    }	
  
  return return_val;
}


/****************************************
 *           Misc widget methods        *
 ****************************************/

static void
calendar_set_background (GtkWidget *widget)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  gint i;
  
  if (GTK_WIDGET_REALIZED (widget))
    {
      for (i = 0; i < 4; i++)
	{
	  if (priv->arrow_win[i])
	    gdk_window_set_background (priv->arrow_win[i], 
				       HEADER_BG_COLOR (widget));
	}
      if (priv->header_win)
	gdk_window_set_background (priv->header_win, 
				   HEADER_BG_COLOR (widget));
      if (priv->day_name_win)
	gdk_window_set_background (priv->day_name_win, 
				   BACKGROUND_COLOR (widget));
      if (priv->week_win)
	gdk_window_set_background (priv->week_win,
				   BACKGROUND_COLOR (widget));
      if (priv->main_win)
	gdk_window_set_background (priv->main_win,
				   BACKGROUND_COLOR (widget));
      if (widget->window)
	gdk_window_set_background (widget->window,
				   BACKGROUND_COLOR (widget)); 
    }
}

static void
gtk_calendar_style_set (GtkWidget *widget,
			GtkStyle  *previous_style)
{
  if (previous_style && GTK_WIDGET_REALIZED (widget))
    calendar_set_background (widget);
}

static void
gtk_calendar_state_changed (GtkWidget	   *widget,
			    GtkStateType    previous_state)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  int i;
  
  if (!GTK_WIDGET_IS_SENSITIVE (widget))
    {
      priv->in_drag = 0;
      calendar_stop_spinning (calendar);    
    }

  for (i = 0; i < 4; i++)
    if (GTK_WIDGET_IS_SENSITIVE (widget))
      priv->arrow_state[i] = GTK_STATE_NORMAL;
    else 
      priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
  
  calendar_set_background (widget);
}

static void
gtk_calendar_grab_notify (GtkWidget *widget,
			  gboolean   was_grabbed)
{
  if (!was_grabbed)
    calendar_stop_spinning (GTK_CALENDAR (widget));
}

static gboolean
gtk_calendar_focus_out (GtkWidget     *widget,
			GdkEventFocus *event)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);

  calendar_stop_spinning (GTK_CALENDAR (widget));
  
  priv->in_drag = 0; 

  return FALSE;
}


/****************************************
 *          Drag and Drop               *
 ****************************************/

static void
gtk_calendar_drag_data_get (GtkWidget        *widget,
			    GdkDragContext   *context,
			    GtkSelectionData *selection_data,
			    guint             info,
			    guint             time)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  GDate *date;
  gchar str[128];
  gsize len;

  date = g_date_new_dmy (calendar->selected_day, calendar->month + 1, calendar->year);
  len = g_date_strftime (str, 127, "%x", date);
  gtk_selection_data_set_text (selection_data, str, len);
  
  g_free (date);
}

/* Get/set whether drag_motion requested the drag data and
 * drag_data_received should thus not actually insert the data,
 * since the data doesn't result from a drop.
 */
static void
set_status_pending (GdkDragContext *context,
                    GdkDragAction   suggested_action)
{
  g_object_set_data (G_OBJECT (context),
                     "gtk-calendar-status-pending",
                     GINT_TO_POINTER (suggested_action));
}

static GdkDragAction
get_status_pending (GdkDragContext *context)
{
  return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
                                             "gtk-calendar-status-pending"));
}

static void
gtk_calendar_drag_leave (GtkWidget      *widget,
			 GdkDragContext *context,
			 guint           time)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);

  priv->drag_highlight = 0;
  gtk_drag_unhighlight (widget);
  
}

static gboolean
gtk_calendar_drag_motion (GtkWidget      *widget,
			  GdkDragContext *context,
			  gint            x,
			  gint            y,
			  guint           time)
{
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
  GdkAtom target;
  
  if (!priv->drag_highlight) 
    {
      priv->drag_highlight = 1;
      gtk_drag_highlight (widget);
    }
  
  target = gtk_drag_dest_find_target (widget, context, NULL);
  if (target == GDK_NONE)
    gdk_drag_status (context, 0, time);
  else
    {
      set_status_pending (context, context->suggested_action);
      gtk_drag_get_data (widget, context, target, time);
    }
  
  return TRUE;
}

static gboolean
gtk_calendar_drag_drop (GtkWidget      *widget,
			GdkDragContext *context,
			gint            x,
			gint            y,
			guint           time)
{
  GdkAtom target;

  target = gtk_drag_dest_find_target (widget, context, NULL);  
  if (target != GDK_NONE)
    {
      gtk_drag_get_data (widget, context, 
			 target, 
			 time);
      return TRUE;
    }

  return FALSE;
}

static void
gtk_calendar_drag_data_received (GtkWidget        *widget,
				 GdkDragContext   *context,
				 gint              x,
				 gint              y,
				 GtkSelectionData *selection_data,
				 guint             info,
				 guint             time)
{
  GtkCalendar *calendar = GTK_CALENDAR (widget);
  guint day, month, year;
  gchar *str;
  GDate *date;
  GdkDragAction suggested_action;

  suggested_action = get_status_pending (context);

  if (suggested_action) 
    {
      set_status_pending (context, 0);
     
      /* We are getting this data due to a request in drag_motion,
       * rather than due to a request in drag_drop, so we are just
       * supposed to call drag_status, not actually paste in the
       * data.
       */
      str = gtk_selection_data_get_text (selection_data);
      if (str) 
	{
	  date = g_date_new ();
	  g_date_set_parse (date, str);
	  if (!g_date_valid (date)) 
	      suggested_action = 0;
	  g_date_free (date);
	  g_free (str);
	}
      else
	suggested_action = 0;

      gdk_drag_status (context, suggested_action, time);

      return;
    }

  date = g_date_new ();
  str = gtk_selection_data_get_text (selection_data);
  if (str) 
    {
      g_date_set_parse (date, str);
      g_free (str);
    }
  
  if (!g_date_valid (date)) 
    {
      g_warning ("Received invalid date data\n");
      g_date_free (date);	
      gtk_drag_finish (context, FALSE, FALSE, time);
      return;
    }

  day = g_date_get_day (date);
  month = g_date_get_month (date);
  year = g_date_get_year (date);
  g_date_free (date);	

  gtk_drag_finish (context, TRUE, FALSE, time);

  
  g_object_freeze_notify (G_OBJECT (calendar));
  if (!(calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
      && (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING))
    gtk_calendar_select_month (calendar, month - 1, year);
  gtk_calendar_select_day (calendar, day);
  g_object_thaw_notify (G_OBJECT (calendar));  
}


/****************************************
 *              Public API              *
 ****************************************/

/**
 * gtk_calendar_new:
 * 
 * Creates a new calendar, with the current date being selected. 
 * 
 * Return value: a newly #GtkCalendar widget
 **/
GtkWidget*
gtk_calendar_new (void)
{
  return g_object_new (GTK_TYPE_CALENDAR, NULL);
}

/**
 * gtk_calendar_display_options:
 * @calendar: a #GtkCalendar.
 * @flags: the display options to set.
 *
 * Sets display options (whether to display the heading and the month headings).
 * 
 * Deprecated: Use gtk_calendar_set_display_options() instead
 **/
void
gtk_calendar_display_options (GtkCalendar	       *calendar,
			      GtkCalendarDisplayOptions flags)
{
  gtk_calendar_set_display_options (calendar, flags);
}

/**
 * gtk_calendar_get_display_options:
 * @calendar: a #GtkCalendar
 * 
 * Returns the current display options of @calendar. 
 * 
 * Return value: the display options.
 *
 * Since: 2.4
 **/
GtkCalendarDisplayOptions 
gtk_calendar_get_display_options (GtkCalendar         *calendar)
{
  g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);

  return calendar->display_flags;
}

/**
 * gtk_calendar_set_display_options:
 * @calendar: a #GtkCalendar
 * @flags: the display options to set
 * 
 * Sets display options (whether to display the heading and the month  
 * headings).
 *
 * Since: 2.4
 **/
void
gtk_calendar_set_display_options (GtkCalendar	       *calendar,
				  GtkCalendarDisplayOptions flags)
{
  GtkWidget *widget = GTK_WIDGET (calendar);
  GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
  gint resize = 0;
  gint i;
  GtkCalendarDisplayOptions old_flags;
  
  g_return_if_fail (GTK_IS_CALENDAR (calendar));
  
  old_flags = calendar->display_flags;
  
  if (GTK_WIDGET_REALIZED (widget))
    {
      if ((flags ^ calendar->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
	{
	  resize ++;
	  if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
	      && (priv->header_win))
	    {
	      calendar->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
	      calendar_realize_arrows (calendar);
	    }
	  else
	    {
	      for (i = 0; i < 4; i++)
		{
		  if (priv->arrow_win[i])
		    {
		      gdk_window_set_user_data (priv->arrow_win[i], 
						NULL);
		      gdk_window_destroy (priv->arrow_win[i]);
		      priv->arrow_win[i] = NULL;
		    }
		}
	    }
	}
      
      if ((flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_HEADING)
	{
	  resize++;
	  
	  if (flags & GTK_CALENDAR_SHOW_HEADING)
	    {
	      calendar->display_flags |= GTK_CALENDAR_SHOW_HEADING;
	      calendar_realize_header (calendar);
	    }
	  else
	    {
	      for (i = 0; i < 4; i++)
		{
		  if (priv->arrow_win[i])
		    {
		      gdk_window_set_user_data (priv->arrow_win[i], 
						NULL);
		      gdk_window_destroy (priv->arrow_win[i]);
		      priv->arrow_win[i] = NULL;
		    }
		}
	      gdk_window_set_user_data (priv->header_win, NULL);
	      gdk_window_destroy (priv->header_win);
	      priv->header_win = NULL;
	    }
	}
      
      
      if ((flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
	{
	  resize++;
	  
	  if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
	    {
	      calendar->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
	      calendar_realize_day_names (calendar);
	    }
	  else
	    {
	      gdk_window_set_user_data (priv->day_name_win, NULL);
	      gdk_window_destroy (priv->day_name_win);
	      priv->day_name_win = NULL;
	    }
	}
      
      if ((flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
	{
	  resize++;
	  
	  if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
	    {
	      calendar->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
	      calendar_realize_week_numbers (calendar);
	    }
	  else
	    {
	      gdk_window_set_user_data (priv->week_win, NULL);
	      gdk_window_destroy (priv->week_win);
	      priv->week_win = NULL;
	    }
	}

      if ((flags ^ calendar->display_flags) & GTK_CALENDAR_WEEK_START_MONDAY)
	g_warning ("GTK_CALENDAR_WEEK_START_MONDAY is ignored; the first day of the week is determined from the locale");
      
      calendar->display_flags = flags;
      if (resize)
	gtk_widget_queue_resize (GTK_WIDGET (calendar));
      
    } 
  else
    calendar->display_flags = flags;
  
  g_object_freeze_notify (G_OBJECT (calendar));
  if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_HEADING)
    g_object_notify (G_OBJECT (calendar), "show-heading");
  if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
    g_object_notify (G_OBJECT (calendar), "show-day-names");
  if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
    g_object_notify (G_OBJECT (calendar), "no-month-change");
  if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
    g_object_notify (G_OBJECT (calendar), "show-week-numbers");
  g_object_thaw_notify (G_OBJECT (calendar));
}

/**
 * gtk_calendar_select_month:
 * @calendar: a #GtkCalendar
 * @month: a month number between 0 and 11.
 * @year: the year the month is in.
 * 
 * Shifts the calendar to a different month.
 * 
 * Return value: %TRUE, always
 **/
gboolean
gtk_calendar_select_month (GtkCalendar *calendar,
			   guint	month,
			   guint	year)
{
  g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
  g_return_val_if_fail (month <= 11, FALSE);
  
  calendar->month = month;
  calendar->year  = year;
  
  calendar_compute_days (calendar);
  
  gtk_widget_queue_draw (GTK_WIDGET (calendar));

  g_object_freeze_notify (G_OBJECT (calendar));
  g_object_notify (G_OBJECT (calendar), "month");
  g_object_notify (G_OBJECT (calendar), "year");
  g_object_thaw_notify (G_OBJECT (calendar));

  g_signal_emit (calendar,
		 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
		 0);
  return TRUE;
}

/**
 * gtk_calendar_select_day:
 * @calendar: a #GtkCalendar.
 * @day: the day number between 1 and 31, or 0 to unselect 
 *   the currently selected day.
 * 
 * Selects a day from the current month.
 **/
void
gtk_calendar_select_day (GtkCalendar *calendar,
			 guint	      day)
{
  g_return_if_fail (GTK_IS_CALENDAR (calendar));
  g_return_if_fail (day <= 31);
  
  /* Deselect the old day */
  if (calendar->selected_day > 0)
    {
      gint selected_day;
      
      selected_day = calendar->selected_day;
      calendar->selected_day = 0;
      if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (calendar)))
	calendar_invalidate_day_num (calendar, selected_day);
    }
  
  calendar->selected_day = day;
  
  /* Select the new day */
  if (day != 0)
    {
      if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (calendar)))
	calendar_invalidate_day_num (calendar, day);
    }
  
  g_object_notify (G_OBJECT (calendar), "day");

  g_signal_emit (calendar,
		 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
		 0);
}

/**
 * gtk_calendar_clear_marks:
 * @calendar: a #GtkCalendar
 * 
 * Remove all visual markers.
 **/
void
gtk_calendar_clear_marks (GtkCalendar *calendar)
{
  guint day;
  
  g_return_if_fail (GTK_IS_CALENDAR (calendar));
  
  for (day = 0; day < 31; day++)
    {
      calendar->marked_date[day] = FALSE;
    }

  gtk_widget_queue_draw (GTK_WIDGET (calendar));
}

/**
 * gtk_calendar_mark_day:
 * @calendar: a #GtkCalendar 
 * @day: the day number to mark between 1 and 31.
 * 
 * Places a visual marker on a particular day.
 * 
 * Return value: %TRUE, always
 **/
gboolean
gtk_calendar_mark_day (GtkCalendar *calendar,
		       guint	    day)
{
  g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
  
  if (day >= 1 && day <= 31 && calendar->marked_date[day-1] == FALSE)
    {
      calendar->marked_date[day - 1] = TRUE;
      calendar_invalidate_day_num (calendar, day);
    }
  
  return TRUE;
}

/**
 * gtk_calendar_unmark_day:
 * @calendar: a #GtkCalendar.
 * @day: the day number to unmark between 1 and 31.
 * 
 * Removes the visual marker from a particular day.
 * 
 * Return value: %TRUE, always
 **/
gboolean
gtk_calendar_unmark_day (GtkCalendar *calendar,
			 guint	      day)
{
  g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
  
  if (day >= 1 && day <= 31 && calendar->marked_date[day-1] == TRUE)
    {
      calendar->marked_date[day - 1] = FALSE;
      calendar_invalidate_day_num (calendar, day);
    }
  
  return TRUE;
}

/**
 * gtk_calendar_get_date:
 * @calendar: a #GtkCalendar
 * @year: location to store the year number, or %NULL
 * @month: location to store the month number (between 0 and 11), or %NULL
 * @day: location to store the day number (between 1 and 31), or %NULL
 * 
 * Obtains the selected date from a #GtkCalendar.
 **/
void
gtk_calendar_get_date (GtkCalendar *calendar,
		       guint	   *year,
		       guint	   *month,
		       guint	   *day)
{
  g_return_if_fail (GTK_IS_CALENDAR (calendar));
  
  if (year)
    *year = calendar->year;
  
  if (month)
    *month = calendar->month;
  
  if (day)
    *day = calendar->selected_day;
}

/**
 * gtk_calendar_freeze:
 * @calendar: a #GtkCalendar
 * 
 * Does nothing. Previously locked the display of the calendar until
 * it was thawed with gtk_calendar_thaw().
 **/
void
gtk_calendar_freeze (GtkCalendar *calendar)
{
  g_return_if_fail (GTK_IS_CALENDAR (calendar));
}

/**
 * gtk_calendar_thaw:
 * @calendar: 
 * 
 * Does nothing. Previously defrosted a calendar; all the changes made
 * since the last gtk_calendar_freeze() were displayed.
 **/
void
gtk_calendar_thaw (GtkCalendar *calendar)
{
  g_return_if_fail (GTK_IS_CALENDAR (calendar));
}

#define __GTK_CALENDAR_C__
#include "gtkaliasdef.c"