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

#include "vim.h"
#include <string.h>

/*
 * gui_riscos.c
 *
 * Thomas Leonard <tal197@ecs.soton.ac.uk>
 * Updated by Andy Wingate <andy@sparse.net>
 */

extern int time_of_last_poll;

int task_handle = 0;		/* Zero means we are not yet a Wimp task */
int child_handle = 0;		/* Task handle of our child process (zero if none). */
int *wimp_menu = (int *) -1;	/* Pointer to a Wimp menu structure (or -1) */
int save_window = -1;		/* Save As window handle */

int *redraw_block = NULL;	/* NULL means not in a redraw loop. */
int ro_return_early = FALSE;	/* Break out of gui_mch_wait_for_chars() */

int leaf_ref = 0;		/* Wimp message number - send via Wimp$Scrap */
char_u *leaf_name = NULL;	/* Leaf name from DataSave */

int default_columns = 120;	/* These values are used if the --rows and --columns */
int default_rows = 32;		/* options aren't used on startup. */

#define DRAG_FALSE	    0
#define DRAG_SELECTION	    1
#define DRAG_RESIZE_WINDOW  2
int ro_dragging = DRAG_FALSE;
int drag_button;
int drag_modifiers;
int drag_x_offset;
int drag_y_offset;

int nested_wimp = FALSE;	/* Bool - can we use the new wimp? */

int changed_mode = FALSE;
int x_eigen_factor;
int y_eigen_factor;

/* If ro_current_font is non-zero then use the outline font with that handle,
 * otherwise, if zap_redraw is TRUE then use ZapRedraw, otherwise use the
 * system font.
 *
 * If zap_redraw is TRUE then zap_file[] contains valid Zap font file
 * pointers (or NULLs).
 */
int ro_current_font = 0;	/* 0 is system font, or ZapRedraw */
int font_x_offset   = 0;	/* Where to position each char in its box */
int font_y_offset   = 0;

int zap_redraw	    = FALSE;
int double_height   = FALSE;	/* Plot each line twice? */

#define grgb(r,g,b) ((b<<16) + (g<<8) + (r))
#define UNUSED_COLOUR (gui.back_pixel)

#define RO_LOAD_CLIPBOARD -2	/* Internal handle for DataSave message. */

/* Changes by John Kortink, 22-23 July 1998
 *
 * Stuff to make redraw a lot faster. Almost all of it is right here below,
 * elsewhere changes are marked with 'JK230798'. Apart from a small change in
 * 'gui.c' all changes are limited to this file, 'gui_riscos.c'. The change in
 * 'gui.c' is to make Vim stop being 'smart' not redrawing characters that are
 * 'already there' (i.e. from the previous line, by coincidence). This caused a
 * lot more calls to the redraw code, which we want to avoid because a few nice
 * big strings at a time is a lot faster than a truckload of small ones. ('Dear
 * Bram ...').
 */

/* The ZapRedraw structure */

static struct
{
    int		r_flags;
    int		r_minx;
    int		r_miny;
    int		r_maxx;
    int		r_maxy;
    int		r_screen;
    int		r_bpl;
    int		r_bpp;
    int		r_charw;
    int		r_charh;
    char	*r_caddr;
    int		r_cbpl;
    int		r_cbpc;
    int		r_linesp;
    int		r_data;
    int		r_scrollx;
    int		r_scrolly;
    int		*r_palette;
    int		r_for;
    int		r_bac;
    char	*r_workarea;
    int		r_magx;
    int		r_magy;
    int		r_xsize;
    int		r_ysize;
    int		r_mode;
}
zap_redraw_block;

/* Other globals */

static int	zap_redraw_initialised = FALSE;
static int	zap_redraw_update_colours;
static int	zap_redraw_colours[2];
static int	zap_redraw_palette[16];

/* Holds the current Zap font file(s).
 * The font is recreated from this block on a mode change.
 * When using zap, element ZAP_NORMAL is always valid, but
 * the others can be NULL.
 */

#define ZAP_NORMAL  0
#define ZAP_BOLD    1
#define ZAP_ITALIC  2
#define ZAP_BITALIC 3
#define ZAP_STYLES  4

/* Zap font file format data */
static char	*zap_file[ZAP_STYLES] = {NULL, NULL, NULL, NULL};

/* r_caddr format for current mode */
static char	*zap_caddr[ZAP_STYLES] = {NULL, NULL, NULL, NULL};

static void ro_remove_menu(int *menu);

/*
 * Initialise all the ZapRedraw stuff.
 * Call this when changing font and after each mode change.
 * zap_redraw_bitmap must contain a valid Zap font file (possibly
 * created from the system font).
 *
 * Return FAIL to revert to system font (if we can't use ZapRedraw).
 */
    int
ro_zap_redraw_initialise()
{
    int	    bytes_per_bitmap_char;
    int	    first, last;
    int	    i;

    /* Can't have initialisers for struct members :-(, ok, this way then ... */
    if (!zap_redraw_initialised)
    {
	zap_redraw_block.r_workarea = NULL;
	zap_redraw_initialised = TRUE;
    }

    /* We redraw in DSA mode */
    zap_redraw_block.r_flags = 0x0;

    /* Let ZapRedraw get the screen address for us */
    zap_redraw_block.r_screen = 0;

    /* Read the font width and height from the font file header.
     * Assume that all styles are the same size.
     * ZAP_NORMAL is always present.
     */
    zap_redraw_block.r_charw = ((int *) zap_file[ZAP_NORMAL])[2];
    zap_redraw_block.r_charh = ((int *) zap_file[ZAP_NORMAL])[3];

    /* We have no linespacing */
    zap_redraw_block.r_linesp = 0;

    /* Fix foreground = colour 1 */
    zap_redraw_block.r_for = 1;

    /* Fix background = colour 0 */
    zap_redraw_block.r_bac = 0;

    /* Colour mask buffer */
    zap_redraw_block.r_palette = zap_redraw_palette;

    /* Allocate local workspace (for the few calls following here) */
    if (zap_redraw_block.r_workarea != NULL)
	free(zap_redraw_block.r_workarea);
    zap_redraw_block.r_workarea = (char*) malloc(128);
    if (!zap_redraw_block.r_workarea)
	return FAIL;	/* Out of memory */

    /* Fill in VDU variables */
    if (xswi(ZapRedraw_ReadVduVars, 0, &zap_redraw_block) & v_flag)
	return FAIL;	    /* Can't find ZapRedraw module - use VDU instead */

    /* Determine cbpl and cbpc */
    swi(ZapRedraw_CachedCharSize, zap_redraw_block.r_bpp, 0,
	zap_redraw_block.r_charw, zap_redraw_block.r_charh);
    zap_redraw_block.r_cbpl = r2;
    zap_redraw_block.r_cbpc = r3;

    /* Allocate general workspace (for the calls outside) */
    if (zap_redraw_block.r_workarea != NULL)
	free(zap_redraw_block.r_workarea);
    zap_redraw_block.r_workarea = (char*) malloc(128 + zap_redraw_block.r_cbpl);
    if (!zap_redraw_block.r_workarea)
	return FAIL;	/* Out of memory */

    /* Now convert the 1 bpp character data ready for the current mode */

    bytes_per_bitmap_char = (zap_redraw_block.r_charw * zap_redraw_block.r_charh + 7) / 8;

    /* Convert the fonts from 1bpp to a format suitable for the
     * current mode.
     */
    for (i = 0; i < ZAP_STYLES; i++)
    {
	first = ((int *) zap_file[i])[4];
	last  = ((int *) zap_file[i])[5];

	if (last > 255)
	    last = 255;	/* Don't convert cursors (overwrites memory!) */

	/* Allocate the font cache */
	vim_free(zap_caddr[i]);
	if (zap_file[i])
	    zap_caddr[i] = (char*) malloc(zap_redraw_block.r_cbpc * 256);
	else
	    zap_caddr[i] = NULL;    /* No file for this style */

	if (zap_caddr[i])
	{
	    zap_redraw_block.r_caddr = zap_caddr[i];

	    swi(ZapRedraw_ConvertBitmap, 0, &zap_redraw_block,
		    first, last,		/* Range of characters to convert */
		    zap_file[i] + 0x20	/* Addr of first char provided by font */
		    - first * bytes_per_bitmap_char);
	}
    }

    if (!zap_caddr[ZAP_NORMAL])
    {
	zap_redraw = FALSE;	/* Out of memory */
	return FAIL;
    }

    /* Next time we need them, we have to update the colour masks */
    zap_redraw_update_colours = TRUE;

    return OK;
}

/*
 * Redraw a string at OS coordinates <x,y> (top-left, x inclusive, y exclusive).
 * Graphics clip window is window[0..3] as in R1+28..40 of Wimp_RedrawWindow.
 * Returns (possibly modified) flags.
 */
    int
ro_zap_redraw_draw_string(x, y, string, length, flags, clip)
    int	    x;
    int	    y;
    char    *string;
    int	    length;
    int	    flags;	/* DRAW_TRANSP, DRAW_BOLD, DRAW_UNDERL, DRAW_ITALIC */
    int	    *clip;
{
    char redraw_data[1024];
    int clip_minx;
    int clip_miny;
    int clip_maxx;
    int clip_maxy;
    int os_xshift = zap_redraw_block.r_magx;
    int os_yshift = zap_redraw_block.r_magy;

    if (flags & DRAW_TRANSP)
	return flags;	/* We don't do transparent plotting yet. */

    if (flags & DRAW_BOLD)
    {
	if (flags & DRAW_ITALIC && zap_caddr[ZAP_BITALIC])
	    zap_redraw_block.r_caddr = zap_caddr[ZAP_BITALIC];
	else
	    zap_redraw_block.r_caddr = zap_caddr[ZAP_BOLD];
    }
    else
    {
	if (flags & DRAW_ITALIC)
	    zap_redraw_block.r_caddr = zap_caddr[ZAP_ITALIC];
	else
	    zap_redraw_block.r_caddr = zap_caddr[ZAP_NORMAL];
    }
    if (!zap_redraw_block.r_caddr)
    {
	zap_redraw_block.r_caddr = zap_caddr[ZAP_NORMAL];
	flags |= DRAW_UNDERL;	    /* Style missing - we can always underline */
    }

    /* Set the vertical scaling flag */
    if (double_height)
	zap_redraw_block.r_flags = 1 << 1;
    else
	zap_redraw_block.r_flags = 0;

    /* Update the colour masks (if needed) */
    if (zap_redraw_update_colours)
    {
	swi(ZapRedraw_CreatePalette, 2,
		&zap_redraw_block,
		zap_redraw_colours,
		zap_redraw_block.r_palette, 2);
	zap_redraw_update_colours = FALSE;
    }

    /* Target rectangle in ZapRedraw rectangle coordinates (pixels, Y-min/max reversed !!!) */
    zap_redraw_block.r_minx = x >> os_xshift;					/* inclusive */
    zap_redraw_block.r_miny = zap_redraw_block.r_ysize - (y >> os_yshift);	/* inclusive */
    zap_redraw_block.r_maxx = (x + length * gui.char_width) >> os_xshift;	/* exclusive */
    zap_redraw_block.r_maxy = zap_redraw_block.r_ysize - ((y - gui.char_height) >> os_yshift);
										/* exclusive */

    /* Clip rectangle in ZapRedraw rectangle coordinates (pixels, Y-min/max reversed !!!) */
    clip_minx = clip[0] >> os_xshift;					/* inclusive */
    clip_miny = zap_redraw_block.r_ysize - (clip[3] >> os_yshift);	/* inclusive */
    clip_maxx = clip[2] >> os_xshift;					/* exclusive */
    clip_maxy = zap_redraw_block.r_ysize - (clip[1] >> os_yshift);	/* exclusive */

    /* Clip target rectangle against the current graphics window */
    if (zap_redraw_block.r_minx < clip_minx)
    {
	zap_redraw_block.r_scrollx = clip_minx - zap_redraw_block.r_minx;
	zap_redraw_block.r_minx = clip_minx;
    }
    else
	zap_redraw_block.r_scrollx = 0;
    if (zap_redraw_block.r_miny < clip_miny)
    {
	zap_redraw_block.r_scrolly = clip_miny - zap_redraw_block.r_miny;
	zap_redraw_block.r_miny = clip_miny;
    }
    else
	zap_redraw_block.r_scrolly = 0;
    if (zap_redraw_block.r_maxx > clip_maxx)
	zap_redraw_block.r_maxx = clip_maxx;
    if (zap_redraw_block.r_maxy > clip_maxy)
	zap_redraw_block.r_maxy = clip_maxy;

    /* Fill in the character data structure */
    if (length > (sizeof(redraw_data) - 2 * 4 - 2))
	length = sizeof(redraw_data) - 2 * 4 - 2;
    ((int*) redraw_data)[0] = 2 * 4;
    ((int*) redraw_data)[1] = 0;
    strncpy(redraw_data + 2 * 4, string, length);
    redraw_data[2 * 4 + length + 0] = '\0';
    redraw_data[2 * 4 + length + 1] = '\x2';
    zap_redraw_block.r_data = (int) redraw_data;

    /* Perform the draw */
    swi(ZapRedraw_RedrawArea, 0, &zap_redraw_block);

    return flags;
}

/*
 * Okay that was it from me, back to Thomas ...
 */

/*
 * Parse the GUI related command-line arguments.  Any arguments used are
 * deleted from argv, and *argc is decremented accordingly.  This is called
 * when vim is started, whether or not the GUI has been started.
 */
    void
gui_mch_prepare(int *argc, char **argv)
{
    int	    arg = 1;

    while (arg < *argc - 1)
    {
	if (strcmp(argv[arg], "--rows") == 0 || strcmp(argv[arg], "--columns") == 0)
	{
	    int	    value;

	    value = atoi(argv[arg + 1]);

	    if (argv[arg][2] == 'r')
		default_rows = value;
	    else
		default_columns = value;

	    /* Delete argument from argv[]. (hope this is read/write!) */

	    *argc -= 2;
	    if (*argc > arg)
	    mch_memmove(&argv[arg], &argv[arg + 2], (*argc - arg)
		    * sizeof(char *));
	}
	else
	    arg++;
    }
}

/* Fatal error on initialisation - report it and die. */
    void
ro_die(error)
    char_u *error;	/* RISC OS error block */
{
    swi(Wimp_ReportError, error, 5, "GVim");
    exit(EXIT_FAILURE);
}

/* Find the sizes of the window tools:
 *
 * Create a test window.
 * Find inner and outer sizes.
 * Find the difference.
 * Delete window.
 *
 * While we're here, find the eigen values too.
 */
    void
ro_measure_tools()
{
    int block[10];
    int vdu[] = { 4, 5, -1};
    int test_window[] =
	{
	    -100, -100,		/* Visible area : min X,Y */
	    -50, -50,		/*		  max X,Y */
	    0,   0,		/* Scroll offsets */
	    -1,			/* Window in front */
	    0xd0800150,		/* Window flags */
	    0xff070207,		/* Colours */
	    0x000c0103,		/* More colours */
	    0, -0x4000,		/* Workarea extent */
	    0x4000, 0,		/* max X,Y */
	    0x00000000,		/* No title */
	    0 << 12,		/* No workarea button type */
	    1,			/* Wimp sprite area */
	    0x00010001,		/* Minimum width, height */
	    0, 0, 0,		/* Title data (none) */
	    0			/* No icons */
	};
    int inner_max_x, inner_min_y;

    swi(Wimp_CreateWindow, 0, test_window);

    block[0] = r0;
    /* Open the window (and read state).
     * GetWindowOutline needs it too if the wimp isn't nested.
     */
    swi(Wimp_OpenWindow, 0, block);
    inner_max_x = block[3];
    inner_min_y = block[2];

    swi(Wimp_GetWindowOutline, 0, block);

    gui.scrollbar_width = block[3] - inner_max_x;
    gui.scrollbar_height = inner_min_y - block[2];

    swi(Wimp_DeleteWindow, 0, block);

    /* Read the size of one pixel. */
    swi(OS_ReadVduVariables, vdu, vdu);
    x_eigen_factor = vdu[0];
    y_eigen_factor = vdu[1];
}

/* Load a template from the current templates file.
 * Create the window and return its handle.
 */
    int
ro_load_template(str_name, title, title_size)
    char_u  *str_name;      /* Identifier of window in file (max 12 chars)   */
    char_u  **title;	    /* If not NULL then return pointer to title here */
    int     *title_size;    /* If not NULL then return the title length here */
{
    int     *window;
    char    *data;
    int     name[4];

    strcpy( (char *) name, str_name);

    /* Find how big we must make the buffers */

    if (xswi(Wimp_LoadTemplate, 0, 0, 0, 0, -1, name, 0) & v_flag)
	ro_die( (char *) r0);

    window = malloc(r1);	/* Don't print text messages from alloc() */
    data = malloc(r2);
    if (window == NULL || data == NULL)
	ro_die("\0\0\0\0Out of memory - Can't load templates");

    /* Load the template into the buffers */

    swi(Wimp_LoadTemplate, 0,
				window,		/* Temp block */
				data,		/* Icon data */
				data + r2 + 1,	/* End of icon data */
				-1,		/* No fonts */
				name, 0);	/* First match */
    if (r6 == 0)
	ro_die("\0\0\0\0Can't find window in Templates file");

    /* Create the window */

    if (xswi(Wimp_CreateWindow, 0, window) & v_flag)
	ro_die( (char *) r0);

    if (title)
	*title = (char_u *) window[18];
    if (title_size)
	*title_size = window[20];

    free(window);	/* Free temp block */
    return r0;		/* Return the window handle */
}

/*
 * Check if the GUI can be started.  Called before gvimrc is sourced.
 * Return OK or FAIL.
 */
    int
gui_mch_init_check()
{
    return OK;		/* TODO: GUI can always be started? */
}

/*
 * Initialise the RISC OS GUI.
 * Create all the windows.
 * Returns OK for success, FAIL when the GUI can't be started.
 */
    int
gui_mch_init()
{
    int     messages[] = {
	    1, 2, 3, 4,	/* DataSave, DataSaveAck, DataLoad, DataLoadAck */
	    8,		/* PreQuit */
	    0xf,	/* ClaimEntity (for clipboard) */
	    0x10,	/* DataRequest (for clipboard) */
	    0x400c1,	/* Mode change */
	    0x400c3,	/* TaskCloseDown */
	    0x400c9,	/* MenusDeleted */
	    0x808c1,	/* TW_Output */
	    0x808c2,    /* TW_Ego */
	    0x808c3,	/* TW_Morio */
	    0x808c4,	/* TW_Morite */
	    0};		/* End-of-list. */


    /* There may have been some errors reported in the
     * command window before we get here. Wait if so.
     */
    swi(Wimp_ReadSysInfo, 3);
    if (r0 == 0)
	swi(Wimp_CommandWindow, 0);	/* Window opened - close with prompt */

    if (xswi(Wimp_Initialise, 310, 0x4b534154, "GVim", messages) & v_flag)
	return FAIL;
    nested_wimp = r0 >= 397;
    task_handle = r1;

    /* Load the templates. */

    if (xswi(Wimp_OpenTemplate, 0, "Vim:Templates") & v_flag)
	ro_die( (char *) r0);

    gui.window_handle = ro_load_template("editor",
	    &gui.window_title,
	    &gui.window_title_size);

    save_window = ro_load_template("save", NULL, NULL);

    swi(Wimp_CloseTemplate);

    /* Set default foreground and background colours. */

    gui.norm_pixel = gui.def_norm_pixel;
    gui.back_pixel = gui.def_back_pixel;

    /* Get the colours from the "Normal" and "Menu" group (set in syntax.c or
     * in a vimrc file) */

    set_normal_colors();

    /*
     * Check that none of the colors are the same as the background color
     */

    gui_check_colors();

    /* Get the colours for the highlight groups (gui_check_colors() might have
     * changed them) */

    highlight_gui_started();		/* re-init colours and fonts */

    /* Set geometry based on values read on initialisation. */

    gui.num_cols = Columns = default_columns;
    gui.num_rows = Rows    = default_rows;

    /* Get some information about our environment. */

    ro_measure_tools();

    return OK;
}

/*
 * Called when the foreground or background colour has been changed.
 */
    void
gui_mch_new_colors()
{
}

/*
 * Open the GUI window which was created by a call to gui_mch_init().
 */
    int
gui_mch_open(void)
{
    int block[10];

    block[0] = gui.window_handle;
    swi(Wimp_GetWindowState, 0, block);
    block[7] = -1;		    /* Open at the top of the stack */
    swi(Wimp_OpenWindow, 0, block);

    /* Give the new window the input focus */
    swi(Wimp_SetCaretPosition, gui.window_handle, -1, 0, 0, -1, -1);

    if (gui_win_x != -1 && gui_win_y != -1)
	gui_mch_set_winpos(gui_win_x, gui_win_y);

    return OK;
}

    void
gui_mch_exit(int rc)
{
    int	    block[64];

    /* Close window. Stops us from getting troublesome events
     * if we take a while to die.
     */
    block[0] = gui.window_handle;
    swi(Wimp_CloseWindow, 0, block);

    if (child_handle)
    {
	/* We still have a sub-task running - kill it */
	block[0] = 20;
	block[3] = 0;
	block[4] = 0;	    /* Quit */
	if ((xswi(Wimp_SendMessage, 17, block, child_handle) & v_flag) == 0)
	{
	    /* Idle until child dies. */
	    while (child_handle)
	    {
		process_event(wimp_poll(1, block), block);
	    }
	}
    }

    exit(rc);
}

/*
 * Get the position of the top left corner of the window.
 */
    int
gui_mch_get_winpos(int *x, int *y)
{
    /* TODO */
    return FAIL;
}

/*
 * Set the position of the top left corner of the window to the given
 * coordinates.
 */
    void
gui_mch_set_winpos(int x, int y)
{
    /* TODO */
}

    void
gui_mch_set_shellsize(width, height, min_width, min_height, base_width, base_height)
    int width;		/* In OS units */
    int height;
    int min_width;	/* Smallest permissable window size (ignored) */
    int min_height;
    int base_width;	/* Space for scroll bars, etc */
    int base_height;
{
    int s_width, s_height;
    int block[] = {
	gui.window_handle,
	0,
	-height + 1,
	width,
	1};

    gui_mch_get_screen_dimensions(&s_width, &s_height);
    s_width -= base_width;
    s_height -= base_height;		    /* Underestimate - ignores titlebar */

    swi(Wimp_GetWindowState, 0, block);
    block[3]  = block[1] + width;
    block[2]  = block[4] - height;
    if (block[3] > s_width)
    {
	block[3] = s_width;
	block[1] = block[3] - width;
    }
    if (block[2] < gui.scrollbar_height)
    {
	block[2] = gui.scrollbar_height;
	block[4] = block[2] + height;
    }
    swi(Wimp_OpenWindow, 0, block);
    swi(Wimp_ForceRedraw, gui.window_handle, 0, -height, width, 0);
}

    void
gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
{
    int block[] = {4, 5, 11, 12, -1};

    swi(OS_ReadVduVariables, block, block);
    *screen_w = (block[2] + 1) << block[0];
    *screen_h = (block[3] + 1) << block[1];
}

/* Take a font name with options and return a font handle, or
 * zero for failure.
 * Replace extension with 'Bold' or 'Italic' depending on modifiers.
 */
    int
ro_get_font(fullname, weight)
    char_u	*fullname;
    int		weight;		/* Initial weights:
				 * BIT	    MEANING
				 * 0	    bold
				 * 1	    italic
				 */
{
    char_u	*arg;
    char_u	font[41];
    int		width = -1;
    int		height = -1;
    int		name_len;
    int		i;
    char_u	c;

    for (i = 0; i < 39;)
    {
	c = fullname[i];
	if (c == ':' || c == NUL || c == '.')
	    break;
	font[i++] = c;
    }

    /* find the first modifier, NULL if none */
    arg = strchr(fullname + i, ':');

    while (arg)
    {
	switch (*++arg)
	{
	    case 'h':
		height = strtol(arg + 1, (char **) &arg, 10);
		break;
	    case 'w':
		width = strtol(arg + 1, (char **) &arg, 10);
		break;
	    case 'b':
		weight |= 1;
		break;
	    case 'i':
		weight |= 2;
		break;
	    default:
		return 0;
	}
	arg = strchr(arg, ':');
    }

    if ((weight & 1) && i < 35)
    {
	/* Bold goes instead of given suffix */
	strncpy(font + i, ".Bold", 5);
	i += 5;
    }
    else
    {
	/* Copy rest of name unless we are using Bold */
	while (i < 39)
	{
	    c = fullname[i];
	    if (c == ':' || c == NUL)
		break;
	    font[i++] = c;
	}
    }
    if ((weight & 2) && i < 32)
    {
	strncpy(font + i, ".Oblique", 8);
	i += 8;
    }

    font[i] = 0;

    if (height < 1 && width < 1)
	height = width = 10;	/* Default to 10pt */
    else if (height < 1)
	height = width;
    else if (width < 1)
	width = height;

    if (xswi(Font_FindFont, 0, font, width << 4, height << 4, 0, 0) & v_flag)
	return NOFONT;		/* Can't find font */

    return r0;
}

/* Load a file into allocated memory and check it is valid.
 * Return a pointer to the allocated block on success.
 */
    char    *
zap_load_file(name, style)
    char_u  *name;	    /* Name of directory containing styles */
    char_u  *style;	    /* Name of style within directory */
{
    char_u  fname[256];
    char_u  *file;

    if (strlen(name) + strlen(style) > 254)
	return NULL;	    /* Names too long */

    sprintf(fname, "%s.%s", name, style);

    /* Load the named font in 1bpp format. */
    if (xswi(OS_File, 13, fname, 0, 0, "VimFonts:") & v_flag || r0 != 1)
	return NULL;	    /* Error reading file info, or not a file */

    /* Allocate enough memory to load the whole file */
    file = (char *) alloc(r4);
    if (!file)
	return NULL;	/* Out of memory */

    if (xswi(OS_File, 12, fname, file, 0, "VimFonts:") & v_flag)
	return NULL;	/* Unable to load file */

    if (strncmp(file, "ZapFont\015", 8) == 0)
	return file;	/* Loaded OK! */

    free(file);
    return NULL;	/* Not a valid font file */
}

/* Load and convert the named font.
 * If name is NULL or a null string then convert the system font.
 * Return OK on success; FAIL and we revert to using the VDU drivers.
 *
 * 'name' is the name of a directory.
 * Tries to load 'name.0', 'name.B', 'name.I' and 'name.IB'.
 */
    int
zap_load_font(name)
    char_u  *name;
{
    int	    i;

    /* Free the existing font files, if any */
    for (i = 0; i < ZAP_STYLES; i++)
    {
	vim_free(zap_file[i]);
	zap_file[i] = NULL;
    }

    if (name && *name == '!')
    {
	name++;
	double_height = TRUE;
    }
    else
	double_height = FALSE;

    if (name && *name)
    {
	zap_file[ZAP_NORMAL]	= zap_load_file(name, "0");
	if (!zap_file[ZAP_NORMAL])
	    return FAIL;	/* Can't load the 'normal' style - error */

	zap_file[ZAP_BOLD]	= zap_load_file(name, "B");
	zap_file[ZAP_ITALIC]	= zap_load_file(name, "I");
	zap_file[ZAP_BITALIC]	= zap_load_file(name, "IB");
    }
    else
    {
	int	*header;
	char	workarea[16];
	char	*old_wa;

	/* Allocate memory for system font (8 x 8 x 256 bits, plus header) */
	header = (int *) alloc(0x20 + 8 * 256);
	if (header == NULL)
	    return FAIL;
	zap_file[ZAP_NORMAL] = (char *) header;

	/* Store details about the system font */
	header[2] = 8;	    /* Width */
	header[3] = 8;	    /* Height */
	header[4] = 0;	    /* First char */
	header[5] = 255;    /* Last char */
	header[6] = header[7] = 0;  /* Reserved */

	/* Get system font bitmap */
	old_wa = zap_redraw_block.r_workarea;
	zap_redraw_block.r_workarea = workarea;
	swi(ZapRedraw_ReadSystemChars, zap_file[ZAP_NORMAL] + 0x20, &zap_redraw_block);
	zap_redraw_block.r_workarea = old_wa;
    }

    return ro_zap_redraw_initialise();
}

/*
 * Initialise vim to use the font with the given name.
 * Return FAIL if the font could not be loaded, OK otherwise.
 */
    int
gui_mch_init_font(char_u *font_name, int fontset)
{
    int	    new_handle	= 0;	    /* Use the system font by default */

    if (font_name[0] == '!')
    {
	/* Select a ZapRedraw font */
	if (zap_load_font(font_name + 1))
	    zap_redraw = TRUE;
	else
	{
	    EMSG2(_("E610: Can't load Zap font '%s'"), font_name);
	    font_name = "System";   /* Error - use system font */
	    zap_redraw = FALSE;
	}
    }
    else
    {
	zap_redraw = FALSE;

	if (font_name)
	{
	    /* Extract any extra details about the font */
	    new_handle = ro_get_font(font_name, 0);
	    if (!new_handle)
		return FAIL;
	}
	else
	    font_name = "System";
    }

    /* Free the previous font, if any */
    gui_mch_free_font(gui.norm_font);
    gui.norm_font = new_handle;
    gui.char_ascent = 0;

    if (new_handle)
    {
	/* Read details about the chosen font */
	swi(Font_ReadInfo, new_handle);

	gui.char_width	= r3 - r1;
	gui.char_height = r4 - r2;

	font_x_offset = -r1;	/* Where to position each char in its box */
	font_y_offset = -r4;

	/* Try to load other fonts for bold, italic, and bold-italic */
	gui_mch_free_font(gui.bold_font);
	gui.bold_font = ro_get_font(font_name, 1);
	gui_mch_free_font(gui.ital_font);
	gui.ital_font = ro_get_font(font_name, 2);
	gui_mch_free_font(gui.boldital_font);
	gui.boldital_font = ro_get_font(font_name, 3);
    }
    else
    {
	/* Use the system font or ZapRedraw. */
	if (zap_redraw)
	{
	    gui.char_width	= zap_redraw_block.r_charw << zap_redraw_block.r_magx;
	    gui.char_height	= zap_redraw_block.r_charh << zap_redraw_block.r_magy;
	    if (double_height)
		gui.char_height <<= 1;
	}
	else
	{
	    gui.char_width	= 16;
	    gui.char_height	= 32;
	}

	gui_mch_free_font(gui.bold_font);
	gui.bold_font = 0;
	gui_mch_free_font(gui.ital_font);
	gui.ital_font = 0;
	gui_mch_free_font(gui.boldital_font);
	gui.boldital_font = 0;
    }
    hl_set_font_name(font_name);

    must_redraw = CLEAR;
    return OK;
}

    int
gui_mch_adjust_charsize()
{
    return FAIL;
}

/*
 * Get a font structure for highlighting.
 */
    GuiFont
gui_mch_get_font(name, giveErrorIfMissing)
    char_u	*name;
    int		giveErrorIfMissing;
{
    int		handle;

    if (!name)
	return NOFONT;		/* System font if no name */

    handle = ro_get_font(name, 0);
    if (!handle)
    {
	if (giveErrorIfMissing)
	    EMSG2(_("E611: Can't use font %s"), name);
	return NOFONT;
    }

    return handle;
}

/*
 * Set the current text font.
 */
    void
gui_mch_set_font(GuiFont font)
{
    ro_current_font = font;

    if (font)
    {
	/* Not the system font or ZapRedraw font - select it */
	swi(Font_SetFont, font);
    }
}

#if 0 /* not used */
/*
 * Return TRUE if the two fonts given are equivalent.
 */
    int
gui_mch_same_font(GuiFont f1, GuiFont f2)
{
    return f1 == f2;
}
#endif

/*
 * If a font is not going to be used, free its structure.
 */
    void
gui_mch_free_font(GuiFont font)
{
    if (font)
	swi(Font_LoseFont, font);
}

/*
 * Return the Pixel value (colour) for the given colour name.
 * Return INVALCOLOR for error.
 * NB: I've changed Green for now, since it looked really sick
 */
    guicolor_T
gui_mch_get_color(char_u *name)
{
    int		i;
    struct colour
    {
	char_u		*name;
	guicolor_T	value;
    } colours[] =
    {
	{ "Red",		grgb(255,	0,	0)	},
	{ "LightRed",		grgb(255,	0,	0)	},
	{ "DarkRed",		grgb(139,	0,	0)	},

	{ "Green",		grgb(50,	200,	50)	},
	{ "LightGreen",		grgb(144,	238,	144)	},
	{ "DarkGreen",		grgb(0,		100,	0)	},
	{ "SeaGreen",		grgb(46,	139,	87)	},

	{ "Blue",		grgb(0,		0,	255)	},
	{ "LightBlue",		grgb(173,	216,	230)	},
	{ "DarkBlue",		grgb(0,		0,	139)	},
	{ "SlateBlue",		grgb(160,	90,	205)	},

	{ "Cyan",		grgb(0,		255,	255)	},
	{ "LightCyan",		grgb(224,	255,	255)	},
	{ "DarkCyan",		grgb(0,		139,	139)	},

	{ "Magenta",		grgb(255,	0,	255)	},
	{ "LightMagenta",	grgb(255,	224,	255)	},
	{ "DarkMagenta",	grgb(139,	0,	139)	},

	{ "Yellow",		grgb(255,	255,	0)	},
	{ "LightYellow",	grgb(255,	255,	224)	},
	{ "DarkYellow",		grgb(139,	139,	0)	},
	{ "Brown",		grgb(165,	42,	42)	},

	{ "Gray",		grgb(190,	190,	190)	},
	{ "Grey",		grgb(190,	190,	190)	},
	{ "LightGray",		grgb(211,	211,	211)	},
	{ "LightGrey",		grgb(211,	211,	211)	},
	{ "DarkGray",		grgb(169,	169,	169)	},
	{ "DarkGrey",		grgb(169,	169,	169)	},

	{ "Black",		grgb(0,		0,	0)	},
	{ "White",		grgb(255,	255,	255)	},

	{ "Orange",		grgb(255,	165,	0)	},
	{ "Purple",		grgb(160,	32,	240)	},
	{ "Violet",		grgb(238,	130,	238)	},
	{NULL, 0}
    };

    if (name[0] == '#')
    {
	char	    *end;
	int	    c;

	c = strtol(name + 1, &end, 16);
	return (guicolor_T) ((c >> 16) & 0xff) | (c & 0xff00) | ((c & 0xff) << 16);
    }

    for (i = 0; colours[i].name != NULL; i++)
    {
	if (STRICMP(name, colours[i].name) == 0)
	    return colours[i].value;
    }
    if (strnicmp(name, "grey", 4) == 0 || strnicmp(name, "gray", 4) == 0)
    {
	int level = (255 * atoi(name + 4)) / 100;
	return (guicolor_T) grgb(level, level, level);
    }
    return INVALCOLOR;
}

/*
 * Set the current text colours.
 * If we are using fonts then set the antialiasing colours too.
 */
    void
gui_mch_set_colors(guicolor_T fg, guicolor_T bg)
{
    zap_redraw_colours[0] = bg << 8;	/* JK230798, register new background colour */
    zap_redraw_colours[1] = fg << 8;	/* JK230798, register new foreground colour */
    zap_redraw_update_colours = TRUE;	/* JK230798, need update of colour masks */

    swi(ColourTrans_ReturnGCOL, fg << 8);
    gui.fg_colour = r0;
    swi(ColourTrans_ReturnGCOL, bg << 8);
    gui.bg_colour = r0;

    if (ro_current_font)
	swi(ColourTrans_SetFontColours, 0, bg << 8, fg << 8, 14);
}

    void
ro_draw_string(x, y, s, len, flags, clip)
    int	    x;		/* Top-left coord to plot at (x incl, y excl) */
    int	    y;		/* (screen coords) */
    char_u  *s;		/* String to plot */
    int	    len;	/* Length of string */
    int	    flags;	/* DRAW_TRANSP, DRAW_BOLD, DRAW_UNDERL */
    int*    clip;	/* JK230798, added clip window */
{
    if (ro_current_font)
    {
	int	fx;
	int	flen = len;	/* Preserve for underline */

	/* Use the Font manager to paint the string.
	 * Must do one char at a time to get monospacing.
	 */

	if (flags & DRAW_ITALIC && !gui.ital_font)
	    flags |= DRAW_UNDERL;	/* No italic - underline instead */

	if ((flags & DRAW_TRANSP) == 0)
	{
	    swi(ColourTrans_SetColour, gui.bg_colour, 0, 0, 0, 0);
	    swi(OS_Plot, 4, x, y - gui.char_height);
	    swi(OS_Plot, 96 + 5, x + len * gui.char_width - 1, y - 1);
	}

	fx = x + font_x_offset;
	while (flen--)
	{
	    swi(Font_Paint, 0, s++, 0x90, fx, y + font_y_offset, 0, 0, 1);
	    fx += gui.char_width;
	}
    }
    else
    {
	if (zap_redraw)
	{
	    /* Using fast Zap redraw. */
	    flags = ro_zap_redraw_draw_string(x, y, s, len, flags, clip);
	}
	else
	{
	    /* Using the system font */
	    if (flags & DRAW_ITALIC)
		flags |= DRAW_UNDERL;

	    if ((flags & DRAW_TRANSP) == 0)
	    {
		swi(ColourTrans_SetColour, gui.bg_colour, 0, 0, 0, 0);
		swi(OS_Plot, 4, x, y - gui.char_height);
		swi(OS_Plot, 96 + 5, x + len * gui.char_width - 1, y - 1);
	    }
	    swi(OS_Plot, 4,			/* Move the drawing cursor */
		    x,
		    y - 1);
	    swi(ColourTrans_SetColour, gui.fg_colour, 0, 0, 0, 0);
	    swi(OS_WriteN, s, len);

	    if (flags & DRAW_BOLD)
	    {
		swi(OS_Plot, 4, x + (1 << x_eigen_factor), y - 1);
		swi(OS_WriteN, s, len);
	    }
	}
    }

    if (flags & DRAW_UNDERL)
    {
	if (ro_current_font || zap_redraw)
	    swi(ColourTrans_SetColour, gui.fg_colour, 0, 0, 0, 0);
	/* Underlined is the same with all plotting methods */
	swi(OS_Plot, 4, x, y - gui.char_height);
	swi(OS_Plot, 1, gui.char_width * len, 0);
    }
}

    void
gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
{
    int x, y;		/* Workarea x,y */
    x = col * gui.char_width;
    y = -row * gui.char_height;

    if (redraw_block)
    {
	ro_draw_string(x + redraw_block[1], y + redraw_block[4],
			s, len, flags, &redraw_block[7]);	/* JK230798, added clip window */
    }
    else
    {
	int block[44];
	block[0] = gui.window_handle;
	block[1] = x;
	block[2] = y - gui.char_height;
	block[3] = (col + len) * gui.char_width;
	block[4] = y;
	swi(Wimp_UpdateWindow, 0, block);
	while (r0)
	{
	    ro_draw_string(x + block[1], y + block[4],
			s, len, flags, &block[7]);	/* JK230798, added clip window */
	    swi(Wimp_GetRectangle, 0, block);
	}
    }
}

/*
 * Return OK if the key with the termcap name "name" is supported.
 */
    int
gui_mch_haskey(char_u *name)
{
    return FAIL;
}

    void
gui_mch_beep(void)
{
    swi(OS_WriteI + 7);
}

/*
 * Visual bell.
 */
    void
gui_mch_flash(int msec)
{
    /* TODO */
}


/*
 * Plot a solid rectangle using the given plot action and colour.
 * Coordinates are inclusive and window-relative.
 */
    void
plot_rectangle(plot, colour, minx, miny, maxx, maxy)
    int plot;		/* OS_Plot action */
    int colour;
    int minx;
    int miny;
    int maxx;
    int maxy;
{
    if (redraw_block)
    {
	swi(ColourTrans_SetColour, colour, 0, 0, 0, 0);
	swi(OS_Plot, 4, minx + redraw_block[1], miny + redraw_block[4]);
	swi(OS_Plot, plot, maxx + redraw_block[1], maxy + redraw_block[4]);
    }
    else
    {
	int block[44];
	block[0] = gui.window_handle;
	block[1] = minx;
	block[2] = miny;
	block[3] = maxx + 1;
	block[4] = maxy + 1;
	swi(Wimp_UpdateWindow, 0, block);
	while (r0)
	{
	    swi(ColourTrans_SetColour, colour, 0, 0, 0, 0);
	    swi(OS_Plot, 4, minx + block[1], miny + block[4]);
	    swi(OS_Plot, plot, maxx + block[1], maxy + block[4]);
	    swi(Wimp_GetRectangle, 0, block);
	}
    }
}

/*
 * Invert a rectangle from row r, column c, for nr rows and nc columns.
 */
    void
gui_mch_invert_rectangle(int r, int c, int nr, int nc)
{
    plot_rectangle(96 + 6, 0, FILL_X(c), -FILL_Y(r + nr), FILL_X(c + nc), -FILL_Y(r));
}

/*
 * Iconify the GUI window.
 */
    void
gui_mch_iconify(void)
{
}

#if defined(FEAT_EVAL) || defined(PROTO)
/*
 * Bring the Vim window to the foreground.
 */
    void
gui_mch_set_foreground()
{
    /* TODO */
}
#endif

/* Draw a hollow rectangle relative to the current
 * graphics cursor position, with the given width
 * and height. Start position is top-left.
 */
    void
draw_hollow(w, h)
    int	w;
    int	h;
{
    swi(OS_Plot, 1, w - 1, 0);
    swi(OS_Plot, 1, 0, 1 - h);
    swi(OS_Plot, 1, 1 - w, 0);
    swi(OS_Plot, 1, 0, h - 1);
}

/*
 * Draw a cursor without focus.
 */
    void
gui_mch_draw_hollow_cursor(guicolor_T colour)
{
    int x = FILL_X(gui.cursor_col);	/* Window relative, top-left */
    int y = -FILL_Y(gui.cursor_row);
    if (redraw_block == NULL)
    {
	int block[11];

	block[0] = gui.window_handle;
	block[1] = x;
	block[2] = y - gui.char_height;
	block[3] = x + gui.char_width;
	block[4] = y;
	swi(Wimp_UpdateWindow, 0, block);
	while (r0)
	{
	    swi(ColourTrans_SetGCOL, colour << 8, 0, 0, 0, 0);

	    swi(OS_Plot, 4, x + block[1], y + block[4] - 1);
	    draw_hollow(gui.char_width, gui.char_height);

	    swi(Wimp_GetRectangle, 0, block);
	}
    }
    else
    {
	swi(ColourTrans_SetGCOL, colour << 8, 0, 0, 0, 0);

	swi(OS_Plot, 4, x + redraw_block[1], y + redraw_block[4] - 1);
	draw_hollow(gui.char_width, gui.char_height);
    }
}

/*
 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
 * color "color".
 */
    void
gui_mch_draw_part_cursor(w, h, colour)
    int w;
    int h;
    guicolor_T colour;
{
    int x = FILL_X(gui.cursor_col);
    int y = -FILL_Y(gui.cursor_row);
    swi(ColourTrans_ReturnGCOL, colour << 8);
    plot_rectangle(96 + 5, r0, x, y - h, x + w - 1, y - 1);
}

/*
 * Catch up with any queued events.  This may put keyboard input into the
 * input buffer, call resize call-backs, trigger timers etc.
 * If there is nothing in the event queue(& no timers pending), then we return
 * immediately (well, after a Wimp_Poll).
 */
    void
gui_mch_update(void)
{
    int block[64];
    int reason;

    swi(OS_ReadMonotonicTime);
    if ((r0 - time_of_last_poll) < 50)
	return;			    /* Don't return too often */

    reason = wimp_poll(0, block);
    if (reason)
	process_event(reason, block);
    ro_return_early = FALSE;		/* We're returning anyway. */
}

    void
redraw_window(block)
    int *block;
{
    int x, y;			/* Vim workarea coords */
    int width, height;
    int blank_col;

    swi(ColourTrans_ReturnGCOL, UNUSED_COLOUR << 8, 0, 0, 1<<7, 0);
    blank_col = r0;

    swi(Wimp_RedrawWindow, 0, block);
    redraw_block = block;
    while (r0)
    {
	x = block[7] - block[1];
	y = block[4] - block[10];
	width  = block[9]  - block[7];
	height = block[10] - block[8];

	if (height + y > Rows * gui.char_height)
	{
	    /* Blank everything off the bottom. */
	    plot_rectangle(96 + 5, blank_col,
				0, block[8] - block[4],
				block[9] - block[1], -FILL_Y(Rows) - 1);
	    height = Rows * gui.char_height;
	}
	if (width + x> Columns * gui.char_width)
	{
	    /* Blank everything off to the right. */
	    plot_rectangle(96 + 5, blank_col,
				FILL_X(Columns), block[8] - block[4],
				block[9] - block[1], 0);
	    width = Columns * gui.char_width;
	}
	gui_redraw(x , y, width, height);
	swi(Wimp_GetRectangle, 0, block);
    }
    redraw_block = NULL;
}

/* Check if we have modified data.
 * If we do then ack the message to stop the shutdown.
 * Otherwise, ignore the message.
 */
    void
ro_prequit(block)
    int	    *block;
{
    if (!ro_ok_to_quit())
    {
	/* Not OK to quit - stop shutdown */
	block[3] = block[2];
	swi(Wimp_SendMessage, 19, block, block[1]);
    }
    /* Do nothing. We may get a Message_Quit later. */
}

/* If there is unsaved data then ask the user if they mind losing it.
 * Return TRUE if we can quit without saving, FALSE to halt the
 * shutdown.
 */
    int
ro_ok_to_quit()
{
    int	    old_confirm = cmdmod.confirm;

    cmdmod.confirm = FALSE;	    /* Use our own, single tasking, box */

    if (check_changed_any(FALSE))
    {
	swi(Wimp_ReportError,
		"\0\0\0\0Vim contains unsaved data - quit anyway?",
		0x17,
		"Vim");
	cmdmod.confirm = old_confirm;
	if (r1 != 1)
	    return FALSE;
    }
    cmdmod.confirm = old_confirm;
    return TRUE;
}

/* Quit without checking for unsaved data. */
    void
ro_quit()
{
    exiting = TRUE;
    getout(0);

    exiting = FALSE;		    /* probably can't get here */
    setcursor();		    /* position cursor */
    out_flush();
}

/* Insent the given vim special code into the input buffer */
    void
ro_press(a, b, modifier)
    char a;
    char b;
    int modifier;	/* %<Ctrl><Shift> 0000 0000 */
{
    char_u buf[6];
    int	    vim_mod;
    int	    key;


    /* Convert RISC OS modifier to Vim modifier. */
    vim_mod = ((modifier & 0x10) ? MOD_MASK_SHIFT : 0)
	       | ((modifier & 0x20) ? MOD_MASK_CTRL : 0);
    key = simplify_key(TERMCAP2KEY(a, b), &vim_mod);

    buf[3] = CSI;
    buf[4] = KEY2TERMCAP0(key);
    buf[5] = KEY2TERMCAP1(key);
    if (vim_mod)
    {
	buf[0] = CSI;
	buf[1] = KS_MODIFIER;
	buf[2] = vim_mod;
	add_to_input_buf(buf, 6);
    }
    else
	add_to_input_buf(buf + 3, 3);
}

/* Take a wimp key code and insert the vim equivalent
 * into vim's input buffer.
 * CTRL-C also sets got_int.
 */
    void
ro_insert_key(code)
    char_u *code;		/* Wimp_ProcessKey code (4 bytes) */
{
    char a = code[0];
    char b = code[1];
    int base, modifier;

    if (a == 3 && ctrl_c_interrupts)
	got_int = TRUE;

    /* Is it a normal key? */
    if (a > 31 && a < 127)
    {
	add_to_input_buf(code, 1);
	return;
    }

    /* We should pass any unrecognised keys on, but
     * for now just pass on F12 combinations.
     */
    switch (b)
    {
	case 0:
	    /* Home and Delete are the only special cases */
	    switch (a)
	    {
		case 0x1e:
		    ro_press('k','h', 0);	/* Home */
		    return;
		case 0x7f:
		    ro_press('k','D', 0);	/* Delete */
		    return;
		case CSI:
		    {
			/* Turn CSI into K_CSI.  Untested! */
			char_u string[3] = {CSI, KS_EXTRA, KE_CSI};

			add_to_input_buf(string, 3);
			return;
		    }
		default:
		    add_to_input_buf(code, 1);
		    return;
	    }
	case 1:
	    if ((a & 0xcf) == 0xcc)
	    {
		/* F12 pressed - pass it on (quick hack) */
		swi(Wimp_ProcessKey, a | 0x100);
		return;
	    }
	    base = a & 0xcf;
	    modifier = a & 0x30;
	    switch (base)
	    {
		case 0x8a:	/* Tab */
		    add_to_input_buf("\011", 1);
		    return;
		case 0x8b:	/* Copy (End) */
		    return ro_press('@', '7', modifier);
		case 0x8c:	/* Left */
		    return ro_press('k', 'l', modifier);
		case 0x8d:	/* Right */
		    return ro_press('k', 'r', modifier);
		case 0x8e:	/* Down */
		    if (modifier & 0x10)
			return ro_press('k', 'N', modifier ^ 0x10);
		    else
			return ro_press('k', 'd', modifier);
		case 0x8f:	/* Up */
		    if (modifier & 0x10)
			return ro_press('k', 'P', modifier ^ 0x10);
		    else
			return ro_press('k', 'u', modifier);
		case 0xca:	/* F10 */
		    return ro_press('k', ';', modifier);
		case 0xcb:	/* F11 */
		    return ro_press('F', '1', modifier);
		case 0xcd:	/* Insert */
		    return ro_press('k', 'I', modifier);
		default:
		    if (base > 0x80 && base < 0x18a)
		    {
			/* One of the other function keys */
			return ro_press('k', '0' + (base & 15), modifier);
		    }
	    }
    }
}

/* Process a mouse event. */
    void
ro_mouse(block)
    int *block;
{
    int x, y, button, vim_button;
    int modifiers = 0;
    int min_x, min_y;		/* Visible area of editor window */
    int max_x, max_y;

    if (block[3] != gui.window_handle || ro_dragging)
	return;			/* Not our window or ignoring clicks*/

    x = block[0];		/* Click position - screen coords */
    y = block[1];
    button = block[2];

    block[0] = gui.window_handle;
    swi(Wimp_GetWindowState, 0, block);
    min_x = block[1];
    min_y = block[2];
    max_x = block[3];
    max_y = block[4];

    if (block[3] - x < gui.scrollbar_width)
    {
	/* Click in that blank area under the scrollbars */

	if (button & 0x444)
	{
	    int	    front_block[10];
	    /* Dragging with Select - bring window to front first */
	    front_block[0] = gui.window_handle;
	    swi(Wimp_GetWindowState, 0, front_block);
	    front_block[7] = -1;
	    ro_open_main(front_block);
	}

	block[0] = gui.window_handle;
	block[1] = 7;			/* Drag point */
	block[2] = block[4] = 0;	/* Coords of point. */
	block[3] = block[5] = 0;
	drag_x_offset = max_x - x;
	drag_y_offset = min_y - y;

	/* Parent box. */
	block[6] = min_x +
			gui.scrollbar_width * 2 +
			MIN_COLUMNS * gui.char_width;
	block[7] = 0;
	gui_mch_get_screen_dimensions(&block[8], &block[9]);
	block[9] = max_y -
			4 * gui.char_height -
			gui.scrollbar_height;

	swi(Wimp_DragBox, 0, block);
	ro_dragging = DRAG_RESIZE_WINDOW;
	drag_button = vim_button;
	drag_modifiers = modifiers;
	return;
    }

    if (button & 0x111)
	vim_button = MOUSE_RIGHT;
    else if (button & 0x222)
	vim_button = MOUSE_MIDDLE;
    else
	vim_button = MOUSE_LEFT;

    swi(OS_Byte, 121, 0x80);
    if (r1 == 0xff)
	modifiers |= MOUSE_SHIFT;
    swi(OS_Byte, 121, 0x81);
    if (r1 == 0xff)
	modifiers |= MOUSE_CTRL;
    swi(OS_Byte, 121, 0x82);
    if (r1 == 0xff)
	modifiers |= MOUSE_ALT;

    if (button == 2)
    {
	/* Menu click:
	 * If shift was pressed then do the paste action.
	 * If not, then open the pop-up menu.
	 */
	modifiers ^= MOUSE_SHIFT;
	if (modifiers && MOUSE_SHIFT)
	{
	    vimmenu_T	main;
	    /* Shift was NOT pressed - show menu */
	    main.dname = (char_u *) "Vim";
	    main.children = root_menu;
	    gui_mch_show_popupmenu(&main);
	    return;
	}
    }

    /* Gain the input focus */
    swi(Wimp_SetCaretPosition, gui.window_handle, -1, 0, 0, -1, -1);

    if (button & 0xf0)
    {
	/* Drag operation:
	 *
	 * Tell the Wimp to start a drag.
	 * Monitor null events.
	 */
	block[1] = 7;			/* Drag a point. */
	block[2] = block[4] = x;	/* Coords of point. */
	block[3] = block[5] = y;
	block[6] = 0;			/* Coords of bounding box. */
	block[7] = 0;
	gui_mch_get_screen_dimensions(&block[8], &block[9]);

	drag_x_offset = drag_y_offset = 0;

	swi(Wimp_DragBox, 0, block);
	ro_dragging = DRAG_SELECTION;
	drag_button = vim_button;
	drag_modifiers = modifiers;

	vim_button |= MOUSE_DRAG;
    }

    gui_send_mouse_event(
		vim_button,
		x - min_x,
		max_y - y,
		button & 0xf ? TRUE : FALSE,	/* dclick */
		modifiers);
}

    void
ro_continue_drag(block)
    int *block;			/* Just used as scrap. */
{
    int x, y;

    /* Get screen coords of pointer. */
    swi(Wimp_GetPointerInfo, 0, block);
    x = block[0] + drag_x_offset;
    y = block[1] + drag_y_offset;

    block[0] = gui.window_handle;
    swi(Wimp_GetWindowState, 0, block);

    if (ro_dragging == DRAG_RESIZE_WINDOW)
    {
	/* Resizeing the main window. */
	block[2] = y;
	block[3] = x;
	ro_open_main(block);
    }
    else
    {
	/* Selecting some text. */
	gui_send_mouse_event(
	    drag_button | MOUSE_DRAG,	/* Always report the same button */
	    x - block[1],
	    block[4] - y,
	    FALSE,			/* Not a double click. */
	    drag_modifiers);
    }
}

/* User has released all mouse buttons, marking the end of a drag. */
    void
ro_drag_finished(block)
    int *block;
{
    int x;
    int y;
    int width, height;

    /* I don't trust the box returned by Wimp_Poll; look at the pointer
     * ourselves.
     */
    swi(Wimp_GetPointerInfo, 0, block);
    x = block[0] + drag_x_offset;
    y = block[1] + drag_y_offset;

    if (ro_dragging == DRAG_RESIZE_WINDOW)
    {
	block[0] = gui.window_handle;
	swi(Wimp_GetWindowState, 0, block);
	block[2] = y;
	block[3] = x;
	ro_open_main(block);

	width = (block[3] - block[1]);
	height = (block[4] - block[2]);

	swi(Wimp_ForceRedraw, gui.window_handle, 0, -height, width, 0);
	gui_resize_shell(width, height);
    }
    else
    {
	block[0] = gui.window_handle;
	swi(Wimp_GetWindowState, 0, block);
	gui_send_mouse_event(
		MOUSE_RELEASE,
		x - block[1],
		block[4] - y,
		FALSE,			/* not a double click */
		drag_modifiers);
    }
    ro_dragging = DRAG_FALSE;
}

/* Load the file/pathname given in block into a [new] buffer.
 *
 * Modifier	Action
 *
 * None		:confirm e <file>
 * Ctrl		:sp <file>
 * Shift	<file>
 *
 * Insert into typebuf, at the start.
 * If loading from !Scrap then use saved leafname instead, and
 * delete the scrap file. Also, ignore shift key.
 *
 * NB: Doesn't send DataLoadAck (other app might delete temp file?).
 */
    void
ro_dataload(block)
    int	    *block;
{
    char_u  new_path[MAXPATHL];
    char_u  *path = ((char_u *) block) + 44;
    int	    scrap = FALSE;

    if (block[3] == leaf_ref && leaf_name)
	scrap = TRUE;

    switch (get_real_state() & 0xff)
    {
	case INSERT:
	case CMDLINE:
	case CMDLINE+LANGMAP:
	    /* For insert mode we can only insert the pathname (currently)
	     * Make sure Shift is pressed.
	     */
	    swi(OS_Byte, 121, 0x80);	    /* Is Shift pressed? */
	    if (r1 == 0xff)
	    {
		ins_typebuf(" ", REMAP_NONE, 0, TRUE, FALSE);
		ins_typebuf(path, REMAP_NONE, 0, TRUE, FALSE);
		ro_return_early = TRUE;		    /* Return even though nothing was typed. */
	    }
	    else
		swi(Wimp_ReportError,
			"\0\0\0\0Sorry, you can only load text in normal mode", 5, "Vim");
	    break;

	case NORMAL:
	    ro_return_early = TRUE;	    /* Return even though nothing was typed. */

	    if (scrap)			    /* Remove <Wimp$Scrap>. Later. */
		ins_typebuf(":!~remove <Wimp$Scrap>\r", REMAP_NONE, 0, TRUE, FALSE);

	    /* Insert {:sp ,:confirm e }[+f\ <leaf> ]<file><CR> */
	    ins_typebuf("\r", REMAP_NONE, 0, TRUE, FALSE);
	    ins_typebuf(path, REMAP_NONE, 0, TRUE, FALSE);
	    ins_typebuf(" ", REMAP_NONE, 0, TRUE, FALSE);

	    if (scrap)
	    {
		/* Loading via !Scrap - change pathname to stored leafname */
		ins_typebuf(leaf_name, REMAP_NONE, 0, TRUE, FALSE);
		ins_typebuf(" +f\\ ", REMAP_NONE, 0, TRUE, FALSE);
		leaf_ref = 0;
		vim_free(leaf_name);
		leaf_name = NULL;
	    }

	    swi(OS_Byte, 121, 0x81);	    /* Is Ctrl pressed? */
	    if (r1 == 0xff)
		/* Yes, split window */
		ins_typebuf(":sp", REMAP_NONE, 0, TRUE, FALSE);
	    else
		ins_typebuf(":confirm e", REMAP_NONE, 0, TRUE, FALSE);
	    break;

	default:
	    swi(Wimp_ReportError, "\0\0\0\0You can only load text in normal mode.", 5, "Vim");
    }
    /* Send DataSaveAck so other program doesn't think we died
     * and delete <Wimp$Scrap>.
     */
    block[3] = block[2];
    block[4] = 4;
    swi(Wimp_SendMessage, 17, block, block[1]);
}

    void
ro_datasave(block)
    int	    *block;
{
    char_u *path = ((char_u *) block) + 44;

    /* Preserve the name given so we can use it, not <Wimp$Scrap> */
    if (leaf_name)
	vim_free(leaf_name);
    leaf_name = vim_strsave(path);

    block[9] = -1;	    /* File is unsafe. */
    strcpy(path, "<Wimp$Scrap>");
    block[0] = 60;
    block[3] = block[2];
    block[4] = 2;
    swi(Wimp_SendMessage, 17, block, block[1]);

    leaf_ref = block[2];
}

    void
ro_message(block)
    int *block;
{
    char_u	*buffer;
    long_u	len;

    if (block[1] == task_handle)
	return;			    /* Don't talk to ourself! */
    switch (block[4])
    {
	case 0:		/* Quit. */
	    if (block[4] == 0)
		ro_quit();
	    break;
	case 1:	/* DataSave */
	    ro_datasave(block);
	    break;
	case 2:		/* DataSaveAck. */
	    if (clip_convert_selection(&buffer, &len, &clip_star) == -1)
		return;

	    /* Save the clipboard contents to a file. */
	    swi(OS_File, 10, ((char_u *) block) + 44, 0xfff, 0, buffer, buffer + len);

	    /* Ack with DataLoad message. */
	    block[3] = block[2];
	    block[4] = 3;
	    block[9] = len;
	    swi(Wimp_SendMessage, 17, block, block[1]);

	    vim_free(buffer);
	    break;
	case 3:		/* DataLoad */
	    ro_dataload(block);
	    break;
	case 8:		/* PreQuit */
	    ro_prequit(block);
	    break;
	case 0xf:	/* Lose clipboard. */
	    if (block[5] & 4)
	    {
		clip_free_selection(&clip_star);
		clip_star.owned = FALSE;
	    }
	    break;
	case 0x10:	/* DataRequest (clip_star) */
	    if (clip_star.owned)
	    {
		int rows;

		/* Tell other program that we have the clipboard. */
		block[0] = 52;
		block[3] = block[2];	    /* Copy myref to yourref. */
		block[4] = 1;		    /* DataSave message. */
		/* Create an estimate for the size (larger or same as true
		 * value) */
		rows = clip_star.end.lnum - clip_star.start.lnum;
		if (rows < 0)
		    rows = -rows;
		block[9] = (rows + 1) * Columns + 1; /* Add one for possible
							final newline. */
		block[10] = 0xfff;	    /* Clipboard is text. */
		strcpy( ((char_u *) block) + 44, "VimClip");
		swi(Wimp_SendMessage, 17, block, block[1]);
	    }
	    break;
	case 0x400c1:	/* Mode change */
	    changed_mode = TRUE;		/* Flag - update on next OpenWindow */
	    if (zap_redraw)
	    {
		/* JK230798, re-initialise ZapRedraw stuff */
		if (ro_zap_redraw_initialise() == FAIL)
		    zap_redraw = FALSE;
	    }
	    break;
	case 0x400c3:	/* TaskCloseDown */
	    if (block[1] == child_handle)
		child_handle = 0;
	    break;
    }
}

/*
 * Converts a scrollbar's window handle into a scrollbar pointer.
 * NULL on failure.
 */
    scrollbar_T *
ro_find_sbar(id)
    int		id;
{
    win_T	*wp;

    if (gui.bottom_sbar.id == id)
	return &gui.bottom_sbar;
    FOR_ALL_WINDOWS(wp)
    {
	if (wp->w_scrollbars[SBAR_LEFT].id == id)
	    return &wp->w_scrollbars[SBAR_LEFT];
	if (wp->w_scrollbars[SBAR_RIGHT].id == id)
	    return &wp->w_scrollbars[SBAR_RIGHT];
    }
    return NULL;
}

    void
scroll_to(line, sb)
    int sb;	/* Scrollbar number */
    int line;
{
    char_u code[8];

    /* Don't put events in the input queue now. */
    if (hold_gui_events)
	return;

    /* Send a scroll event:
     *
     * A scrollbar event is CSI (NOT K_SPECIAL), KS_VER_SCROLLBAR,
     * KE_FILLER followed by:
     * one byte representing the scrollbar number, and then four bytes
     * representing a long_u which is the new value of the scrollbar.
     */
    code[0] = CSI;
    code[1] = KS_VER_SCROLLBAR;
    code[2] = KE_FILLER;
    code[3] = sb;
    code[4] = line >> 24;
    code[5] = line >> 16;
    code[6] = line >> 8;
    code[7] = line;
    add_to_input_buf(code, 8);
}

    void
h_scroll_to(col)
    int col;
{
    char_u code[8];

    /* Don't put events in the input queue now. */
    if (hold_gui_events)
	return;

    /* Send a scroll event:
     *
     * A scrollbar event is CSI (NOT K_SPECIAL)
     *
     * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
     * KE_FILLER followed by four bytes representing a long_u which is the
     * new value of the scrollbar.
     */
    code[0] = CSI;
    code[1] = KS_HOR_SCROLLBAR;
    code[2] = KE_FILLER;
    code[4] = col >> 24;
    code[5] = col >> 16;
    code[6] = col >> 8;
    code[7] = col;
    add_to_input_buf(code, 8);
}

    void
ro_scroll(block)
    int		*block;
{
    scrollbar_T	*sb;
    int		offset;
    win_T	*wp;

    /* Block is ready for Wimp_OpenWindow, and also contains:
     *
     * +32 = scroll X direction (-2 .. +2)
     * +36 = scroll Y direction (-2 .. +2)
     */

    sb = ro_find_sbar(block[0]);
    if (!sb)
	return;		/* Window not found (error). */

    wp = sb-> wp;

    if (wp == NULL)
    {
	/* Horizontal bar. */
	offset = block[8];
	if (offset == -2)
	    offset = (block[1] - block[3]) / gui.char_width;
	else if (offset == 2)
	    offset = (block[3] - block[1]) / gui.char_width;

	block[5] += offset * gui.char_width;

	gui_drag_scrollbar(sb, block[5] / gui.char_width, FALSE);

	swi(Wimp_OpenWindow, 0, block);
    }
    else
    {
	offset = -block[9];
	if (offset == -2)
	    offset = -(wp -> w_height - 1);
	else if (offset == 2)
	    offset = wp -> w_height - 1;

	/* Possibly we should reposition the scrollbar?
	 * Vim seems to update the bar anyway...
	 */
	gui_drag_scrollbar(sb, offset - (block[6] / gui.char_height), FALSE);
    }
}

/* Move a window by a given offset. Used to simulate the function of the
 * nested wimp.
 */
    void
ro_move_child(window, x, y, pos_wanted, pos_got)
    int	window;
    int x,y;		/* offset to move by */
    int	pos_wanted, pos_got;
{
    int	block[10];

    block[0] = window;
    swi(Wimp_GetWindowState, 0, block);
    block[1] += x;
    block[2] += y;
    block[3] += x;
    block[4] += y;
    if (pos_wanted == -1)
	block[7] = -1;
    else if (pos_wanted == -2)
	block[7] = pos_got;
    swi(Wimp_OpenWindow, 0, block);
}

/* Open the main window. Also updates scrollbars if we are not
 * using the nested Wimp.
 * If we have just changed mode then re-read all values.
 */
    void
ro_open_main(block)
    int	    *block;
{
    int	    toggle_size;

    /* Find out if the user clicked on the toggle size icon. */
    block[20] = block[0];
    swi(Wimp_GetWindowState, 0, block + 20);
    toggle_size = block[28] & (1 << 19);

    if (nested_wimp)
    {
	swi(Wimp_OpenWindow, 0, block);
    }
    else
    {
	int	old[10];
	int	x_offset, y_offset;	    /* Move children same as parent. */
	int	pos_wanted, pos_got;
	int	left_bar  = gui.which_scrollbars[SBAR_LEFT];
	int	right_bar = gui.which_scrollbars[SBAR_RIGHT];
	win_T	*wp;

	/* Three cases to think about:
	 * 1) Move to top. Open each window at the top.
	 * 2) Same stack position. Open each with same position.
	 * 3) Open at bottom. Open children with parent's new position.
	 */

	old[0] = block[0];
	swi(Wimp_GetWindowState, 0, old);
	pos_wanted = block[7];
	swi(Wimp_OpenWindow, 0, block);
	/* Block updated by OpenWindow? I don't think so! */
	swi(Wimp_GetWindowState, 0, block);
	pos_got = block[7];

	x_offset = block[1] - old[1];
	y_offset = block[4] - old[4];
	if (x_offset || y_offset || pos_wanted == -1 || pos_wanted == -2)
	{
	    /* If parent has moved, re-open all the child windows. */
	    FOR_ALL_WINDOWS(wp)
	    {
		/* Reopen scrollbars for this window. */
		if (left_bar)
		    ro_move_child(wp -> w_scrollbars[SBAR_LEFT].id,
				x_offset, y_offset,
				pos_wanted, pos_got);
		if (right_bar)
		    ro_move_child(wp -> w_scrollbars[SBAR_RIGHT].id,
				x_offset, y_offset,
				pos_wanted, pos_got);
	    }
	}
    }
    if (changed_mode || toggle_size)
    {
	int	width, height;

	if (changed_mode)
	    ro_measure_tools();
	block[0] = gui.window_handle;
	swi(Wimp_GetWindowState, 0, block);

	width = block[3] - block[1];
	height = block[4] - block[2];
	swi(Wimp_ForceRedraw, gui.window_handle, 0, -height, width, 0);
	gui_resize_shell(width, height);
	changed_mode = FALSE;
    }
}

    void
ro_open_window(block)
    int		*block;
{
    int		pos;
    scrollbar_T *sb;

    if (block[0] == gui.window_handle)
	ro_open_main(block);
    else
    {
	swi(Wimp_OpenWindow, 0, block);
	if (block[0] != gui.window_handle)
	{
	    sb = ro_find_sbar(block[0]);
	    if (sb)
	    {
		if (sb-> wp != NULL)
		    gui_drag_scrollbar(sb, -block[6] / gui.char_height, FALSE);
		else
		    gui_drag_scrollbar(sb, block[5] / gui.char_width, FALSE);
	    }
	}
    }
}

    void
ro_menu_selection(block)
    int		*block;
{
    int		*item = wimp_menu + 7;
    vimmenu_T	*menu;
    /* wimp_menu points to a wimp menu structure */

    for (;;)
    {
	while (block[0]--)
	    item += 6;
	if (block[1] == -1)
	    break;
	item = ((int *) item[1]) + 7;
	block++;
    }
    /* item points to the wimp menu item structure chosen */
    menu = (vimmenu_T *) item[5];

    swi(Wimp_GetPointerInfo, 0, block);
    if (block[2] == 1)
	/* Adjust used - keep menu open */
	swi(Wimp_CreateMenu, 0, wimp_menu);

    if (menu-> cb)
	menu-> cb(menu);
}

    void
ro_open_parent()
{
    int head;
    char_u *i = curbuf-> b_ffname;
    char_u  buffer[256];

    head = 0;
    for (; *i; i++)
    {
	if (*i == '.')
	    head = i - curbuf-> b_ffname;
    }

    /* Append head chars to buffer */
    if (head < 240 && curbuf-> b_ffname && head)
    {
	strcpy(buffer, "%filer_opendir ");
	strncpy(buffer + 15, curbuf-> b_ffname, head);
	buffer[15 + head] = '\0';
	swi(OS_CLI, buffer);
    }
}

    void
process_event(event, block)
    int event;
    int *block;
{
    switch (event)
    {
	case 0:		/* Nothing - update drag state. */
	    if (ro_dragging)
		ro_continue_drag(block);
	    break;
	case 1:		/* Redraw window. */
	    redraw_window(block);
	    break;
	case 2:		/* Open window. */
	    ro_open_window(block);
	    break;
	case 3:		/* Close window. */
	    swi(Wimp_GetPointerInfo, 0, block + 1);
	    if (block[3] == 1)
		ro_open_parent();
	    else
		if (ro_ok_to_quit())
		    ro_quit();
	    break;
	case 6:		/* Mouse click. */
	    ro_mouse(block);
	    break;
	case 7:		/* Finished drag. */
	    ro_drag_finished(block);
	    break;
	case 8:		/* Key pressed. */
	    ro_insert_key((char_u *) &block[6]);
	    break;
	case 9:
	    ro_menu_selection(block);
	    break;
	case 10:	/* Scroll request. */
	    ro_scroll(block);
	    break;
	case 11:	/* Lose caret. */
	    if (block[0] == gui.window_handle)
		gui_focus_change(FALSE);
	    break;
	case 12:	/* Gain caret. */
	    if (block[0] == gui.window_handle)
		gui_focus_change(TRUE);
	    break;
	case 17:	/* User message. */
	case 18:	/* User message recorded. */
	    ro_message(block);
	    break;
    }
}

/*
 * GUI input routine called by gui_wait_for_chars().  Waits for a character
 * from the keyboard.
 *  wtime == -1	    Wait forever.
 *  wtime == 0	    This should never happen.
 *  wtime > 0	    Wait wtime milliseconds for a character.
 * Returns OK if a character was found to be available within the given time,
 * or FAIL otherwise.
 */
    int
gui_mch_wait_for_chars(long wtime)
{
    int block[64];
    int	reason;
    int start_time = -1;
    int ctime = wtime / 10;	/* delay in cs */

    if (wtime != -1)
    {
	swi(OS_ReadMonotonicTime);
	start_time = r0;
    }

    for (;;)
    {
	if (ro_dragging)
	    reason = wimp_poll(0, block);	/* Always return immediately */
	else if (wtime == -1)
	    reason = wimp_poll(1, block);
	else
	    reason = wimp_pollidle(0, block, start_time + ctime);

	process_event(reason, block);

	if (input_available() || ro_return_early)
	{
	    ro_return_early = FALSE;
	    return OK;	    /* There is something to process (key / menu event) */
	}

	if (wtime != -1)
	{
	    swi(OS_ReadMonotonicTime);
	    if (r0 - start_time > ctime)
		return FAIL;	/* We've been waiting too long - return failure */
	}
    }
}

/* Flush any output to the screen */
    void
gui_mch_flush(void)
{
}

/*
 * Clear a rectangular region of the screen from text pos(row1, col1) to
 * (row2, col2) inclusive.
 */
    void
gui_mch_clear_block(int row1, int col1, int row2, int col2)
{
    swi(ColourTrans_ReturnGCOL, gui.back_pixel << 8, 0, 0, 1<<7, 0);
    plot_rectangle(96 + 5, r0,
			FILL_X(col1), -FILL_Y(row2 + 1),
			FILL_X(col2 + 1), -FILL_Y(row1));
}

    void
gui_mch_clear_all(void)
{
    if (redraw_block)
    {
	swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 1<<7, 0);
	swi(OS_WriteI + 16);
    }
    else
    {
	int block[44];
	block[0] = gui.window_handle;
	block[1] = 0;
	block[2] = -gui.num_rows * gui.char_height;
	block[3] = gui.num_cols * gui.char_width;
	block[4] = 0;
	swi(Wimp_UpdateWindow, 0, block);
	while (r0)
	{
	    swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 1<<7, 0);
	    swi(OS_WriteI + 16);
	    swi(Wimp_GetRectangle, 0, block);
	}
    }
}

/*
 * Delete the given number of lines from the given row, scrolling up any
 * text further down within the scroll region.
 */
    void
gui_mch_delete_lines(int row, int num_lines)
{
    int top_from = -row - num_lines;
    int bot_from = -gui.scroll_region_bot - 1;
    int bot_to   = bot_from + num_lines;

    swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 0x80, 0);

    /* Changed without checking! */
    swi(Wimp_BlockCopy, gui.window_handle,
			    gui.scroll_region_left * gui.char_width,
			    bot_from * gui.char_height,
			    (gui.scroll_region_right - gui.scroll_region_left
							+ 1) * gui.char_width,
			    top_from * gui.char_height,

			    gui.scroll_region_left * gui.char_width,
			    bot_to * gui.char_height);

    gui_clear_block(gui.scroll_region_bot - num_lines + 1,
						       gui.scroll_region_left,
	gui.scroll_region_bot, gui.scroll_region_right);
}

/*
 * Insert the given number of lines before the given row, scrolling down any
 * following text within the scroll region.
 */
    void
gui_mch_insert_lines(int row, int num_lines)
{
    int top_from = -row;
    int bot_to   = -gui.scroll_region_bot - 1;
    int bot_from = bot_to + num_lines;

    swi(ColourTrans_SetGCOL, gui.back_pixel << 8, 0, 0, 0x80, 0);

    swi(Wimp_BlockCopy, gui.window_handle,
			    gui.scroll_region_left * gui.char_width,
			    bot_from * gui.char_height,
			    (gui.scroll_region_right - gui.scroll_region_left
							+ 1) * gui.char_width,
			    top_from * gui.char_height,

			    gui.scroll_region_left * gui.char_width,
			    bot_to * gui.char_height);

    gui_clear_block(row, gui.scroll_region_left,
				row + num_lines - 1, gui.scroll_region_right);
}

/* Put selection in clipboard buffer.
 * Should we become the new owner?
 */
    void
clip_mch_request_selection(VimClipboard *cbd)
{
    int		block[64];	/* Will be used in Wimp_Poll. */
    int		reason;
    char_u	*buffer;
    long_u	length;

    block[0] = 48;			/* Size of block. */
    block[3] = 0;			/* Orinial message. */
    block[4] = 0x10;			/* Data request. */
    block[5] = gui.window_handle;
    block[6] = RO_LOAD_CLIPBOARD;	/* Internal handle. */
    block[7] = block[8] = 0;		/* (x,y) not used. */
    block[9] = 4;
    block[10] = 0xfff;	    /* We want text files if possible, I think. */
    block[11] = -1;	    /* End of list. */
    swi(Wimp_SendMessage, 17, block, 0);    /* Broadcast request. */

    /* OK, we've sent the request. Poll until we get a null poll (failure) or
     * we load the clipboard.
     * If we receive a DataSave event with icon handle = -2 then put it on the
     * clipboard. RISC OS should ensure that key events will not be delivered
     * until the clipboard operation completes (unless the owner starts idling
     * - we can't wait forever!).
     */
    for (;;)
    {
	reason = wimp_poll(0, block);
	if (reason == 0)
	    return;	    /* Failed to get clipboard. */
	if ((reason == 17 || reason == 18) &&
		block[4] == 1 && block[6] == RO_LOAD_CLIPBOARD)
	    break;	    /* Got it - stop waiting. */
	process_event(reason, block);
	if (ro_return_early)
	    return;
    }
    /* Tell owner to save data in <Wimp$Scrap>. */
    block[0] = 60;
    block[3] = block[2];   /* Copy myref -> yourref */
    block[4] = 2;	    /* DataSaveAck. */
    block[9] = -1;	    /* Data is unsafe. */
    strcpy( ((char_u *) block) + 44, "<Wimp$Scrap>");
    swi(Wimp_SendMessage, 17, block, block[1]);

    /* Wait again for reply. */
    for (;;)
    {
	reason = wimp_poll(0, block);
	if (reason == 0)
	    return;	/* Other program has given up! */
	if ((reason == 17 || reason == 18) && block[4] == 3 && block[6] == RO_LOAD_CLIPBOARD)
	    break;	/* Clipboard data saved to <Wimp$Scrap> */
	process_event(reason, block);
	if (ro_return_early)
	    return;
    }

    /* <Wimp$Scrap> contains clipboard - load it. */
    if (xswi(OS_File, 17, "<Wimp$Scrap>") & v_flag)
	return;		/* Error! */
    if (r0 != 1 && r0 != 3)
	return;
    length = r4;

    buffer = lalloc(length, TRUE);  /* Claim memory (and report errors). */
    if (buffer == NULL)
	return;

    if (xswi(OS_File, 16, "<Wimp$Scrap>", buffer, 0) & v_flag)
	return;

    clip_yank_selection(MCHAR, buffer, length, cbd);

    vim_free(buffer);

    swi(OS_FSControl, 27, "<Wimp$Scrap>", 0, 0);    /* Delete temp file. */

    block[4] = 4;		    /* Send DataLoadAck. */
    block[3] = block[2];	    /* Copy myref -> yourref. */
    swi(Wimp_SendMessage, 17, block, block[1]);
}

/* Not sure what this means under RISC OS. */
    void
clip_mch_lose_selection(VimClipboard *cbd)
{
}

/* Tell everyone that we now own the clipboard.
 * Return OK if our claim is accepted (always, under RISC OS)
 */
    int
clip_mch_own_selection(VimClipboard *cbd)
{
    int block[6];
    block[0] = 24;	/* Length of block.  */
    block[3] = 0;	/* Original message. */
    block[4] = 0xf;	/* ClaimEntity. */
    block[5] = 0x4;	/* Claim clipboard only. */
    swi(Wimp_SendMessage, 17, block, 0);
    return OK;
}

/*
 * Send the current selection to the clipboard.  Do nothing for X because we
 * will fill in the selection only when requested by another app. Sounds good
 * for RISC OS too.
 */
    void
clip_mch_set_selection(VimClipboard *cbd)
{
    clip_get_selection(cbd);
}

/*
 * Make a menu either grey or not grey.
 */
    void
gui_mch_menu_grey(vimmenu_T *menu, int grey)
{
    menu-> greyed_out = grey;
}

/*
 * Make menu item hidden or not hidden
 */
    void
gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
{
    menu-> hidden = hidden;
}

/*
 * This is called after setting all the menus to grey/hidden or not.
 */
    void
gui_mch_draw_menubar(void)
{
    swi(Wimp_CreateMenu, 0, -1);
    if (wimp_menu != (int *) -1)
    {
	ro_remove_menu(wimp_menu);
	wimp_menu = (int *) -1;
    }
}

/* Add or remove a scrollbar. Note that this is only called when
 * the scrollbar state is changing.
 * The scroll bar window has already been created.
 * We can't do anything except remove the scroll bar
 * until we know what size to use.
 */
    void
gui_mch_enable_scrollbar(sb, flag)
    scrollbar_T	*sb;
    int		flag;
{
    if (!flag)
	swi(Wimp_CloseWindow, 0, & (sb->id) );
    return;
}

    void
gui_mch_set_blinking(long waittime, long on, long off)
{
}

/*
 * Stop the cursor blinking.  Show the cursor if it wasn't shown.
 */
    void
gui_mch_stop_blink(void)
{
}

/*
 * Start the cursor blinking.  If it was already blinking, this restarts the
 * waiting time and shows the cursor.
 */
    void
gui_mch_start_blink(void)
{
}

/*
 * Return the RGB value of a pixel as a long.
 */
    long_u
gui_mch_get_rgb(guicolor_T pixel)
{
    return (long_u)pixel;
}

    void
gui_mch_set_text_area_pos(int x, int y, int w, int h)
{
}

    void
gui_mch_enable_menu(int flag)
{
}

    void
gui_mch_set_menu_pos(int x, int y, int w, int h)
{
}

    void
gui_mch_add_menu(vimmenu_T *menu, int idx)
{
}

    void
gui_mch_add_menu_item(vimmenu_T *menu, int idx)
{
}

    void
gui_mch_new_menu_colors(void)
{
}

    void
gui_mch_destroy_menu(vimmenu_T *menu)
{
}

/* Size of buffer has changed.
 * Add one to max since gui.c substracts one more than it should!
 */
    void
gui_mch_set_scrollbar_thumb(sb, val, size, max)
    scrollbar_T	*sb;
    long	val;
    long	size;
    long	max;
{
    int		block[10], width, height;

    width = (max + 1) * gui.char_width;
    height = (max + 1 + W_STATUS_HEIGHT(sb->wp)) * gui.char_height;

    block[0] = block[3] = 0;
    block[1] = -height + (1 << y_eigen_factor);
    block[2] = width;

    swi(Wimp_SetExtent, sb -> id, block);

    block[0] = sb -> id;
    swi(Wimp_GetWindowState, 0, block);
    block[5] = val * gui.char_width;
    block[6] = -val * gui.char_height;
    swi(Wimp_OpenWindow, 0, block, 0x4b534154,
			gui.window_handle,	/* Parent window handle. */
			(CHILD_FIX_TO_RIGHT  << CHILD_LEFT  )   |
			(CHILD_FIX_TO_RIGHT  << CHILD_RIGHT )   |
			(CHILD_FIX_TO_BOTTOM << CHILD_TOP   )   |
			(CHILD_FIX_TO_BOTTOM << CHILD_BOTTOM)   |
			(CHILD_SELF_SCROLL   << CHILD_SCROLL_X) |
			(CHILD_SELF_SCROLL   << CHILD_SCROLL_Y)
			);
}

/* Set the position of the scrollbar within the editor
 * window. Note that, for vertical scrollbars, x and w
 * are ignored. For horizontal bars y and h are ignored.
 */
    void
gui_mch_set_scrollbar_pos(sb, x, y, w, h)
    scrollbar_T *sb;
    int		x;		/* Horizontal sb position */
    int		y;		/* Top of scroll bar */
    int		w;		/* Width */
    int		h;		/* Height */
{
    int		block[24];
    int		px1, py1;	/* Parent window min coords */
    int		px2, py2;	/* Parent window max coords */

    /* Find where the parent window is. */
    block[0] = gui.window_handle;
    swi(Wimp_GetWindowState, 0, block);
    px1 = block[1];
    py1 = block[2];
    px2 = block[3];
    py2 = block[4];

    block[0] = sb -> id;

    /* Find out how big the scroll window is at the moment. */
    swi(Wimp_GetWindowInfo, 0, ((char_u *)block) + 1);

    if (block[13] < w || block[12] > -h)
    {
	/* Current window is too small! */
	if (block[12] > -h)
	    block[12] = -h;
	if (block[13] < w)
	    block[13] = w;
	swi(Wimp_SetExtent, block[0], block + 11);
    }

    /* This works better on the nested_wimp. */
    if (sb-> wp)
    {
	/* This is a vertical scrollbar. */
	block[1] = block[3] = px2 - gui.scrollbar_width + (1 << x_eigen_factor);
	block[2] = 1 + py2 - (y + h) + (1 << y_eigen_factor);
	block[4] = 1 + py2 - y;
    }
    else
    {
	/* This is a horizontal scrollbar. */
	block[2] = block[4] = py1 + gui.scrollbar_height;
	block[1] = px1;
	block[3] = px2 - gui.scrollbar_width;
    }

    block[5] = 0;
    block[6] = 0;
    block[7] = -1;

    swi(Wimp_OpenWindow, 0, block, 0x4b534154,
	    gui.window_handle,	/* Parent window handle. */
	    (CHILD_FIX_TO_RIGHT  << CHILD_LEFT  )   |
	    (CHILD_FIX_TO_RIGHT  << CHILD_RIGHT )   |
	    (CHILD_FIX_TO_BOTTOM << CHILD_TOP   )   |
	    (CHILD_FIX_TO_BOTTOM << CHILD_BOTTOM)   |
	    (CHILD_SELF_SCROLL   << CHILD_SCROLL_X) |
	    (CHILD_SELF_SCROLL   << CHILD_SCROLL_Y)
       );
}

/* Create a window with no workarea to place inside editor window.
 * (what happens without the nested wimp?)
 * Data for scrollbar is invalid.
 */
    void
gui_mch_create_scrollbar(sb, orient)
    scrollbar_T *sb;
    int		orient;	/* orient is SBAR_HORIZ or SBAR_VERT */
{
    int bar[] =
	{
	    0,   0,		/* Visible area : min X,Y */
	    100, 100,		/*		  max X,Y */
	    0,   0,		/* Scroll offsets */
	    -1,			/* Window in front */
	    0x80800150 | (orient == SBAR_HORIZ ? (1 << 30) : (1 << 28)),
	    0xff070207,		/* Colours */
	    0x000c0103,		/* More colours */
	    0, -0x4000,		/* Workarea extent */
	    0x4000, 0,		/* max X,Y */
	    0x00000000,		/* No title */
	    0 << 12,		/* No workarea button type */
	    1,			/* Wimp sprite area */
	    0x00010001,		/* Minimum width, height */
	    0, 0, 0,		/* Title data (none) */
	    0			/* No icons */
	};
    swi(Wimp_CreateWindow, 0, bar);
    sb -> id = r0;
}

#if defined(FEAT_WINDOWS) || defined(PROTO)
    void
gui_mch_destroy_scrollbar(scrollbar_T *sb)
{
    swi(Wimp_DeleteWindow, 0, & (sb->id));
    sb -> id = -1;
}
#endif

    void
gui_mch_set_scrollbar_colors(scrollbar_T *sb)
{
    /* Always use default RO colour scheme. */
}

/*
 * Get current x mouse coordinate in text window.
 * Note: (0,0) is the bottom left corner, positive y is UP.
 * Return -1 when unknown.
 */
    int
gui_mch_get_mouse_x()
{
    int left;
    int block[10];

    block[0] = gui.window_handle;
    swi(Wimp_GetWindowState, 0, block);
    left = block[1];

    swi(Wimp_GetPointerInfo, 0, block);
    return block[0] - left;
}

/*
 * Get current y mouse coordinate in text window.
 * Return -1 when unknown.
 */
    int
gui_mch_get_mouse_y()
{
    int top;
    int block[10];

    block[0] = gui.window_handle;
    swi(Wimp_GetWindowState, 0, block);
    top = block[4];

    swi(Wimp_GetPointerInfo, 0, block);
    return top - block[1];
}

/* MouseTo(x, y) */
    void
gui_mch_setmouse(x, y)
    int		x;
    int		y;
{
}

    void
gui_mch_toggle_tearoffs(enable)
    int		enable;
{
    /* no tearoff menus */
}

/* Redraw a window's title.
 * For the nested wimp we use the new 'redraw-title-bar' reason code.
 * For older wimps we mark the area of the screen where the title bar
 * is as invalid.
 */
    void
ro_redraw_title(window)
    int window;
{
    if (nested_wimp)
    {
	swi(Wimp_ForceRedraw, window, 0x4b534154, 3);
    }
    else
    {
	int block[10];
	int miny;

	block[0] = window;
	swi(Wimp_GetWindowState, 0, block);
	miny = block[4];
	swi(Wimp_GetWindowOutline, 0, block);
	swi(Wimp_ForceRedraw, -1,
			block[1], miny,
			block[3], block[4]);
    }
}

/* Turn a vimmenu_T structure into a wimp menu structure.
 * -1 if resulting menu is empty.
 * Only the children and dname items in the root menu are used.
 */
    int *
ro_build_menu(menu)
    vimmenu_T	*menu;
{
    int		*wimp_menu;
    int		width = 4;
    int		w;
    int		size = 28;
    vimmenu_T	*item;
    int		*wimp_item;

    /* Find out how big the menu is so we can allocate memory for it */
    for (item = menu-> children; item; item = item-> next)
    {
	if (item-> hidden == FALSE && !menu_is_separator(item->name))
	    size += 24;
    }

    if (size <= 28)
	return (int *) -1;		/* No children - shouldn't happen */

    wimp_menu = (int *) alloc(size);

    wimp_menu[0] = (int) menu-> dname;
    wimp_menu[1] = -1;
    wimp_menu[2] = 0;
    wimp_menu[3] = 0x00070207;
    wimp_menu[5] = 44;
    wimp_menu[6] = 0;

    wimp_item = wimp_menu + 7;

    for (item = menu-> children; item; item = item-> next)
    {
	if (menu_is_separator(item-> name))
	{
	    /* This menu entry is actually a separator. If it is not the first
	     * menu entry then mark the previous menu item as needing a dotted
	     * line after it.
	     */
	    if (wimp_item > wimp_menu + 7)
		wimp_item[-6] |= 0x2;
	}
	else if (item-> hidden == FALSE)
	{
	    wimp_item[0] = 0;
	    wimp_item[1] = item-> children ? (int) ro_build_menu(item) : -1;
	    wimp_item[2] = 0x07009131 | (item-> greyed_out << 22);
	    wimp_item[3] = (int) item-> dname;
	    wimp_item[4] = -1;
	    wimp_item[5] = (int) item;  /* Stuff the menu address in this unused space */

	    w = strlen(item-> dname) + 1;
	    if (w > width)
		width = w;
	    wimp_item += 6;
	}
    }

    wimp_menu[4] = (width + 2) * 16;
    wimp_menu[7]  |= 0x100;	    /* Menu title is indirected */
    wimp_item[-6] |= 0x080;	    /* Last entry in menu */
    return wimp_menu;
}

    static void
ro_remove_menu(menu)
    int	    *menu;
{
    int	    *item = menu + 7;

    if (menu == NULL || menu == (int *) -1)
	return;

    for (;;)
    {
	if (item[1] != -1)
	    ro_remove_menu((int *) item[1]);	/* Remove sub-menu */
	if (item[0] & 0x80)
	    break;			/* This was the last entry */
	item += 6;
    }
    vim_free(menu);
}

    void
gui_mch_show_popupmenu(menu)
    vimmenu_T	*menu;
{
    int		block[10];

    /* Remove the existing menu, if any */
    if (wimp_menu != (int *) -1)
    {
	swi(Wimp_CreateMenu, 0, -1);
	ro_remove_menu(wimp_menu);
	wimp_menu = (int *) -1;
    }

    wimp_menu = ro_build_menu(menu);
    if (wimp_menu != (int *) -1)
    {
	swi(Wimp_GetPointerInfo, 0, block);
	swi(Wimp_CreateMenu, 0, wimp_menu, block[0] - 64, block[1] + 64);
    }
}

/* Run a command using the TaskWindow module.
 * If SHELL_FILTER is set then output is not echoed to the screen,
 * If it is not set, then \r is not sent to the output file.
 */
    int
gui_mch_call_shell(cmd, options)
    char_u  *cmd;
    int	    options;	/* SHELL_FILTER if called by do_filter() */
			/* SHELL_COOKED if term needs cooked mode */
{
    char_u  task_cmd[256];	/* Contains *TaskWindow command. */
    int	    block[64];
    int	    reason;
    char_u  *out;
    char_u  c;
    int	    old_msg_col;
    char_u  *out_redir;
    int	    length;
    FILE    *out_file = NULL;

    out_redir = strstr(cmd, " > ");
    if (out_redir == NULL)
	length = strlen(cmd);	/* No redirection. */
    else
    {
	length = out_redir - cmd;
	out_file = fopen(out_redir + 3, "wb");
	if (out_file == NULL)
	    msg_str("WARNING : Can't open file %s for writing\n",
							       out_redir + 3);
    }

    if (length > 180)
    {
	if (out_file)
	    fclose(out_file);
	return FAIL;		/* Command too long. */
    }

    strcpy(task_cmd, "TaskWindow \"");
    strncpy(task_cmd + 12, cmd, length);
    sprintf(task_cmd + 12 + length,
	    "\" -task &%08x -ctrl -quit -name \"Vim command\"",
	    task_handle);

    if (options & SHELL_COOKED)
	settmode(TMODE_COOK);

    if (xswi(Wimp_StartTask, task_cmd) & v_flag)
    {
	/* Failed to even start a new task (out of memory?) */
	settmode(TMODE_RAW);
	if (out_file)
	    fclose(out_file);
	return FAIL;
    }

    /* Wait for the child process to initialise. */
    child_handle = 0;
    while (!child_handle)
    {
	reason = wimp_poll(0, block);
	if ((reason == 17 || reason == 18) && block[4] == 0x808c2)
	    child_handle = block[1];
	else
	    process_event(reason, block);
    }

    /* Block until finished */
    while (child_handle)
    {
	reason = wimp_poll(1, block);
	if (reason == 3 || (reason == 8 && block[6] == 3))
	{
	    /* Close window request or CTRL-C - kill child task. */
	    block[0] = 20;
	    block[3] = 0;
	    block[4] = 0x808c4;	    /* Morite */
	    swi(Wimp_SendMessage, 17, block, child_handle);
	    MSG_PUTS(_("\nSending message to terminate child process.\n"));
	    continue;
	}
	else if (reason == 8)
	{
	    block[0] = 28;
	    block[3] = 0;
	    block[4] = 0x808c0;	    /* Input */
	    block[5] = 1;
	    /* Block[6] is OK as it is! */
	    swi(Wimp_SendMessage, 17, block, child_handle);
	    continue;
	}
	else if (reason == 17 || reason == 18)
	{
	    if (block[4] == 0x808c1)
	    {
		/* Ack message. */
		block[3] = block[2];
		swi(Wimp_SendMessage, 19, block, block[1]);
		out = (char_u *)block + 24;
		old_msg_col = msg_col;
		while (block[5]--)
		{
		    c = *out++;
		    if (out_file && (c != '\r' || (options & SHELL_FILTER)))
			fputc(c, out_file);
		    if ((options & SHELL_FILTER) == 0)
		    {
			if (c == 127)
			    msg_puts("\b \b");
			else if (c > 31)
			    msg_putchar(c);
			else if (c == 10)
			{
			    lines_left = 8;	/* Don't do More prompt! */
			    msg_putchar(10);
			}
		    }
		}
		/* Flush output to the screen. */
		windgoto(msg_row, msg_col);
		out_flush();
		continue;
	    }
	}
	process_event(reason, block);
    }
    msg_putchar('\n');
    settmode(TMODE_RAW);
    if (out_file)
	fclose(out_file);
    return OK;
}

/* Like strsave(), but stops at any control char */
    char_u *
wimp_strsave(str)
    char    *str;
{
    int	    strlen = 0;
    char_u  *retval;
    while (str[strlen] > 31)
	strlen++;
    retval = alloc(strlen + 1);
    if (retval)
    {
	memcpy(retval, str, strlen);
	retval[strlen] = '\0';
    }
    return retval;
}

/* If we are saving then pop up a standard RISC OS save box.
 * Otherwise, open a directory viewer on the given directory (and return NULL)
 * The string we return will be freed later.
 */
    char_u *
gui_mch_browse(saving, title, dflt, ext, initdir, filter)
    int		saving;		/* write action */
    char_u	*title;		/* title for the window */
    char_u	*dflt;		/* default file name */
    char_u	*ext;		/* extension added */
    char_u	*initdir;	/* initial directory, NULL for current dir */
    char_u	*filter;	/* file name filter */
{
    char command[256];
    int length;

    if (saving)
    {
	int	block[64];
	int	reason;
	int	done_save = FALSE;
	char_u	*retval = NULL;
	char_u  *sprname;
	char_u	*fname;
	int	dragging_icon = FALSE;
	int	filetype;

	if (!dflt)
	    dflt = "TextFile";

	block[0] = save_window;
	block[1] = 0;
	swi(Wimp_GetIconState, 0, block);
	sprname = ((char_u *) block[7]);
	block[1] = 1;
	swi(Wimp_GetIconState, 0, block);
	fname = ((char *) block[7]);
	strncpy(fname, dflt, 255);

	if (xswi(OS_FSControl, 31, curbuf->b_p_oft) & v_flag)
	{
	    filetype = 0xfff;
	    strcpy(sprname + 5, "xxx");
	}
	else
	{
	    filetype = r2;
	    sprintf(sprname + 5, "%03x", filetype);
	}

	/* Open the save box */

	swi(Wimp_GetPointerInfo, 0, block);
	swi(Wimp_CreateMenu, 0, save_window, block[0] - 64, block[1] + 64);
	swi(Wimp_SetCaretPosition, save_window, 1, 0, 0, -1, -1);

	while (!done_save)
	{
	    reason = wimp_poll(1, block);
	    switch (reason)
	    {
		case 1:
		    redraw_window(block);
		    break;
		case 2:
		    if (block[0] == save_window)
			swi(Wimp_OpenWindow, 0, block);
		    else
			ro_open_window(block);
		    break;
		case 3:
		    done_save = TRUE;
		    break;
		case 6:
		    if (block[3] != save_window)
			done_save = TRUE;
		    else
		    {
			int drag_box[4];
			int min_x, max_y;

			switch (block[4])
			{
			    case    0: /* Start drag */
				block[0] = save_window;
				swi(Wimp_GetWindowState, 0, block);
				min_x = block[1];
				max_y = block[4];
				block[1] = 0;
				swi(Wimp_GetIconState, 0, block);
				drag_box[0] = block[2] + min_x;
				drag_box[1] = block[3] + max_y;
				drag_box[2] = block[4] + min_x;
				drag_box[3] = block[5] + max_y;

				swi(DragASprite_Start,
					0x45,
					1,
					sprname,
					drag_box);
				dragging_icon = TRUE;
				break;
			    case    2: /* OK */
				retval = wimp_strsave(fname);
				done_save = TRUE;
				break;
			    case    3: /* Cancel */
				done_save = TRUE;
				break;
			}
		    }
		    break;
		case 7:
		    if (dragging_icon)
		    {
			int len = 0;

			dragging_icon = FALSE;
			swi(Wimp_GetPointerInfo, 0, block);
			block[5] = block[3];
			block[6] = block[4];
			block[7] = block[0];
			block[8] = block[1];
			block[9] = 0;		/* Don't know the size */
			block[10] = filetype;

			while (fname[len] > 31)
			{
			    if (fname[len] == '.')
			    {
				fname += len + 1;
				len = 0;
			    }
			    else
				len++;
			}
			if (len > 211)
			    len = 211;

			memcpy(((char_u *) block) + 44, fname, len);
			((char_u *)block)[44 + len] = '\0';

			block[0] = (len + 48) & 0xfc;
			block[3] = 0;
			block[4] = 1;	    /* DataSave */

			swi(Wimp_SendMessage, 17, block, block[5], block[6]);
		    }
		    else
			ro_drag_finished(block);
		    break;
		case 8:
		    if (block[6] == 13)
		    {
			retval = wimp_strsave(fname);
			done_save = TRUE;
		    }
		    else if (block[6] == 0x1b)
			done_save = TRUE;
		    else
			swi(Wimp_ProcessKey, block[6]);
		    break;
		case 17:
		case 18:
		    if (block[4] == 2 && block[9] != -1)
		    {
			/* DataSaveAck from dragging icon. */
			retval = wimp_strsave(((char_u *) block) + 44);
			done_save = TRUE;
		    }
		    else if (block[4] == 0x400c9)
		    {
			/* MenusDeleted */
			done_save = TRUE;
		    }
		    else
			ro_message(block);
		    break;
	    }
	}
	block[0] = save_window;
	swi(Wimp_CloseWindow, 0, block);
	swi(Wimp_GetCaretPosition, 0, block);
	if (block[0] == -1)
	    swi(Wimp_SetCaretPosition, gui.window_handle, -1, 0, 0, -1, -1);

	return retval;
    }
    else if (initdir)
    {
	/* Open a directory viewer */
	length = strlen(initdir);

	if (length > 240)
	    return NULL;	/* Path too long! */

	length = sprintf(command, "Filer_OpenDir %s", initdir);
	while (command[length - 1] == '.')
	    length--;
	command[length] = '\0';
	swi(OS_CLI, command);
    }
    return NULL;
}