aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-day-view.c
blob: 3e5054b9f7c35e6dd937849cfe052462d3beacd2 (plain) (blame)
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
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* 
 * Author : 
 *  Damon Chaplin <damon@helixcode.com>
 *
 * Copyright 1999, Helix Code, Inc.
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

/*
 * EDayView - displays the Day & Work-Week views of the calendar.
 */

#include <config.h>
#include <math.h>
#include <time.h>
#include <gnome.h>
#include <gdk/gdkx.h>
#include <cal-util/timeutil.h>
#include "e-day-view.h"
#include "e-day-view-time-item.h"
#include "e-day-view-top-item.h"
#include "e-day-view-main-item.h"
#include "calendar-commands.h"
#include "popup-menu.h"
#include "eventedit.h"
#include "../e-util/e-canvas.h"
#include "../widgets/e-text/e-text.h"

/* Images */
#include "bell.xpm"
#include "recur.xpm"

/* The minimum amount of space wanted on each side of the date string. */
#define E_DAY_VIEW_DATE_X_PAD   4

#define E_DAY_VIEW_LARGE_FONT   \
    "-adobe-utopia-regular-r-normal-*-*-240-*-*-p-*-iso8859-*"
#define E_DAY_VIEW_LARGE_FONT_FALLBACK  \
    "-adobe-helvetica-bold-r-normal-*-*-240-*-*-p-*-iso8859-*"

/* The offset from the top/bottom of the canvas before auto-scrolling starts.*/
#define E_DAY_VIEW_AUTO_SCROLL_OFFSET   16

/* The time between each auto-scroll, in milliseconds. */
#define E_DAY_VIEW_AUTO_SCROLL_TIMEOUT  50

/* The number of timeouts we skip before we start scrolling. */
#define E_DAY_VIEW_AUTO_SCROLL_DELAY    5

/* The number of pixels the mouse has to be moved with the button down before
   we start a drag. */
#define E_DAY_VIEW_DRAG_START_OFFSET    4

/* Drag and Drop stuff. */
enum {
    TARGET_CALENDAR_EVENT
};
static GtkTargetEntry target_table[] = {
    { "application/x-e-calendar-event",     0, TARGET_CALENDAR_EVENT }
};
static guint n_targets = sizeof(target_table) / sizeof(target_table[0]);

static void e_day_view_class_init (EDayViewClass *class);
static void e_day_view_init (EDayView *day_view);
static void e_day_view_destroy (GtkObject *object);
static void e_day_view_realize (GtkWidget *widget);
static void e_day_view_unrealize (GtkWidget *widget);
static void e_day_view_style_set (GtkWidget *widget,
                  GtkStyle  *previous_style);
static void e_day_view_size_allocate (GtkWidget *widget,
                      GtkAllocation *allocation);
static gboolean e_day_view_update_scroll_regions (EDayView *day_view);
static gint e_day_view_focus_in (GtkWidget *widget,
                 GdkEventFocus *event);
static gint e_day_view_focus_out (GtkWidget *widget,
                  GdkEventFocus *event);
static gint e_day_view_key_press (GtkWidget *widget,
                  GdkEventKey *event);

static void e_day_view_on_canvas_realized (GtkWidget *widget,
                       EDayView *day_view);

static gboolean e_day_view_on_top_canvas_button_press (GtkWidget *widget,
                               GdkEventButton *event,
                               EDayView *day_view);
static gboolean e_day_view_on_top_canvas_button_release (GtkWidget *widget,
                             GdkEventButton *event,
                             EDayView *day_view);
static gboolean e_day_view_on_top_canvas_motion (GtkWidget *widget,
                         GdkEventMotion *event,
                         EDayView *day_view);

static gboolean e_day_view_on_main_canvas_button_press (GtkWidget *widget,
                            GdkEventButton *event,
                            EDayView *day_view);
static gboolean e_day_view_on_main_canvas_button_release (GtkWidget *widget,
                              GdkEventButton *event,
                              EDayView *day_view);
static void e_day_view_update_calendar_selection_time (EDayView *day_view);
static gboolean e_day_view_on_main_canvas_motion (GtkWidget *widget,
                          GdkEventMotion *event,
                          EDayView *day_view);
static gboolean e_day_view_convert_event_coords (EDayView *day_view,
                         GdkEvent *event,
                         GdkWindow *window,
                         gint *x_return,
                         gint *y_return);
static void e_day_view_update_selection (EDayView *day_view,
                     gint row,
                     gint col);
static void e_day_view_update_long_event_resize (EDayView *day_view,
                         gint day);
static void e_day_view_update_resize (EDayView *day_view,
                      gint row);
static void e_day_view_finish_long_event_resize (EDayView *day_view);
static void e_day_view_finish_resize (EDayView *day_view);
static void e_day_view_abort_resize (EDayView *day_view,
                     guint32 time);


static gboolean e_day_view_on_long_event_button_press (EDayView     *day_view,
                               gint      event_num,
                               GdkEventButton   *event,
                               EDayViewPosition  pos,
                               gint      event_x,
                               gint      event_y);
static gboolean e_day_view_on_event_button_press (EDayView   *day_view,
                          gint        day,
                          gint        event_num,
                          GdkEventButton *event,
                          EDayViewPosition pos,
                          gint        event_x,
                          gint        event_y);
static void e_day_view_on_long_event_click (EDayView *day_view,
                        gint event_num,
                        GdkEventButton  *bevent,
                        EDayViewPosition pos,
                        gint         event_x,
                        gint         event_y);
static void e_day_view_on_event_click (EDayView *day_view,
                       gint day,
                       gint event_num,
                       GdkEventButton  *event,
                       EDayViewPosition pos,
                       gint     event_x,
                       gint     event_y);
static void e_day_view_on_event_double_click (EDayView *day_view,
                          gint day,
                          gint event_num);
static void e_day_view_on_event_right_click (EDayView *day_view,
                         GdkEventButton *bevent,
                         gint day,
                         gint event_num);

static void e_day_view_recalc_day_starts (EDayView *day_view,
                      time_t start_time);
static void e_day_view_recalc_num_rows  (EDayView   *day_view);

static EDayViewPosition e_day_view_convert_position_in_top_canvas (EDayView *day_view,
                                   gint x,
                                   gint y,
                                   gint *day_return,
                                   gint *event_num_return);
static EDayViewPosition e_day_view_convert_position_in_main_canvas (EDayView *day_view,
                                    gint x,
                                    gint y,
                                    gint *day_return,
                                    gint *row_return,
                                    gint *event_num_return);
static gboolean e_day_view_find_event_from_item (EDayView *day_view,
                         GnomeCanvasItem *item,
                         gint *day_return,
                         gint *event_num_return);
static gboolean e_day_view_find_event_from_uid (EDayView *day_view,
                        const gchar *uid,
                        gint *day_return,
                        gint *event_num_return);

typedef gboolean (* EDayViewForeachEventCallback) (EDayView *day_view,
                           gint day,
                           gint event_num,
                           gpointer data);

static void e_day_view_foreach_event_with_uid (EDayView *day_view,
                           const gchar *uid,
                           EDayViewForeachEventCallback callback,
                           gpointer data);

static void e_day_view_reload_events (EDayView *day_view);
static void e_day_view_free_events (EDayView *day_view);
static void e_day_view_free_event_array (EDayView *day_view,
                     GArray *array);
static int e_day_view_add_event (iCalObject *ico,
                 time_t   start,
                 time_t   end,
                 gpointer data);
static void e_day_view_update_event_label (EDayView *day_view,
                       gint day,
                       gint event_num);
static void e_day_view_update_long_event_label (EDayView *day_view,
                        gint event_num);

static void e_day_view_layout_long_events (EDayView *day_view);
static void e_day_view_layout_long_event (EDayView     *day_view,
                      EDayViewEvent *event,
                      guint8       *grid);
static void e_day_view_reshape_long_events (EDayView *day_view);
static void e_day_view_reshape_long_event (EDayView *day_view,
                       gint event_num);
static void e_day_view_layout_day_events (EDayView *day_view,
                      gint  day);
static void e_day_view_layout_day_event (EDayView   *day_view,
                     gint       day,
                     EDayViewEvent *event,
                     guint8    *grid,
                     guint16   *group_starts);
static void e_day_view_expand_day_event (EDayView      *day_view,
                     gint       day,
                     EDayViewEvent *event,
                     guint8    *grid);
static void e_day_view_recalc_cols_per_row (EDayView *day_view,
                        gint      day,
                        guint16  *group_starts);
static void e_day_view_reshape_day_events (EDayView *day_view,
                       gint day);
static void e_day_view_reshape_day_event (EDayView *day_view,
                      gint  day,
                      gint  event_num);
static void e_day_view_reshape_main_canvas_resize_bars (EDayView *day_view);
static void e_day_view_reshape_resize_long_event_rect_item (EDayView *day_view);
static void e_day_view_reshape_resize_rect_item (EDayView *day_view);

static void e_day_view_ensure_events_sorted (EDayView *day_view);
static gint e_day_view_event_sort_func (const void *arg1,
                    const void *arg2);

static void e_day_view_start_editing_event (EDayView *day_view,
                        gint day,
                        gint event_num,
                        gchar *initial_text);
static void e_day_view_stop_editing_event (EDayView *day_view);
static gboolean e_day_view_on_text_item_event (GnomeCanvasItem *item,
                           GdkEvent *event,
                           EDayView *day_view);
static void e_day_view_on_editing_started (EDayView *day_view,
                       GnomeCanvasItem *item);
static void e_day_view_on_editing_stopped (EDayView *day_view,
                       GnomeCanvasItem *item);

static time_t e_day_view_convert_grid_position_to_time (EDayView *day_view,
                            gint col,
                            gint row);
static gboolean e_day_view_convert_time_to_grid_position (EDayView *day_view,
                              time_t time,
                              gint *col,
                              gint *row);

static void e_day_view_check_auto_scroll (EDayView *day_view,
                      gint event_y);
static void e_day_view_start_auto_scroll (EDayView *day_view,
                      gboolean scroll_up);
static void e_day_view_stop_auto_scroll (EDayView *day_view);
static gboolean e_day_view_auto_scroll_handler (gpointer data);

static void e_day_view_on_new_appointment (GtkWidget *widget,
                       gpointer data);
static void e_day_view_on_edit_appointment (GtkWidget *widget,
                        gpointer data);
static void e_day_view_on_delete_occurrence (GtkWidget *widget,
                         gpointer data);
static void e_day_view_on_delete_appointment (GtkWidget *widget,
                          gpointer data);
static void e_day_view_on_unrecur_appointment (GtkWidget *widget,
                           gpointer data);
static EDayViewEvent* e_day_view_get_popup_menu_event (EDayView *day_view);

static gint e_day_view_on_top_canvas_drag_motion (GtkWidget      *widget,
                          GdkDragContext *context,
                          gint            x,
                          gint            y,
                          guint           time,
                          EDayView    *day_view);
static void e_day_view_update_top_canvas_drag (EDayView *day_view,
                           gint day);
static void e_day_view_reshape_top_canvas_drag_item (EDayView *day_view);
static gint e_day_view_on_main_canvas_drag_motion (GtkWidget      *widget,
                           GdkDragContext *context,
                           gint            x,
                           gint            y,
                           guint           time,
                           EDayView   *day_view);
static void e_day_view_reshape_main_canvas_drag_item (EDayView *day_view);
static void e_day_view_update_main_canvas_drag (EDayView *day_view,
                        gint row,
                        gint day);
static void e_day_view_on_top_canvas_drag_leave (GtkWidget      *widget,
                         GdkDragContext *context,
                         guint           time,
                         EDayView        *day_view);
static void e_day_view_on_main_canvas_drag_leave (GtkWidget      *widget,
                          GdkDragContext *context,
                          guint           time,
                          EDayView   *day_view);
static void e_day_view_on_drag_begin (GtkWidget      *widget,
                      GdkDragContext *context,
                      EDayView     *day_view);
static void e_day_view_on_drag_end (GtkWidget      *widget,
                    GdkDragContext *context,
                    EDayView       *day_view);
static void e_day_view_on_drag_data_get (GtkWidget          *widget,
                     GdkDragContext     *context,
                     GtkSelectionData   *selection_data,
                     guint               info,
                     guint               time,
                     EDayView       *day_view);
static void e_day_view_on_top_canvas_drag_data_received (GtkWidget  *widget,
                             GdkDragContext *context,
                             gint            x,
                             gint            y,
                             GtkSelectionData *data,
                             guint           info,
                             guint           time,
                             EDayView   *day_view);
static void e_day_view_on_main_canvas_drag_data_received (GtkWidget      *widget,
                              GdkDragContext *context,
                              gint            x,
                              gint            y,
                              GtkSelectionData *data,
                              guint           info,
                              guint           time,
                              EDayView   *day_view);
static gboolean e_day_view_update_event_cb (EDayView *day_view,
                        gint day,
                        gint event_num,
                        gpointer data);
static gboolean e_day_view_remove_event_cb (EDayView *day_view,
                        gint day,
                        gint event_num,
                        gpointer data);


static GtkTableClass *parent_class;


GtkType
e_day_view_get_type (void)
{
    static GtkType e_day_view_type = 0;

    if (!e_day_view_type){
        GtkTypeInfo e_day_view_info = {
            "EDayView",
            sizeof (EDayView),
            sizeof (EDayViewClass),
            (GtkClassInitFunc) e_day_view_class_init,
            (GtkObjectInitFunc) e_day_view_init,
            NULL, /* reserved 1 */
            NULL, /* reserved 2 */
            (GtkClassInitFunc) NULL
        };

        parent_class = gtk_type_class (GTK_TYPE_TABLE);
        e_day_view_type = gtk_type_unique (GTK_TYPE_TABLE,
                           &e_day_view_info);
    }

    return e_day_view_type;
}


static void
e_day_view_class_init (EDayViewClass *class)
{
    GtkObjectClass *object_class;
    GtkWidgetClass *widget_class;

    object_class = (GtkObjectClass *) class;
    widget_class = (GtkWidgetClass *) class;

    /* Method override */
    object_class->destroy       = e_day_view_destroy;

    widget_class->realize       = e_day_view_realize;
    widget_class->unrealize     = e_day_view_unrealize;
    widget_class->style_set     = e_day_view_style_set;
    widget_class->size_allocate = e_day_view_size_allocate;
    widget_class->focus_in_event    = e_day_view_focus_in;
    widget_class->focus_out_event   = e_day_view_focus_out;
    widget_class->key_press_event   = e_day_view_key_press;
}


static void
e_day_view_init (EDayView *day_view)
{
    GdkColormap *colormap;
    gboolean success[E_DAY_VIEW_COLOR_LAST];
    gint day, nfailed;
    GnomeCanvasGroup *canvas_group;

    GTK_WIDGET_SET_FLAGS (day_view, GTK_CAN_FOCUS);

    colormap = gtk_widget_get_colormap (GTK_WIDGET (day_view));

    day_view->calendar = NULL;

    day_view->long_events = g_array_new (FALSE, FALSE,
                         sizeof (EDayViewEvent));
    day_view->long_events_sorted = TRUE;
    day_view->long_events_need_layout = FALSE;
    day_view->long_events_need_reshape = FALSE;

    for (day = 0; day < E_DAY_VIEW_MAX_DAYS; day++) {
        day_view->events[day] = g_array_new (FALSE, FALSE,
                             sizeof (EDayViewEvent));
        day_view->events_sorted[day] = TRUE;
        day_view->need_layout[day] = FALSE;
        day_view->need_reshape[day] = FALSE;
    }

    /* These indicate that the times haven't been set. */
    day_view->lower = 0;
    day_view->upper = 0;

    /* FIXME: Initialize day_starts. */
    day_view->days_shown = 1;

    day_view->mins_per_row = 30;
    day_view->date_format = E_DAY_VIEW_DATE_FULL;
    day_view->rows_in_top_display = 0;

    /* Note that these don't work yet. It would need a few fixes to the
       way event->start_minute and event->end_minute are used, and there
       may be problems with events that go outside the visible times. */
    day_view->first_hour_shown = 0;
    day_view->first_minute_shown = 0;
    day_view->last_hour_shown = 24;
    day_view->last_minute_shown = 0;

    day_view->main_gc = NULL;
    e_day_view_recalc_num_rows (day_view);

    day_view->work_day_start_hour = 9;
    day_view->work_day_start_minute = 0;
    day_view->work_day_end_hour = 17;
    day_view->work_day_end_minute = 0;
    day_view->scroll_to_work_day = TRUE;

    day_view->editing_event_day = -1;
    day_view->editing_event_num = -1;
    day_view->editing_new_event = FALSE;

    day_view->resize_bars_event_day = -1;
    day_view->resize_bars_event_num = -1;

    day_view->selection_start_row = -1;
    day_view->selection_start_col = -1;
    day_view->selection_end_row = -1;
    day_view->selection_end_col = -1;
    day_view->selection_drag_pos = E_DAY_VIEW_DRAG_NONE;
    day_view->selection_in_top_canvas = FALSE;

    day_view->resize_drag_pos = E_DAY_VIEW_POS_NONE;

    day_view->pressed_event_day = -1;

    day_view->drag_event_day = -1;
    day_view->drag_last_day = -1;

    day_view->auto_scroll_timeout_id = 0;

    /* Create the large font. */
    day_view->large_font = gdk_font_load (E_DAY_VIEW_LARGE_FONT);
    if (!day_view->large_font)
        day_view->large_font = gdk_font_load (E_DAY_VIEW_LARGE_FONT_FALLBACK);
    if (!day_view->large_font)
        g_warning ("Couldn't load font");

    
    /* Allocate the colors. */
#if 1
    day_view->colors[E_DAY_VIEW_COLOR_BG_WORKING].red   = 255 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_WORKING].green = 255 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_WORKING].blue  = 131 * 257;

    day_view->colors[E_DAY_VIEW_COLOR_BG_NOT_WORKING].red   = 211 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_NOT_WORKING].green = 208 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_NOT_WORKING].blue  =   6 * 257;
#else

    /* FG: MistyRose1, LightPink3 | RosyBrown | MistyRose3. */

    day_view->colors[E_DAY_VIEW_COLOR_BG_WORKING].red   = 255 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_WORKING].green = 228 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_WORKING].blue  = 225 * 257;

    day_view->colors[E_DAY_VIEW_COLOR_BG_NOT_WORKING].red   = 238 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_NOT_WORKING].green = 162 * 257;
    day_view->colors[E_DAY_VIEW_COLOR_BG_NOT_WORKING].blue  = 173 * 257;
#endif

    day_view->colors[E_DAY_VIEW_COLOR_EVENT_VBAR].red   = 0;
    day_view->colors[E_DAY_VIEW_COLOR_EVENT_VBAR].green = 0;
    day_view->colors[E_DAY_VIEW_COLOR_EVENT_VBAR].blue  = 65535;

    day_view->colors[E_DAY_VIEW_COLOR_EVENT_BACKGROUND].red   = 65535;
    day_view->colors[E_DAY_VIEW_COLOR_EVENT_BACKGROUND].green = 65535;
    day_view->colors[E_DAY_VIEW_COLOR_EVENT_BACKGROUND].blue  = 65535;

    day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER].red   = 0;
    day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER].green = 0;
    day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER].blue  = 0;

    nfailed = gdk_colormap_alloc_colors (colormap, day_view->colors,
                         E_DAY_VIEW_COLOR_LAST, FALSE,
                         TRUE, success);
    if (nfailed)
        g_warning ("Failed to allocate all colors");



    /*
     * Top Canvas
     */
    day_view->top_canvas = e_canvas_new ();
    gtk_table_attach (GTK_TABLE (day_view), day_view->top_canvas,
              1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
    gtk_widget_show (day_view->top_canvas);
    gtk_signal_connect_after (GTK_OBJECT (day_view->top_canvas), "button_press_event",
                  GTK_SIGNAL_FUNC (e_day_view_on_top_canvas_button_press),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->top_canvas), "button_release_event",
                  GTK_SIGNAL_FUNC (e_day_view_on_top_canvas_button_release),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->top_canvas), "motion_notify_event",
                  GTK_SIGNAL_FUNC (e_day_view_on_top_canvas_motion),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->top_canvas),
                  "drag_motion",
                  GTK_SIGNAL_FUNC (e_day_view_on_top_canvas_drag_motion),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->top_canvas),
                  "drag_leave",
                  GTK_SIGNAL_FUNC (e_day_view_on_top_canvas_drag_leave),
                  day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->top_canvas),
                "drag_begin",
                GTK_SIGNAL_FUNC (e_day_view_on_drag_begin),
                day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->top_canvas),
                "drag_end",
                GTK_SIGNAL_FUNC (e_day_view_on_drag_end),
                day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->top_canvas),
                "drag_data_get",
                GTK_SIGNAL_FUNC (e_day_view_on_drag_data_get),
                day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->top_canvas),
                "drag_data_received",
                GTK_SIGNAL_FUNC (e_day_view_on_top_canvas_drag_data_received),
                day_view);

    canvas_group = GNOME_CANVAS_GROUP (GNOME_CANVAS (day_view->top_canvas)->root);

    day_view->top_canvas_item =
        gnome_canvas_item_new (canvas_group,
                       e_day_view_top_item_get_type (),
                       "EDayViewTopItem::day_view", day_view,
                       NULL);

    day_view->resize_long_event_rect_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type(),
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BACKGROUND],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->resize_long_event_rect_item);

    day_view->drag_long_event_rect_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type (),
                       "width_pixels", 1,
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BACKGROUND],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->drag_long_event_rect_item);

    day_view->drag_long_event_item =
        gnome_canvas_item_new (canvas_group,
                       e_text_get_type (),
                       "anchor", GTK_ANCHOR_NW,
                       "line_wrap", TRUE,
                       "clip", TRUE,
                       "max_lines", 1,
                       "editable", TRUE,
                       NULL);
    gnome_canvas_item_hide (day_view->drag_long_event_item);

    /*
     * Main Canvas
     */
    day_view->main_canvas = e_canvas_new ();
    gtk_table_attach (GTK_TABLE (day_view), day_view->main_canvas,
              1, 2, 1, 2,
              GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
    gtk_widget_show (day_view->main_canvas);
    gtk_signal_connect (GTK_OBJECT (day_view->main_canvas), "realize",
                GTK_SIGNAL_FUNC (e_day_view_on_canvas_realized),
                day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->main_canvas),
                  "button_press_event",
                  GTK_SIGNAL_FUNC (e_day_view_on_main_canvas_button_press),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->main_canvas),
                  "button_release_event",
                  GTK_SIGNAL_FUNC (e_day_view_on_main_canvas_button_release),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->main_canvas),
                  "motion_notify_event",
                  GTK_SIGNAL_FUNC (e_day_view_on_main_canvas_motion),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->main_canvas),
                  "drag_motion",
                  GTK_SIGNAL_FUNC (e_day_view_on_main_canvas_drag_motion),
                  day_view);
    gtk_signal_connect_after (GTK_OBJECT (day_view->main_canvas),
                  "drag_leave",
                  GTK_SIGNAL_FUNC (e_day_view_on_main_canvas_drag_leave),
                  day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->main_canvas),
                "drag_begin",
                GTK_SIGNAL_FUNC (e_day_view_on_drag_begin),
                day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->main_canvas),
                "drag_end",
                GTK_SIGNAL_FUNC (e_day_view_on_drag_end),
                day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->main_canvas),
                "drag_data_get",
                GTK_SIGNAL_FUNC (e_day_view_on_drag_data_get),
                day_view);
    gtk_signal_connect (GTK_OBJECT (day_view->main_canvas),
                "drag_data_received",
                GTK_SIGNAL_FUNC (e_day_view_on_main_canvas_drag_data_received),
                day_view);

    canvas_group = GNOME_CANVAS_GROUP (GNOME_CANVAS (day_view->main_canvas)->root);

    day_view->main_canvas_item =
        gnome_canvas_item_new (canvas_group,
                       e_day_view_main_item_get_type (),
                       "EDayViewMainItem::day_view", day_view,
                       NULL);

    day_view->resize_rect_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type(),
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BACKGROUND],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->resize_rect_item);

    day_view->resize_bar_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type(),
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_VBAR],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->resize_bar_item);

    day_view->main_canvas_top_resize_bar_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type (),
                       "width_pixels", 1,
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_VBAR],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->main_canvas_top_resize_bar_item);

    day_view->main_canvas_bottom_resize_bar_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type (),
                       "width_pixels", 1,
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_VBAR],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->main_canvas_bottom_resize_bar_item);


    day_view->drag_rect_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type (),
                       "width_pixels", 1,
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BACKGROUND],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->drag_rect_item);

    day_view->drag_bar_item =
        gnome_canvas_item_new (canvas_group,
                       gnome_canvas_rect_get_type (),
                       "width_pixels", 1,
                       "fill_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_VBAR],
                       "outline_color_gdk", &day_view->colors[E_DAY_VIEW_COLOR_EVENT_BORDER],
                       NULL);
    gnome_canvas_item_hide (day_view->drag_bar_item);

    day_view->drag_item =
        gnome_canvas_item_new (canvas_group,
                       e_text_get_type (),
                       "anchor", GTK_ANCHOR_NW,
                       "line_wrap", TRUE,
                       "clip", TRUE,
                       "editable", TRUE,
                       NULL);
    gnome_canvas_item_hide (day_view->drag_item);


    /*
     * Times Canvas
     */
    day_view->time_canvas = e_canvas_new ();
    gtk_layout_set_vadjustment (GTK_LAYOUT (day_view->time_canvas),
                    GTK_LAYOUT (day_view->main_canvas)->vadjustment);
    gtk_table_attach (GTK_TABLE (day_view), day_view->time_canvas,
              0, 1, 1, 2,
              GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
    gtk_widget_show (day_view->time_canvas);

    canvas_group = GNOME_CANVAS_GROUP (GNOME_CANVAS (day_view->time_canvas)->root);

    day_view->time_canvas_item =
        gnome_canvas_item_new (canvas_group,
                       e_day_view_time_item_get_type (),
                       "EDayViewTimeItem::day_view", day_view,
                       NULL);


    /*
     * Scrollbar.
     */
    day_view->vscrollbar = gtk_vscrollbar_new (GTK_LAYOUT (day_view->main_canvas)->vadjustment);
    gtk_table_attach (GTK_TABLE (day_view), day_view->vscrollbar,
              2, 3, 1, 2, 0, GTK_EXPAND | GTK_FILL, 0, 0);
    gtk_widget_show (day_view->vscrollbar);


    /* Create the pixmaps. */
    day_view->reminder_icon = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &day_view->reminder_mask, NULL, bell_xpm);
    day_view->recurrence_icon = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &day_view->recurrence_mask, NULL, recur_xpm);


    /* Create the cursors. */
    day_view->normal_cursor = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
    day_view->move_cursor = gdk_cursor_new (GDK_FLEUR);
    day_view->resize_width_cursor = gdk_cursor_new (GDK_SB_H_DOUBLE_ARROW);
    day_view->resize_height_cursor = gdk_cursor_new (GDK_SB_V_DOUBLE_ARROW);
    day_view->last_cursor_set_in_top_canvas = NULL;
    day_view->last_cursor_set_in_main_canvas = NULL;

    /* Set up the drop sites. */
    gtk_drag_dest_set (day_view->top_canvas,
               GTK_DEST_DEFAULT_ALL,
               target_table, n_targets,
               GDK_ACTION_COPY | GDK_ACTION_MOVE);
    gtk_drag_dest_set (day_view->main_canvas,
               GTK_DEST_DEFAULT_ALL,
               target_table, n_targets,
               GDK_ACTION_COPY | GDK_ACTION_MOVE);
}


/* Turn off the background of the canvas windows. This reduces flicker
   considerably when scrolling. (Why isn't it in GnomeCanvas?). */
static void
e_day_view_on_canvas_realized (GtkWidget *widget,
                   EDayView *day_view)
{
    gdk_window_set_back_pixmap (GTK_LAYOUT (widget)->bin_window,
                    NULL, FALSE);
}


/**
 * e_day_view_new:
 * @Returns: a new #EDayView.
 *
 * Creates a new #EDayView.
 **/
GtkWidget *
e_day_view_new (void)
{
    GtkWidget *day_view;

    day_view = GTK_WIDGET (gtk_type_new (e_day_view_get_type ()));

    return day_view;
}


static void
e_day_view_destroy (GtkObject *object)
{
    EDayView *day_view;
    gint day;

    day_view = E_DAY_VIEW (object);

    e_day_view_stop_auto_scroll (day_view);

    if (day_view->large_font)
        gdk_font_unref (day_view->large_font);

    gdk_cursor_destroy (day_view->normal_cursor);
    gdk_cursor_destroy (day_view->move_cursor);
    gdk_cursor_destroy (day_view->resize_width_cursor);
    gdk_cursor_destroy (day_view->resize_height_cursor);

    e_day_view_free_events (day_view);
    g_array_free (day_view->long_events, TRUE);
    for (day = 0; day < E_DAY_VIEW_MAX_DAYS; day++)
        g_array_free (day_view->events[day], TRUE);

    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}


static void
e_day_view_realize (GtkWidget *widget)
{
    EDayView *day_view;

    if (GTK_WIDGET_CLASS (parent_class)->realize)
        (*GTK_WIDGET_CLASS (parent_class)->realize)(widget);

    day_view = E_DAY_VIEW (widget);
    day_view->main_gc = gdk_gc_new (widget->window);
}


static void
e_day_view_unrealize (GtkWidget *widget)
{
    EDayView *day_view;

    day_view = E_DAY_VIEW (widget);

    gdk_gc_unref (day_view->main_gc);
    day_view->main_gc = NULL;

    if (GTK_WIDGET_CLASS (parent_class)->unrealize)
        (*GTK_WIDGET_CLASS (parent_class)->unrealize)(widget);
}


static void
e_day_view_style_set (GtkWidget *widget,
              GtkStyle  *previous_style)
{
    EDayView *day_view;
    GdkFont *font;
    gint top_rows, top_canvas_height;
    gint month, max_month_width, max_abbr_month_width, number_width;
    gint hour, max_large_hour_width, month_width;
    gint minute, max_minute_width, i;
    GDate date;
    gchar buffer[128];
    gint times_width;

    if (GTK_WIDGET_CLASS (parent_class)->style_set)
        (*GTK_WIDGET_CLASS (parent_class)->style_set)(widget, previous_style);

    day_view = E_DAY_VIEW (widget);
    font = widget->style->font;

    /* Recalculate the height of each row based on the font size. */
    day_view->row_height = font->ascent + font->descent + E_DAY_VIEW_EVENT_BORDER_HEIGHT * 2 + E_DAY_VIEW_EVENT_Y_PAD * 2 + 2 /* FIXME */;
    day_view->row_height = MAX (day_view->row_height, E_DAY_VIEW_ICON_HEIGHT + E_DAY_VIEW_ICON_Y_PAD + 2);
    GTK_LAYOUT (day_view->main_canvas)->vadjustment->step_increment = day_view->row_height;

    day_view->top_row_height = font->ascent + font->descent + E_DAY_VIEW_LONG_EVENT_BORDER_HEIGHT * 2 + E_DAY_VIEW_LONG_EVENT_Y_PAD * 2 + E_DAY_VIEW_TOP_CANVAS_Y_GAP;
    day_view->top_row_height = MAX (day_view->top_row_height, E_DAY_VIEW_ICON_HEIGHT + E_DAY_VIEW_ICON_Y_PAD + 2 + E_DAY_VIEW_TOP_CANVAS_Y_GAP);

    /* Set the height of the top canvas based on the row height and the
       number of rows needed (min 1 + 1 for the dates + 1 space for DnD).*/
    top_rows = MAX (1, day_view->rows_in_top_display);
    top_canvas_height = (top_rows + 2) * day_view->top_row_height;
    gtk_widget_set_usize (day_view->top_canvas, -1, top_canvas_height);

    /* Find the biggest full month name. */
    g_date_clear (&date, 1);
    g_date_set_dmy (&date, 20, 1, 2000);
    max_month_width = 0;
    max_abbr_month_width = 0;
    for (month = 1; month <= 12; month++) {
        g_date_set_month (&date, month);

        g_date_strftime (buffer, 128, "%B", &date);
        month_width = gdk_string_width (font, buffer);
        max_month_width = MAX (max_month_width, month_width);

        g_date_strftime (buffer, 128, "%b", &date);
        month_width = gdk_string_width (font, buffer);
        max_abbr_month_width = MAX (max_abbr_month_width, month_width);
    }
    number_width = gdk_string_width (font, "31 ");
    day_view->long_format_width = number_width + max_month_width
        + E_DAY_VIEW_DATE_X_PAD;
    day_view->abbreviated_format_width = number_width
        + max_abbr_month_width + E_DAY_VIEW_DATE_X_PAD;

    /* Calculate the widths of all the time strings necessary. */
    day_view->max_small_hour_width = 0;
    max_large_hour_width = 0;
    for (hour = 0; hour < 24; hour++) {
        sprintf (buffer, "%02i", hour);
        day_view->small_hour_widths[hour] = gdk_string_width (font, buffer);
        day_view->large_hour_widths[hour] = gdk_string_width (day_view->large_font, buffer);
        day_view->max_small_hour_width = MAX (day_view->max_small_hour_width, day_view->small_hour_widths[hour]);
        max_large_hour_width = MAX (max_large_hour_width, day_view->large_hour_widths[hour]);
    }
    day_view->max_large_hour_width = max_large_hour_width;

    max_minute_width = 0;
    for (minute = 0, i = 0; minute < 60; minute += 5, i++) {
        sprintf (buffer, "%02i", minute);
        day_view->minute_widths[i] = gdk_string_width (font, buffer);
        max_minute_width = MAX (max_minute_width, day_view->minute_widths[i]);
    }
    day_view->max_minute_width = max_minute_width;
    day_view->colon_width = gdk_string_width (font, ":");

    /* Calculate the width of the time column. */
    times_width = e_day_view_time_item_get_column_width (E_DAY_VIEW_TIME_ITEM (day_view->time_canvas_item));
    gtk_widget_set_usize (day_view->time_canvas, times_width, -1);
}


/* This recalculates the sizes of each column. */
static void
e_day_view_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
    EDayView *day_view;
    gfloat width, offset;
    gint col, day, scroll_y;
    gboolean need_reshape;
    gdouble old_x2, old_y2, new_x2, new_y2;

#if 0
    g_print ("In e_day_view_size_allocate\n");
#endif
    day_view = E_DAY_VIEW (widget);

    (*GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);

    /* Calculate the column sizes, using floating point so that pixels
       get divided evenly. Note that we use one more element than the
       number of columns, to make it easy to get the column widths. */
    width = day_view->main_canvas->allocation.width;
    width /= day_view->days_shown;
    offset = 0;
    for (col = 0; col <= day_view->days_shown; col++) {
        day_view->day_offsets[col] = floor (offset + 0.5);
        offset += width;
    }

    /* Calculate the days widths based on the offsets. */
    for (col = 0; col < day_view->days_shown; col++) {
        day_view->day_widths[col] = day_view->day_offsets[col + 1] - day_view->day_offsets[col];
    }

    /* Determine which date format to use, based on the column widths. */
    if (day_view->day_widths[0] > day_view->long_format_width)
        day_view->date_format = E_DAY_VIEW_DATE_FULL;
    else if (day_view->day_widths[0] > day_view->abbreviated_format_width)
        day_view->date_format = E_DAY_VIEW_DATE_ABBREVIATED;
    else
        day_view->date_format = E_DAY_VIEW_DATE_SHORT;

    /* Set the scroll region of the top canvas to its allocated size. */
    gnome_canvas_get_scroll_region (GNOME_CANVAS (day_view->top_canvas),
                    NULL, NULL, &old_x2, &old_y2);
    new_x2 = day_view->top_canvas->allocation.width - 1;
    new_y2 = day_view->top_canvas->allocation.height - 1;
    if (old_x2 != new_x2 || old_y2 != new_y2)
        gnome_canvas_set_scroll_region (GNOME_CANVAS (day_view->top_canvas),
                        0, 0, new_x2, new_y2);

    need_reshape = e_day_view_update_scroll_regions (day_view);

    /* Scroll to the start of the working day, if this is the initial
       allocation. */
    if (day_view->scroll_to_work_day) {
        scroll_y = e_day_view_convert_time_to_position (day_view, day_view->work_day_start_hour, day_view->work_day_start_minute);
        gnome_canvas_scroll_to (GNOME_CANVAS (day_view->main_canvas),
                    0, scroll_y);
        day_view->scroll_to_work_day = FALSE;
    }

    /* Flag that we need to reshape the events. Note that changes in height
       don't matter, since the rows are always the same height. */
    if (need_reshape) {
        day_view->long_events_need_reshape = TRUE;
        for (day = 0; day < E_DAY_VIEW_MAX_DAYS; day++)
            day_view->need_reshape[day] = TRUE;

        e_day_view_check_layout (day_view);
    }
}


static gint
e_day_view_focus_in (GtkWidget *widget, GdkEventFocus *event)
{
    EDayView *day_view;

    g_return_val_if_fail (widget != NULL, FALSE);
    g_return_val_if_fail (E_IS_DAY_VIEW (widget), FALSE);
    g_return_val_if_fail (event != NULL, FALSE);

    day_view = E_DAY_VIEW (widget);

    GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);

    gtk_widget_queue_draw (day_view->top_canvas);
    gtk_widget_queue_draw (day_view->main_canvas);

    return FALSE;
}


static gint
e_day_view_focus_out (GtkWidget *widget, GdkEventFocus *event)
{
    EDayView *day_view;

    g_return_val_if_fail (widget != NULL, FALSE);
    g_return_val_if_fail (E_IS_DAY_VIEW (widget), FALSE);
    g_return_val_if_fail (event != NULL, FALSE);

    day_view = E_DAY_VIEW (widget);

    GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);

    gtk_widget_queue_draw (day_view->top_canvas);
    gtk_widget_queue_draw (day_view->main_canvas);

    return FALSE;
}


void
e_day_view_set_calendar     (EDayView   *day_view,
                 GnomeCalendar  *calendar)
{
    g_return_if_fail (E_IS_DAY_VIEW (day_view));

    day_view->calendar = calendar;

    /* FIXME: free current events? */
}


/* This reloads all calendar events. */
void
e_day_view_update_all_events    (EDayView   *day_view)
{
    e_day_view_reload_events (day_view);
}


/* This is called when one event has been added or updated. */
void
e_day_view_update_event     (EDayView   *day_view,
                 const gchar    *uid)
{
    EDayViewEvent *event;
    gchar *obj_string;
    iCalObject *ico;
    CalObjFindStatus status;
    gint day, event_num;

    g_return_if_fail (E_IS_DAY_VIEW (day_view));

#if 0
    /* FIXME: Just for testing. */
    chdir ("/home/damon/tmp");

    g_print ("In e_day_view_update_event day_view:%p uid:%s\n",
         day_view, uid);
#endif

    /* If our calendar or time hasn't been set yet, just return. */
    if (!day_view->calendar
        || (day_view->lower == 0 && day_view->upper == 0))
        return;

    /* Get the event from the server. */
    obj_string = cal_client_get_object (day_view->calendar->client, uid);
    status = ical_object_find_in_string (uid, obj_string, &ico);
    g_free (obj_string);

    switch (status) {
    case CAL_OBJ_FIND_SUCCESS:
        /* Do nothing. */
        break;
    case CAL_OBJ_FIND_SYNTAX_ERROR:
        g_warning ("syntax error uid=%s\n", uid);
        return;
    case CAL_OBJ_FIND_NOT_FOUND:
        g_warning ("obj not found uid=%s\n", uid);
        return;
    }

    /* We only care about events. */
    if (ico && ico->type != ICAL_EVENT) {
        ical_object_unref (ico);
        return;
    }

    /* If the event already exists and the dates didn't change, we can
       update the event fairly easily without changing the events arrays
       or computing a new layout. */
    if (e_day_view_find_event_from_uid (day_view, uid, &day, &event_num)) {
        if (day == E_DAY_VIEW_LONG_EVENT)
            event = &g_array_index (day_view->long_events,
                        EDayViewEvent, event_num);
        else
            event = &g_array_index (day_view->events[day],
                        EDayViewEvent, event_num);

        if (ical_object_compare_dates (event->ico, ico)) {
            e_day_view_foreach_event_with_uid (day_view, uid, e_day_view_update_event_cb, ico);
            ical_object_unref (ico);
            gtk_widget_queue_draw (day_view->top_canvas);
            gtk_widget_queue_draw (day_view->main_canvas);
            return;
        }

        /* The dates have changed, so we need to remove the
           old occurrrences before adding the new ones. */
        e_day_view_foreach_event_with_uid (day_view, uid,
                           e_day_view_remove_event_cb,
                           NULL);
    }

    /* Add the occurrences of the event. */
    ical_object_generate_events (ico, day_view->lower, day_view->upper,
                     e_day_view_add_event, day_view);
    ical_object_unref (ico);

    e_day_view_check_layout (day_view);

    gtk_widget_queue_draw (day_view->top_canvas);
    gtk_widget_queue_draw (day_view->main_canvas);
}


static gboolean
e_day_view_update_event_cb (EDayView *day_view,
                gint day,
                gint event_num,
                gpointer data)
{
    EDayViewEvent *event;
    iCalObject *ico;

    ico = data;
#if 0
    g_print ("In e_day_view_update_event_cb day:%i event_num:%i\n",
         day, event_num);
#endif
    if (day == E_DAY_VIEW_LONG_EVENT) {
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);
    } else {
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);
    }

    ical_object_unref (event->ico);
    event->ico = ico;
    ical_object_ref (ico);

    /* If we are editing an event which we have just created, we will get
       an update_event callback from the server. But we need to ignore it
       or we will lose the text the user has already typed in. */
    if (day_view->editing_new_event
        && day_view->editing_event_day == day
        && day_view->editing_event_num == event_num) {
        return TRUE;
    }

    if (day == E_DAY_VIEW_LONG_EVENT) {
        e_day_view_update_long_event_label (day_view, event_num);
        e_day_view_reshape_long_event (day_view, event_num);
    } else {
        e_day_view_update_event_label (day_view, day, event_num);
        e_day_view_reshape_day_event (day_view, day, event_num);
    }
    return TRUE;
}


/* This calls a given function for each event instance that matches the given
   uid. Note that it is safe for the callback to remove the event (since we
   step backwards through the arrays). */
static void
e_day_view_foreach_event_with_uid (EDayView *day_view,
                   const gchar *uid,
                   EDayViewForeachEventCallback callback,
                   gpointer data)
{
    EDayViewEvent *event;
    gint day, event_num;

    for (day = 0; day < day_view->days_shown; day++) {
        for (event_num = day_view->events[day]->len - 1;
             event_num >= 0;
             event_num--) {
            event = &g_array_index (day_view->events[day],
                        EDayViewEvent, event_num);
            if (event->ico->uid
                && !strcmp (uid, event->ico->uid)) {
                if (!(*callback) (day_view, day, event_num,
                          data))
                    return;
            }
        }
    }

    for (event_num = day_view->long_events->len - 1;
         event_num >= 0;
         event_num--) {
        event = &g_array_index (day_view->long_events,
                    EDayViewEvent, event_num);
        if (event->ico->uid
            && !strcmp (uid, event->ico->uid)) {
            if (!(*callback) (day_view, E_DAY_VIEW_LONG_EVENT,
                      event_num, data))
                return;
        }
    }
}


/* This removes all the events associated with the given uid. Note that for
   recurring events there may be more than one. If any events are found and
   removed we need to layout the events again. */
void
e_day_view_remove_event (EDayView   *day_view,
             const gchar    *uid)
{
    g_return_if_fail (E_IS_DAY_VIEW (day_view));

#if 0
    g_print ("In e_day_view_remove_event day_view:%p uid:%s\n",
         day_view, uid);
#endif

    e_day_view_foreach_event_with_uid (day_view, uid,
                       e_day_view_remove_event_cb, NULL);

    e_day_view_check_layout (day_view);
    gtk_widget_queue_draw (day_view->top_canvas);
    gtk_widget_queue_draw (day_view->main_canvas);
}


static gboolean
e_day_view_remove_event_cb (EDayView *day_view,
                gint day,
                gint event_num,
                gpointer data)
{
    EDayViewEvent *event;

    if (day == E_DAY_VIEW_LONG_EVENT)
        event = &g_array_index (day_view->long_events,
                    EDayViewEvent, event_num);
    else
        event = &g_array_index (day_view->events[day],
                    EDayViewEvent, event_num);

    /* We set the event's uid to NULL so we don't try to update it in
       on_editing_stopped(). */
    g_free (event->ico->uid);
    event->ico->uid = NULL;

    if (event->canvas_item)
        gtk_object_destroy (GTK_OBJECT (event->canvas_item));
    ical_object_unref (event->ico);

    if (day == E_DAY_VIEW_LONG_EVENT) {
        g_array_remove_index (day_view->long_events, event_num);
        day_view->long_events_need_layout = TRUE;
    } else {
        g_array_remove_index (day_view->events[day], event_num);
        day_view->need_layout[day] = TRUE;
    }
    return TRUE;
}


/* This updates the text shown for an event. If the event start or end do not
   lie on a row boundary, the time is displayed before the summary. */
static void
e_day_view_update_event_label (EDayView *day_view,
                   gint day,
                   gint event_num)
{
    EDayViewEvent *event;
    gchar *text;
    gboolean free_text = FALSE, editing_event = FALSE;
    gint offset, start_minute, end_minute;

    event = &g_array_index (day_view->events[day], EDayViewEvent,
                event_num);

    text = event->ico->summary ? event->ico->summary : "";

    if (day_view->editing_event_day == day
        && day_view->editing_event_num == event_num)
        editing_event = TRUE;

    if (!editing_event
        && (event->start_minute % day_view->mins_per_row != 0
        || event->end_minute % day_view->mins_per_row != 0)) {
        offset = day_view->first_hour_shown * 60
            + day_view->first_minute_shown;
        start_minute = offset + event->start_minute;
        end_minute = offset + event->end_minute;
        text = g_strdup_printf ("%02i:%02i-%02i:%02i %s",
                    start_minute / 60,
                    start_minute % 60,
                    end_minute / 60,
                    end_minute % 60,
                    text);
        free_text = TRUE;
    }

    gnome_canvas_item_set (event->canvas_item,
                   "text", text,
                   NULL);

    if (free_text)
        g_free (text);
}


static void
e_day_view_update_long_event_label (EDayView *day_view,
                    gint      event_num)
{
    EDayViewEvent *event;

    event = &g_array_index (day_view->long_events, EDayViewEvent,
                event_num);

    gnome_canvas_item_set (event->canvas_item,
                   "text", event->ico->summary ? event->ico->summary : "",
                   NULL);
}


/* Finds the day and index of the event with the given canvas item.
   If is is a long event, -1 is returned as the day.
   Returns TRUE if the event was found. */
static gboolean
e_day_view_find_event_from_item (EDayView *day_view,
                 GnomeCanvasItem *item,
                 gint *day_return,
                 gint *event_num_return)
{
    EDayViewEvent *event;
    gint day, event_num;

    for (day = 0; day < day_view->days_shown; day++) {
        for (event_num = 0; event_num < day_view->events[day]->len;
             event_num++) {
            event = &g_array_index (day_view->events[day],
                        EDayViewEvent, event_num);
            if (event->canvas_item == item) {
                *day_return = day;
                *event_num_return = event_num;
                return TRUE;
            }
        }
    }

    for (event_num = 0; event_num < day_view->long_events->len;
         event_num++) {
        event = &g_array_index (day_view->long_events,
                    EDayViewEvent, event_num);
        if (event->canvas_item == item) {
            *day_return = E_DAY_VIEW_LONG_EVENT;
            *event_num_return = event_num;
            return TRUE;
        }
    }

    return FALSE;
}


/* Finds the day and index of the event with the given uid.
   If is is a long event, E_DAY_VIEW_LONG_EVENT is returned as the day.
   Returns TRUE if an event with the uid was found.
   Note that for recurring events there may be several EDayViewEvents, one
   for each instance, all with the same iCalObject and uid. So only use this
   function if you know the event doesn't recur or you are just checking to
   see if any events with the uid exist. */
static gboolean
e_day_view_find_event_from_uid (EDayView *day_view,
                const gchar *uid,
                gint *day_return,
                gint *event_num_return)
{
    EDayViewEvent *event;
    gint day, event_num;

    for (day = 0; day < day_view->days_shown; day++) {
        for (event_num = 0; event_num < day_view->events[day]->len;
             event_num++) {
            event = &g_array_index (day_view->events[day],
                        EDayViewEvent, event_num);
            if (event->ico->uid
                && !strcmp (uid, event->ico->uid)) {
                *day_return = day;
                *event_num_return = event_num;
                return TRUE;
            }
        }
    }

    for (event_num = 0; event_num < day_view->long_events->len;
         event_num++) {
        event = &g_array_index (day_view->long_events,
                    EDayViewEvent, event_num);
        if (event->ico->uid
            && !strcmp (uid, event->ico->uid)) {
            *day_return = E_DAY_VIEW_LONG_EVENT;
            *event_num_return = event_num;
            return TRUE;
        }
    }

    return FALSE;
}


/* This sets the selected time range. The EDayView will show the day or week
   corresponding to the start time. If the start_time & end_time are not equal
   and are both visible in the view, then the selection is set to those times,
   otherwise it is set to 1 hour from the start of the working day. */
void
e_day_view_set_selected_time_range  (EDayView   *day_view,
                     time_t      start_time,
                     time_t      end_time)
{
    GDate date;
    time_t lower;
    gint start_row, start_col, end_row, end_col;
    gboolean need_redraw = FALSE, start_in_grid, end_in_grid;

    g_return_if_fail (E_IS_DAY_VIEW (day_view));

    /* Calculate the first day that should be shown, based on start_time
       and the days_shown setting. If we are showing 1 day it is just the
       start of the day given by start_time, otherwise it is the previous
       Monday. */
    if (day_view->days_shown == 1)
        lower = time_day_begin (start_time);
    else {
        g_date_clear (&date, 1);
        g_date_set_time (&date, start_time);
        g_date_subtract_days (&date, g_date_weekday (&date) - 1);
        lower = time_from_day (g_date_year (&date),
                       g_date_month (&date) - 1,
                       g_date_day (&date));
    }

    /* See if we need to change the days shown. */
    if (lower != day_view->lower) {
        e_day_view_recalc_day_starts (day_view, lower);
        e_day_view_reload_events (day_view);
        need_redraw = TRUE;
    }

    /* Set the selection. */
    start_in_grid = e_day_view_convert_time_to_grid_position (day_view,
                                  start_time,
                                  &start_col,
                                  &start_row);
    end_in_grid = e_day_view_convert_time_to_grid_position (day_view,
                                end_time - 60,
                                &end_col,
                                &end_row);

    /* If either of the times isn't in the grid, or the selection covers
       an entire day, we set the selection to 1 row from the start of the
       working day, in the day corresponding to the start time. */
    if (!start_in_grid || !end_in_grid
        || (start_row == 0 && end_row == day_view->rows - 1)) {
        end_col = start_col;

        start_row = e_day_view_convert_time_to_row (day_view, day_view->work_day_start_hour, day_view->work_day_start_minute);
        start_row = CLAMP (start_row, 0, day_view->rows - 1);
        end_row = start_row;
    }

    if (start_row != day_view->selection_start_row
        || start_col != day_view->selection_start_col) {
        need_redraw = TRUE;
        day_view->selection_in_top_canvas = FALSE;
        day_view->selection_start_row = start_row;
        day_view->selection_start_col = start_col;
    }

    if (end_row != day_view->selection_end_row
        || end_col != day_view->selection_end_col) {
        need_redraw = TRUE;
        day_view->selection_in_top_canvas = FALSE;
        day_view->selection_end_row = end_row;
        day_view->selection_end_col = end_col;
    }

    if (need_redraw) {
        gtk_widget_queue_draw (day_view->top_canvas);
        gtk_widget_queue_draw (day_view->main_canvas);
    }
}


/* Returns the selected time range. */
void
e_day_view_get_selected_time_range  (EDayView   *day_view,
                     time_t     *start_time,
                     time_t     *end_time)
{
    gint start_col, start_row, end_col, end_row;

    start_col = day_view->selection_start_col;
    start_row = day_view->selection_start_row;
    end_col = day_view->selection_end_col;
    end_row = day_view->selection_end_row;

    if (start_col == -1) {
        start_col = 0;
        start_row = 0;
        end_col = 0;
        end_row = 0;
    }

    /* Check if the selection is only in the top canvas, in which case
       we can simply use the day_starts array. */
    if (day_view->selection_in_top_canvas) {
        *start_time = day_view->day_starts[start_col];
        *end_time = day_view->day_starts[end_col + 1];
    } else {
        /* Convert the start col + row into a time. */
        *start_time = e_day_view_convert_grid_position_to_time (day_view, start_col, start_row);
        *end_time = e_day_view_convert_grid_position_to_time (day_view, end_col, end_row + 1);
    }
}


static void
e_day_view_recalc_day_starts (EDayView *day_view,
                  time_t start_time)
{
    gint day;

    day_view->day_starts[0] = start_time;
    for (day = 1; day <= day_view->days_shown; day++) {
        day_view->day_starts[day] = time_add_day (day_view->day_starts[day - 1], 1);
    }

    day_view->lower = start_time;
    day_view->upper = day_view->day_starts[day_view->days_shown];
}


gint
e_day_view_get_days_shown   (EDayView   *day_view)
{
    g_return_val_if_fail (E_IS_DAY_VIEW (day_view), -1);

    return day_view->days_shown;
}


void
e_day_view_set_days_shown   (EDayView   *day_view,
                 gint        days_shown)
{
    g_return_if_fail (E_IS_DAY_VIEW (day_view));
    g_return_if_fail (days_shown >= 1);
    g_return_if_fail (days_shown <= E_DAY_VIEW_MAX_DAYS);

    if (day_view->days_shown != days_shown) {
        day_view->days_shown = days_shown;

        /* FIXME: Update everything. */
    }
}


gint
e_day_view_get_mins_per_row (EDayView   *day_view)
{
    g_return_val_if_fail (E_IS_DAY_VIEW (day_view), -1);

    return day_view->mins_per_row;
}


void
e_day_view_set_mins_per_row (EDayView   *day_view,
                 gint        mins_per_row)
{
    gint day;

    g_return_if_fail (E_IS_DAY_VIEW (day_view));

    if (mins_per_row != 5 && mins_per_row != 10 && mins_per_row != 15
        && mins_per_row != 30 && mins_per_row != 60) {
        g_warning ("Invalid minutes per row setting");
        return;
    }

    if (day_view->mins_per_row == mins_per_row)
        return;

    day_view->mins_per_row = mins_per_row;
    e_day_view_recalc_num_rows (day_view);

    /* If we aren't visible, we'll sort it out later. */
    if (!GTK_WIDGET_VISIBLE (day_view))
        return;

    for (day = 0; day < E_DAY_VIEW_MAX_DAYS; day++)
        day_view->need_layout[day] = TRUE;

    /* We must layout the events before updating the scroll region, since
       that will result in a redraw which would crash otherwise. */
    e_day_view_check_layout (day_view);
    gtk_widget_queue_draw (day_view->time_canvas);
    gtk_widget_queue_draw (day_view->main_canvas);

    e_day_view_update_scroll_regions (day_view);
}


static gboolean
e_day_view_update_scroll_regions (EDayView *day_view)
{
    gdouble old_x2, old_y2, new_x2, new_y2;
    gboolean need_reshape = FALSE;

    /* Set the scroll region of the time canvas to its allocated width,
       but with the height the same as the main canvas. */
    gnome_canvas_get_scroll_region (GNOME_CANVAS (day_view->time_canvas),
                    NULL, NULL, &old_x2, &old_y2);
    new_x2 = day_view->time_canvas->allocation.width - 1;
    new_y2 = MAX (day_view->rows * day_view->row_height,
              day_view->main_canvas->allocation.height) - 1;
    if (old_x2 != new_x2 || old_y2 != new_y2)
        gnome_canvas_set_scroll_region (GNOME_CANVAS (day_view->time_canvas),
                        0, 0, new_x2, new_y2);

    /* Set the scroll region of the main canvas to its allocated width,
       but with the height depending on the number of rows needed. */
    gnome_canvas_get_scroll_region (GNOME_CANVAS (day_view->main_canvas),
                    NULL, NULL, &old_x2, &old_y2);
    new_x2 = day_view->main_canvas->allocation.width - 1;
    if (old_x2 != new_x2 || old_y2 != new_y2) {
        need_reshape = TRUE;
        gnome_canvas_set_scroll_region (GNOME_CANVAS (day_view->main_canvas),
                        0, 0, new_x2, new_y2);
    }

    return need_reshape;
}


/* This recalculates the number of rows to display, based on the time range
   shown and the minutes per row. */
static void
e_day_view_recalc_num_rows  (EDayView   *day_view)
{
    gint hours, minutes, total_minutes;

    hours = day_view->last_hour_shown - day_view->first_hour_shown;
    /* This could be negative but it works out OK. */
    minutes = day_view->last_minute_shown - day_view->first_minute_shown;
    total_minutes = hours * 60 + minutes;
    day_view->rows = total_minutes / day_view->mins_per_row;
}


/* Converts an hour and minute to a row in the canvas. Note that if we aren't
   showing all 24 hours of the day, the returned row may be negative or
   greater than day_view->rows. */
gint
e_day_view_convert_time_to_row  (EDayView   *day_view,
                 gint        hour,
                 gint        minute)
{
    gint total_minutes, start_minute, offset;

    total_minutes = hour * 60 + minute;
    start_minute = day_view->first_hour_shown * 60
        + day_view->first_minute_shown;
    offset = total_minutes - start_minute;
    if (offset < 0)
        return -1;
    else
        return offset / day_view->mins_per_row;
}


/* Converts an hour and minute to a y coordinate in the canvas. */
gint
e_day_view_convert_time_to_position (EDayView   *day_view,
                     gint    hour,
                     gint    minute)
{
    gint total_minutes, start_minute, offset;

    total_minutes = hour * 60 + minute;
    start_minute = day_view->first_hour_shown * 60
        + day_view->first_minute_shown;
    offset = total_minutes - start_minute;

    return offset * day_view->row_height / day_view->mins_per_row;
}


static gboolean
e_day_view_on_top_canvas_button_press (GtkWidget *widget,
                       GdkEventButton *event,
                       EDayView *day_view)
{
    gint event_x, event_y, scroll_x, scroll_y, day, event_num;
    EDayViewPosition pos;

    /* Convert the coords to the main canvas window, or return if the
       window is not found. */
    if (!e_day_view_convert_event_coords (day_view, (GdkEvent*) event,
                          GTK_LAYOUT (widget)->bin_window,
                          &event_x, &event_y))
        return FALSE;

    /* The top canvas doesn't scroll, but just in case. */
    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (widget),
                     &scroll_x, &scroll_y);
    event_x += scroll_x;
    event_y += scroll_y;

    pos = e_day_view_convert_position_in_top_canvas (day_view,
                             event_x, event_y,
                             &day, &event_num);

    if (pos == E_DAY_VIEW_POS_OUTSIDE)
        return FALSE;

    if (pos != E_DAY_VIEW_POS_NONE)
        return e_day_view_on_long_event_button_press (day_view,
                                  event_num,
                                  event, pos,
                                  event_x,
                                  event_y);

    e_day_view_stop_editing_event (day_view);

    if (event->button == 1) {
        if (!GTK_WIDGET_HAS_FOCUS (day_view))
            gtk_widget_grab_focus (GTK_WIDGET (day_view));

        if (gdk_pointer_grab (GTK_LAYOUT (widget)->bin_window, FALSE,
                      GDK_POINTER_MOTION_MASK
                      | GDK_BUTTON_RELEASE_MASK,
                      FALSE, NULL, event->time) == 0) {
            day_view->selection_start_row = -1;
            day_view->selection_start_col = day;
            day_view->selection_end_row = -1;
            day_view->selection_end_col = day;
            day_view->selection_drag_pos = E_DAY_VIEW_DRAG_END;
            day_view->selection_in_top_canvas = TRUE;

            /* FIXME: Optimise? */
            gtk_widget_queue_draw (day_view->top_canvas);
            gtk_widget_queue_draw (day_view->main_canvas);
        }
    } else if (event->button == 3) {
        e_day_view_on_event_right_click (day_view, event, -1, -1);
    }

    return TRUE;
}


static gboolean
e_day_view_convert_event_coords (EDayView *day_view,
                 GdkEvent *event,
                 GdkWindow *window,
                 gint *x_return,
                 gint *y_return)
{
    gint event_x, event_y, win_x, win_y;
    GdkWindow *event_window;;

    /* Get the event window, x & y from the appropriate event struct. */
    switch (event->type) {
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
        event_x = event->button.x;
        event_y = event->button.y;
        event_window = event->button.window;
        break;
    case GDK_MOTION_NOTIFY:
        event_x = event->motion.x;
        event_y = event->motion.y;
        event_window = event->motion.window;
        break;
    default:
        /* Shouldn't get here. */
        g_assert_not_reached ();
        return FALSE;
    }

    while (event_window && event_window != window
           && event_window != GDK_ROOT_PARENT()) {
        gdk_window_get_position (event_window, &win_x, &win_y);
        event_x += win_x;
        event_y += win_y;
        event_window = gdk_window_get_parent (event_window);
    }

    *x_return = event_x;
    *y_return = event_y;

    if (event_window != window)
        g_warning ("Couldn't find event window\n");

    return (event_window == window) ? TRUE : FALSE;
}


static gboolean
e_day_view_on_main_canvas_button_press (GtkWidget *widget,
                    GdkEventButton *event,
                    EDayView *day_view)
{
    gint event_x, event_y, scroll_x, scroll_y, row, day, event_num;
    EDayViewPosition pos;

    /* Convert the coords to the main canvas window, or return if the
       window is not found. */
    if (!e_day_view_convert_event_coords (day_view, (GdkEvent*) event,
                          GTK_LAYOUT (widget)->bin_window,
                          &event_x, &event_y))
        return FALSE;

    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (widget),
                     &scroll_x, &scroll_y);
    event_x += scroll_x;
    event_y += scroll_y;

    /* Find out where the mouse is. */
    pos = e_day_view_convert_position_in_main_canvas (day_view,
                              event_x, event_y,
                              &day, &row,
                              &event_num);

    if (pos == E_DAY_VIEW_POS_OUTSIDE)
        return FALSE;

    if (pos != E_DAY_VIEW_POS_NONE)
        return e_day_view_on_event_button_press (day_view, day,
                             event_num, event, pos,
                             event_x, event_y);

    e_day_view_stop_editing_event (day_view);

    /* Start the selection drag. */
    if (event->button == 1) {
        if (!GTK_WIDGET_HAS_FOCUS (day_view))
            gtk_widget_grab_focus (GTK_WIDGET (day_view));

        if (gdk_pointer_grab (GTK_LAYOUT (widget)->bin_window, FALSE,
                      GDK_POINTER_MOTION_MASK
                      | GDK_BUTTON_RELEASE_MASK,
                      FALSE, NULL, event->time) == 0) {
            day_view->selection_start_row = row;
            day_view->selection_start_col = day;
            day_view->selection_end_row = row;
            day_view->selection_end_col = day;
            day_view->selection_drag_pos = E_DAY_VIEW_DRAG_END;
            day_view->selection_in_top_canvas = FALSE;

            /* FIXME: Optimise? */
            gtk_widget_queue_draw (day_view->top_canvas);
            gtk_widget_queue_draw (day_view->main_canvas);
        }
    } else if (event->button == 3) {
        e_day_view_on_event_right_click (day_view, event, -1, -1);
    }

    return TRUE;
}


static gboolean
e_day_view_on_long_event_button_press (EDayView     *day_view,
                       gint      event_num,
                       GdkEventButton   *event,
                       EDayViewPosition  pos,
                       gint      event_x,
                       gint      event_y)
{
    if (event->button == 1) {
        if (event->type == GDK_BUTTON_PRESS) {
            e_day_view_on_long_event_click (day_view, event_num,
                            event, pos,
                            event_x, event_y);
            return TRUE;
        } else if (event->type == GDK_2BUTTON_PRESS) {
            e_day_view_on_event_double_click (day_view, -1,
                              event_num);
            return TRUE;
        }
    } else if (event->button == 3) {
        e_day_view_on_event_right_click (day_view, event, 
                         E_DAY_VIEW_LONG_EVENT,
                         event_num);
        return TRUE;
    }
    return FALSE;
}


static gboolean
e_day_view_on_event_button_press (EDayView    *day_view,
                  gint         day,
                  gint         event_num,
                  GdkEventButton  *event,
                  EDayViewPosition pos,
                  gint         event_x,
                  gint         event_y)
{
    if (event->button == 1) {
        if (event->type == GDK_BUTTON_PRESS) {
            e_day_view_on_event_click (day_view, day, event_num,
                           event, pos,
                           event_x, event_y);
            return TRUE;
        } else if (event->type == GDK_2BUTTON_PRESS) {
            e_day_view_on_event_double_click (day_view, day,
                              event_num);
            return TRUE;
        }
    } else if (event->button == 3) {
        e_day_view_on_event_right_click (day_view, event,
                         day, event_num);
        return TRUE;
    }
    return FALSE;
}


static void
e_day_view_on_long_event_click (EDayView *day_view,
                gint event_num,
                GdkEventButton  *bevent,
                EDayViewPosition pos,
                gint         event_x,
                gint         event_y)
{
    EDayViewEvent *event;
    gint start_day, end_day, day;
    gint item_x, item_y, item_w, item_h;

    event = &g_array_index (day_view->long_events, EDayViewEvent,
                event_num);

    /* Ignore clicks on the EText while editing. */
    if (pos == E_DAY_VIEW_POS_EVENT
        && E_TEXT (event->canvas_item)->editing)
        return;

    if (!event->ico->recur
        && (pos == E_DAY_VIEW_POS_LEFT_EDGE
        || pos == E_DAY_VIEW_POS_RIGHT_EDGE)) {
        if (!e_day_view_find_long_event_days (day_view, event,
                              &start_day, &end_day))
            return;

        /* Grab the keyboard focus, so the event being edited is saved
           and we can use the Escape key to abort the resize. */
        if (!GTK_WIDGET_HAS_FOCUS (day_view))
            gtk_widget_grab_focus (GTK_WIDGET (day_view));

        if (gdk_pointer_grab (GTK_LAYOUT (day_view->top_canvas)->bin_window, FALSE,
                      GDK_POINTER_MOTION_MASK
                      | GDK_BUTTON_RELEASE_MASK,
                      FALSE, NULL, bevent->time) == 0) {

            day_view->resize_event_day = E_DAY_VIEW_LONG_EVENT;
            day_view->resize_event_num = event_num;
            day_view->resize_drag_pos = pos;
            day_view->resize_start_row = start_day;
            day_view->resize_end_row = end_day;

            /* Create the edit rect if necessary. */
            e_day_view_reshape_resize_long_event_rect_item (day_view);

            /* Make sure the text item is on top. */
            gnome_canvas_item_raise_to_top (day_view->resize_long_event_rect_item);

            /* Raise the event's item, above the rect as well. */
            gnome_canvas_item_raise_to_top (event->canvas_item);
        }
    } else if (e_day_view_get_long_event_position (day_view, event_num,
                               &start_day, &end_day,
                               &item_x, &item_y,
                               &item_w, &item_h)) {
        /* Remember the item clicked and the mouse position,
           so we can start a drag if the mouse moves. */
        day_view->pressed_event_day = E_DAY_VIEW_LONG_EVENT;
        day_view->pressed_event_num = event_num;

        day_view->drag_event_x = event_x;
        day_view->drag_event_y = event_y;

        e_day_view_convert_position_in_top_canvas (day_view,
                               event_x, event_y,
                               &day, NULL);
        day_view->drag_event_offset = day - start_day;
    }
}


static void
e_day_view_on_event_click (EDayView *day_view,
               gint day,
               gint event_num,
               GdkEventButton  *bevent,
               EDayViewPosition pos,
               gint       event_x,
               gint       event_y)
{
    EDayViewEvent *event;
    gint tmp_day, row, start_row;

    event = &g_array_index (day_view->events[day], EDayViewEvent,
                event_num);

    /* Ignore clicks on the EText while editing. */
    if (pos == E_DAY_VIEW_POS_EVENT
        && E_TEXT (event->canvas_item)->editing)
        return;

    if (!event->ico->recur
        && (pos == E_DAY_VIEW_POS_TOP_EDGE
        || pos == E_DAY_VIEW_POS_BOTTOM_EDGE)) {
        /* Grab the keyboard focus, so the event being edited is saved
           and we can use the Escape key to abort the resize. */
        if (!GTK_WIDGET_HAS_FOCUS (day_view))
            gtk_widget_grab_focus (GTK_WIDGET (day_view));

        if (gdk_pointer_grab (GTK_LAYOUT (day_view->main_canvas)->bin_window, FALSE,
                      GDK_POINTER_MOTION_MASK
                      | GDK_BUTTON_RELEASE_MASK,
                      FALSE, NULL, bevent->time) == 0) {

            day_view->resize_event_day = day;
            day_view->resize_event_num = event_num;
            day_view->resize_drag_pos = pos;
            day_view->resize_start_row = event->start_minute / day_view->mins_per_row;
            day_view->resize_end_row = (event->end_minute - 1) / day_view->mins_per_row;

            day_view->resize_bars_event_day = day;
            day_view->resize_bars_event_num = event_num;

            /* Create the edit rect if necessary. */
            e_day_view_reshape_resize_rect_item (day_view);

            e_day_view_reshape_main_canvas_resize_bars (day_view);

            /* Make sure the text item is on top. */
            gnome_canvas_item_raise_to_top (day_view->resize_rect_item);
            gnome_canvas_item_raise_to_top (day_view->resize_bar_item);

            /* Raise the event's item, above the rect as well. */
            gnome_canvas_item_raise_to_top (event->canvas_item);
        }

    } else {
        /* Remember the item clicked and the mouse position,
           so we can start a drag if the mouse moves. */
        day_view->pressed_event_day = day;
        day_view->pressed_event_num = event_num;

        day_view->drag_event_x = event_x;
        day_view->drag_event_y = event_y;

        e_day_view_convert_position_in_main_canvas (day_view,
                                event_x, event_y,
                                &tmp_day, &row,
                                NULL);
        start_row = event->start_minute / day_view->mins_per_row;
        day_view->drag_event_offset = row - start_row;
    }
}


static void
e_day_view_reshape_resize_long_event_rect_item (EDayView *day_view)
{
    gint day, event_num, start_day, end_day;
    gint item_x, item_y, item_w, item_h;
    gdouble x1, y1, x2, y2;

    day = day_view->resize_event_day;
    event_num = day_view->resize_event_num;

    /* If we're not resizing an event, or the event is not shown,
       hide the resize bars. */
    if (day_view->resize_drag_pos == E_DAY_VIEW_POS_NONE
        || !e_day_view_get_long_event_position (day_view, event_num,
                            &start_day, &end_day,
                            &item_x, &item_y,
                            &item_w, &item_h)) {
        gnome_canvas_item_hide (day_view->resize_long_event_rect_item);
        return;
    }

    x1 = item_x;
    y1 = item_y;
    x2 = item_x + item_w - 1;
    y2 = item_y + item_h - 1;

    gnome_canvas_item_set (day_view->resize_long_event_rect_item,
                   "x1", x1,
                   "y1", y1,
                   "x2", x2,
                   "y2", y2,
                   NULL);
    gnome_canvas_item_show (day_view->resize_long_event_rect_item);
}


static void
e_day_view_reshape_resize_rect_item (EDayView *day_view)
{
    gint day, event_num;
    gint item_x, item_y, item_w, item_h;
    gdouble x1, y1, x2, y2;

    day = day_view->resize_event_day;
    event_num = day_view->resize_event_num;

    /* If we're not resizing an event, or the event is not shown,
       hide the resize bars. */
    if (day_view->resize_drag_pos == E_DAY_VIEW_POS_NONE
        || !e_day_view_get_event_position (day_view, day, event_num,
                           &item_x, &item_y,
                           &item_w, &item_h)) {
        gnome_canvas_item_hide (day_view->resize_rect_item);
        return;
    }

    x1 = item_x;
    y1 = item_y;
    x2 = item_x + item_w - 1;
    y2 = item_y + item_h - 1;

    gnome_canvas_item_set (day_view->resize_rect_item,
                   "x1", x1 + E_DAY_VIEW_BAR_WIDTH - 1,
                   "y1", y1,
                   "x2", x2,
                   "y2", y2,
                   NULL);
    gnome_canvas_item_show (day_view->resize_rect_item);

    gnome_canvas_item_set (day_view->resize_bar_item,
                   "x1", x1,
                   "y1", y1,
                   "x2", x1 + E_DAY_VIEW_BAR_WIDTH - 1,
                   "y2", y2,
                   NULL);
    gnome_canvas_item_show (day_view->resize_bar_item);
}


static void
e_day_view_on_event_double_click (EDayView *day_view,
                  gint day,
                  gint event_num)
{
#if 0
    g_print ("In e_day_view_on_event_double_click\n");
#endif

}


static void
e_day_view_on_event_right_click (EDayView *day_view,
                 GdkEventButton *bevent,
                 gint day,
                 gint event_num)
{
    EDayViewEvent *event;
    int have_selection, not_being_edited, items, i;
    struct menu_item *context_menu;
    
    static struct menu_item main_items[] = {
        { N_("New appointment..."), (GtkSignalFunc) e_day_view_on_new_appointment, NULL, TRUE }
    };

    static struct menu_item child_items[] = {
        { N_("Edit this appointment..."), (GtkSignalFunc) e_day_view_on_edit_appointment, NULL, TRUE },
        { N_("Delete this appointment"), (GtkSignalFunc) e_day_view_on_delete_appointment, NULL, TRUE },
        { NULL, NULL, NULL, TRUE },
        { N_("New appointment..."), (GtkSignalFunc) e_day_view_on_new_appointment, NULL, TRUE }
    };

    static struct menu_item recur_child_items[] = {
        { N_("Edit this appointment..."), (GtkSignalFunc) e_day_view_on_edit_appointment, NULL, TRUE },
        { N_("Make this appointment movable"), (GtkSignalFunc) e_day_view_on_unrecur_appointment, NULL, TRUE },
        { N_("Delete this occurrence"), (GtkSignalFunc) e_day_view_on_delete_occurrence, NULL, TRUE },
        { N_("Delete all occurrences"), (GtkSignalFunc) e_day_view_on_delete_appointment, NULL, TRUE },
        { NULL, NULL, NULL, TRUE },
        { N_("New appointment..."), (GtkSignalFunc) e_day_view_on_new_appointment, NULL, TRUE }
    };

    have_selection = GTK_WIDGET_HAS_FOCUS (day_view)
        && day_view->selection_start_col != -1;

    if (event_num == -1) {
        items = 1;
        context_menu = &main_items[0];
        context_menu[0].sensitive = have_selection;
    } else {
        if (day == E_DAY_VIEW_LONG_EVENT)
            event = &g_array_index (day_view->long_events,
                        EDayViewEvent, event_num);
        else
            event = &g_array_index (day_view->events[day],
                        EDayViewEvent, event_num);

        /* This used to be set only if the event wasn't being edited
           in the event editor, but we can't check that at present.
           We could possibly set up another method of checking it. */
        not_being_edited = TRUE;

        if (event->ico->recur) {
            items = 6;
            context_menu = &recur_child_items[0];
            context_menu[0].sensitive = not_being_edited;
            context_menu[1].sensitive = not_being_edited;
            context_menu[2].sensitive = not_being_edited;
            context_menu[3].sensitive = not_being_edited;
            context_menu[5].sensitive = have_selection;
        } else {
            items = 4;
            context_menu = &child_items[0];
            context_menu[0].sensitive = not_being_edited;
            context_menu[1].sensitive = not_being_edited;
            context_menu[3].sensitive = have_selection;
        }
    }

    for (i = 0; i < items; i++)
        context_menu[i].data = day_view;
             
    day_view->popup_event_day = day;
    day_view->popup_event_num = event_num;
    popup_menu (context_menu, items, bevent);
}


static void
e_day_view_on_new_appointment (GtkWidget *widget, gpointer data)
{
    EDayView *day_view;
    GtkWidget *event_editor;
    iCalObject *ico;
    
    day_view = E_DAY_VIEW (data);

    ico = ical_new ("", user_name, "");
    ico->new = 1;
    
    e_day_view_get_selected_time_range (day_view, &ico->dtstart,
                        &ico->dtend);
    event_editor = event_editor_new (day_view->calendar, ico);
    ical_object_unref (ico);
    gtk_widget_show (event_editor);
}


static void
e_day_view_on_edit_appointment (GtkWidget *widget, gpointer data)
{
    EDayView *day_view;
    EDayViewEvent *event;
    GtkWidget *event_editor;
    iCalObject *ico;

    day_view = E_DAY_VIEW (data);

    event = e_day_view_get_popup_menu_event (day_view);
    if (event == NULL)
        return;

    /* We must duplicate the iCalObject, since the event editor will
       change the fields. */
    ico = ical_object_duplicate (event->ico);

    event_editor = event_editor_new (day_view->calendar, ico);
    ical_object_unref (ico);
    gtk_widget_show (event_editor);
}


static void
e_day_view_on_delete_occurrence (GtkWidget *widget, gpointer data)
{
    EDayView *day_view;
    EDayViewEvent *event;

    day_view = E_DAY_VIEW (data);

    event = e_day_view_get_popup_menu_event (day_view);
    if (event == NULL)
        return;

    ical_object_add_exdate (event->ico, event->start);
    gnome_calendar_object_changed (day_view->calendar, event->ico,
                       CHANGE_DATES);
}


static void
e_day_view_on_delete_appointment (GtkWidget *widget, gpointer data)
{
    EDayView *day_view;
    EDayViewEvent *event;

    day_view = E_DAY_VIEW (data);

    event = e_day_view_get_popup_menu_event (day_view);
    if (event == NULL)
        return;

    gnome_calendar_remove_object (day_view->calendar, event->ico);
}


static void
e_day_view_on_unrecur_appointment (GtkWidget *widget, gpointer data)
{
    EDayView *day_view;
    EDayViewEvent *event;
    iCalObject *ico;

    day_view = E_DAY_VIEW (data);

    event = e_day_view_get_popup_menu_event (day_view);
    if (event == NULL)
        return;
    
    /* For the unrecurred instance we duplicate the original object,
       create a new uid for it, get rid of the recurrence rules, and set
       the start & end times to the instances times. */
    ico = ical_object_duplicate (event->ico);
    g_free (ico->uid);
    ico->uid = ical_gen_uid ();
    g_free (ico->recur);
    ico->recur = 0;
    ico->dtstart = event->start;
    ico->dtend   = event->end;
    
    /* For the recurring object, we add a exception to get rid of the
       instance. */
    ical_object_add_exdate (event->ico, event->start);

    gnome_calendar_object_changed (day_view->calendar, event->ico,
                       CHANGE_ALL);
    gnome_calendar_add_object (day_view->calendar, ico);

    ical_object_unref (ico);
}


static EDayViewEvent*
e_day_view_get_popup_menu_event (EDayView *day_view)
{
    if (day_view->popup_event_num == -1)
        return NULL;

    if (day_view->popup_event_day == E_DAY_VIEW_LONG_EVENT)
        return &g_array_index (day_view->long_events,
                       EDayViewEvent,
                       day_view->popup_event_num);
    else
        return &g_array_index (day_view->events[day_view->popup_event_day],
                       EDayViewEvent,
                       day_view->popup_event_num);
}


static gboolean
e_day_view_on_top_canvas_button_release (GtkWidget *widget,
                     GdkEventButton *event,
                     EDayView *day_view)
{
    if (day_view->selection_drag_pos != E_DAY_VIEW_DRAG_NONE) {
        day_view->selection_drag_pos = E_DAY_VIEW_DRAG_NONE;
        gdk_pointer_ungrab (event->time);
        e_day_view_update_calendar_selection_time (day_view);
    } else if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE) {
        e_day_view_finish_long_event_resize (day_view);
        gdk_pointer_ungrab (event->time);
    } else if (day_view->pressed_event_day != -1) {
        e_day_view_start_editing_event (day_view,
                        day_view->pressed_event_day,
                        day_view->pressed_event_num,
                        NULL);
    }

    day_view->pressed_event_day = -1;

    return FALSE;
}


static gboolean
e_day_view_on_main_canvas_button_release (GtkWidget *widget,
                      GdkEventButton *event,
                      EDayView *day_view)
{
    if (day_view->selection_drag_pos != E_DAY_VIEW_DRAG_NONE) {
        day_view->selection_drag_pos = E_DAY_VIEW_DRAG_NONE;
        gdk_pointer_ungrab (event->time);
        e_day_view_stop_auto_scroll (day_view);
        e_day_view_update_calendar_selection_time (day_view);
    } else if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE) {
        e_day_view_finish_resize (day_view);
        gdk_pointer_ungrab (event->time);
        e_day_view_stop_auto_scroll (day_view);
    } else if (day_view->pressed_event_day != -1) {
        e_day_view_start_editing_event (day_view,
                        day_view->pressed_event_day,
                        day_view->pressed_event_num,
                        NULL);
    }

    day_view->pressed_event_day = -1;

    return FALSE;
}


static void
e_day_view_update_calendar_selection_time (EDayView *day_view)
{
    time_t start, end;

    e_day_view_get_selected_time_range (day_view, &start, &end);
    gnome_calendar_set_selected_time_range (day_view->calendar,
                        start, end);
}


static gboolean
e_day_view_on_top_canvas_motion (GtkWidget *widget,
                 GdkEventMotion *mevent,
                 EDayView *day_view)
{
    EDayViewEvent *event = NULL;
    EDayViewPosition pos;
    gint event_x, event_y, scroll_x, scroll_y, canvas_x, canvas_y;
    gint day, event_num;
    GdkCursor *cursor;

#if 0
    g_print ("In e_day_view_on_top_canvas_motion\n");
#endif

    /* Convert the coords to the main canvas window, or return if the
       window is not found. */
    if (!e_day_view_convert_event_coords (day_view, (GdkEvent*) mevent,
                          GTK_LAYOUT (widget)->bin_window,
                          &event_x, &event_y))
        return FALSE;

    /* The top canvas doesn't scroll, but just in case. */
    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (widget),
                     &scroll_x, &scroll_y);
    canvas_x = event_x + scroll_x;
    canvas_y = event_y + scroll_y;

    pos = e_day_view_convert_position_in_top_canvas (day_view,
                             canvas_x, canvas_y,
                             &day, &event_num);
    if (event_num != -1)
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);

    if (day_view->selection_drag_pos != E_DAY_VIEW_DRAG_NONE) {
        e_day_view_update_selection (day_view, -1, day);
        return TRUE;
    } else if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE) {
        if (pos != E_DAY_VIEW_POS_OUTSIDE) {
            e_day_view_update_long_event_resize (day_view, day);
            return TRUE;
        }
    } else if (day_view->pressed_event_day == E_DAY_VIEW_LONG_EVENT) {
        GtkTargetList *target_list;

        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    day_view->pressed_event_num);

        if (!event->ico->recur
            && (abs (canvas_x - day_view->drag_event_x) > E_DAY_VIEW_DRAG_START_OFFSET
            || abs (canvas_y - day_view->drag_event_y) > E_DAY_VIEW_DRAG_START_OFFSET)) {
            day_view->drag_event_day = day_view->pressed_event_day;
            day_view->drag_event_num = day_view->pressed_event_num;
            day_view->pressed_event_day = -1;

            /* Hide the horizontal bars. */
            if (day_view->resize_bars_event_day != -1) {
                day_view->resize_bars_event_day = -1;
                day_view->resize_bars_event_num = -1;
                gnome_canvas_item_hide (day_view->main_canvas_top_resize_bar_item);
                gnome_canvas_item_hide (day_view->main_canvas_bottom_resize_bar_item);
            }

            target_list = gtk_target_list_new (target_table,
                               n_targets);
            gtk_drag_begin (widget, target_list,
                    GDK_ACTION_COPY | GDK_ACTION_MOVE,
                    1, (GdkEvent*)mevent);
            gtk_target_list_unref (target_list);
        }
    } else {
        cursor = day_view->normal_cursor;

        /* Recurring events can't be resized. */
        if (event && !event->ico->recur) {
            switch (pos) {
            case E_DAY_VIEW_POS_LEFT_EDGE:
            case E_DAY_VIEW_POS_RIGHT_EDGE:
                cursor = day_view->resize_width_cursor;
                break;
            default:
                break;
            }
        }

        /* Only set the cursor if it is different to last one set. */
        if (day_view->last_cursor_set_in_top_canvas != cursor) {
            day_view->last_cursor_set_in_top_canvas = cursor;
            gdk_window_set_cursor (widget->window, cursor);
        }

    }

    return FALSE;
}


static gboolean
e_day_view_on_main_canvas_motion (GtkWidget *widget,
                  GdkEventMotion *mevent,
                  EDayView *day_view)
{
    EDayViewEvent *event = NULL;
    EDayViewPosition pos;
    gint event_x, event_y, scroll_x, scroll_y, canvas_x, canvas_y;
    gint row, day, event_num;
    GdkCursor *cursor;

#if 0
    g_print ("In e_day_view_on_main_canvas_motion\n");
#endif

    /* Convert the coords to the main canvas window, or return if the
       window is not found. */
    if (!e_day_view_convert_event_coords (day_view, (GdkEvent*) mevent,
                          GTK_LAYOUT (widget)->bin_window,
                          &event_x, &event_y))
        return FALSE;

    day_view->last_mouse_x = event_x;
    day_view->last_mouse_y = event_y;

    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (widget),
                     &scroll_x, &scroll_y);
    canvas_x = event_x + scroll_x;
    canvas_y = event_y + scroll_y;

    pos = e_day_view_convert_position_in_main_canvas (day_view,
                              canvas_x, canvas_y,
                              &day, &row,
                              &event_num);
    if (event_num != -1)
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);

    if (day_view->selection_drag_pos != E_DAY_VIEW_DRAG_NONE) {
        if (pos != E_DAY_VIEW_POS_OUTSIDE) {
            e_day_view_update_selection (day_view, row, day);
            e_day_view_check_auto_scroll (day_view, event_y);
            return TRUE;
        }
    } else if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE) {
        if (pos != E_DAY_VIEW_POS_OUTSIDE) {
            e_day_view_update_resize (day_view, row);
            e_day_view_check_auto_scroll (day_view, event_y);
            return TRUE;
        }
    } else if (day_view->pressed_event_day != -1
           && day_view->pressed_event_day != E_DAY_VIEW_LONG_EVENT) {
        GtkTargetList *target_list;

        event = &g_array_index (day_view->events[day_view->pressed_event_day], EDayViewEvent, day_view->pressed_event_num);

        if (!event->ico->recur
            && (abs (canvas_x - day_view->drag_event_x) > E_DAY_VIEW_DRAG_START_OFFSET
            || abs (canvas_y - day_view->drag_event_y) > E_DAY_VIEW_DRAG_START_OFFSET)) {
            day_view->drag_event_day = day_view->pressed_event_day;
            day_view->drag_event_num = day_view->pressed_event_num;
            day_view->pressed_event_day = -1;

            /* Hide the horizontal bars. */
            if (day_view->resize_bars_event_day != -1) {
                day_view->resize_bars_event_day = -1;
                day_view->resize_bars_event_num = -1;
                gnome_canvas_item_hide (day_view->main_canvas_top_resize_bar_item);
                gnome_canvas_item_hide (day_view->main_canvas_bottom_resize_bar_item);
            }

            target_list = gtk_target_list_new (target_table,
                               n_targets);
            gtk_drag_begin (widget, target_list,
                    GDK_ACTION_COPY | GDK_ACTION_MOVE,
                    1, (GdkEvent*)mevent);
            gtk_target_list_unref (target_list);
        }
    } else {
        cursor = day_view->normal_cursor;

        /* Recurring events can't be resized. */
        if (event && !event->ico->recur) {
            switch (pos) {
            case E_DAY_VIEW_POS_LEFT_EDGE:
                cursor = day_view->move_cursor;
                break;
            case E_DAY_VIEW_POS_TOP_EDGE:
            case E_DAY_VIEW_POS_BOTTOM_EDGE:
                cursor = day_view->resize_height_cursor;
                break;
            default:
                break;
            }
        }

        /* Only set the cursor if it is different to last one set. */
        if (day_view->last_cursor_set_in_main_canvas != cursor) {
            day_view->last_cursor_set_in_main_canvas = cursor;
            gdk_window_set_cursor (widget->window, cursor);
        }
    }

    return FALSE;
}


static void
e_day_view_update_selection (EDayView *day_view,
                 gint row,
                 gint col)
{
    gint tmp_row, tmp_col;
    gboolean need_redraw = FALSE;

#if 0
    g_print ("Updating selection %i,%i\n", col, row);
#endif

    day_view->selection_in_top_canvas = (row == -1) ? TRUE : FALSE;

    if (day_view->selection_drag_pos == E_DAY_VIEW_DRAG_START) {
        if (row != day_view->selection_start_row
            || col != day_view->selection_start_col) {
            need_redraw = TRUE;
            day_view->selection_start_row = row;
            day_view->selection_start_col = col;
        }
    } else {
        if (row != day_view->selection_end_row
            || col != day_view->selection_end_col) {
            need_redraw = TRUE;
            day_view->selection_end_row = row;
            day_view->selection_end_col = col;
        }
    }

    /* Switch the drag position if necessary. */
    if (day_view->selection_start_col > day_view->selection_end_col
        || (day_view->selection_start_col == day_view->selection_end_col
        && day_view->selection_start_row > day_view->selection_end_row)) {
        tmp_row = day_view->selection_start_row;
        tmp_col = day_view->selection_start_col;
        day_view->selection_start_col = day_view->selection_end_col;
        day_view->selection_start_row = day_view->selection_end_row;
        day_view->selection_end_col = tmp_col;
        day_view->selection_end_row = tmp_row;
        if (day_view->selection_drag_pos == E_DAY_VIEW_DRAG_START)
            day_view->selection_drag_pos = E_DAY_VIEW_DRAG_END;
        else
            day_view->selection_drag_pos = E_DAY_VIEW_DRAG_START;
    }

    /* FIXME: Optimise? */
    if (need_redraw) {
        gtk_widget_queue_draw (day_view->top_canvas);
        gtk_widget_queue_draw (day_view->main_canvas);
    }
}


static void
e_day_view_update_long_event_resize (EDayView *day_view,
                     gint day)
{
    EDayViewEvent *event;
    gint event_num;
    gboolean need_reshape = FALSE;

#if 0
    g_print ("Updating resize Day:%i\n", day);
#endif

    event_num = day_view->resize_event_num;
    event = &g_array_index (day_view->long_events, EDayViewEvent,
                event_num);

    if (day_view->resize_drag_pos == E_DAY_VIEW_POS_LEFT_EDGE) {
        day = MIN (day, day_view->resize_end_row);
        if (day != day_view->resize_start_row) {
            need_reshape = TRUE;
            day_view->resize_start_row = day;
            
        }
    } else {
        day = MAX (day, day_view->resize_start_row);
        if (day != day_view->resize_end_row) {
            need_reshape = TRUE;
            day_view->resize_end_row = day;
        }
    }

    /* FIXME: Optimise? */
    if (need_reshape) {
        e_day_view_reshape_long_event (day_view, event_num);
        e_day_view_reshape_resize_long_event_rect_item (day_view);
        gtk_widget_queue_draw (day_view->top_canvas);
    }
}


static void
e_day_view_update_resize (EDayView *day_view,
              gint row)
{
    EDayViewEvent *event;
    gint day, event_num;
    gboolean need_reshape = FALSE;

#if 0
    g_print ("Updating resize Row:%i\n", row);
#endif

    day = day_view->resize_event_day;
    event_num = day_view->resize_event_num;
    event = &g_array_index (day_view->events[day], EDayViewEvent,
                event_num);

    if (day_view->resize_drag_pos == E_DAY_VIEW_POS_TOP_EDGE) {
        row = MIN (row, day_view->resize_end_row);
        if (row != day_view->resize_start_row) {
            need_reshape = TRUE;
            day_view->resize_start_row = row;
            
        }
    } else {
        row = MAX (row, day_view->resize_start_row);
        if (row != day_view->resize_end_row) {
            need_reshape = TRUE;
            day_view->resize_end_row = row;
        }
    }

    /* FIXME: Optimise? */
    if (need_reshape) {
        e_day_view_reshape_day_event (day_view, day, event_num);
        e_day_view_reshape_resize_rect_item (day_view);
        e_day_view_reshape_main_canvas_resize_bars (day_view);
        gtk_widget_queue_draw (day_view->main_canvas);
    }
}


/* This converts the resize start or end row back to a time and updates the
   event. */
static void
e_day_view_finish_long_event_resize (EDayView *day_view)
{
    EDayViewEvent *event;
    gint event_num;
    iCalObject ico;

    event_num = day_view->resize_event_num;
    event = &g_array_index (day_view->long_events, EDayViewEvent,
                event_num);

    /* We use a temporary shallow copy of the ico since we don't want to
       change the original ico here. Otherwise we would not detect that
       the event's time had changed in the "update_event" callback. */
    ico = *event->ico;

    if (day_view->resize_drag_pos == E_DAY_VIEW_POS_LEFT_EDGE) {
        ico.dtstart = day_view->day_starts[day_view->resize_start_row];
    } else {
        ico.dtend = day_view->day_starts[day_view->resize_end_row + 1];
    }

    gnome_canvas_item_hide (day_view->resize_long_event_rect_item);

    day_view->resize_drag_pos = E_DAY_VIEW_POS_NONE;

    /* Notify calendar of change */
    gnome_calendar_object_changed (day_view->calendar, &ico, CHANGE_DATES);
}


/* This converts the resize start or end row back to a time and updates the
   event. */
static void
e_day_view_finish_resize (EDayView *day_view)
{
    EDayViewEvent *event;
    gint day, event_num;
    iCalObject ico;

    day = day_view->resize_event_day;
    event_num = day_view->resize_event_num;
    event = &g_array_index (day_view->events[day], EDayViewEvent,
                event_num);

    /* We use a temporary shallow copy of the ico since we don't want to
       change the original ico here. Otherwise we would not detect that
       the event's time had changed in the "update_event" callback. */
    ico = *event->ico;

    if (day_view->resize_drag_pos == E_DAY_VIEW_POS_TOP_EDGE) {
        ico.dtstart = e_day_view_convert_grid_position_to_time (day_view, day, day_view->resize_start_row);
    } else {
        ico.dtend = e_day_view_convert_grid_position_to_time (day_view, day, day_view->resize_end_row + 1);
    }

    gnome_canvas_item_hide (day_view->resize_rect_item);
    gnome_canvas_item_hide (day_view->resize_bar_item);

    /* Hide the horizontal bars. */
    day_view->resize_bars_event_day = -1;
    day_view->resize_bars_event_num = -1;
    gnome_canvas_item_hide (day_view->main_canvas_top_resize_bar_item);
    gnome_canvas_item_hide (day_view->main_canvas_bottom_resize_bar_item);

    day_view->resize_drag_pos = E_DAY_VIEW_POS_NONE;

    /* Notify calendar of change */
    gnome_calendar_object_changed (day_view->calendar, &ico, CHANGE_DATES);
}


static void
e_day_view_abort_resize (EDayView *day_view,
             guint32 time)
{
    gint day, event_num;

    if (day_view->resize_drag_pos == E_DAY_VIEW_POS_NONE)
        return;

    day_view->resize_drag_pos = E_DAY_VIEW_POS_NONE;
    gdk_pointer_ungrab (time);

    day = day_view->resize_event_day;
    event_num = day_view->resize_event_num;

    if (day == E_DAY_VIEW_LONG_EVENT) {
        e_day_view_reshape_long_event (day_view, event_num);
        gtk_widget_queue_draw (day_view->top_canvas);
    
        day_view->last_cursor_set_in_top_canvas = day_view->normal_cursor;
        gdk_window_set_cursor (day_view->top_canvas->window,
                       day_view->normal_cursor);
        gnome_canvas_item_hide (day_view->resize_long_event_rect_item);
    } else {
        e_day_view_reshape_day_event (day_view, day, event_num);
        e_day_view_reshape_main_canvas_resize_bars (day_view);
        gtk_widget_queue_draw (day_view->main_canvas);
    
        day_view->last_cursor_set_in_main_canvas = day_view->normal_cursor;
        gdk_window_set_cursor (day_view->main_canvas->window,
                       day_view->normal_cursor);
        gnome_canvas_item_hide (day_view->resize_rect_item);
        gnome_canvas_item_hide (day_view->resize_bar_item);
    }
}


static void
e_day_view_reload_events (EDayView *day_view)
{
    e_day_view_free_events (day_view);

    /* Reset all our indices. */
    day_view->editing_event_day = -1;
    day_view->popup_event_day = -1;
    day_view->resize_bars_event_day = -1;
    day_view->resize_event_day = -1;
    day_view->pressed_event_day = -1;
    day_view->drag_event_day = -1;

    /* If both lower & upper are 0, then the time range hasn't been set,
       so we don't try to load any events. */
    if (day_view->calendar
        && (day_view->lower != 0 || day_view->upper != 0)) {
        calendar_iterate (day_view->calendar,
                  day_view->lower,
                  day_view->upper,
                  e_day_view_add_event,
                  day_view);
    }

    /* We need to do this to make sure the top canvas is resized. */
    day_view->long_events_need_layout = TRUE;

    e_day_view_check_layout (day_view);
    e_day_view_reshape_main_canvas_resize_bars (day_view);

    gtk_widget_queue_draw (day_view->top_canvas);
    gtk_widget_queue_draw (day_view->main_canvas);
}


static void
e_day_view_free_events (EDayView *day_view)
{
    gint day;

    e_day_view_free_event_array (day_view, day_view->long_events);

    for (day = 0; day < E_DAY_VIEW_MAX_DAYS; day++)
        e_day_view_free_event_array (day_view, day_view->events[day]);
}


static void
e_day_view_free_event_array (EDayView *day_view,
                 GArray *array)
{
    EDayViewEvent *event;
    gint event_num;

    for (event_num = 0; event_num < array->len; event_num++) {
        event = &g_array_index (array, EDayViewEvent, event_num);
        if (event->canvas_item)
            gtk_object_destroy (GTK_OBJECT (event->canvas_item));
        ical_object_unref (event->ico);
    }

    g_array_set_size (array, 0);
}


/* This adds one event to the view, adding it to the appropriate array. */
static int
e_day_view_add_event (iCalObject *ico,
              time_t      start,
              time_t      end,
              gpointer    data)

{
    EDayView *day_view;
    EDayViewEvent event;
    gint day, offset;
    struct tm start_tm, end_tm;

    day_view = E_DAY_VIEW (data);

    /* Check that the event times are valid. */
    g_return_val_if_fail (start <= end, TRUE);
    g_return_val_if_fail (start < day_view->upper, TRUE);
    g_return_val_if_fail (end > day_view->lower, TRUE);

    start_tm = *(localtime (&start));
    end_tm = *(localtime (&end));
    
    event.ico = ico;
    ical_object_ref (ico);
    event.start = start;
    event.end = end;
    event.canvas_item = NULL;

    /* Calculate the start & end minute, relative to the top of the
       display. */
    offset = day_view->first_hour_shown * 60
        + day_view->first_minute_shown;
    event.start_minute = start_tm.tm_hour * 60 + start_tm.tm_min - offset;
    event.end_minute = end_tm.tm_hour * 60 + end_tm.tm_min - offset;

    event.start_row_or_col = -1;
    event.num_columns = -1;

    /* Find out which array to add the event to. */
    for (day = 0; day < day_view->days_shown; day++) {
        if (start >= day_view->day_starts[day]
            && end <= day_view->day_starts[day + 1]) {

            /* Special case for when the appointment ends at
               midnight, i.e. the start of the next day. */
            if (end == day_view->day_starts[day + 1]) {

                /* If the event last the entire day, then we
                   skip it here so it gets added to the top
                   canvas. */
                if (start == day_view->day_starts[day])
                    break;

                event.end_minute = 24 * 60;
            }

            g_array_append_val (day_view->events[day], event);
            day_view->events_sorted[day] = FALSE;
            day_view->need_layout[day] = TRUE;
            return TRUE;
        }
    }

    /* The event wasn't within one day so it must be a long event,
       i.e. shown in the top canvas. */
    g_array_append_val (day_view->long_events, event);
    day_view->long_events_sorted = FALSE;
    day_view->long_events_need_layout = TRUE;
    return TRUE;
}


/* This lays out the short (less than 1 day) events in the columns. 
   Any long events are simply skipped. */
void
e_day_view_check_layout (EDayView *day_view)
{
    gint day;

    /* Don't bother if we aren't visible. */
    if (!GTK_WIDGET_VISIBLE (day_view))
        return;

    /* Make sure the events are sorted (by start and size). */
    e_day_view_ensure_events_sorted (day_view);

    for (day = 0; day < day_view->days_shown; day++) {
        if (day_view->need_layout[day])
            e_day_view_layout_day_events (day_view, day);

        if (day_view->need_layout[day]
            || day_view->need_reshape[day]) {
            e_day_view_reshape_day_events (day_view, day);

            if (day_view->resize_bars_event_day == day)
                e_day_view_reshape_main_canvas_resize_bars (day_view);
        }

        day_view->need_layout[day] = FALSE;
        day_view->need_reshape[day] = FALSE;
    }

    if (day_view->long_events_need_layout)
        e_day_view_layout_long_events (day_view);

    if (day_view->long_events_need_layout
        || day_view->long_events_need_reshape)
        e_day_view_reshape_long_events (day_view);

    day_view->long_events_need_layout = FALSE;
    day_view->long_events_need_reshape = FALSE;
}


static void
e_day_view_layout_long_events (EDayView *day_view)
{
    EDayViewEvent *event;
    gint event_num, old_rows_in_top_display, top_canvas_height, top_rows;
    guint8 *grid;

    /* This is a temporary 2-d grid which is used to place events.
       Each element is 0 if the position is empty, or 1 if occupied.
       We allocate the maximum size possible here, assuming that each
       event will need its own row. */
    grid = g_new0 (guint8,
               day_view->long_events->len * E_DAY_VIEW_MAX_DAYS);

    /* Reset the number of rows in the top display to 0. It will be
       updated as events are layed out below. */
    old_rows_in_top_display = day_view->rows_in_top_display;
    day_view->rows_in_top_display = 0;

    /* Iterate over the events, finding which days they cover, and putting
       them in the first free row available. */
    for (event_num = 0; event_num < day_view->long_events->len;
         event_num++) {
        event = &g_array_index (day_view->long_events,
                    EDayViewEvent, event_num);
        e_day_view_layout_long_event (day_view, event, grid);
    }

    /* Free the grid. */
    g_free (grid);

    /* Set the height of the top canvas based on the row height and the
       number of rows needed (min 1 + 1 for the dates + 1 space for DnD).*/
    if (day_view->rows_in_top_display != old_rows_in_top_display) {
        top_rows = MAX (1, day_view->rows_in_top_display);
        top_canvas_height = (top_rows + 2) * day_view->top_row_height;
        gtk_widget_set_usize (day_view->top_canvas, -1,
                      top_canvas_height);
    }
}


static void
e_day_view_layout_long_event (EDayView     *day_view,
                  EDayViewEvent *event,
                  guint8       *grid)
{
    gint start_day, end_day, free_row, day, row;

    event->num_columns = 0;

    if (!e_day_view_find_long_event_days (day_view, event,
                          &start_day, &end_day))
        return;

    /* Try each row until we find a free one. */
    row = 0;
    do {
        free_row = row;
        for (day = start_day; day <= end_day; day++) {
            if (grid[row * E_DAY_VIEW_MAX_DAYS + day]) {
                free_row = -1;
                break;
            }
        }
        row++;
    } while (free_row == -1);

    event->start_row_or_col = free_row;
    event->num_columns = 1;

    /* Mark the cells as full. */
    for (day = start_day; day <= end_day; day++) {
        grid[free_row * E_DAY_VIEW_MAX_DAYS + day] = 1;
    }

    /* Update the number of rows in the top canvas if necessary. */
    day_view->rows_in_top_display = MAX (day_view->rows_in_top_display,
                         free_row + 1);
}


static void
e_day_view_reshape_long_events (EDayView *day_view)
{
    EDayViewEvent *event;
    gint event_num;

    for (event_num = 0; event_num < day_view->long_events->len;
         event_num++) {
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);

        if (event->num_columns == 0) {
            if (event->canvas_item) {
                gtk_object_destroy (GTK_OBJECT (event->canvas_item));
                event->canvas_item = NULL;
            }
        } else {
            e_day_view_reshape_long_event (day_view, event_num);
        }
    }
}


static void
e_day_view_reshape_long_event (EDayView *day_view,
                   gint  event_num)
{
    EDayViewEvent *event;
    GdkFont *font;
    gint start_day, end_day, item_x, item_y, item_w, item_h;
    gint text_x, text_w, num_icons, icons_width, width, time_width;
    iCalObject *ico;
    gint min_text_x, max_text_w, text_width, line_len;
    gchar *text, *end_of_line;
    gboolean show_icons = TRUE, use_max_width = FALSE;

    if (!e_day_view_get_long_event_position (day_view, event_num,
                         &start_day, &end_day,
                         &item_x, &item_y,
                         &item_w, &item_h)) {
        if (event->canvas_item) {
            gtk_object_destroy (GTK_OBJECT (event->canvas_item));
            event->canvas_item = NULL;
        }
        return;
    }

    /* Take off the border and padding. */
    item_x += E_DAY_VIEW_LONG_EVENT_BORDER_WIDTH + E_DAY_VIEW_LONG_EVENT_X_PAD;
    item_w -= (E_DAY_VIEW_LONG_EVENT_BORDER_WIDTH + E_DAY_VIEW_LONG_EVENT_X_PAD) * 2;
    item_y += E_DAY_VIEW_LONG_EVENT_BORDER_HEIGHT + E_DAY_VIEW_LONG_EVENT_Y_PAD;
    item_h -= (E_DAY_VIEW_LONG_EVENT_BORDER_HEIGHT + E_DAY_VIEW_LONG_EVENT_Y_PAD) * 2;

    event = &g_array_index (day_view->long_events, EDayViewEvent,
                event_num);
    /* We don't show the icons while resizing, since we'd have to
       draw them on top of the resize rect. Nor when editing. */
    num_icons = 0;
    ico = event->ico;
    font = GTK_WIDGET (day_view)->style->font;

    if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE
        && day_view->resize_event_day == E_DAY_VIEW_LONG_EVENT
        && day_view->resize_event_num == event_num)
        show_icons = FALSE;

    if (day_view->editing_event_day == E_DAY_VIEW_LONG_EVENT
        && day_view->editing_event_num == event_num) {
        show_icons = FALSE;
        use_max_width = TRUE;
    }

    if (show_icons) {
        if (ico->dalarm.enabled || ico->malarm.enabled
            || ico->palarm.enabled || ico->aalarm.enabled)
            num_icons++;
        if (ico->recur)
            num_icons++;
    }

    if (!event->canvas_item) {
        event->canvas_item =
            gnome_canvas_item_new (GNOME_CANVAS_GROUP (GNOME_CANVAS (day_view->top_canvas)->root),
                           e_text_get_type (),
                           "font_gdk", GTK_WIDGET (day_view)->style->font,
                           "anchor", GTK_ANCHOR_NW,
                           "clip", TRUE,
                           "max_lines", 1,
                           "editable", TRUE,
                           NULL);
        gtk_signal_connect (GTK_OBJECT (event->canvas_item), "event",
                    GTK_SIGNAL_FUNC (e_day_view_on_text_item_event),
                    day_view);
        e_day_view_update_long_event_label (day_view, event_num);
    }

    /* Calculate its position. We first calculate the ideal position which
       is centered with the icons. We then make sure we haven't gone off
       the left edge of the available space. Finally we make sure we don't
       go off the right edge. */
    icons_width = (E_DAY_VIEW_ICON_WIDTH + E_DAY_VIEW_ICON_X_PAD)
        * num_icons;
    time_width = day_view->max_small_hour_width + day_view->colon_width
        + day_view->max_minute_width;

    if (use_max_width) {
        text_x = item_x;
        text_w = item_w;
    } else {
        /* Get the requested size of the label. */
        gtk_object_get (GTK_OBJECT (event->canvas_item),
                "text", &text,
                NULL);
        text_width = 0;
        if (text) {
            end_of_line = strchr (text, '\n');
            if (end_of_line)
                line_len = end_of_line - text;
            else
                line_len = strlen (text);
            text_width = gdk_text_width (font, text, line_len);
            g_free (text);
        }

        width = text_width + icons_width;
        text_x = item_x + (item_w - width) / 2;

        min_text_x = item_x;
        if (event->start > day_view->day_starts[start_day])
            min_text_x += time_width + E_DAY_VIEW_LONG_EVENT_TIME_X_PAD;

        text_x = MAX (text_x, min_text_x);

        max_text_w = item_x + item_w - text_x;
        if (event->end < day_view->day_starts[end_day + 1])
            max_text_w -= time_width + E_DAY_VIEW_LONG_EVENT_TIME_X_PAD;

        text_w = MIN (width, max_text_w);

        /* Now take out the space for the icons. */
        text_x += icons_width;
        text_w -= icons_width;
    }

    text_w = MAX (text_w, 0);
    gnome_canvas_item_set (event->canvas_item,
                   "x", (gdouble) text_x,
                   "y", (gdouble) item_y,
                   "clip_width", (gdouble) text_w,
                   "clip_height", (gdouble) item_h,
                   NULL);
}


/* Find the start and end days for the event. */
gboolean
e_day_view_find_long_event_days (EDayView   *day_view,
                 EDayViewEvent  *event,
                 gint       *start_day_return,
                 gint       *end_day_return)
{
    gint day, start_day, end_day;

    start_day = -1;
    end_day = -1;

    for (day = 0; day < day_view->days_shown; day++) {
        if (start_day == -1
            && event->start < day_view->day_starts[day + 1])
            start_day = day;
        if (event->end > day_view->day_starts[day])
            end_day = day;
    }

    /* Sanity check. */
    if (start_day < 0 || start_day >= day_view->days_shown
        || end_day < 0 || end_day >= day_view->days_shown
        || end_day < start_day) {
        g_warning ("Invalid date range for event");
        return FALSE;
    }

    *start_day_return = start_day;
    *end_day_return = end_day;

    return TRUE;
}


static void
e_day_view_layout_day_events (EDayView *day_view,
                  gint  day)
{
    EDayViewEvent *event;
    gint row, event_num;
    guint8 *grid;

    /* This is a temporary array which keeps track of rows which are
       connected. When an appointment spans multiple rows then the number
       of columns in each of these rows must be the same (i.e. the maximum
       of all of them). Each element in the array corresponds to one row
       and contains the index of the first row in the group of connected
       rows. */
    guint16 group_starts[12 * 24];

    /* Reset the cols_per_row array, and initialize the connected rows. */
    for (row = 0; row < day_view->rows; row++) {
        day_view->cols_per_row[day][row] = 0;
        group_starts[row] = row;
    }

    /* This is a temporary 2-d grid which is used to place events.
       Each element is 0 if the position is empty, or 1 if occupied. */
    grid = g_new0 (guint8, day_view->rows * E_DAY_VIEW_MAX_COLUMNS);


    /* Iterate over the events, finding which rows they cover, and putting
       them in the first free column available. Increment the number of
       events in each of the rows it covers, and make sure they are all
       in one group. */
    for (event_num = 0; event_num < day_view->events[day]->len;
         event_num++) {
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);

        e_day_view_layout_day_event (day_view, day, event,
                         grid, group_starts);
    }

    /* Recalculate the number of columns needed in each row. */
    e_day_view_recalc_cols_per_row (day_view, day, group_starts);

    /* Iterate over the events again, trying to expand events horizontally
       if there is enough space. */
    for (event_num = 0; event_num < day_view->events[day]->len;
         event_num++) {
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);
        e_day_view_expand_day_event (day_view, day, event, grid);
    }

    /* Free the grid. */
    g_free (grid);
}


/* Finds the first free position to place the event in.
   Increments the number of events in each of the rows it covers, and makes
   sure they are all in one group. */
static void
e_day_view_layout_day_event (EDayView      *day_view,
                 gint       day,
                 EDayViewEvent *event,
                 guint8    *grid,
                 guint16       *group_starts)
{
    gint start_row, end_row, free_col, col, row, group_start;

    start_row = event->start_minute / day_view->mins_per_row;
    end_row = (event->end_minute - 1) / day_view->mins_per_row;

    event->num_columns = 0;

    /* If the event can't currently be seen, just return. */
    if (start_row >= day_view->rows || end_row < 0)
        return;

    /* Make sure we don't go outside the visible times. */
    start_row = CLAMP (start_row, 0, day_view->rows - 1);
    end_row = CLAMP (end_row, 0, day_view->rows - 1);

    /* Try each column until we find a free one. */
    for (col = 0; col < E_DAY_VIEW_MAX_COLUMNS; col++) {
        free_col = col;
        for (row = start_row; row <= end_row; row++) {
            if (grid[row * E_DAY_VIEW_MAX_COLUMNS + col]) {
                free_col = -1;
                break;
            }
        }

        if (free_col != -1)
            break;
    }

    /* If we can't find space for the event, just return. */
    if (free_col == -1)
        return;

    /* The event is assigned 1 col initially, but may be expanded later. */
    event->start_row_or_col = free_col;
    event->num_columns = 1;

    /* Determine the start index of the group. */
    group_start = group_starts[start_row];

    /* Increment number of events in each of the rows the event covers.
       We use the cols_per_row array for this. It will be sorted out after
       all the events have been layed out. Also make sure all the rows that
       the event covers are in one group. */
    for (row = start_row; row <= end_row; row++) {
        grid[row * E_DAY_VIEW_MAX_COLUMNS + free_col] = 1;
        day_view->cols_per_row[day][row]++;
        group_starts[row] = group_start;
    }

    /* If any following rows should be in the same group, add them. */
    for (row = end_row + 1; row < day_view->rows; row++) {
        if (group_starts[row] > end_row)
            break;
        group_starts[row] = group_start;
    }
}


/* For each group of rows, find the max number of events in all the
   rows, and set the number of cols in each of the rows to that. */
static void
e_day_view_recalc_cols_per_row (EDayView *day_view,
                gint      day,
                guint16  *group_starts)
{
    gint start_row = 0, row, next_start_row, max_events;

    while (start_row < day_view->rows) {

        max_events = 0;
        for (row = start_row; row < day_view->rows && group_starts[row] == start_row; row++)
            max_events = MAX (max_events, day_view->cols_per_row[day][row]);

        next_start_row = row;

        for (row = start_row; row < next_start_row; row++)
            day_view->cols_per_row[day][row] = max_events;

        start_row = next_start_row;
    }
}


/* Expands the event horizontally to fill any free space. */
static void
e_day_view_expand_day_event (EDayView      *day_view,
                 gint       day,
                 EDayViewEvent *event,
                 guint8    *grid)
{
    gint start_row, end_row, col, row;
    gboolean clashed;

    start_row = event->start_minute / day_view->mins_per_row;
    end_row = (event->end_minute - 1) / day_view->mins_per_row;

    /* Try each column until we find a free one. */
    clashed = FALSE;
    for (col = event->start_row_or_col + 1; col < day_view->cols_per_row[day][start_row]; col++) {
        for (row = start_row; row <= end_row; row++) {
            if (grid[row * E_DAY_VIEW_MAX_COLUMNS + col]) {
                clashed = TRUE;
                break;
            }
        }

        if (clashed)
            break;

        event->num_columns++;
    }
}


/* This creates or updates the sizes of the canvas items for one day of the
   main canvas. */
static void
e_day_view_reshape_day_events (EDayView *day_view,
                   gint  day)
{
    gint event_num;

    for (event_num = 0; event_num < day_view->events[day]->len;
         event_num++) {
        e_day_view_reshape_day_event (day_view, day, event_num);
    }
}


static void
e_day_view_reshape_day_event (EDayView *day_view,
                  gint  day,
                  gint  event_num)
{
    EDayViewEvent *event;
    gint item_x, item_y, item_w, item_h;
    gint num_icons, icons_offset;
    iCalObject *ico;

    event = &g_array_index (day_view->events[day], EDayViewEvent,
                event_num);
    ico = event->ico;

    if (!e_day_view_get_event_position (day_view, day, event_num,
                        &item_x, &item_y,
                        &item_w, &item_h)) {
        if (event->canvas_item) {
            gtk_object_destroy (GTK_OBJECT (event->canvas_item));
            event->canvas_item = NULL;
        }
    } else {
        /* Skip the border and padding. */
        item_x += E_DAY_VIEW_BAR_WIDTH + E_DAY_VIEW_EVENT_X_PAD;
        item_w -= E_DAY_VIEW_BAR_WIDTH + E_DAY_VIEW_EVENT_X_PAD * 2;
        item_y += E_DAY_VIEW_EVENT_BORDER_HEIGHT + E_DAY_VIEW_EVENT_Y_PAD;
        item_h -= (E_DAY_VIEW_EVENT_BORDER_HEIGHT + E_DAY_VIEW_EVENT_Y_PAD) * 2;

        /* We don't show the icons while resizing, since we'd have to
           draw them on top of the resize rect. */
        num_icons = 0;
        if (day_view->resize_drag_pos == E_DAY_VIEW_POS_NONE
            || day_view->resize_event_day != day
            || day_view->resize_event_num != event_num) {
            if (ico->dalarm.enabled || ico->malarm.enabled
                || ico->palarm.enabled || ico->aalarm.enabled)
                num_icons++;
            if (ico->recur)
                num_icons++;
        }

        if (num_icons > 0) {
            if (item_h >= (E_DAY_VIEW_ICON_HEIGHT + E_DAY_VIEW_ICON_Y_PAD) * num_icons)
                icons_offset = E_DAY_VIEW_ICON_WIDTH + E_DAY_VIEW_ICON_X_PAD * 2;
            else
                icons_offset = (E_DAY_VIEW_ICON_WIDTH + E_DAY_VIEW_ICON_X_PAD) * num_icons + E_DAY_VIEW_ICON_X_PAD;
            item_x += icons_offset;
            item_w -= icons_offset;
        }

        if (!event->canvas_item) {
            event->canvas_item =
                gnome_canvas_item_new (GNOME_CANVAS_GROUP (GNOME_CANVAS (day_view->main_canvas)->root),
                               e_text_get_type (),
                               "font_gdk", GTK_WIDGET (day_view)->style->font,
                               "anchor", GTK_ANCHOR_NW,
                               "line_wrap", TRUE,
                               "editable", TRUE,
                               "clip", TRUE,
                               NULL);
            gtk_signal_connect (GTK_OBJECT (event->canvas_item),
                        "event",
                        GTK_SIGNAL_FUNC (e_day_view_on_text_item_event),
                        day_view);
            e_day_view_update_event_label (day_view, day,
                               event_num);
        }

        item_w = MAX (item_w, 0);
        gnome_canvas_item_set (event->canvas_item,
                       "x", (gdouble) item_x,
                       "y", (gdouble) item_y,
                       "clip_width", (gdouble) item_w,
                       "clip_height", (gdouble) item_h,
                       NULL);
    }
}


/* This creates or resizes the horizontal bars used to resize events in the
   main canvas. */
static void
e_day_view_reshape_main_canvas_resize_bars (EDayView *day_view)
{
    gint day, event_num;
    gint item_x, item_y, item_w, item_h;
    gdouble x, y, w, h;

    day = day_view->resize_bars_event_day;
    event_num = day_view->resize_bars_event_num;

    /* If we're not editing an event, or the event is not shown,
       hide the resize bars. */
    if (day != -1 && day == day_view->drag_event_day
        && event_num == day_view->drag_event_num) {
        gtk_object_get (GTK_OBJECT (day_view->drag_rect_item),
                "x1", &x,
                "y1", &y,
                "x2", &w,
                "y2", &h,
                NULL);
        w -= x;
        x++;
        h -= y;
    } else if (day != -1
           && e_day_view_get_event_position (day_view, day, event_num,
                             &item_x, &item_y,
                             &item_w, &item_h)) {
        x = item_x + E_DAY_VIEW_BAR_WIDTH;
        y = item_y;
        w = item_w - E_DAY_VIEW_BAR_WIDTH;
        h = item_h;
    } else {
        gnome_canvas_item_hide (day_view->main_canvas_top_resize_bar_item);
        gnome_canvas_item_hide (day_view->main_canvas_bottom_resize_bar_item);
        return;
    }

    gnome_canvas_item_set (day_view->main_canvas_top_resize_bar_item,
                   "x1", x - E_DAY_VIEW_BAR_WIDTH,
                   "y1", y - E_DAY_VIEW_BAR_HEIGHT,
                   "x2", x + w - 1,
                   "y2", y - 1,
                   NULL);
    gnome_canvas_item_show (day_view->main_canvas_top_resize_bar_item);

    gnome_canvas_item_set (day_view->main_canvas_bottom_resize_bar_item,
                   "x1", x - E_DAY_VIEW_BAR_WIDTH,
                   "y1", y + h,
                   "x2", x + w - 1,
                   "y2", y + h + E_DAY_VIEW_BAR_HEIGHT - 1,
                   NULL);
    gnome_canvas_item_show (day_view->main_canvas_bottom_resize_bar_item);
}


static void
e_day_view_ensure_events_sorted (EDayView *day_view)
{
    gint day;

    /* Sort the long events. */
    if (!day_view->long_events_sorted) {
        qsort (day_view->long_events->data,
               day_view->long_events->len,
               sizeof (EDayViewEvent),
               e_day_view_event_sort_func);
        day_view->long_events_sorted = TRUE;
    }

    /* Sort the events for each day. */
    for (day = 0; day < day_view->days_shown; day++) {
        if (!day_view->events_sorted[day]) {
            qsort (day_view->events[day]->data,
                   day_view->events[day]->len,
                   sizeof (EDayViewEvent),
                   e_day_view_event_sort_func);
            day_view->events_sorted[day] = TRUE;
        }
    }
}


static gint
e_day_view_event_sort_func (const void *arg1,
                const void *arg2)
{
    EDayViewEvent *event1, *event2;

    event1 = (EDayViewEvent*) arg1;
    event2 = (EDayViewEvent*) arg2;

    if (event1->start < event2->start)
        return -1;
    if (event1->start > event2->start)
        return 1;

    if (event1->end > event2->end)
        return -1;
    if (event1->end < event2->end)
        return 1;

    return 0;
}


static gint
e_day_view_key_press (GtkWidget *widget, GdkEventKey *event)
{
    EDayView *day_view;
    iCalObject *ico;
    gint day, event_num;
    gchar *initial_text;

    g_return_val_if_fail (widget != NULL, FALSE);
    g_return_val_if_fail (E_IS_DAY_VIEW (widget), FALSE);
    g_return_val_if_fail (event != NULL, FALSE);

    day_view = E_DAY_VIEW (widget);

    /* The Escape key aborts a resize operation. */
    if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE) {
        if (event->keyval == GDK_Escape) {
            e_day_view_abort_resize (day_view, event->time);
        }
        return FALSE;
    }


    if (day_view->selection_start_col == -1)
        return FALSE;

    /* We only want to start an edit with a return key or a simple
       character. */
    if (event->keyval == GDK_Return) {
        initial_text = NULL;
    } else if ((event->keyval < 0x20)
           || (event->keyval > 0xFF)
           || (event->length == 0)
           || (event->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK))) {
        return FALSE;
    } else {
        initial_text = event->string;
    }

    /* Add a new event covering the selected range.
       Note that user_name is a global variable. */
    ico = ical_new ("", user_name, "");
    ico->new = 1;
    ico->created = time (NULL);
    ico->last_mod = ico->created;

    e_day_view_get_selected_time_range (day_view, &ico->dtstart,
                        &ico->dtend);

    /* We add the event locally and start editing it. When we get the
       "update_event" callback from the server, we basically ignore it.
       If we were to wait for the "update_event" callback it wouldn't be
       as responsive and we may lose a few keystrokes. */ 
    e_day_view_add_event (ico, ico->dtstart, ico->dtend, day_view);
    e_day_view_check_layout (day_view);
    gtk_widget_queue_draw (day_view->top_canvas);
    gtk_widget_queue_draw (day_view->main_canvas);

    if (e_day_view_find_event_from_uid (day_view, ico->uid,
                        &day, &event_num)) {
        e_day_view_start_editing_event (day_view, day, event_num,
                        initial_text);
        day_view->editing_new_event = TRUE;
    } else {
        g_warning ("Couldn't find event to start editing.\n");
    }

    gnome_calendar_add_object (day_view->calendar, ico);

    ical_object_unref (ico);

    return TRUE;
}


static void
e_day_view_start_editing_event (EDayView *day_view,
                gint      day,
                gint      event_num,
                gchar    *initial_text)
{
    EDayViewEvent *event;
    ETextEventProcessor *event_processor = NULL;
    ETextEventProcessorCommand command;

    /* If we are already editing the event, just return. */
    if (day == day_view->editing_event_day
        && event_num == day_view->editing_event_num)
        return;

    if (day == E_DAY_VIEW_LONG_EVENT) {
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);
    } else {
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);
    }

    /* If the event is not shown, don't try to edit it. */
    if (!event->canvas_item)
        return;

    /* We must grab the focus before setting the initial text, since
       grabbing the focus will result in a call to
       e_day_view_on_editing_started(), which will reset the text to get
       rid of the start and end times. */
    e_canvas_item_grab_focus (event->canvas_item);

    if (initial_text) {
        gnome_canvas_item_set (event->canvas_item,
                       "text", initial_text,
                       NULL);
    }

    /* Try to move the cursor to the end of the text. */
    gtk_object_get (GTK_OBJECT (event->canvas_item),
            "event_processor", &event_processor,
            NULL);
    if (event_processor) {
        command.action = E_TEP_MOVE;
        command.position = E_TEP_END_OF_BUFFER;
        gtk_signal_emit_by_name (GTK_OBJECT (event_processor),
                     "command", &command);
    }
}


/* This stops the current edit. If accept is TRUE the event summary is update,
   else the edit is cancelled. */
static void
e_day_view_stop_editing_event (EDayView *day_view)
{
    GtkWidget *toplevel;

    /* Check we are editing an event. */
    if (day_view->editing_event_day == -1)
        return;

    /* Set focus to the toplevel so the item loses focus. */
    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (day_view));
    if (toplevel && GTK_IS_WINDOW (toplevel))
        gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
}


static gboolean
e_day_view_on_text_item_event (GnomeCanvasItem *item,
                   GdkEvent *event,
                   EDayView *day_view)
{
    switch (event->type) {
    case GDK_KEY_PRESS:
        if (event && event->key.keyval == GDK_Return) {
            /* We set the keyboard focus to the EDayView, so the
               EText item loses it and stops the edit. */
            gtk_widget_grab_focus (GTK_WIDGET (day_view));

            /* Stop the signal last or we will also stop any
               other events getting to the EText item. */
            gtk_signal_emit_stop_by_name (GTK_OBJECT (item),
                              "event");
            return TRUE;
        }
        break;
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
        /* Only let the EText handle the event while editing. */
        if (!E_TEXT (item)->editing)
            gtk_signal_emit_stop_by_name (GTK_OBJECT (item),
                              "event");
        break;
    case GDK_FOCUS_CHANGE:
        if (event->focus_change.in)
            e_day_view_on_editing_started (day_view, item);
        else
            e_day_view_on_editing_stopped (day_view, item);

        return FALSE;
    default:
        break;
    }

    return FALSE;
}


static void
e_day_view_on_editing_started (EDayView *day_view,
                   GnomeCanvasItem *item)
{
    gint day, event_num;

    if (!e_day_view_find_event_from_item (day_view, item,
                          &day, &event_num))
        return;

#if 0
    g_print ("In e_day_view_on_editing_started Day:%i Event:%i\n",
         day, event_num);
#endif

    /* FIXME: This is a temporary workaround for a bug which seems to stop
       us getting focus_out signals. It is not a complete fix since if we
       don't get focus_out signals we don't save the appointment text so
       this may be lost. */
    if (day_view->editing_event_day == day
        && day_view->editing_event_num == event_num)
        return;

    day_view->editing_event_day = day;
    day_view->editing_event_num = event_num;

    if (day == E_DAY_VIEW_LONG_EVENT) {
        e_day_view_reshape_long_event (day_view, event_num);
    } else {
        day_view->resize_bars_event_day = day;
        day_view->resize_bars_event_num = event_num;
        e_day_view_update_event_label (day_view, day, event_num);
        e_day_view_reshape_main_canvas_resize_bars (day_view);
    }
}


static void
e_day_view_on_editing_stopped (EDayView *day_view,
                   GnomeCanvasItem *item)
{
    gint day, event_num;
    gboolean editing_long_event = FALSE;
    EDayViewEvent *event;
    gchar *text = NULL;

    /* Note: the item we are passed here isn't reliable, so we just stop
       the edit of whatever item was being edited. We also receive this
       event twice for some reason. */
    day = day_view->editing_event_day;
    event_num = day_view->editing_event_num;

    /* If no item is being edited, just return. */
    if (day == -1)
        return;

#if 0
    g_print ("In e_day_view_on_editing_stopped Day:%i Event:%i\n",
         day, event_num);
#endif

    if (day == E_DAY_VIEW_LONG_EVENT) {
        editing_long_event = TRUE;
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);
    } else {
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);

        /* Hide the horizontal bars. */
        gnome_canvas_item_hide (day_view->main_canvas_top_resize_bar_item);
        gnome_canvas_item_hide (day_view->main_canvas_bottom_resize_bar_item);
    }

    /* Reset the edit fields. */
    day_view->editing_event_day = -1;
    day_view->editing_event_num = -1;
    day_view->editing_new_event = FALSE;

    day_view->resize_bars_event_day = -1;
    day_view->resize_bars_event_num = -1;

    /* Check that the event is still valid. */
    if (!event->ico->uid)
        return;

    gtk_object_get (GTK_OBJECT (event->canvas_item),
            "text", &text,
            NULL);

    /* Only update the summary if necessary. */
    if (text && event->ico->summary
        && !strcmp (text, event->ico->summary)) {
        g_free (text);

        if (day == E_DAY_VIEW_LONG_EVENT)
            e_day_view_reshape_long_event (day_view, event_num);
        return;
    }

    if (event->ico->summary)
        g_free (event->ico->summary);

    event->ico->summary = text;

    /* Notify calendar of change. This will result in a call to update,
       which will reset the event label as appropriate. */
    gnome_calendar_object_changed (day_view->calendar, event->ico,
                       CHANGE_SUMMARY);
}


/* FIXME: It is possible that we may produce an invalid time due to daylight
   saving times (i.e. when clocks go forward there is a range of time which
   is not valid). I don't know the best way to handle daylight saving time. */
static time_t
e_day_view_convert_grid_position_to_time (EDayView *day_view,
                      gint col,
                      gint row)
{
    struct tm *tmp_tm;
    time_t val;
    gint minutes;

    /* Calulate the number of minutes since the start of the day. */
    minutes = day_view->first_hour_shown * 60
        + day_view->first_minute_shown
        + row * day_view->mins_per_row;

    /* A special case for midnight, where we can use the start of the
       next day. */
    if (minutes == 60 * 24)
        return day_view->day_starts[col + 1];

    /* We convert the start of the day to a struct tm, then set the
       hour and minute, then convert it back to a time_t. */
    tmp_tm = localtime (&day_view->day_starts[col]);

    tmp_tm->tm_hour = minutes / 60;
    tmp_tm->tm_min = minutes % 60;
    tmp_tm->tm_isdst = -1;

    val = mktime (tmp_tm);
    return val;
}


static gboolean
e_day_view_convert_time_to_grid_position (EDayView *day_view,
                      time_t time,
                      gint *col,
                      gint *row)
{
    struct tm *tmp_tm;
    gint day, minutes;

    *col = *row = 0;

    if (time < day_view->lower || time >= day_view->upper)
        return FALSE;

    /* We can find the column easily using the day_starts array. */
    for (day = 1; day <= day_view->days_shown; day++) {
        if (time < day_view->day_starts[day]) {
            *col = day - 1;
            break;
        }
    }

    /* To find the row we need to convert the time to a struct tm,
       calculate the offset in minutes from the top of the display and
       divide it by the mins per row setting. */
    tmp_tm = localtime (&time);
    minutes = tmp_tm->tm_hour * 60 + tmp_tm->tm_min;
    minutes -= day_view->first_hour_shown * 60
        + day_view->first_minute_shown;
    
    *row = minutes / day_view->mins_per_row;

    if (*row < 0 || *row >= day_view->rows)
        return FALSE;

    return TRUE;
}


/* This starts or stops auto-scrolling when dragging a selection or resizing
   an event. */
static void
e_day_view_check_auto_scroll (EDayView *day_view,
                  gint event_y)
{
    if (event_y < E_DAY_VIEW_AUTO_SCROLL_OFFSET)
        e_day_view_start_auto_scroll (day_view, TRUE);
    else if (event_y >= day_view->main_canvas->allocation.height
         - E_DAY_VIEW_AUTO_SCROLL_OFFSET)
        e_day_view_start_auto_scroll (day_view, FALSE);
    else
        e_day_view_stop_auto_scroll (day_view);
}


static void
e_day_view_start_auto_scroll (EDayView *day_view,
                  gboolean scroll_up)
{
    if (day_view->auto_scroll_timeout_id == 0) {
        day_view->auto_scroll_timeout_id = g_timeout_add (E_DAY_VIEW_AUTO_SCROLL_TIMEOUT, e_day_view_auto_scroll_handler, day_view);
        day_view->auto_scroll_delay = E_DAY_VIEW_AUTO_SCROLL_DELAY;
    }
    day_view->auto_scroll_up = scroll_up;
}


static void
e_day_view_stop_auto_scroll (EDayView *day_view)
{
    if (day_view->auto_scroll_timeout_id != 0) {
        gtk_timeout_remove (day_view->auto_scroll_timeout_id);
        day_view->auto_scroll_timeout_id = 0;
    }
}


static gboolean
e_day_view_auto_scroll_handler (gpointer data)
{
    EDayView *day_view;
    EDayViewPosition pos;
    gint scroll_x, scroll_y, new_scroll_y, canvas_x, canvas_y, row, day;
    GtkAdjustment *adj;

    g_return_val_if_fail (E_IS_DAY_VIEW (data), FALSE);

    day_view = E_DAY_VIEW (data);

    GDK_THREADS_ENTER ();

    if (day_view->auto_scroll_delay > 0) {
        day_view->auto_scroll_delay--;
        GDK_THREADS_LEAVE ();
        return TRUE;
    }

    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (day_view->main_canvas),
                     &scroll_x, &scroll_y);

    adj = GTK_LAYOUT (day_view->main_canvas)->vadjustment;

    if (day_view->auto_scroll_up)
        new_scroll_y = MAX (scroll_y - adj->step_increment, 0);
    else
        new_scroll_y = MIN (scroll_y + adj->step_increment,
                    adj->upper - adj->page_size);

    if (new_scroll_y != scroll_y) {
        /* NOTE: This reduces flicker, but only works if we don't use
           canvas items which have X windows. */
        gtk_layout_freeze (GTK_LAYOUT (day_view->main_canvas));

        gnome_canvas_scroll_to (GNOME_CANVAS (day_view->main_canvas),
                    scroll_x, new_scroll_y);

        gtk_layout_thaw (GTK_LAYOUT (day_view->main_canvas));
    }

    canvas_x = day_view->last_mouse_x + scroll_x;
    canvas_y = day_view->last_mouse_y + new_scroll_y;

    /* Update the selection/resize/drag if necessary. */
    pos = e_day_view_convert_position_in_main_canvas (day_view,
                              canvas_x, canvas_y,
                              &day, &row, NULL);

    if (pos != E_DAY_VIEW_POS_OUTSIDE) {
        if (day_view->selection_drag_pos != E_DAY_VIEW_DRAG_NONE) {
            e_day_view_update_selection (day_view, row, day);
        } else if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE) {
            e_day_view_update_resize (day_view, row);
        } else if (day_view->drag_item->object.flags
               & GNOME_CANVAS_ITEM_VISIBLE) {
            e_day_view_update_main_canvas_drag (day_view, row,
                                day);
        }
    }

    GDK_THREADS_LEAVE ();
    return TRUE;
}


gboolean
e_day_view_get_event_position (EDayView *day_view,
                   gint day,
                   gint event_num,
                   gint *item_x,
                   gint *item_y,
                   gint *item_w,
                   gint *item_h)
{
    EDayViewEvent *event;
    gint start_row, end_row, cols_in_row, start_col, num_columns;

    event = &g_array_index (day_view->events[day], EDayViewEvent,
                event_num);

    /* If the event is flagged as not displayed, return FALSE. */
    if (event->num_columns == 0)
        return FALSE;

    start_row = event->start_minute / day_view->mins_per_row;
    end_row = (event->end_minute - 1) / day_view->mins_per_row;
    cols_in_row = day_view->cols_per_row[day][start_row];
    start_col = event->start_row_or_col;
    num_columns = event->num_columns;

    /* If the event is being resize, use the resize position. */
    if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE
        && day_view->resize_event_day == day
        && day_view->resize_event_num == event_num) {
        if (day_view->resize_drag_pos == E_DAY_VIEW_POS_TOP_EDGE)
            start_row = day_view->resize_start_row;
        else if (day_view->resize_drag_pos == E_DAY_VIEW_POS_BOTTOM_EDGE)
            end_row = day_view->resize_end_row;
    }

    *item_x = day_view->day_offsets[day] + day_view->day_widths[day] * start_col / cols_in_row;
    *item_w = day_view->day_widths[day] * num_columns / cols_in_row - E_DAY_VIEW_GAP_WIDTH;
    *item_w = MAX (*item_w, 0);
    *item_y = start_row * day_view->row_height;
    *item_h = (end_row - start_row + 1) * day_view->row_height;

    return TRUE;
}


gboolean
e_day_view_get_long_event_position  (EDayView   *day_view,
                     gint        event_num,
                     gint       *start_day,
                     gint       *end_day,
                     gint       *item_x,
                     gint       *item_y,
                     gint       *item_w,
                     gint       *item_h)
{
    EDayViewEvent *event;

    event = &g_array_index (day_view->long_events, EDayViewEvent,
                event_num);

    /* If the event is flagged as not displayed, return FALSE. */
    if (event->num_columns == 0)
        return FALSE;

    if (!e_day_view_find_long_event_days (day_view, event,
                          start_day, end_day))
        return FALSE;

    /* If the event is being resize, use the resize position. */
    if (day_view->resize_drag_pos != E_DAY_VIEW_POS_NONE
        && day_view->resize_event_day == E_DAY_VIEW_LONG_EVENT
        && day_view->resize_event_num == event_num) {
        if (day_view->resize_drag_pos == E_DAY_VIEW_POS_LEFT_EDGE)
            *start_day = day_view->resize_start_row;
        else if (day_view->resize_drag_pos == E_DAY_VIEW_POS_RIGHT_EDGE)
            *end_day = day_view->resize_end_row;
    }

    *item_x = day_view->day_offsets[*start_day] + E_DAY_VIEW_BAR_WIDTH;
    *item_w = day_view->day_offsets[*end_day + 1] - *item_x
        - E_DAY_VIEW_GAP_WIDTH;
    *item_w = MAX (*item_w, 0);
    *item_y = (event->start_row_or_col + 1) * day_view->top_row_height;
    *item_h = day_view->top_row_height - E_DAY_VIEW_TOP_CANVAS_Y_GAP;
    return TRUE;
}


/* Converts a position within the entire top canvas to a day & event and
   a place within the event if appropriate. If event_num_return is NULL, it
   simply returns the grid position without trying to find the event. */
static EDayViewPosition
e_day_view_convert_position_in_top_canvas (EDayView *day_view,
                       gint x,
                       gint y,
                       gint *day_return,
                       gint *event_num_return)
{
    EDayViewEvent *event;
    gint day, row, col;
    gint event_num, start_day, end_day, item_x, item_y, item_w, item_h;

    *day_return = -1;
    if (event_num_return)
        *event_num_return = -1;

    if (x < 0 || y < 0)
        return E_DAY_VIEW_POS_OUTSIDE;

    row = y / day_view->top_row_height - 1;

    day = -1;
    for (col = 1; col <= day_view->days_shown; col++) {
        if (x < day_view->day_offsets[col]) {
            day = col - 1;  
            break;
        }
    }
    if (day == -1)
        return E_DAY_VIEW_POS_OUTSIDE;

    *day_return = day;

    /* If only the grid position is wanted, return. */
    if (event_num_return == NULL)
        return E_DAY_VIEW_POS_NONE;

    for (event_num = 0; event_num < day_view->long_events->len;
         event_num++) {
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);

        if (event->start_row_or_col != row)
            continue;

        if (!e_day_view_get_long_event_position (day_view, event_num,
                             &start_day, &end_day,
                             &item_x, &item_y,
                             &item_w, &item_h))
            continue;

        if (x < item_x)
            continue;

        if (x >= item_x + item_w)
            continue;

        *event_num_return = event_num;

        if (x < item_x + E_DAY_VIEW_LONG_EVENT_BORDER_WIDTH
            + E_DAY_VIEW_LONG_EVENT_X_PAD)
            return E_DAY_VIEW_POS_LEFT_EDGE;

        if (x >= item_x + item_w - E_DAY_VIEW_LONG_EVENT_BORDER_WIDTH
            - E_DAY_VIEW_LONG_EVENT_X_PAD)
            return E_DAY_VIEW_POS_RIGHT_EDGE;

        return E_DAY_VIEW_POS_EVENT;
    }

    return E_DAY_VIEW_POS_NONE;
}


/* Converts a position within the entire main canvas to a day, row, event and
   a place within the event if appropriate. If event_num_return is NULL, it
   simply returns the grid position without trying to find the event. */
static EDayViewPosition
e_day_view_convert_position_in_main_canvas (EDayView *day_view,
                        gint x,
                        gint y,
                        gint *day_return,
                        gint *row_return,
                        gint *event_num_return)
{
    gint day, row, col, event_num;
    gint item_x, item_y, item_w, item_h;

    *day_return = -1;
    *row_return = -1;
    if (event_num_return)
        *event_num_return = -1;

    /* Check the position is inside the canvas, and determine the day
       and row. */
    if (x < 0 || y < 0)
        return E_DAY_VIEW_POS_OUTSIDE;

    row = y / day_view->row_height;
    if (row >= day_view->rows)
        return E_DAY_VIEW_POS_OUTSIDE;

    day = -1;
    for (col = 1; col <= day_view->days_shown; col++) {
        if (x < day_view->day_offsets[col]) {
            day = col - 1;  
            break;
        }
    }
    if (day == -1)
        return E_DAY_VIEW_POS_OUTSIDE;

    *day_return = day;
    *row_return = row;

    /* If only the grid position is wanted, return. */
    if (event_num_return == NULL)
        return E_DAY_VIEW_POS_NONE;

    /* Check the selected item first, since the horizontal resizing bars
       may be above other events. */
    if (day_view->resize_bars_event_day == day) {
        if (e_day_view_get_event_position (day_view, day,
                           day_view->resize_bars_event_num,
                           &item_x, &item_y,
                           &item_w, &item_h)) {
            if (x >= item_x && x < item_x + item_w) {
                *event_num_return = day_view->resize_bars_event_num;
                if (y >= item_y - E_DAY_VIEW_BAR_HEIGHT
                    && y < item_y + E_DAY_VIEW_EVENT_BORDER_HEIGHT)
                    return E_DAY_VIEW_POS_TOP_EDGE;
                if (y >= item_y + item_h - E_DAY_VIEW_EVENT_BORDER_HEIGHT
                    && y < item_y + item_h + E_DAY_VIEW_BAR_HEIGHT)
                    return E_DAY_VIEW_POS_BOTTOM_EDGE;
            }
        }
    }

    /* Try to find the event at the found position. */
    *event_num_return = -1;
    for (event_num = 0; event_num < day_view->events[day]->len;
         event_num++) {
        if (!e_day_view_get_event_position (day_view, day, event_num,
                            &item_x, &item_y,
                            &item_w, &item_h))
            continue;

        if (x < item_x || x >= item_x + item_w
            || y < item_y || y >= item_y + item_h)
            continue;

        *event_num_return = event_num;

        if (x < item_x + E_DAY_VIEW_BAR_WIDTH)
            return E_DAY_VIEW_POS_LEFT_EDGE;

        if (y < item_y + E_DAY_VIEW_EVENT_BORDER_HEIGHT
            + E_DAY_VIEW_EVENT_Y_PAD)
            return E_DAY_VIEW_POS_TOP_EDGE;

        if (y >= item_y + item_h - E_DAY_VIEW_EVENT_BORDER_HEIGHT
            - E_DAY_VIEW_EVENT_Y_PAD)
            return E_DAY_VIEW_POS_BOTTOM_EDGE;

        return E_DAY_VIEW_POS_EVENT;
    }

    return E_DAY_VIEW_POS_NONE;
}


static gint
e_day_view_on_top_canvas_drag_motion (GtkWidget      *widget,
                      GdkDragContext *context,
                      gint            x,
                      gint            y,
                      guint           time,
                      EDayView       *day_view)
{
    gint scroll_x, scroll_y;

    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (widget),
                     &scroll_x, &scroll_y);
    day_view->drag_event_x = x + scroll_x;
    day_view->drag_event_y = y + scroll_y;

    e_day_view_reshape_top_canvas_drag_item (day_view);

    return TRUE;
}


static void
e_day_view_reshape_top_canvas_drag_item (EDayView *day_view)
{
    EDayViewPosition pos;
    gint x, y, day;

    /* Calculate the day & start row of the event being dragged, using
       the current mouse position. */
    x = day_view->drag_event_x;
    y = day_view->drag_event_y;
    pos = e_day_view_convert_position_in_top_canvas (day_view, x, y,
                             &day, NULL);
    /* This shouldn't really happen in a drag. */
    if (pos == E_DAY_VIEW_POS_OUTSIDE)
        return;

    if (day_view->drag_event_day == E_DAY_VIEW_LONG_EVENT)
        day -= day_view->drag_event_offset;
    day = MAX (day, 0);

    e_day_view_update_top_canvas_drag (day_view, day);
}


static void
e_day_view_update_top_canvas_drag (EDayView *day_view,
                   gint day)
{
    EDayViewEvent *event = NULL;
    gint row, num_days, start_day, end_day;
    gdouble item_x, item_y, item_w, item_h;
    GdkFont *font;
    gchar *text;


    /* Calculate the event's position. If the event is in the same
       position we started in, we use the same columns. */
    row = day_view->rows_in_top_display + 1;
    num_days = 1;

    if (day_view->drag_event_day == E_DAY_VIEW_LONG_EVENT) {
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    day_view->drag_event_num);
        row = event->start_row_or_col + 1;

        if (!e_day_view_find_long_event_days (day_view, event,
                              &start_day, &end_day))
            return;

        num_days = end_day - start_day + 1;

        /* Make sure we don't go off the screen. */
        day = MIN (day, day_view->days_shown - num_days);

    } else if (day_view->drag_event_day != -1) {
        event = &g_array_index (day_view->events[day_view->drag_event_day],
                    EDayViewEvent,
                    day_view->drag_event_num);
    }

    /* If the position hasn't changed, just return. */
    if (day_view->drag_last_day == day
        && (day_view->drag_long_event_item->object.flags
        & GNOME_CANVAS_ITEM_VISIBLE))
        return;

    day_view->drag_last_day = day;


    item_x = day_view->day_offsets[day] + E_DAY_VIEW_BAR_WIDTH;
    item_w = day_view->day_offsets[day + num_days] - item_x
        - E_DAY_VIEW_GAP_WIDTH;
    item_y = row * day_view->top_row_height;
    item_h = day_view->top_row_height - E_DAY_VIEW_TOP_CANVAS_Y_GAP;


    /* Set the positions of the event & associated items. */
    gnome_canvas_item_set (day_view->drag_long_event_rect_item,
                   "x1", item_x,
                   "y1", item_y,
                   "x2", item_x + item_w - 1,
                   "y2", item_y + item_h - 1,
                   NULL);

    font = GTK_WIDGET (day_view)->style->font;
    gnome_canvas_item_set (day_view->drag_long_event_item,
                   "font_gdk", font,
                   "x", item_x + E_DAY_VIEW_LONG_EVENT_BORDER_WIDTH + E_DAY_VIEW_LONG_EVENT_X_PAD,
                   "y", item_y + E_DAY_VIEW_LONG_EVENT_BORDER_HEIGHT + E_DAY_VIEW_LONG_EVENT_Y_PAD,
                   "clip_width", item_w - (E_DAY_VIEW_LONG_EVENT_BORDER_WIDTH + E_DAY_VIEW_LONG_EVENT_X_PAD) * 2,
                   "clip_height", item_h - (E_DAY_VIEW_LONG_EVENT_BORDER_HEIGHT + E_DAY_VIEW_LONG_EVENT_Y_PAD) * 2,
                   NULL);

    if (!(day_view->drag_long_event_rect_item->object.flags & GNOME_CANVAS_ITEM_VISIBLE)) {
        gnome_canvas_item_raise_to_top (day_view->drag_long_event_rect_item);
        gnome_canvas_item_show (day_view->drag_long_event_rect_item);
    }

    /* Set the text, if necessary. We don't want to set the text every
       time it moves, so we check if it is currently invisible and only
       set the text then. */
    if (!(day_view->drag_long_event_item->object.flags
          & GNOME_CANVAS_ITEM_VISIBLE)) {
        if (event)
            text = event->ico->summary;
        else
            text = NULL;

        gnome_canvas_item_set (day_view->drag_long_event_item,
                       "text", text ? text : "",
                       NULL);
        gnome_canvas_item_raise_to_top (day_view->drag_long_event_item);
        gnome_canvas_item_show (day_view->drag_long_event_item);
    }
}


static gint
e_day_view_on_main_canvas_drag_motion (GtkWidget      *widget,
                       GdkDragContext *context,
                       gint            x,
                       gint            y,
                       guint           time,
                       EDayView       *day_view)
{
    gint scroll_x, scroll_y;

    day_view->last_mouse_x = x;
    day_view->last_mouse_y = y;

    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (widget),
                     &scroll_x, &scroll_y);
    day_view->drag_event_x = x + scroll_x;
    day_view->drag_event_y = y + scroll_y;

    e_day_view_reshape_main_canvas_drag_item (day_view);
    e_day_view_reshape_main_canvas_resize_bars (day_view);

    e_day_view_check_auto_scroll (day_view, y);

    return TRUE;
}


static void
e_day_view_reshape_main_canvas_drag_item (EDayView *day_view)
{
    EDayViewPosition pos;
    gint x, y, day, row;

    /* Calculate the day & start row of the event being dragged, using
       the current mouse position. */
    x = day_view->drag_event_x;
    y = day_view->drag_event_y;
    pos = e_day_view_convert_position_in_main_canvas (day_view, x, y,
                              &day, &row, NULL);
    /* This shouldn't really happen in a drag. */
    if (pos == E_DAY_VIEW_POS_OUTSIDE)
        return;

    if (day_view->drag_event_day != -1
        && day_view->drag_event_day != E_DAY_VIEW_LONG_EVENT)
        row -= day_view->drag_event_offset;
    row = MAX (row, 0);

    e_day_view_update_main_canvas_drag (day_view, row, day);
}


static void
e_day_view_update_main_canvas_drag (EDayView *day_view,
                    gint row,
                    gint day)
{
    EDayViewEvent *event = NULL;
    gint cols_in_row, start_col, num_columns, num_rows, start_row, end_row;
    gdouble item_x, item_y, item_w, item_h;
    GdkFont *font;
    gchar *text;

    /* If the position hasn't changed, just return. */
    if (day_view->drag_last_day == day
        && day_view->drag_last_row == row
        && (day_view->drag_item->object.flags & GNOME_CANVAS_ITEM_VISIBLE))
        return;

    day_view->drag_last_day = day;
    day_view->drag_last_row = row;

    /* Calculate the event's position. If the event is in the same
       position we started in, we use the same columns. */
    cols_in_row = 1;
    start_col = 0;
    num_columns = 1;
    num_rows = 1;

    if (day_view->drag_event_day == E_DAY_VIEW_LONG_EVENT) {
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    day_view->drag_event_num);
    } else if (day_view->drag_event_day != -1) {
        event = &g_array_index (day_view->events[day_view->drag_event_day],
                    EDayViewEvent,
                    day_view->drag_event_num);
        start_row = event->start_minute / day_view->mins_per_row;
        end_row = (event->end_minute - 1) / day_view->mins_per_row;
        num_rows = end_row - start_row + 1;
    }

    if (day_view->drag_event_day == day && start_row == row) {
        cols_in_row = day_view->cols_per_row[day][row];
        start_col = event->start_row_or_col;
        num_columns = event->num_columns;
    }

    item_x = day_view->day_offsets[day]
        + day_view->day_widths[day] * start_col / cols_in_row;
    item_w = day_view->day_widths[day] * num_columns / cols_in_row
        - E_DAY_VIEW_GAP_WIDTH;
    item_y = row * day_view->row_height;
    item_h = num_rows * day_view->row_height;

    /* Set the positions of the event & associated items. */
    gnome_canvas_item_set (day_view->drag_rect_item,
                   "x1", item_x + E_DAY_VIEW_BAR_WIDTH - 1,
                   "y1", item_y,
                   "x2", item_x + item_w - 1,
                   "y2", item_y + item_h - 1,
                   NULL);

    gnome_canvas_item_set (day_view->drag_bar_item,
                   "x1", item_x,
                   "y1", item_y,
                   "x2", item_x + E_DAY_VIEW_BAR_WIDTH - 1,
                   "y2", item_y + item_h - 1,
                   NULL);

    font = GTK_WIDGET (day_view)->style->font;
    gnome_canvas_item_set (day_view->drag_item,
                   "font_gdk", font,
                   "x", item_x + E_DAY_VIEW_BAR_WIDTH + E_DAY_VIEW_EVENT_X_PAD,
                   "y", item_y + E_DAY_VIEW_EVENT_BORDER_HEIGHT + E_DAY_VIEW_EVENT_Y_PAD,
                   "clip_width", item_w - E_DAY_VIEW_BAR_WIDTH - E_DAY_VIEW_EVENT_X_PAD * 2,
                   "clip_height", item_h - (E_DAY_VIEW_EVENT_BORDER_HEIGHT + E_DAY_VIEW_EVENT_Y_PAD) * 2,
                   NULL);

    if (!(day_view->drag_bar_item->object.flags & GNOME_CANVAS_ITEM_VISIBLE)) {
        gnome_canvas_item_raise_to_top (day_view->drag_bar_item);
        gnome_canvas_item_show (day_view->drag_bar_item);
    }

    if (!(day_view->drag_rect_item->object.flags & GNOME_CANVAS_ITEM_VISIBLE)) {
        gnome_canvas_item_raise_to_top (day_view->drag_rect_item);
        gnome_canvas_item_show (day_view->drag_rect_item);
    }

    /* Set the text, if necessary. We don't want to set the text every
       time it moves, so we check if it is currently invisible and only
       set the text then. */
    if (!(day_view->drag_item->object.flags & GNOME_CANVAS_ITEM_VISIBLE)) {
        if (event)
            text = event->ico->summary;
        else
            text = NULL;

        gnome_canvas_item_set (day_view->drag_item,
                       "text", text ? text : "",
                       NULL);
        gnome_canvas_item_raise_to_top (day_view->drag_item);
        gnome_canvas_item_show (day_view->drag_item);
    }
}


static void
e_day_view_on_top_canvas_drag_leave (GtkWidget      *widget,
                     GdkDragContext *context,
                     guint           time,
                     EDayView        *day_view)
{
    day_view->drag_last_day = -1;

    gnome_canvas_item_hide (day_view->drag_long_event_rect_item);
    gnome_canvas_item_hide (day_view->drag_long_event_item);
}


static void
e_day_view_on_main_canvas_drag_leave (GtkWidget      *widget,
                      GdkDragContext *context,
                      guint           time,
                      EDayView       *day_view)
{
    day_view->drag_last_day = -1;

    e_day_view_stop_auto_scroll (day_view);

    gnome_canvas_item_hide (day_view->drag_rect_item);
    gnome_canvas_item_hide (day_view->drag_bar_item);
    gnome_canvas_item_hide (day_view->drag_item);

    /* Hide the resize bars if they are being used in the drag. */
    if (day_view->drag_event_day == day_view->resize_bars_event_day
        && day_view->drag_event_num == day_view->resize_bars_event_num) {
        gnome_canvas_item_hide (day_view->main_canvas_top_resize_bar_item);
        gnome_canvas_item_hide (day_view->main_canvas_bottom_resize_bar_item);
    }
}


static void
e_day_view_on_drag_begin (GtkWidget      *widget,
              GdkDragContext *context,
              EDayView   *day_view)
{
    EDayViewEvent *event;
    gint day, event_num;

    day = day_view->drag_event_day;
    event_num = day_view->drag_event_num;

    /* These should both be set. */
    g_return_if_fail (day != -1);
    g_return_if_fail (event_num != -1);

    if (day == E_DAY_VIEW_LONG_EVENT)
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);
    else
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);

    /* Hide the text item, since it will be shown in the special drag
       items. */
    gnome_canvas_item_hide (event->canvas_item);
}


static void
e_day_view_on_drag_end (GtkWidget      *widget,
            GdkDragContext *context,
            EDayView       *day_view)
{
    EDayViewEvent *event;
    gint day, event_num;

    day = day_view->drag_event_day;
    event_num = day_view->drag_event_num;

    /* If the calendar has already been updated in drag_data_received()
       we just return. */
    if (day == -1 || event_num == -1)
        return;

    if (day == E_DAY_VIEW_LONG_EVENT) {
        event = &g_array_index (day_view->long_events, EDayViewEvent,
                    event_num);
        gtk_widget_queue_draw (day_view->top_canvas);
    } else {
        event = &g_array_index (day_view->events[day], EDayViewEvent,
                    event_num);
        gtk_widget_queue_draw (day_view->main_canvas);
    }

    /* Show the text item again. */
    gnome_canvas_item_show (event->canvas_item);

    day_view->drag_event_day = -1;
    day_view->drag_event_num = -1;
}


static void
e_day_view_on_drag_data_get (GtkWidget          *widget,
                 GdkDragContext     *context,
                 GtkSelectionData   *selection_data,
                 guint               info,
                 guint               time,
                 EDayView       *day_view)
{
    EDayViewEvent *event;
    gint day, event_num;
    gchar *event_uid;

    day = day_view->drag_event_day;
    event_num = day_view->drag_event_num;

    /* These should both be set. */
    g_return_if_fail (day != -1);
    g_return_if_fail (event_num != -1);

    if (day == E_DAY_VIEW_LONG_EVENT)
        event = &g_array_index (day_view->long_events,
                    EDayViewEvent, event_num);
    else
        event = &g_array_index (day_view->events[day],
                    EDayViewEvent, event_num);

    event_uid = event->ico->uid;
    g_return_if_fail (event_uid != NULL);

    if (info == TARGET_CALENDAR_EVENT) {
        gtk_selection_data_set (selection_data, selection_data->target,
                    8, event_uid, strlen (event_uid));
    }
}


static void  
e_day_view_on_top_canvas_drag_data_received  (GtkWidget          *widget,
                          GdkDragContext     *context,
                          gint                x,
                          gint                y,
                          GtkSelectionData   *data,
                          guint               info,
                          guint               time,
                          EDayView           *day_view)
{
    EDayViewEvent *event;
    EDayViewPosition pos;
    gint day, start_day, end_day, num_days;
    gchar *event_uid;
    iCalObject ico;

    if ((data->length >= 0) && (data->format == 8)) {
        pos = e_day_view_convert_position_in_top_canvas (day_view,
                                 x, y, &day,
                                 NULL);
        if (pos != E_DAY_VIEW_POS_OUTSIDE) {
            num_days = 1;
            if (day_view->drag_event_day == E_DAY_VIEW_LONG_EVENT) {
                event = &g_array_index (day_view->long_events, EDayViewEvent,
                            day_view->drag_event_num);
                day -= day_view->drag_event_offset;
                day = MAX (day, 0);

                e_day_view_find_long_event_days (day_view,
                                 event,
                                 &start_day,
                                 &end_day);
                num_days = end_day - start_day + 1;
                /* Make sure we don't go off the screen. */
                day = MIN (day, day_view->days_shown - num_days);
            } else if (day_view->drag_event_day != -1) {
                event = &g_array_index (day_view->events[day_view->drag_event_day],
                            EDayViewEvent,
                            day_view->drag_event_num);
            }

            event_uid = data->data;

            if (!event_uid || !event->ico->uid
                || strcmp (event_uid, event->ico->uid))
                g_warning ("Unexpected event UID");

            /* We use a temporary shallow copy of the ico since we
               don't want to change the original ico here.
               Otherwise we would not detect that the event's time
               had changed in the "update_event" callback. */
            ico = *event->ico;

            ico.dtstart = day_view->day_starts[day];
            ico.dtend = day_view->day_starts[day + num_days];

            gtk_drag_finish (context, TRUE, TRUE, time);

            /* Reset this since it will be invalid. */
            day_view->drag_event_day = -1;

            /* Show the text item again, just in case it hasn't
               moved. If we don't do this it may not appear. */
            if (event->canvas_item)
                gnome_canvas_item_show (event->canvas_item);

            /* Notify calendar of change */
            gnome_calendar_object_changed (day_view->calendar,
                               &ico, CHANGE_DATES);

            return;
        }
    }

    gtk_drag_finish (context, FALSE, FALSE, time);
}


static void  
e_day_view_on_main_canvas_drag_data_received  (GtkWidget          *widget,
                           GdkDragContext     *context,
                           gint                x,
                           gint                y,
                           GtkSelectionData   *data,
                           guint               info,
                           guint               time,
                           EDayView       *day_view)
{
    EDayViewEvent *event;
    EDayViewPosition pos;
    gint day, row, start_row, end_row, num_rows, scroll_x, scroll_y;
    gchar *event_uid;
    iCalObject ico;

    gnome_canvas_get_scroll_offsets (GNOME_CANVAS (widget),
                     &scroll_x, &scroll_y);
    x += scroll_x;
    y += scroll_y;

    if ((data->length >= 0) && (data->format == 8)) {
        pos = e_day_view_convert_position_in_main_canvas (day_view,
                                  x, y, &day,
                                  &row, NULL);
        if (pos != E_DAY_VIEW_POS_OUTSIDE) {
            num_rows = 1;
            if (day_view->drag_event_day == E_DAY_VIEW_LONG_EVENT) {
                event = &g_array_index (day_view->long_events, EDayViewEvent,
                            day_view->drag_event_num);
            } else if (day_view->drag_event_day != -1) {
                event = &g_array_index (day_view->events[day_view->drag_event_day],
                            EDayViewEvent,
                            day_view->drag_event_num);
                row -= day_view->drag_event_offset;

                /* Calculate time offset from start row. */
                start_row = event->start_minute / day_view->mins_per_row;
                end_row = (event->end_minute - 1) / day_view->mins_per_row;
                num_rows = end_row - start_row + 1;
            }

            event_uid = data->data;

            if (!event_uid || !event->ico->uid
                || strcmp (event_uid, event->ico->uid))
                g_warning ("Unexpected event UID");

            /* We use a temporary shallow copy of the ico since we
               don't want to change the original ico here.
               Otherwise we would not detect that the event's time
               had changed in the "update_event" callback. */
            ico = *event->ico;

            ico.dtstart = e_day_view_convert_grid_position_to_time (day_view, day, row);
            ico.dtend = e_day_view_convert_grid_position_to_time (day_view, day, row + num_rows);

            gtk_drag_finish (context, TRUE, TRUE, time);

            /* Reset this since it will be invalid. */
            day_view->drag_event_day = -1;

            /* Show the text item again, just in case it hasn't
               moved. If we don't do this it may not appear. */
            if (event->canvas_item)
                gnome_canvas_item_show (event->canvas_item);

            /* Notify calendar of change */
            gnome_calendar_object_changed (day_view->calendar,
                               &ico, CHANGE_DATES);

            return;
        }
    }

    gtk_drag_finish (context, FALSE, FALSE, time);
}