aboutsummaryrefslogtreecommitdiff
path: root/form/frm_driver.c
blob: 0c0912576dbb9d089c3142cf602bbf0fd44ed933 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
/****************************************************************************
 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
 *                                                                          *
 * Permission is hereby granted, free of charge, to any person obtaining a  *
 * copy of this software and associated documentation files (the            *
 * "Software"), to deal in the Software without restriction, including      *
 * without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute, distribute with modifications, sublicense, and/or sell       *
 * copies of the Software, and to permit persons to whom the Software is    *
 * furnished to do so, subject to the following conditions:                 *
 *                                                                          *
 * The above copyright notice and this permission notice shall be included  *
 * in all copies or substantial portions of the Software.                   *
 *                                                                          *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
 *                                                                          *
 * Except as contained in this notice, the name(s) of the above copyright   *
 * holders shall not be used in advertising or otherwise to promote the     *
 * sale, use or other dealings in this Software without prior written       *
 * authorization.                                                           *
 ****************************************************************************/

/****************************************************************************
 *   Author:  Juergen Pfeifer, 1995,1997                                    *
 ****************************************************************************/

#include "form.priv.h"

MODULE_ID("$Id: frm_driver.c,v 1.86 2008/01/19 20:11:03 tom Exp $")

/*----------------------------------------------------------------------------
  This is the core module of the form library. It contains the majority
  of the driver routines as well as the form_driver function.

  Essentially this module is nearly the whole library. This is because
  all the functions in this module depends on some others in the module,
  so it makes no sense to split them into separate files because they
  will always be linked together. The only acceptable concern is turnaround
  time for this module, but now we have all Pentiums or RISCs, so what!

  The driver routines are grouped into nine generic categories:

   a)   Page Navigation            ( all functions prefixed by PN_ )
        The current page of the form is left and some new page is
        entered.
   b)   Inter-Field Navigation     ( all functions prefixed by FN_ )
        The current field of the form is left and some new field is
        entered.
   c)   Intra-Field Navigation     ( all functions prefixed by IFN_ )
        The current position in the current field is changed.
   d)   Vertical Scrolling         ( all functions prefixed by VSC_ )
        Essentially this is a specialization of Intra-Field navigation.
        It has to check for a multi-line field.
   e)   Horizontal Scrolling       ( all functions prefixed by HSC_ )
        Essentially this is a specialization of Intra-Field navigation.
        It has to check for a single-line field.
   f)   Field Editing              ( all functions prefixed by FE_ )
        The content of the current field is changed
   g)   Edit Mode requests         ( all functions prefixed by EM_ )
        Switching between insert and overlay mode
   h)   Field-Validation requests  ( all functions prefixed by FV_ )
        Perform verifications of the field.
   i)   Choice requests            ( all functions prefixed by CR_ )
        Requests to enumerate possible field values
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Some remarks on the placements of assert() macros :
  I use them only on "strategic" places, i.e. top level entries where
  I want to make sure that things are set correctly. Throughout subordinate
  routines I omit them mostly.
  --------------------------------------------------------------------------*/

/*
Some options that may effect compatibility in behavior to SVr4 forms,
but they are here to allow a more intuitive and user friendly behavior of
our form implementation. This doesn't affect the API, so we feel it is
uncritical.

The initial implementation tries to stay very close with the behavior
of the original SVr4 implementation, although in some areas it is quite
clear that this isn't the most appropriate way. As far as possible this
sources will allow you to build a forms lib that behaves quite similar
to SVr4, but now and in the future we will give you better options.
Perhaps at some time we will make this configurable at runtime.
*/

/* Implement a more user-friendly previous/next word behavior */
#define FRIENDLY_PREV_NEXT_WORD (1)
/* Fix the wrong behavior for forms with all fields inactive */
#define FIX_FORM_INACTIVE_BUG (1)
/* Allow dynamic field growth also when navigating past the end */
#define GROW_IF_NAVIGATE (1)

#if USE_WIDEC_SUPPORT
#define myADDNSTR(w, s, n) wadd_wchnstr(w, s, n)
#define myINSNSTR(w, s, n) wins_wchnstr(w, s, n)
#define myINNSTR(w, s, n)  fix_wchnstr(w, s, n)
#define myWCWIDTH(w, y, x) cell_width(w, y, x)
#else
#define myADDNSTR(w, s, n) waddnstr(w, s, n)
#define myINSNSTR(w, s, n) winsnstr(w, s, n)
#define myINNSTR(w, s, n)  winnstr(w, s, n)
#define myWCWIDTH(w, y, x) 1
#endif

/*----------------------------------------------------------------------------
  Forward references to some internally used static functions
  --------------------------------------------------------------------------*/
static int Inter_Field_Navigation(int (*const fct) (FORM *), FORM *form);
static int FN_Next_Field(FORM *form);
static int FN_Previous_Field(FORM *form);
static int FE_New_Line(FORM *);
static int FE_Delete_Previous(FORM *);

/*----------------------------------------------------------------------------
  Macro Definitions.

  Some Remarks on that: I use the convention to use UPPERCASE for constants
  defined by Macros. If I provide a macro as a kind of inline routine to
  provide some logic, I use my Upper_Lower case style.
  --------------------------------------------------------------------------*/

/* Calculate the position of a single row in a field buffer */
#define Position_Of_Row_In_Buffer(field,row) ((row)*(field)->dcols)

/* Calculate start address for the fields buffer# N */
#define Address_Of_Nth_Buffer(field,N) \
  ((field)->buf + (N)*(1+Buffer_Length(field)))

/* Calculate the start address of the row in the fields specified buffer# N */
#define Address_Of_Row_In_Nth_Buffer(field,N,row) \
  (Address_Of_Nth_Buffer(field,N) + Position_Of_Row_In_Buffer(field,row))

/* Calculate the start address of the row in the fields primary buffer */
#define Address_Of_Row_In_Buffer(field,row) \
  Address_Of_Row_In_Nth_Buffer(field,0,row)

/* Calculate the start address of the row in the forms current field
   buffer# N */
#define Address_Of_Current_Row_In_Nth_Buffer(form,N) \
   Address_Of_Row_In_Nth_Buffer((form)->current,N,(form)->currow)

/* Calculate the start address of the row in the forms current field
   primary buffer */
#define Address_Of_Current_Row_In_Buffer(form) \
   Address_Of_Current_Row_In_Nth_Buffer(form,0)

/* Calculate the address of the cursor in the forms current field
   primary buffer */
#define Address_Of_Current_Position_In_Nth_Buffer(form,N) \
   (Address_Of_Current_Row_In_Nth_Buffer(form,N) + (form)->curcol)

/* Calculate the address of the cursor in the forms current field
   buffer# N */
#define Address_Of_Current_Position_In_Buffer(form) \
  Address_Of_Current_Position_In_Nth_Buffer(form,0)

/* Logic to decide whether or not a field is actually a field with
   vertical or horizontal scrolling */
#define Is_Scroll_Field(field)          \
   (((field)->drows > (field)->rows) || \
    ((field)->dcols > (field)->cols))

/* Logic to decide whether or not a field needs to have an individual window
   instead of a derived window because it contains invisible parts.
   This is true for non-public fields and for scrollable fields. */
#define Has_Invisible_Parts(field)     \
  (!((field)->opts & O_PUBLIC)      || \
   Is_Scroll_Field(field))

/* Logic to decide whether or not a field needs justification */
#define Justification_Allowed(field)        \
   (((field)->just != NO_JUSTIFICATION)  && \
    (Single_Line_Field(field))           && \
    (((field)->dcols == (field)->cols)   && \
    ((field)->opts & O_STATIC))             )

/* Logic to determine whether or not a dynamic field may still grow */
#define Growable(field) ((field)->status & _MAY_GROW)

/* Macro to set the attributes for a fields window */
#define Set_Field_Window_Attributes(field,win) \
(  wbkgdset((win),(chtype)((field)->pad | (field)->back)), \
   wattrset((win),(field)->fore) )

/* Logic to decide whether or not a field really appears on the form */
#define Field_Really_Appears(field)         \
  ((field->form)                          &&\
   (field->form->status & _POSTED)        &&\
   (field->opts & O_VISIBLE)              &&\
   (field->page == field->form->curpage))

/* Logic to determine whether or not we are on the first position in the
   current field */
#define First_Position_In_Current_Field(form) \
  (((form)->currow==0) && ((form)->curcol==0))

#define Minimum(a,b) (((a)<=(b)) ? (a) : (b))
#define Maximum(a,b) (((a)>=(b)) ? (a) : (b))

/*----------------------------------------------------------------------------
  Useful constants
  --------------------------------------------------------------------------*/
static FIELD_CELL myBLANK = BLANK;
static FIELD_CELL myZEROS;

#ifdef TRACE
static void
check_pos(FORM *form, int lineno)
{
  int y, x;

  if (form && form->w)
    {
      getyx(form->w, y, x);
      if (y != form->currow || x != form->curcol)
	{
	  T(("CHECKPOS %s@%d have position %d,%d vs want %d,%d",
	     __FILE__, lineno,
	     y, x,
	     form->currow, form->curcol));
	}
    }
}
#define CHECKPOS(form) check_pos(form, __LINE__)
#else
#define CHECKPOS(form)		/* nothing */
#endif

/*----------------------------------------------------------------------------
  Wide-character special functions
  --------------------------------------------------------------------------*/
#if USE_WIDEC_SUPPORT
/* like winsnstr */
static int
wins_wchnstr(WINDOW *w, cchar_t *s, int n)
{
  int code = ERR;
  int y, x;

  while (n-- > 0)
    {
      getyx(w, y, x);
      if ((code = wins_wch(w, s++)) != OK)
	break;
      if ((code = wmove(w, y, x + 1)) != OK)
	break;
    }
  return code;
}

/* win_wchnstr is inconsistent with winnstr, since it returns OK rather than
 * the number of items transferred.
 */
static int
fix_wchnstr(WINDOW *w, cchar_t *s, int n)
{
  int x;

  win_wchnstr(w, s, n);
  /*
   * This function is used to extract the text only from the window.
   * Strip attributes and color from the string so they will not be added
   * back when copying the string to the window.
   */
  for (x = 0; x < n; ++x)
    {
      RemAttr(s[x], A_ATTRIBUTES);
      SetPair(s[x], 0);
    }
  return n;
}

/*
 * Returns the column of the base of the given cell.
 */
static int
cell_base(WINDOW *win, int y, int x)
{
  int result = x;

  while (LEGALYX(win, y, x))
    {
      cchar_t *data = &(win->_line[y].text[x]);

      if (isWidecBase(CHDEREF(data)) || !isWidecExt(CHDEREF(data)))
	{
	  result = x;
	  break;
	}
      --x;
    }
  return result;
}

/*
 * Returns the number of columns needed for the given cell in a window.
 */
static int
cell_width(WINDOW *win, int y, int x)
{
  int result = 1;

  if (LEGALYX(win, y, x))
    {
      cchar_t *data = &(win->_line[y].text[x]);

      if (isWidecExt(CHDEREF(data)))
	{
	  /* recur, providing the number of columns to the next character */
	  result = cell_width(win, y, x - 1);
	}
      else
	{
	  result = wcwidth(CharOf(CHDEREF(data)));
	}
    }
  return result;
}

/*
 * There is no wide-character function such as wdel_wch(), so we must find
 * all of the cells that comprise a multi-column character and delete them
 * one-by-one.
 */
static void
delete_char(FORM *form)
{
  int cells = cell_width(form->w, form->currow, form->curcol);

  form->curcol = cell_base(form->w, form->currow, form->curcol);
  wmove(form->w, form->currow, form->curcol);
  while (cells-- > 0)
    {
      wdelch(form->w);
    }
}
#define DeleteChar(form) delete_char(form)
#else
#define DeleteChar(form) \
	  wmove((form)->w, (form)->currow, (form)->curcol), \
	  wdelch((form)->w)
#endif

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static char *Get_Start_Of_Data(char * buf, int blen)
|
|   Description   :  Return pointer to first non-blank position in buffer.
|                    If buffer is empty return pointer to buffer itself.
|
|   Return Values :  Pointer to first non-blank position in buffer
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD_CELL *
Get_Start_Of_Data(FIELD_CELL *buf, int blen)
{
  FIELD_CELL *p = buf;
  FIELD_CELL *end = &buf[blen];

  assert(buf && blen >= 0);
  while ((p < end) && ISBLANK(*p))
    p++;
  return ((p == end) ? buf : p);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static char *After_End_Of_Data(char * buf, int blen)
|
|   Description   :  Return pointer after last non-blank position in buffer.
|                    If buffer is empty, return pointer to buffer itself.
|
|   Return Values :  Pointer to position after last non-blank position in
|                    buffer.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD_CELL *
After_End_Of_Data(FIELD_CELL *buf, int blen)
{
  FIELD_CELL *p = &buf[blen];

  assert(buf && blen >= 0);
  while ((p > buf) && ISBLANK(p[-1]))
    p--;
  return (p);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static char *Get_First_Whitespace_Character(
|                                     char * buf, int   blen)
|
|   Description   :  Position to the first whitespace character.
|
|   Return Values :  Pointer to first whitespace character in buffer.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD_CELL *
Get_First_Whitespace_Character(FIELD_CELL *buf, int blen)
{
  FIELD_CELL *p = buf;
  FIELD_CELL *end = &p[blen];

  assert(buf && blen >= 0);
  while ((p < end) && !ISBLANK(*p))
    p++;
  return ((p == end) ? buf : p);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static char *After_Last_Whitespace_Character(
|                                     char * buf, int blen)
|
|   Description   :  Get the position after the last whitespace character.
|
|   Return Values :  Pointer to position after last whitespace character in
|                    buffer.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD_CELL *
After_Last_Whitespace_Character(FIELD_CELL *buf, int blen)
{
  FIELD_CELL *p = &buf[blen];

  assert(buf && blen >= 0);
  while ((p > buf) && !ISBLANK(p[-1]))
    p--;
  return (p);
}

/* Set this to 1 to use the div_t version. This is a good idea if your
   compiler has an intrinsic div() support. Unfortunately GNU-C has it
   not yet.
   N.B.: This only works if form->curcol follows immediately form->currow
         and both are of type int.
*/
#define USE_DIV_T (0)

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static void Adjust_Cursor_Position(
|                                       FORM * form, const char * pos)
|
|   Description   :  Set current row and column of the form to values
|                    corresponding to the buffer position.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
NCURSES_INLINE static void
Adjust_Cursor_Position(FORM *form, const FIELD_CELL *pos)
{
  FIELD *field;
  int idx;

  field = form->current;
  assert(pos >= field->buf && field->dcols > 0);
  idx = (int)(pos - field->buf);
#if USE_DIV_T
  *((div_t *) & (form->currow)) = div(idx, field->dcols);
#else
  form->currow = idx / field->dcols;
  form->curcol = idx - field->cols * form->currow;
#endif
  if (field->drows < form->currow)
    form->currow = 0;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static void Buffer_To_Window(
|                                      const FIELD  * field,
|                                      WINDOW * win)
|
|   Description   :  Copy the buffer to the window. If it is a multi-line
|                    field, the buffer is split to the lines of the
|                    window without any editing.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void
Buffer_To_Window(const FIELD *field, WINDOW *win)
{
  int width, height;
  int y, x;
  int len;
  int row;
  FIELD_CELL *pBuffer;

  assert(win && field);

  getyx(win, y, x);
  width = getmaxx(win);
  height = getmaxy(win);

  for (row = 0, pBuffer = field->buf;
       row < height;
       row++, pBuffer += width)
    {
      if ((len = (int)(After_End_Of_Data(pBuffer, width) - pBuffer)) > 0)
	{
	  wmove(win, row, 0);
	  myADDNSTR(win, pBuffer, len);
	}
    }
  wmove(win, y, x);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static void Window_To_Buffer(
|                                          WINDOW * win,
|                                          FIELD  * field)
|
|   Description   :  Copy the content of the window into the buffer.
|                    The multiple lines of a window are simply
|                    concatenated into the buffer. Pad characters in
|                    the window will be replaced by blanks in the buffer.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void
Window_To_Buffer(WINDOW *win, FIELD *field)
{
  int pad;
  int len = 0;
  FIELD_CELL *p;
  int row, height;

  assert(win && field && field->buf);

  pad = field->pad;
  p = field->buf;
  height = getmaxy(win);

  for (row = 0; (row < height) && (row < field->drows); row++)
    {
      wmove(win, row, 0);
      len += myINNSTR(win, p + len, field->dcols);
    }
  p[len] = myZEROS;

  /* replace visual padding character by blanks in buffer */
  if (pad != C_BLANK)
    {
      int i;

      for (i = 0; i < len; i++, p++)
	{
	  if ((unsigned long)CharOf(*p) == ChCharOf(pad)
#if USE_WIDEC_SUPPORT
	      && p->chars[1] == 0
#endif
	    )
	    *p = myBLANK;
	}
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static void Synchronize_Buffer(FORM * form)
|
|   Description   :  If there was a change, copy the content of the
|                    window into the buffer, so the buffer is synchronized
|                    with the windows content. We have to indicate that the
|                    buffer needs validation due to the change.
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
NCURSES_INLINE static void
Synchronize_Buffer(FORM *form)
{
  if (form->status & _WINDOW_MODIFIED)
    {
      form->status &= ~_WINDOW_MODIFIED;
      form->status |= _FCHECK_REQUIRED;
      Window_To_Buffer(form->w, form->current);
      wmove(form->w, form->currow, form->curcol);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static bool Field_Grown( FIELD *field, int amount)
|
|   Description   :  This function is called for growable dynamic fields
|                    only. It has to increase the buffers and to allocate
|                    a new window for this field.
|                    This function has the side effect to set a new
|                    field-buffer pointer, the dcols and drows values
|                    as well as a new current Window for the field.
|
|   Return Values :  TRUE     - field successfully increased
|                    FALSE    - there was some error
+--------------------------------------------------------------------------*/
static bool
Field_Grown(FIELD *field, int amount)
{
  bool result = FALSE;

  if (field && Growable(field))
    {
      bool single_line_field = Single_Line_Field(field);
      int old_buflen = Buffer_Length(field);
      int new_buflen;
      int old_dcols = field->dcols;
      int old_drows = field->drows;
      FIELD_CELL *oldbuf = field->buf;
      FIELD_CELL *newbuf;

      int growth;
      FORM *form = field->form;
      bool need_visual_update = ((form != (FORM *)0) &&
				 (form->status & _POSTED) &&
				 (form->current == field));

      if (need_visual_update)
	Synchronize_Buffer(form);

      if (single_line_field)
	{
	  growth = field->cols * amount;
	  if (field->maxgrow)
	    growth = Minimum(field->maxgrow - field->dcols, growth);
	  field->dcols += growth;
	  if (field->dcols == field->maxgrow)
	    field->status &= ~_MAY_GROW;
	}
      else
	{
	  growth = (field->rows + field->nrow) * amount;
	  if (field->maxgrow)
	    growth = Minimum(field->maxgrow - field->drows, growth);
	  field->drows += growth;
	  if (field->drows == field->maxgrow)
	    field->status &= ~_MAY_GROW;
	}
      /* drows, dcols changed, so we get really the new buffer length */
      new_buflen = Buffer_Length(field);
      newbuf = (FIELD_CELL *)malloc(Total_Buffer_Size(field));
      if (!newbuf)
	{
	  /* restore to previous state */
	  field->dcols = old_dcols;
	  field->drows = old_drows;
	  if ((single_line_field && (field->dcols != field->maxgrow)) ||
	      (!single_line_field && (field->drows != field->maxgrow)))
	    field->status |= _MAY_GROW;
	}
      else
	{
	  /* Copy all the buffers.  This is the reason why we can't just use
	   * realloc().
	   */
	  int i, j;
	  FIELD_CELL *old_bp;
	  FIELD_CELL *new_bp;

	  result = TRUE;	/* allow sharing of recovery on failure */

	  T((T_CREATE("fieldcell %p"), newbuf));
	  field->buf = newbuf;
	  for (i = 0; i <= field->nbuf; i++)
	    {
	      new_bp = Address_Of_Nth_Buffer(field, i);
	      old_bp = oldbuf + i * (1 + old_buflen);
	      for (j = 0; j < old_buflen; ++j)
		new_bp[j] = old_bp[j];
	      while (j < new_buflen)
		new_bp[j++] = myBLANK;
	      new_bp[new_buflen] = myZEROS;
	    }

#if USE_WIDEC_SUPPORT && NCURSES_EXT_FUNCS
	  if (wresize(field->working, 1, Buffer_Length(field) + 1) == ERR)
	    result = FALSE;
#endif

	  if (need_visual_update && result)
	    {
	      WINDOW *new_window = newpad(field->drows, field->dcols);

	      if (new_window != 0)
		{
		  assert(form != (FORM *)0);
		  if (form->w)
		    delwin(form->w);
		  form->w = new_window;
		  Set_Field_Window_Attributes(field, form->w);
		  werase(form->w);
		  Buffer_To_Window(field, form->w);
		  untouchwin(form->w);
		  wmove(form->w, form->currow, form->curcol);
		}
	      else
		result = FALSE;
	    }

	  if (result)
	    {
	      free(oldbuf);
	      /* reflect changes in linked fields */
	      if (field != field->link)
		{
		  FIELD *linked_field;

		  for (linked_field = field->link;
		       linked_field != field;
		       linked_field = linked_field->link)
		    {
		      linked_field->buf = field->buf;
		      linked_field->drows = field->drows;
		      linked_field->dcols = field->dcols;
		    }
		}
	    }
	  else
	    {
	      /* restore old state */
	      field->dcols = old_dcols;
	      field->drows = old_drows;
	      field->buf = oldbuf;
	      if ((single_line_field &&
		   (field->dcols != field->maxgrow)) ||
		  (!single_line_field &&
		   (field->drows != field->maxgrow)))
		field->status |= _MAY_GROW;
	      free(newbuf);
	    }
	}
    }
  return (result);
}

#ifdef NCURSES_MOUSE_VERSION
/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int Field_encloses(FIELD *field, int ry, int rx)
|
|   Description   :  Check if the given coordinates lie within the given field.
|
|   Return Values :  E_OK              - success
|                    E_BAD_ARGUMENT    - invalid form pointer
|                    E_SYSTEM_ERROR    - form has no current field or
|                                        field-window
+--------------------------------------------------------------------------*/
static int
Field_encloses(FIELD *field, int ry, int rx)
{
  T((T_CALLED("Field_encloses(%p)"), field));
  if (field != 0
      && field->frow <= ry
      && (field->frow + field->rows) > ry
      && field->fcol <= rx
      && (field->fcol + field->cols) > rx)
    {
      RETURN(E_OK);
    }
  RETURN(E_INVALID_FIELD);
}
#endif

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int _nc_Position_Form_Cursor(FORM * form)
|
|   Description   :  Position the cursor in the window for the current
|                    field to be in sync. with the currow and curcol
|                    values.
|
|   Return Values :  E_OK              - success
|                    E_BAD_ARGUMENT    - invalid form pointer
|                    E_SYSTEM_ERROR    - form has no current field or
|                                        field-window
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
_nc_Position_Form_Cursor(FORM *form)
{
  FIELD *field;
  WINDOW *formwin;

  if (!form)
    return (E_BAD_ARGUMENT);

  if (!form->w || !form->current)
    return (E_SYSTEM_ERROR);

  field = form->current;
  formwin = Get_Form_Window(form);

  wmove(form->w, form->currow, form->curcol);
  if (Has_Invisible_Parts(field))
    {
      /* in this case fieldwin isn't derived from formwin, so we have
         to move the cursor in formwin by hand... */
      wmove(formwin,
	    field->frow + form->currow - form->toprow,
	    field->fcol + form->curcol - form->begincol);
      wcursyncup(formwin);
    }
  else
    wcursyncup(form->w);
  return (E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int _nc_Refresh_Current_Field(FORM * form)
|
|   Description   :  Propagate the changes in the fields window to the
|                    window of the form.
|
|   Return Values :  E_OK              - on success
|                    E_BAD_ARGUMENT    - invalid form pointer
|                    E_SYSTEM_ERROR    - general error
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
_nc_Refresh_Current_Field(FORM *form)
{
  WINDOW *formwin;
  FIELD *field;

  T((T_CALLED("_nc_Refresh_Current_Field(%p)"), form));

  if (!form)
    RETURN(E_BAD_ARGUMENT);

  if (!form->w || !form->current)
    RETURN(E_SYSTEM_ERROR);

  field = form->current;
  formwin = Get_Form_Window(form);

  if (field->opts & O_PUBLIC)
    {
      if (Is_Scroll_Field(field))
	{
	  /* Again, in this case the fieldwin isn't derived from formwin,
	     so we have to perform a copy operation. */
	  if (Single_Line_Field(field))
	    {
	      /* horizontal scrolling */
	      if (form->curcol < form->begincol)
		form->begincol = form->curcol;
	      else
		{
		  if (form->curcol >= (form->begincol + field->cols))
		    form->begincol = form->curcol - field->cols + 1;
		}
	      copywin(form->w,
		      formwin,
		      0,
		      form->begincol,
		      field->frow,
		      field->fcol,
		      field->frow,
		      field->cols + field->fcol - 1,
		      0);
	    }
	  else
	    {
	      /* A multi-line, i.e. vertical scrolling field */
	      int row_after_bottom, first_modified_row, first_unmodified_row;

	      if (field->drows > field->rows)
		{
		  row_after_bottom = form->toprow + field->rows;
		  if (form->currow < form->toprow)
		    {
		      form->toprow = form->currow;
		      field->status |= _NEWTOP;
		    }
		  if (form->currow >= row_after_bottom)
		    {
		      form->toprow = form->currow - field->rows + 1;
		      field->status |= _NEWTOP;
		    }
		  if (field->status & _NEWTOP)
		    {
		      /* means we have to copy whole range */
		      first_modified_row = form->toprow;
		      first_unmodified_row = first_modified_row + field->rows;
		      field->status &= ~_NEWTOP;
		    }
		  else
		    {
		      /* we try to optimize : finding the range of touched
		         lines */
		      first_modified_row = form->toprow;
		      while (first_modified_row < row_after_bottom)
			{
			  if (is_linetouched(form->w, first_modified_row))
			    break;
			  first_modified_row++;
			}
		      first_unmodified_row = first_modified_row;
		      while (first_unmodified_row < row_after_bottom)
			{
			  if (!is_linetouched(form->w, first_unmodified_row))
			    break;
			  first_unmodified_row++;
			}
		    }
		}
	      else
		{
		  first_modified_row = form->toprow;
		  first_unmodified_row = first_modified_row + field->rows;
		}
	      if (first_unmodified_row != first_modified_row)
		copywin(form->w,
			formwin,
			first_modified_row,
			0,
			field->frow + first_modified_row - form->toprow,
			field->fcol,
			field->frow + first_unmodified_row - form->toprow - 1,
			field->cols + field->fcol - 1,
			0);
	    }
	  wsyncup(formwin);
	}
      else
	{
	  /* if the field-window is simply a derived window, i.e. contains no
	   * invisible parts, the whole thing is trivial
	   */
	  wsyncup(form->w);
	}
    }
  untouchwin(form->w);
  returnCode(_nc_Position_Form_Cursor(form));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static void Perform_Justification(
|                                        FIELD  * field,
|                                        WINDOW * win)
|
|   Description   :  Output field with requested justification
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void
Perform_Justification(FIELD *field, WINDOW *win)
{
  FIELD_CELL *bp;
  int len;
  int col = 0;

  bp = Get_Start_Of_Data(field->buf, Buffer_Length(field));
  len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);

  if (len > 0)
    {
      assert(win && (field->drows == 1) && (field->dcols == field->cols));

      switch (field->just)
	{
	case JUSTIFY_LEFT:
	  break;
	case JUSTIFY_CENTER:
	  col = (field->cols - len) / 2;
	  break;
	case JUSTIFY_RIGHT:
	  col = field->cols - len;
	  break;
	default:
	  break;
	}

      wmove(win, 0, col);
      myADDNSTR(win, bp, len);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static void Undo_Justification(
|                                     FIELD  * field,
|                                     WINDOW * win)
|
|   Description   :  Display field without any justification, i.e.
|                    left justified
|
|   Return Values :  -
+--------------------------------------------------------------------------*/
static void
Undo_Justification(FIELD *field, WINDOW *win)
{
  FIELD_CELL *bp;
  int len;

  bp = Get_Start_Of_Data(field->buf, Buffer_Length(field));
  len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);

  if (len > 0)
    {
      assert(win);
      wmove(win, 0, 0);
      myADDNSTR(win, bp, len);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static bool Check_Char(
|                                           FIELDTYPE * typ,
|                                           int ch,
|                                           TypeArgument *argp)
|
|   Description   :  Perform a single character check for character ch
|                    according to the fieldtype instance.
|
|   Return Values :  TRUE             - Character is valid
|                    FALSE            - Character is invalid
+--------------------------------------------------------------------------*/
static bool
Check_Char(FIELDTYPE *typ, int ch, TypeArgument *argp)
{
  if (typ)
    {
      if (typ->status & _LINKED_TYPE)
	{
	  assert(argp);
	  return (
		   Check_Char(typ->left, ch, argp->left) ||
		   Check_Char(typ->right, ch, argp->right));
	}
      else
	{
	  if (typ->ccheck)
	    return typ->ccheck(ch, (void *)argp);
	}
    }
  return (!iscntrl(UChar(ch)) ? TRUE : FALSE);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Display_Or_Erase_Field(
|                                           FIELD * field,
|                                           bool bEraseFlag)
|
|   Description   :  Create a subwindow for the field and display the
|                    buffer contents (apply justification if required)
|                    or simply erase the field.
|
|   Return Values :  E_OK           - on success
|                    E_SYSTEM_ERROR - some error (typical no memory)
+--------------------------------------------------------------------------*/
static int
Display_Or_Erase_Field(FIELD *field, bool bEraseFlag)
{
  WINDOW *win;
  WINDOW *fwin;

  if (!field)
    return E_SYSTEM_ERROR;

  fwin = Get_Form_Window(field->form);
  win = derwin(fwin,
	       field->rows, field->cols, field->frow, field->fcol);

  if (!win)
    return E_SYSTEM_ERROR;
  else
    {
      if (field->opts & O_VISIBLE)
	Set_Field_Window_Attributes(field, win);
      else
	wattrset(win, WINDOW_ATTRS(fwin));
      werase(win);
    }

  if (!bEraseFlag)
    {
      if (field->opts & O_PUBLIC)
	{
	  if (Justification_Allowed(field))
	    Perform_Justification(field, win);
	  else
	    Buffer_To_Window(field, win);
	}
      field->status &= ~_NEWTOP;
    }
  wsyncup(win);
  delwin(win);
  return E_OK;
}

/* Macros to preset the bEraseFlag */
#define Display_Field(field) Display_Or_Erase_Field(field,FALSE)
#define Erase_Field(field)   Display_Or_Erase_Field(field,TRUE)

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Synchronize_Field(FIELD * field)
|
|   Description   :  Synchronize the windows content with the value in
|                    the buffer.
|
|   Return Values :  E_OK                - success
|                    E_BAD_ARGUMENT      - invalid field pointer
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
static int
Synchronize_Field(FIELD *field)
{
  FORM *form;
  int res = E_OK;

  if (!field)
    return (E_BAD_ARGUMENT);

  if (((form = field->form) != (FORM *)0)
      && Field_Really_Appears(field))
    {
      if (field == form->current)
	{
	  form->currow = form->curcol = form->toprow = form->begincol = 0;
	  werase(form->w);

	  if ((field->opts & O_PUBLIC) && Justification_Allowed(field))
	    Undo_Justification(field, form->w);
	  else
	    Buffer_To_Window(field, form->w);

	  field->status |= _NEWTOP;
	  res = _nc_Refresh_Current_Field(form);
	}
      else
	res = Display_Field(field);
    }
  field->status |= _CHANGED;
  return (res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Synchronize_Linked_Fields(FIELD * field)
|
|   Description   :  Propagate the Synchronize_Field function to all linked
|                    fields. The first error that occurs in the sequence
|                    of updates is the return value.
|
|   Return Values :  E_OK                - success
|                    E_BAD_ARGUMENT      - invalid field pointer
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
static int
Synchronize_Linked_Fields(FIELD *field)
{
  FIELD *linked_field;
  int res = E_OK;
  int syncres;

  if (!field)
    return (E_BAD_ARGUMENT);

  if (!field->link)
    return (E_SYSTEM_ERROR);

  for (linked_field = field->link;
       linked_field != field;
       linked_field = linked_field->link)
    {
      if (((syncres = Synchronize_Field(linked_field)) != E_OK) &&
	  (res == E_OK))
	res = syncres;
    }
  return (res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int _nc_Synchronize_Attributes(FIELD * field)
|
|   Description   :  If a fields visual attributes have changed, this
|                    routine is called to propagate those changes to the
|                    screen.
|
|   Return Values :  E_OK             - success
|                    E_BAD_ARGUMENT   - invalid field pointer
|                    E_SYSTEM_ERROR   - some severe basic error
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
_nc_Synchronize_Attributes(FIELD *field)
{
  FORM *form;
  int res = E_OK;
  WINDOW *formwin;

  T((T_CALLED("_nc_Synchronize_Attributes(%p)"), field));

  if (!field)
    returnCode(E_BAD_ARGUMENT);

  CHECKPOS(field->form);
  if (((form = field->form) != (FORM *)0)
      && Field_Really_Appears(field))
    {
      if (form->current == field)
	{
	  Synchronize_Buffer(form);
	  Set_Field_Window_Attributes(field, form->w);
	  werase(form->w);
	  wmove(form->w, form->currow, form->curcol);

	  if (field->opts & O_PUBLIC)
	    {
	      if (Justification_Allowed(field))
		Undo_Justification(field, form->w);
	      else
		Buffer_To_Window(field, form->w);
	    }
	  else
	    {
	      formwin = Get_Form_Window(form);
	      copywin(form->w, formwin,
		      0, 0,
		      field->frow, field->fcol,
		      field->rows - 1, field->cols - 1, 0);
	      wsyncup(formwin);
	      Buffer_To_Window(field, form->w);
	      field->status |= _NEWTOP;		/* fake refresh to paint all */
	      _nc_Refresh_Current_Field(form);
	    }
	}
      else
	{
	  res = Display_Field(field);
	}
    }
  CHECKPOS(form);
  returnCode(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int _nc_Synchronize_Options(FIELD * field,
|                                                Field_Options newopts)
|
|   Description   :  If a fields options have changed, this routine is
|                    called to propagate these changes to the screen and
|                    to really change the behavior of the field.
|
|   Return Values :  E_OK                - success
|                    E_BAD_ARGUMENT      - invalid field pointer
|                    E_CURRENT           - field is the current one
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
_nc_Synchronize_Options(FIELD *field, Field_Options newopts)
{
  Field_Options oldopts;
  Field_Options changed_opts;
  FORM *form;
  int res = E_OK;

  T((T_CALLED("_nc_Synchronize_Options(%p,%#x)"), field, newopts));

  if (!field)
    returnCode(E_BAD_ARGUMENT);

  oldopts = field->opts;
  changed_opts = oldopts ^ newopts;
  field->opts = newopts;
  form = field->form;

  if (form)
    {
      if (form->current == field)
	{
	  field->opts = oldopts;
	  returnCode(E_CURRENT);
	}

      if (form->status & _POSTED)
	{
	  if ((form->curpage == field->page))
	    {
	      if (changed_opts & O_VISIBLE)
		{
		  if (newopts & O_VISIBLE)
		    res = Display_Field(field);
		  else
		    res = Erase_Field(field);
		}
	      else
		{
		  if ((changed_opts & O_PUBLIC) &&
		      (newopts & O_VISIBLE))
		    res = Display_Field(field);
		}
	    }
	}
    }

  if (changed_opts & O_STATIC)
    {
      bool single_line_field = Single_Line_Field(field);
      int res2 = E_OK;

      if (newopts & O_STATIC)
	{
	  /* the field becomes now static */
	  field->status &= ~_MAY_GROW;
	  /* if actually we have no hidden columns, justification may
	     occur again */
	  if (single_line_field &&
	      (field->cols == field->dcols) &&
	      (field->just != NO_JUSTIFICATION) &&
	      Field_Really_Appears(field))
	    {
	      res2 = Display_Field(field);
	    }
	}
      else
	{
	  /* field is no longer static */
	  if ((field->maxgrow == 0) ||
	      (single_line_field && (field->dcols < field->maxgrow)) ||
	      (!single_line_field && (field->drows < field->maxgrow)))
	    {
	      field->status |= _MAY_GROW;
	      /* a field with justification now changes its behavior,
	         so we must redisplay it */
	      if (single_line_field &&
		  (field->just != NO_JUSTIFICATION) &&
		  Field_Really_Appears(field))
		{
		  res2 = Display_Field(field);
		}
	    }
	}
      if (res2 != E_OK)
	res = res2;
    }

  returnCode(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int _nc_Set_Current_Field(FORM  * form,
|                                              FIELD * newfield)
|
|   Description   :  Make the newfield the new current field.
|
|   Return Values :  E_OK              - success
|                    E_BAD_ARGUMENT    - invalid form or field pointer
|                    E_SYSTEM_ERROR    - some severe basic error
|                    E_NOT_CONNECTED   - no fields are connected to the form
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
_nc_Set_Current_Field(FORM *form, FIELD *newfield)
{
  FIELD *field;
  WINDOW *new_window;

  T((T_CALLED("_nc_Set_Current_Field(%p,%p)"), form, newfield));

  if (!form || !newfield || !form->current || (newfield->form != form))
    returnCode(E_BAD_ARGUMENT);

  if ((form->status & _IN_DRIVER))
    returnCode(E_BAD_STATE);

  if (!(form->field))
    returnCode(E_NOT_CONNECTED);

  field = form->current;

  if ((field != newfield) ||
      !(form->status & _POSTED))
    {
      if ((form->w) &&
	  (field->opts & O_VISIBLE) &&
	  (field->form->curpage == field->page))
	{
	  _nc_Refresh_Current_Field(form);
	  if (field->opts & O_PUBLIC)
	    {
	      if (field->drows > field->rows)
		{
		  if (form->toprow == 0)
		    field->status &= ~_NEWTOP;
		  else
		    field->status |= _NEWTOP;
		}
	      else
		{
		  if (Justification_Allowed(field))
		    {
		      Window_To_Buffer(form->w, field);
		      werase(form->w);
		      Perform_Justification(field, form->w);
		      wsyncup(form->w);
		    }
		}
	    }
	  delwin(form->w);
	  form->w = (WINDOW *)0;
	}

      field = newfield;

      if (Has_Invisible_Parts(field))
	new_window = newpad(field->drows, field->dcols);
      else
	new_window = derwin(Get_Form_Window(form),
			    field->rows, field->cols, field->frow, field->fcol);

      if (!new_window)
	returnCode(E_SYSTEM_ERROR);

      form->current = field;

      if (form->w)
	delwin(form->w);
      form->w = new_window;

      form->status &= ~_WINDOW_MODIFIED;
      Set_Field_Window_Attributes(field, form->w);

      if (Has_Invisible_Parts(field))
	{
	  werase(form->w);
	  Buffer_To_Window(field, form->w);
	}
      else
	{
	  if (Justification_Allowed(field))
	    {
	      werase(form->w);
	      Undo_Justification(field, form->w);
	      wsyncup(form->w);
	    }
	}

      untouchwin(form->w);
    }

  form->currow = form->curcol = form->toprow = form->begincol = 0;
  returnCode(E_OK);
}

/*----------------------------------------------------------------------------
  Intra-Field Navigation routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Next_Character(FORM * form)
|
|   Description   :  Move to the next character in the field. In a multi-line
|                    field this wraps at the end of the line.
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the rightmost position
+--------------------------------------------------------------------------*/
static int
IFN_Next_Character(FORM *form)
{
  FIELD *field = form->current;
  int step = myWCWIDTH(form->w, form->currow, form->curcol);

  T((T_CALLED("IFN_Next_Character(%p)"), form));
  if ((form->curcol += step) == field->dcols)
    {
      if ((++(form->currow)) == field->drows)
	{
#if GROW_IF_NAVIGATE
	  if (!Single_Line_Field(field) && Field_Grown(field, 1))
	    {
	      form->curcol = 0;
	      returnCode(E_OK);
	    }
#endif
	  form->currow--;
#if GROW_IF_NAVIGATE
	  if (Single_Line_Field(field) && Field_Grown(field, 1))
	    returnCode(E_OK);
#endif
	  form->curcol -= step;
	  returnCode(E_REQUEST_DENIED);
	}
      form->curcol = 0;
    }
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Previous_Character(FORM * form)
|
|   Description   :  Move to the previous character in the field. In a
|                    multi-line field this wraps and the beginning of the
|                    line.
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the leftmost position
+--------------------------------------------------------------------------*/
static int
IFN_Previous_Character(FORM *form)
{
  int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
  int oldcol = form->curcol;

  T((T_CALLED("IFN_Previous_Character(%p)"), form));
  if ((form->curcol -= amount) < 0)
    {
      if ((--(form->currow)) < 0)
	{
	  form->currow++;
	  form->curcol = oldcol;
	  returnCode(E_REQUEST_DENIED);
	}
      form->curcol = form->current->dcols - 1;
    }
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Next_Line(FORM * form)
|
|   Description   :  Move to the beginning of the next line in the field
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the last line
+--------------------------------------------------------------------------*/
static int
IFN_Next_Line(FORM *form)
{
  FIELD *field = form->current;

  T((T_CALLED("IFN_Next_Line(%p)"), form));
  if ((++(form->currow)) == field->drows)
    {
#if GROW_IF_NAVIGATE
      if (!Single_Line_Field(field) && Field_Grown(field, 1))
	returnCode(E_OK);
#endif
      form->currow--;
      returnCode(E_REQUEST_DENIED);
    }
  form->curcol = 0;
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Previous_Line(FORM * form)
|
|   Description   :  Move to the beginning of the previous line in the field
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - at the first line
+--------------------------------------------------------------------------*/
static int
IFN_Previous_Line(FORM *form)
{
  T((T_CALLED("IFN_Previous_Line(%p)"), form));
  if ((--(form->currow)) < 0)
    {
      form->currow++;
      returnCode(E_REQUEST_DENIED);
    }
  form->curcol = 0;
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Next_Word(FORM * form)
|
|   Description   :  Move to the beginning of the next word in the field.
|
|   Return Values :  E_OK             - success
|                    E_REQUEST_DENIED - there is no next word
+--------------------------------------------------------------------------*/
static int
IFN_Next_Word(FORM *form)
{
  FIELD *field = form->current;
  FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
  FIELD_CELL *s;
  FIELD_CELL *t;

  T((T_CALLED("IFN_Next_Word(%p)"), form));

  /* We really need access to the data, so we have to synchronize */
  Synchronize_Buffer(form);

  /* Go to the first whitespace after the current position (including
     current position). This is then the starting point to look for the
     next non-blank data */
  s = Get_First_Whitespace_Character(bp, Buffer_Length(field) -
				     (int)(bp - field->buf));

  /* Find the start of the next word */
  t = Get_Start_Of_Data(s, Buffer_Length(field) -
			(int)(s - field->buf));
#if !FRIENDLY_PREV_NEXT_WORD
  if (s == t)
    returnCode(E_REQUEST_DENIED);
  else
#endif
    {
      Adjust_Cursor_Position(form, t);
      returnCode(E_OK);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Previous_Word(FORM * form)
|
|   Description   :  Move to the beginning of the previous word in the field.
|
|   Return Values :  E_OK             - success
|                    E_REQUEST_DENIED - there is no previous word
+--------------------------------------------------------------------------*/
static int
IFN_Previous_Word(FORM *form)
{
  FIELD *field = form->current;
  FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
  FIELD_CELL *s;
  FIELD_CELL *t;
  bool again = FALSE;

  T((T_CALLED("IFN_Previous_Word(%p)"), form));

  /* We really need access to the data, so we have to synchronize */
  Synchronize_Buffer(form);

  s = After_End_Of_Data(field->buf, (int)(bp - field->buf));
  /* s points now right after the last non-blank in the buffer before bp.
     If bp was in a word, s equals bp. In this case we must find the last
     whitespace in the buffer before bp and repeat the game to really find
     the previous word! */
  if (s == bp)
    again = TRUE;

  /* And next call now goes backward to look for the last whitespace
     before that, pointing right after this, so it points to the begin
     of the previous word.
   */
  t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
#if !FRIENDLY_PREV_NEXT_WORD
  if (s == t)
    returnCode(E_REQUEST_DENIED);
#endif
  if (again)
    {
      /* and do it again, replacing bp by t */
      s = After_End_Of_Data(field->buf, (int)(t - field->buf));
      t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
#if !FRIENDLY_PREV_NEXT_WORD
      if (s == t)
	returnCode(E_REQUEST_DENIED);
#endif
    }
  Adjust_Cursor_Position(form, t);
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Beginning_Of_Field(FORM * form)
|
|   Description   :  Place the cursor at the first non-pad character in
|                    the field.
|
|   Return Values :  E_OK             - success
+--------------------------------------------------------------------------*/
static int
IFN_Beginning_Of_Field(FORM *form)
{
  FIELD *field = form->current;

  T((T_CALLED("IFN_Beginning_Of_Field(%p)"), form));
  Synchronize_Buffer(form);
  Adjust_Cursor_Position(form,
			 Get_Start_Of_Data(field->buf, Buffer_Length(field)));
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_End_Of_Field(FORM * form)
|
|   Description   :  Place the cursor after the last non-pad character in
|                    the field. If the field occupies the last position in
|                    the buffer, the cursor is positioned on the last
|                    character.
|
|   Return Values :  E_OK              - success
+--------------------------------------------------------------------------*/
static int
IFN_End_Of_Field(FORM *form)
{
  FIELD *field = form->current;
  FIELD_CELL *pos;

  T((T_CALLED("IFN_End_Of_Field(%p)"), form));
  Synchronize_Buffer(form);
  pos = After_End_Of_Data(field->buf, Buffer_Length(field));
  if (pos == (field->buf + Buffer_Length(field)))
    pos--;
  Adjust_Cursor_Position(form, pos);
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Beginning_Of_Line(FORM * form)
|
|   Description   :  Place the cursor on the first non-pad character in
|                    the current line of the field.
|
|   Return Values :  E_OK         - success
+--------------------------------------------------------------------------*/
static int
IFN_Beginning_Of_Line(FORM *form)
{
  FIELD *field = form->current;

  T((T_CALLED("IFN_Beginning_Of_Line(%p)"), form));
  Synchronize_Buffer(form);
  Adjust_Cursor_Position(form,
			 Get_Start_Of_Data(Address_Of_Current_Row_In_Buffer(form),
					   field->dcols));
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_End_Of_Line(FORM * form)
|
|   Description   :  Place the cursor after the last non-pad character in the
|                    current line of the field. If the field occupies the
|                    last column in the line, the cursor is positioned on the
|                    last character of the line.
|
|   Return Values :  E_OK        - success
+--------------------------------------------------------------------------*/
static int
IFN_End_Of_Line(FORM *form)
{
  FIELD *field = form->current;
  FIELD_CELL *pos;
  FIELD_CELL *bp;

  T((T_CALLED("IFN_End_Of_Line(%p)"), form));
  Synchronize_Buffer(form);
  bp = Address_Of_Current_Row_In_Buffer(form);
  pos = After_End_Of_Data(bp, field->dcols);
  if (pos == (bp + field->dcols))
    pos--;
  Adjust_Cursor_Position(form, pos);
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Left_Character(FORM * form)
|
|   Description   :  Move one character to the left in the current line.
|                    This doesn't cycle.
|
|   Return Values :  E_OK             - success
|                    E_REQUEST_DENIED - already in first column
+--------------------------------------------------------------------------*/
static int
IFN_Left_Character(FORM *form)
{
  int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
  int oldcol = form->curcol;

  T((T_CALLED("IFN_Left_Character(%p)"), form));
  if ((form->curcol -= amount) < 0)
    {
      form->curcol = oldcol;
      returnCode(E_REQUEST_DENIED);
    }
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Right_Character(FORM * form)
|
|   Description   :  Move one character to the right in the current line.
|                    This doesn't cycle.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - already in last column
+--------------------------------------------------------------------------*/
static int
IFN_Right_Character(FORM *form)
{
  int amount = myWCWIDTH(form->w, form->currow, form->curcol);
  int oldcol = form->curcol;

  T((T_CALLED("IFN_Right_Character(%p)"), form));
  if ((form->curcol += amount) >= form->current->dcols)
    {
#if GROW_IF_NAVIGATE
      FIELD *field = form->current;

      if (Single_Line_Field(field) && Field_Grown(field, 1))
	returnCode(E_OK);
#endif
      form->curcol = oldcol;
      returnCode(E_REQUEST_DENIED);
    }
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Up_Character(FORM * form)
|
|   Description   :  Move one line up. This doesn't cycle through the lines
|                    of the field.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - already in last column
+--------------------------------------------------------------------------*/
static int
IFN_Up_Character(FORM *form)
{
  T((T_CALLED("IFN_Up_Character(%p)"), form));
  if ((--(form->currow)) < 0)
    {
      form->currow++;
      returnCode(E_REQUEST_DENIED);
    }
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int IFN_Down_Character(FORM * form)
|
|   Description   :  Move one line down. This doesn't cycle through the
|                    lines of the field.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - already in last column
+--------------------------------------------------------------------------*/
static int
IFN_Down_Character(FORM *form)
{
  FIELD *field = form->current;

  T((T_CALLED("IFN_Down_Character(%p)"), form));
  if ((++(form->currow)) == field->drows)
    {
#if GROW_IF_NAVIGATE
      if (!Single_Line_Field(field) && Field_Grown(field, 1))
	returnCode(E_OK);
#endif
      --(form->currow);
      returnCode(E_REQUEST_DENIED);
    }
  returnCode(E_OK);
}
/*----------------------------------------------------------------------------
  END of Intra-Field Navigation routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Vertical scrolling helper routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int VSC_Generic(FORM *form, int nlines)
|
|   Description   :  Scroll multi-line field forward (nlines>0) or
|                    backward (nlines<0) this many lines.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - can't scroll
+--------------------------------------------------------------------------*/
static int
VSC_Generic(FORM *form, int nlines)
{
  FIELD *field = form->current;
  int res = E_REQUEST_DENIED;
  int rows_to_go = (nlines > 0 ? nlines : -nlines);

  if (nlines > 0)
    {
      if ((rows_to_go + form->toprow) > (field->drows - field->rows))
	rows_to_go = (field->drows - field->rows - form->toprow);

      if (rows_to_go > 0)
	{
	  form->currow += rows_to_go;
	  form->toprow += rows_to_go;
	  res = E_OK;
	}
    }
  else
    {
      if (rows_to_go > form->toprow)
	rows_to_go = form->toprow;

      if (rows_to_go > 0)
	{
	  form->currow -= rows_to_go;
	  form->toprow -= rows_to_go;
	  res = E_OK;
	}
    }
  return (res);
}
/*----------------------------------------------------------------------------
  End of Vertical scrolling helper routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Vertical scrolling routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Vertical_Scrolling(
|                                           int (* const fct) (FORM *),
|                                           FORM * form)
|
|   Description   :  Performs the generic vertical scrolling routines.
|                    This has to check for a multi-line field and to set
|                    the _NEWTOP flag if scrolling really occurred.
|
|   Return Values :  Propagated error code from low-level driver calls
+--------------------------------------------------------------------------*/
static int
Vertical_Scrolling(int (*const fct) (FORM *), FORM *form)
{
  int res = E_REQUEST_DENIED;

  if (!Single_Line_Field(form->current))
    {
      res = fct(form);
      if (res == E_OK)
	form->current->status |= _NEWTOP;
    }
  return (res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int VSC_Scroll_Line_Forward(FORM * form)
|
|   Description   :  Scroll multi-line field forward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data ahead
+--------------------------------------------------------------------------*/
static int
VSC_Scroll_Line_Forward(FORM *form)
{
  T((T_CALLED("VSC_Scroll_Line_Forward(%p)"), form));
  returnCode(VSC_Generic(form, 1));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int VSC_Scroll_Line_Backward(FORM * form)
|
|   Description   :  Scroll multi-line field backward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int
VSC_Scroll_Line_Backward(FORM *form)
{
  T((T_CALLED("VSC_Scroll_Line_Backward(%p)"), form));
  returnCode(VSC_Generic(form, -1));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int VSC_Scroll_Page_Forward(FORM * form)
|
|   Description   :  Scroll a multi-line field forward a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data ahead
+--------------------------------------------------------------------------*/
static int
VSC_Scroll_Page_Forward(FORM *form)
{
  T((T_CALLED("VSC_Scroll_Page_Forward(%p)"), form));
  returnCode(VSC_Generic(form, form->current->rows));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int VSC_Scroll_Half_Page_Forward(FORM * form)
|
|   Description   :  Scroll a multi-line field forward half a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data ahead
+--------------------------------------------------------------------------*/
static int
VSC_Scroll_Half_Page_Forward(FORM *form)
{
  T((T_CALLED("VSC_Scroll_Half_Page_Forward(%p)"), form));
  returnCode(VSC_Generic(form, (form->current->rows + 1) / 2));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int VSC_Scroll_Page_Backward(FORM * form)
|
|   Description   :  Scroll a multi-line field backward a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data behind
+--------------------------------------------------------------------------*/
static int
VSC_Scroll_Page_Backward(FORM *form)
{
  T((T_CALLED("VSC_Scroll_Page_Backward(%p)"), form));
  returnCode(VSC_Generic(form, -(form->current->rows)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int VSC_Scroll_Half_Page_Backward(FORM * form)
|
|   Description   :  Scroll a multi-line field backward half a page
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - no data behind
+--------------------------------------------------------------------------*/
static int
VSC_Scroll_Half_Page_Backward(FORM *form)
{
  T((T_CALLED("VSC_Scroll_Half_Page_Backward(%p)"), form));
  returnCode(VSC_Generic(form, -((form->current->rows + 1) / 2)));
}
/*----------------------------------------------------------------------------
  End of Vertical scrolling routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Horizontal scrolling helper routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int HSC_Generic(FORM *form, int ncolumns)
|
|   Description   :  Scroll single-line field forward (ncolumns>0) or
|                    backward (ncolumns<0) this many columns.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - can't scroll
+--------------------------------------------------------------------------*/
static int
HSC_Generic(FORM *form, int ncolumns)
{
  FIELD *field = form->current;
  int res = E_REQUEST_DENIED;
  int cols_to_go = (ncolumns > 0 ? ncolumns : -ncolumns);

  if (ncolumns > 0)
    {
      if ((cols_to_go + form->begincol) > (field->dcols - field->cols))
	cols_to_go = field->dcols - field->cols - form->begincol;

      if (cols_to_go > 0)
	{
	  form->curcol += cols_to_go;
	  form->begincol += cols_to_go;
	  res = E_OK;
	}
    }
  else
    {
      if (cols_to_go > form->begincol)
	cols_to_go = form->begincol;

      if (cols_to_go > 0)
	{
	  form->curcol -= cols_to_go;
	  form->begincol -= cols_to_go;
	  res = E_OK;
	}
    }
  return (res);
}
/*----------------------------------------------------------------------------
  End of Horizontal scrolling helper routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Horizontal scrolling routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Horizontal_Scrolling(
|                                          int (* const fct) (FORM *),
|                                          FORM * form)
|
|   Description   :  Performs the generic horizontal scrolling routines.
|                    This has to check for a single-line field.
|
|   Return Values :  Propagated error code from low-level driver calls
+--------------------------------------------------------------------------*/
static int
Horizontal_Scrolling(int (*const fct) (FORM *), FORM *form)
{
  if (Single_Line_Field(form->current))
    return fct(form);
  else
    return (E_REQUEST_DENIED);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int HSC_Scroll_Char_Forward(FORM * form)
|
|   Description   :  Scroll single-line field forward a character
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data ahead
+--------------------------------------------------------------------------*/
static int
HSC_Scroll_Char_Forward(FORM *form)
{
  T((T_CALLED("HSC_Scroll_Char_Forward(%p)"), form));
  returnCode(HSC_Generic(form, 1));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int HSC_Scroll_Char_Backward(FORM * form)
|
|   Description   :  Scroll single-line field backward a character
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int
HSC_Scroll_Char_Backward(FORM *form)
{
  T((T_CALLED("HSC_Scroll_Char_Backward(%p)"), form));
  returnCode(HSC_Generic(form, -1));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int HSC_Horizontal_Line_Forward(FORM* form)
|
|   Description   :  Scroll single-line field forward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data ahead
+--------------------------------------------------------------------------*/
static int
HSC_Horizontal_Line_Forward(FORM *form)
{
  T((T_CALLED("HSC_Horizontal_Line_Forward(%p)"), form));
  returnCode(HSC_Generic(form, form->current->cols));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int HSC_Horizontal_Half_Line_Forward(FORM* form)
|
|   Description   :  Scroll single-line field forward half a line
|
|   Return Values :  E_OK               - success
|                    E_REQUEST_DENIED   - no data ahead
+--------------------------------------------------------------------------*/
static int
HSC_Horizontal_Half_Line_Forward(FORM *form)
{
  T((T_CALLED("HSC_Horizontal_Half_Line_Forward(%p)"), form));
  returnCode(HSC_Generic(form, (form->current->cols + 1) / 2));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int HSC_Horizontal_Line_Backward(FORM* form)
|
|   Description   :  Scroll single-line field backward a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int
HSC_Horizontal_Line_Backward(FORM *form)
{
  T((T_CALLED("HSC_Horizontal_Line_Backward(%p)"), form));
  returnCode(HSC_Generic(form, -(form->current->cols)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int HSC_Horizontal_Half_Line_Backward(FORM* form)
|
|   Description   :  Scroll single-line field backward half a line
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - no data behind
+--------------------------------------------------------------------------*/
static int
HSC_Horizontal_Half_Line_Backward(FORM *form)
{
  T((T_CALLED("HSC_Horizontal_Half_Line_Backward(%p)"), form));
  returnCode(HSC_Generic(form, -((form->current->cols + 1) / 2)));
}

/*----------------------------------------------------------------------------
  End of Horizontal scrolling routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Helper routines for Field Editing
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static bool Is_There_Room_For_A_Line(FORM * form)
|
|   Description   :  Check whether or not there is enough room in the
|                    buffer to enter a whole line.
|
|   Return Values :  TRUE   - there is enough space
|                    FALSE  - there is not enough space
+--------------------------------------------------------------------------*/
NCURSES_INLINE static bool
Is_There_Room_For_A_Line(FORM *form)
{
  FIELD *field = form->current;
  FIELD_CELL *begin_of_last_line, *s;

  Synchronize_Buffer(form);
  begin_of_last_line = Address_Of_Row_In_Buffer(field, (field->drows - 1));
  s = After_End_Of_Data(begin_of_last_line, field->dcols);
  return ((s == begin_of_last_line) ? TRUE : FALSE);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
|
|   Description   :  Checks whether or not there is room for a new character
|                    in the current line.
|
|   Return Values :  TRUE    - there is room
|                    FALSE   - there is not enough room (line full)
+--------------------------------------------------------------------------*/
NCURSES_INLINE static bool
Is_There_Room_For_A_Char_In_Line(FORM *form)
{
  int last_char_in_line;

  wmove(form->w, form->currow, form->current->dcols - 1);
  last_char_in_line = (int)(winch(form->w) & A_CHARTEXT);
  wmove(form->w, form->currow, form->curcol);
  return (((last_char_in_line == form->current->pad) ||
	   is_blank(last_char_in_line)) ? TRUE : FALSE);
}

#define There_Is_No_Room_For_A_Char_In_Line(f) \
  !Is_There_Room_For_A_Char_In_Line(f)

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Insert_String(
|                                             FORM * form,
|                                             int row,
|                                             char *txt,
|                                             int  len )
|
|   Description   :  Insert the 'len' characters beginning at pointer 'txt'
|                    into the 'row' of the 'form'. The insertion occurs
|                    on the beginning of the row, all other characters are
|                    moved to the right. After the text a pad character will
|                    be inserted to separate the text from the rest. If
|                    necessary the insertion moves characters on the next
|                    line to make place for the requested insertion string.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  -
|                    E_SYSTEM_ERROR    - system error
+--------------------------------------------------------------------------*/
static int
Insert_String(FORM *form, int row, FIELD_CELL *txt, int len)
{
  FIELD *field = form->current;
  FIELD_CELL *bp = Address_Of_Row_In_Buffer(field, row);
  int datalen = (int)(After_End_Of_Data(bp, field->dcols) - bp);
  int freelen = field->dcols - datalen;
  int requiredlen = len + 1;
  FIELD_CELL *split;
  int result = E_REQUEST_DENIED;

  if (freelen >= requiredlen)
    {
      wmove(form->w, row, 0);
      myINSNSTR(form->w, txt, len);
      wmove(form->w, row, len);
      myINSNSTR(form->w, &myBLANK, 1);
      return E_OK;
    }
  else
    {
      /* we have to move characters on the next line. If we are on the
         last line this may work, if the field is growable */
      if ((row == (field->drows - 1)) && Growable(field))
	{
	  if (!Field_Grown(field, 1))
	    return (E_SYSTEM_ERROR);
	  /* !!!Side-Effect : might be changed due to growth!!! */
	  bp = Address_Of_Row_In_Buffer(field, row);
	}

      if (row < (field->drows - 1))
	{
	  split =
	    After_Last_Whitespace_Character(bp,
					    (int)(Get_Start_Of_Data(bp
								    + field->dcols
								    - requiredlen,
								    requiredlen)
						  - bp));
	  /* split points now to the first character of the portion of the
	     line that must be moved to the next line */
	  datalen = (int)(split - bp);	/* + freelen has to stay on this line   */
	  freelen = field->dcols - (datalen + freelen);		/* for the next line */

	  if ((result = Insert_String(form, row + 1, split, freelen)) == E_OK)
	    {
	      wmove(form->w, row, datalen);
	      wclrtoeol(form->w);
	      wmove(form->w, row, 0);
	      myINSNSTR(form->w, txt, len);
	      wmove(form->w, row, len);
	      myINSNSTR(form->w, &myBLANK, 1);
	      return E_OK;
	    }
	}
      return (result);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Wrapping_Not_Necessary_Or_Wrapping_Ok(
|                                             FORM * form)
|
|   Description   :  If a character has been entered into a field, it may
|                    be that wrapping has to occur. This routine checks
|                    whether or not wrapping is required and if so, performs
|                    the wrapping.
|
|   Return Values :  E_OK              - no wrapping required or wrapping
|                                        was successful
|                    E_REQUEST_DENIED  -
|                    E_SYSTEM_ERROR    - some system error
+--------------------------------------------------------------------------*/
static int
Wrapping_Not_Necessary_Or_Wrapping_Ok(FORM *form)
{
  FIELD *field = form->current;
  int result = E_REQUEST_DENIED;
  bool Last_Row = ((field->drows - 1) == form->currow);

  if ((field->opts & O_WRAP) &&	/* wrapping wanted     */
      (!Single_Line_Field(field)) &&	/* must be multi-line  */
      (There_Is_No_Room_For_A_Char_In_Line(form)) &&	/* line is full        */
      (!Last_Row || Growable(field)))	/* there are more lines */
    {
      FIELD_CELL *bp;
      FIELD_CELL *split;
      int chars_to_be_wrapped;
      int chars_to_remain_on_line;

      if (Last_Row)
	{
	  /* the above logic already ensures, that in this case the field
	     is growable */
	  if (!Field_Grown(field, 1))
	    return E_SYSTEM_ERROR;
	}
      bp = Address_Of_Current_Row_In_Buffer(form);
      Window_To_Buffer(form->w, field);
      split = After_Last_Whitespace_Character(bp, field->dcols);
      /* split points to the first character of the sequence to be brought
         on the next line */
      chars_to_remain_on_line = (int)(split - bp);
      chars_to_be_wrapped = field->dcols - chars_to_remain_on_line;
      if (chars_to_remain_on_line > 0)
	{
	  if ((result = Insert_String(form, form->currow + 1, split,
				      chars_to_be_wrapped)) == E_OK)
	    {
	      wmove(form->w, form->currow, chars_to_remain_on_line);
	      wclrtoeol(form->w);
	      if (form->curcol >= chars_to_remain_on_line)
		{
		  form->currow++;
		  form->curcol -= chars_to_remain_on_line;
		}
	      return E_OK;
	    }
	}
      else
	return E_OK;
      if (result != E_OK)
	{
	  DeleteChar(form);
	  Window_To_Buffer(form->w, field);
	  result = E_REQUEST_DENIED;
	}
    }
  else
    result = E_OK;		/* wrapping was not necessary */
  return (result);
}

/*----------------------------------------------------------------------------
  Field Editing routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Field_Editing(
|                                    int (* const fct) (FORM *),
|                                    FORM * form)
|
|   Description   :  Generic routine for field editing requests. The driver
|                    routines are only called for editable fields, the
|                    _WINDOW_MODIFIED flag is set if editing occurred.
|                    This is somewhat special due to the overload semantics
|                    of the NEW_LINE and DEL_PREV requests.
|
|   Return Values :  Error code from low level drivers.
+--------------------------------------------------------------------------*/
static int
Field_Editing(int (*const fct) (FORM *), FORM *form)
{
  int res = E_REQUEST_DENIED;

  /* We have to deal here with the specific case of the overloaded
     behavior of New_Line and Delete_Previous requests.
     They may end up in navigational requests if we are on the first
     character in a field. But navigation is also allowed on non-
     editable fields.
   */
  if ((fct == FE_Delete_Previous) &&
      (form->opts & O_BS_OVERLOAD) &&
      First_Position_In_Current_Field(form))
    {
      res = Inter_Field_Navigation(FN_Previous_Field, form);
    }
  else
    {
      if (fct == FE_New_Line)
	{
	  if ((form->opts & O_NL_OVERLOAD) &&
	      First_Position_In_Current_Field(form))
	    {
	      res = Inter_Field_Navigation(FN_Next_Field, form);
	    }
	  else
	    /* FE_New_Line deals itself with the _WINDOW_MODIFIED flag */
	    res = fct(form);
	}
      else
	{
	  /* From now on, everything must be editable */
	  if (form->current->opts & O_EDIT)
	    {
	      res = fct(form);
	      if (res == E_OK)
		form->status |= _WINDOW_MODIFIED;
	    }
	}
    }
  return res;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_New_Line(FORM * form)
|
|   Description   :  Perform a new line request. This is rather complex
|                    compared to other routines in this code due to the
|                    rather difficult to understand description in the
|                    manuals.
|
|   Return Values :  E_OK               - success
|                    E_REQUEST_DENIED   - new line not allowed
|                    E_SYSTEM_ERROR     - system error
+--------------------------------------------------------------------------*/
static int
FE_New_Line(FORM *form)
{
  FIELD *field = form->current;
  FIELD_CELL *bp, *t;
  bool Last_Row = ((field->drows - 1) == form->currow);

  T((T_CALLED("FE_New_Line(%p)"), form));
  if (form->status & _OVLMODE)
    {
      if (Last_Row &&
	  (!(Growable(field) && !Single_Line_Field(field))))
	{
	  if (!(form->opts & O_NL_OVERLOAD))
	    returnCode(E_REQUEST_DENIED);
	  wmove(form->w, form->currow, form->curcol);
	  wclrtoeol(form->w);
	  /* we have to set this here, although it is also
	     handled in the generic routine. The reason is,
	     that FN_Next_Field may fail, but the form is
	     definitively changed */
	  form->status |= _WINDOW_MODIFIED;
	  returnCode(Inter_Field_Navigation(FN_Next_Field, form));
	}
      else
	{
	  if (Last_Row && !Field_Grown(field, 1))
	    {
	      /* N.B.: due to the logic in the 'if', LastRow==TRUE
	         means here that the field is growable and not
	         a single-line field */
	      returnCode(E_SYSTEM_ERROR);
	    }
	  wmove(form->w, form->currow, form->curcol);
	  wclrtoeol(form->w);
	  form->currow++;
	  form->curcol = 0;
	  form->status |= _WINDOW_MODIFIED;
	  returnCode(E_OK);
	}
    }
  else
    {
      /* Insert Mode */
      if (Last_Row &&
	  !(Growable(field) && !Single_Line_Field(field)))
	{
	  if (!(form->opts & O_NL_OVERLOAD))
	    returnCode(E_REQUEST_DENIED);
	  returnCode(Inter_Field_Navigation(FN_Next_Field, form));
	}
      else
	{
	  bool May_Do_It = !Last_Row && Is_There_Room_For_A_Line(form);

	  if (!(May_Do_It || Growable(field)))
	    returnCode(E_REQUEST_DENIED);
	  if (!May_Do_It && !Field_Grown(field, 1))
	    returnCode(E_SYSTEM_ERROR);

	  bp = Address_Of_Current_Position_In_Buffer(form);
	  t = After_End_Of_Data(bp, field->dcols - form->curcol);
	  wmove(form->w, form->currow, form->curcol);
	  wclrtoeol(form->w);
	  form->currow++;
	  form->curcol = 0;
	  wmove(form->w, form->currow, form->curcol);
	  winsertln(form->w);
	  myADDNSTR(form->w, bp, (int)(t - bp));
	  form->status |= _WINDOW_MODIFIED;
	  returnCode(E_OK);
	}
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Insert_Character(FORM * form)
|
|   Description   :  Insert blank character at the cursor position
|
|   Return Values :  E_OK
|                    E_REQUEST_DENIED
+--------------------------------------------------------------------------*/
static int
FE_Insert_Character(FORM *form)
{
  FIELD *field = form->current;
  int result = E_REQUEST_DENIED;

  T((T_CALLED("FE_Insert_Character(%p)"), form));
  if (Check_Char(field->type, (int)C_BLANK, (TypeArgument *)(field->arg)))
    {
      bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);

      if (There_Is_Room ||
	  ((Single_Line_Field(field) && Growable(field))))
	{
	  if (!There_Is_Room && !Field_Grown(field, 1))
	    result = E_SYSTEM_ERROR;
	  else
	    {
	      winsch(form->w, (chtype)C_BLANK);
	      result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form);
	    }
	}
    }
  returnCode(result);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Insert_Line(FORM * form)
|
|   Description   :  Insert a blank line at the cursor position
|
|   Return Values :  E_OK               - success
|                    E_REQUEST_DENIED   - line can not be inserted
+--------------------------------------------------------------------------*/
static int
FE_Insert_Line(FORM *form)
{
  FIELD *field = form->current;
  int result = E_REQUEST_DENIED;

  T((T_CALLED("FE_Insert_Line(%p)"), form));
  if (Check_Char(field->type, (int)C_BLANK, (TypeArgument *)(field->arg)))
    {
      bool Maybe_Done = (form->currow != (field->drows - 1)) &&
      Is_There_Room_For_A_Line(form);

      if (!Single_Line_Field(field) &&
	  (Maybe_Done || Growable(field)))
	{
	  if (!Maybe_Done && !Field_Grown(field, 1))
	    result = E_SYSTEM_ERROR;
	  else
	    {
	      form->curcol = 0;
	      winsertln(form->w);
	      result = E_OK;
	    }
	}
    }
  returnCode(result);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Delete_Character(FORM * form)
|
|   Description   :  Delete character at the cursor position
|
|   Return Values :  E_OK    - success
+--------------------------------------------------------------------------*/
static int
FE_Delete_Character(FORM *form)
{
  T((T_CALLED("FE_Delete_Character(%p)"), form));
  DeleteChar(form);
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Delete_Previous(FORM * form)
|
|   Description   :  Delete character before cursor. Again this is a rather
|                    difficult piece compared to others due to the overloading
|                    semantics of backspace.
|                    N.B.: The case of overloaded BS on first field position
|                          is already handled in the generic routine.
|
|   Return Values :  E_OK                - success
|                    E_REQUEST_DENIED    - Character can't be deleted
+--------------------------------------------------------------------------*/
static int
FE_Delete_Previous(FORM *form)
{
  FIELD *field = form->current;

  T((T_CALLED("FE_Delete_Previous(%p)"), form));
  if (First_Position_In_Current_Field(form))
    returnCode(E_REQUEST_DENIED);

  if ((--(form->curcol)) < 0)
    {
      FIELD_CELL *this_line, *prev_line, *prev_end, *this_end;
      int this_row = form->currow;

      form->curcol++;
      if (form->status & _OVLMODE)
	returnCode(E_REQUEST_DENIED);

      prev_line = Address_Of_Row_In_Buffer(field, (form->currow - 1));
      this_line = Address_Of_Row_In_Buffer(field, (form->currow));
      Synchronize_Buffer(form);
      prev_end = After_End_Of_Data(prev_line, field->dcols);
      this_end = After_End_Of_Data(this_line, field->dcols);
      if ((int)(this_end - this_line) >
	  (field->cols - (int)(prev_end - prev_line)))
	returnCode(E_REQUEST_DENIED);
      wmove(form->w, form->currow, form->curcol);
      wdeleteln(form->w);
      Adjust_Cursor_Position(form, prev_end);
      /*
       * If we did not really move to the previous line, help the user a
       * little.  It is however a little inconsistent.  Normally, when
       * backspacing around the point where text wraps to a new line in a
       * multi-line form, we absorb one keystroke for the wrapping point.  That
       * is consistent with SVr4 forms.  However, SVr4 does not allow typing
       * into the last column of the field, and requires the user to enter a
       * newline to move to the next line.  Therefore it can consistently eat
       * that keystroke.  Since ncurses allows the last column, it wraps
       * automatically (given the proper options).  But we cannot eat the
       * keystroke to back over the wrapping point, since that would put the
       * cursor past the end of the form field.  In this case, just delete the
       * character at the end of the field.
       */
      if (form->currow == this_row && this_row > 0)
	{
	  form->currow -= 1;
	  form->curcol = field->dcols - 1;
	  DeleteChar(form);
	}
      else
	{
	  wmove(form->w, form->currow, form->curcol);
	  myADDNSTR(form->w, this_line, (int)(this_end - this_line));
	}
    }
  else
    {
      DeleteChar(form);
    }
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Delete_Line(FORM * form)
|
|   Description   :  Delete line at cursor position.
|
|   Return Values :  E_OK  - success
+--------------------------------------------------------------------------*/
static int
FE_Delete_Line(FORM *form)
{
  T((T_CALLED("FE_Delete_Line(%p)"), form));
  form->curcol = 0;
  wdeleteln(form->w);
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Delete_Word(FORM * form)
|
|   Description   :  Delete word at cursor position
|
|   Return Values :  E_OK               - success
|                    E_REQUEST_DENIED   - failure
+--------------------------------------------------------------------------*/
static int
FE_Delete_Word(FORM *form)
{
  FIELD *field = form->current;
  FIELD_CELL *bp = Address_Of_Current_Row_In_Buffer(form);
  FIELD_CELL *ep = bp + field->dcols;
  FIELD_CELL *cp = bp + form->curcol;
  FIELD_CELL *s;

  T((T_CALLED("FE_Delete_Word(%p)"), form));
  Synchronize_Buffer(form);
  if (ISBLANK(*cp))
    returnCode(E_REQUEST_DENIED);	/* not in word */

  /* move cursor to begin of word and erase to end of screen-line */
  Adjust_Cursor_Position(form,
			 After_Last_Whitespace_Character(bp, form->curcol));
  wmove(form->w, form->currow, form->curcol);
  wclrtoeol(form->w);

  /* skip over word in buffer */
  s = Get_First_Whitespace_Character(cp, (int)(ep - cp));
  /* to begin of next word    */
  s = Get_Start_Of_Data(s, (int)(ep - s));
  if ((s != cp) && !ISBLANK(*s))
    {
      /* copy remaining line to window */
      myADDNSTR(form->w, s, (int)(s - After_End_Of_Data(s, (int)(ep - s))));
    }
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Clear_To_End_Of_Line(FORM * form)
|
|   Description   :  Clear to end of current line.
|
|   Return Values :  E_OK   - success
+--------------------------------------------------------------------------*/
static int
FE_Clear_To_End_Of_Line(FORM *form)
{
  T((T_CALLED("FE_Clear_To_End_Of_Line(%p)"), form));
  wmove(form->w, form->currow, form->curcol);
  wclrtoeol(form->w);
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Clear_To_End_Of_Field(FORM * form)
|
|   Description   :  Clear to end of field.
|
|   Return Values :  E_OK   - success
+--------------------------------------------------------------------------*/
static int
FE_Clear_To_End_Of_Field(FORM *form)
{
  T((T_CALLED("FE_Clear_To_End_Of_Field(%p)"), form));
  wmove(form->w, form->currow, form->curcol);
  wclrtobot(form->w);
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FE_Clear_Field(FORM * form)
|
|   Description   :  Clear entire field.
|
|   Return Values :  E_OK   - success
+--------------------------------------------------------------------------*/
static int
FE_Clear_Field(FORM *form)
{
  T((T_CALLED("FE_Clear_Field(%p)"), form));
  form->currow = form->curcol = 0;
  werase(form->w);
  returnCode(E_OK);
}
/*----------------------------------------------------------------------------
  END of Field Editing routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Edit Mode routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int EM_Overlay_Mode(FORM * form)
|
|   Description   :  Switch to overlay mode.
|
|   Return Values :  E_OK   - success
+--------------------------------------------------------------------------*/
static int
EM_Overlay_Mode(FORM *form)
{
  T((T_CALLED("EM_Overlay_Mode(%p)"), form));
  form->status |= _OVLMODE;
  returnCode(E_OK);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int EM_Insert_Mode(FORM * form)
|
|   Description   :  Switch to insert mode
|
|   Return Values :  E_OK   - success
+--------------------------------------------------------------------------*/
static int
EM_Insert_Mode(FORM *form)
{
  T((T_CALLED("EM_Insert_Mode(%p)"), form));
  form->status &= ~_OVLMODE;
  returnCode(E_OK);
}

/*----------------------------------------------------------------------------
  END of Edit Mode routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Helper routines for Choice Requests
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static bool Next_Choice(
|                                            FIELDTYPE * typ,
|                                            FIELD * field,
|                                            TypeArgument *argp)
|
|   Description   :  Get the next field choice. For linked types this is
|                    done recursively.
|
|   Return Values :  TRUE    - next choice successfully retrieved
|                    FALSE   - couldn't retrieve next choice
+--------------------------------------------------------------------------*/
static bool
Next_Choice(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
{
  if (!typ || !(typ->status & _HAS_CHOICE))
    return FALSE;

  if (typ->status & _LINKED_TYPE)
    {
      assert(argp);
      return (
	       Next_Choice(typ->left, field, argp->left) ||
	       Next_Choice(typ->right, field, argp->right));
    }
  else
    {
      assert(typ->next);
      return typ->next(field, (void *)argp);
    }
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static bool Previous_Choice(
|                                                FIELDTYPE * typ,
|                                                FIELD * field,
|                                                TypeArgument *argp)
|
|   Description   :  Get the previous field choice. For linked types this
|                    is done recursively.
|
|   Return Values :  TRUE    - previous choice successfully retrieved
|                    FALSE   - couldn't retrieve previous choice
+--------------------------------------------------------------------------*/
static bool
Previous_Choice(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
{
  if (!typ || !(typ->status & _HAS_CHOICE))
    return FALSE;

  if (typ->status & _LINKED_TYPE)
    {
      assert(argp);
      return (
	       Previous_Choice(typ->left, field, argp->left) ||
	       Previous_Choice(typ->right, field, argp->right));
    }
  else
    {
      assert(typ->prev);
      return typ->prev(field, (void *)argp);
    }
}
/*----------------------------------------------------------------------------
  End of Helper routines for Choice Requests
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Routines for Choice Requests
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int CR_Next_Choice(FORM * form)
|
|   Description   :  Get the next field choice.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - next choice couldn't be retrieved
+--------------------------------------------------------------------------*/
static int
CR_Next_Choice(FORM *form)
{
  FIELD *field = form->current;

  T((T_CALLED("CR_Next_Choice(%p)"), form));
  Synchronize_Buffer(form);
  returnCode((Next_Choice(field->type, field, (TypeArgument *)(field->arg)))
	     ? E_OK
	     : E_REQUEST_DENIED);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int CR_Previous_Choice(FORM * form)
|
|   Description   :  Get the previous field choice.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - prev. choice couldn't be retrieved
+--------------------------------------------------------------------------*/
static int
CR_Previous_Choice(FORM *form)
{
  FIELD *field = form->current;

  T((T_CALLED("CR_Previous_Choice(%p)"), form));
  Synchronize_Buffer(form);
  returnCode((Previous_Choice(field->type, field, (TypeArgument *)(field->arg)))
	     ? E_OK
	     : E_REQUEST_DENIED);
}
/*----------------------------------------------------------------------------
  End of Routines for Choice Requests
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Helper routines for Field Validations.
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static bool Check_Field(
|                                            FIELDTYPE * typ,
|                                            FIELD * field,
|                                            TypeArgument * argp)
|
|   Description   :  Check the field according to its fieldtype and its
|                    actual arguments. For linked fieldtypes this is done
|                    recursively.
|
|   Return Values :  TRUE       - field is valid
|                    FALSE      - field is invalid.
+--------------------------------------------------------------------------*/
static bool
Check_Field(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
{
  if (typ)
    {
      if (field->opts & O_NULLOK)
	{
	  FIELD_CELL *bp = field->buf;

	  assert(bp);
	  while (ISBLANK(*bp))
	    {
	      bp++;
	    }
	  if (CharOf(*bp) == 0)
	    return TRUE;
	}

      if (typ->status & _LINKED_TYPE)
	{
	  assert(argp);
	  return (
		   Check_Field(typ->left, field, argp->left) ||
		   Check_Field(typ->right, field, argp->right));
	}
      else
	{
	  if (typ->fcheck)
	    return typ->fcheck(field, (void *)argp);
	}
    }
  return TRUE;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  bool _nc_Internal_Validation(FORM * form )
|
|   Description   :  Validate the current field of the form.
|
|   Return Values :  TRUE  - field is valid
|                    FALSE - field is invalid
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(bool)
_nc_Internal_Validation(FORM *form)
{
  FIELD *field;

  field = form->current;

  Synchronize_Buffer(form);
  if ((form->status & _FCHECK_REQUIRED) ||
      (!(field->opts & O_PASSOK)))
    {
      if (!Check_Field(field->type, field, (TypeArgument *)(field->arg)))
	return FALSE;
      form->status &= ~_FCHECK_REQUIRED;
      field->status |= _CHANGED;
      Synchronize_Linked_Fields(field);
    }
  return TRUE;
}
/*----------------------------------------------------------------------------
  End of Helper routines for Field Validations.
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Routines for Field Validation.
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FV_Validation(FORM * form)
|
|   Description   :  Validate the current field of the form.
|
|   Return Values :  E_OK             - field valid
|                    E_INVALID_FIELD  - field not valid
+--------------------------------------------------------------------------*/
static int
FV_Validation(FORM *form)
{
  T((T_CALLED("FV_Validation(%p)"), form));
  if (_nc_Internal_Validation(form))
    returnCode(E_OK);
  else
    returnCode(E_INVALID_FIELD);
}
/*----------------------------------------------------------------------------
  End of routines for Field Validation.
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Helper routines for Inter-Field Navigation
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Next_Field_On_Page(FIELD * field)
|
|   Description   :  Get the next field after the given field on the current
|                    page. The order of fields is the one defined by the
|                    fields array. Only visible and active fields are
|                    counted.
|
|   Return Values :  Pointer to the next field.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD *
Next_Field_On_Page(FIELD *field)
{
  FORM *form = field->form;
  FIELD **field_on_page = &form->field[field->index];
  FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
  FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];

  do
    {
      field_on_page =
	(field_on_page == last_on_page) ? first_on_page : field_on_page + 1;
      if (Field_Is_Selectable(*field_on_page))
	break;
    }
  while (field != (*field_on_page));
  return (*field_on_page);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  FIELD* _nc_First_Active_Field(FORM * form)
|
|   Description   :  Get the first active field on the current page,
|                    if there are such. If there are none, get the first
|                    visible field on the page. If there are also none,
|                    we return the first field on page and hope the best.
|
|   Return Values :  Pointer to calculated field.
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(FIELD *)
_nc_First_Active_Field(FORM *form)
{
  FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
  FIELD *proposed = Next_Field_On_Page(*last_on_page);

  if (proposed == *last_on_page)
    {
      /* there might be the special situation, where there is no
         active and visible field on the current page. We then select
         the first visible field on this readonly page
       */
      if (Field_Is_Not_Selectable(proposed))
	{
	  FIELD **field = &form->field[proposed->index];
	  FIELD **first = &form->field[form->page[form->curpage].pmin];

	  do
	    {
	      field = (field == last_on_page) ? first : field + 1;
	      if (((*field)->opts & O_VISIBLE))
		break;
	    }
	  while (proposed != (*field));

	  proposed = *field;

	  if ((proposed == *last_on_page) && !(proposed->opts & O_VISIBLE))
	    {
	      /* This means, there is also no visible field on the page.
	         So we propose the first one and hope the very best...
	         Some very clever user has designed a readonly and invisible
	         page on this form.
	       */
	      proposed = *first;
	    }
	}
    }
  return (proposed);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Previous_Field_On_Page(FIELD * field)
|
|   Description   :  Get the previous field before the given field on the
|                    current page. The order of fields is the one defined by
|                    the fields array. Only visible and active fields are
|                    counted.
|
|   Return Values :  Pointer to the previous field.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD *
Previous_Field_On_Page(FIELD *field)
{
  FORM *form = field->form;
  FIELD **field_on_page = &form->field[field->index];
  FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
  FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];

  do
    {
      field_on_page =
	(field_on_page == first_on_page) ? last_on_page : field_on_page - 1;
      if (Field_Is_Selectable(*field_on_page))
	break;
    }
  while (field != (*field_on_page));

  return (*field_on_page);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Sorted_Next_Field(FIELD * field)
|
|   Description   :  Get the next field after the given field on the current
|                    page. The order of fields is the one defined by the
|                    (row,column) geometry, rows are major.
|
|   Return Values :  Pointer to the next field.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD *
Sorted_Next_Field(FIELD *field)
{
  FIELD *field_on_page = field;

  do
    {
      field_on_page = field_on_page->snext;
      if (Field_Is_Selectable(field_on_page))
	break;
    }
  while (field_on_page != field);

  return (field_on_page);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Sorted_Previous_Field(FIELD * field)
|
|   Description   :  Get the previous field before the given field on the
|                    current page. The order of fields is the one defined
|                    by the (row,column) geometry, rows are major.
|
|   Return Values :  Pointer to the previous field.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD *
Sorted_Previous_Field(FIELD *field)
{
  FIELD *field_on_page = field;

  do
    {
      field_on_page = field_on_page->sprev;
      if (Field_Is_Selectable(field_on_page))
	break;
    }
  while (field_on_page != field);

  return (field_on_page);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Left_Neighbor_Field(FIELD * field)
|
|   Description   :  Get the left neighbor of the field on the same line
|                    and the same page. Cycles through the line.
|
|   Return Values :  Pointer to left neighbor field.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD *
Left_Neighbor_Field(FIELD *field)
{
  FIELD *field_on_page = field;

  /* For a field that has really a left neighbor, the while clause
     immediately fails and the loop is left, positioned at the right
     neighbor. Otherwise we cycle backwards through the sorted field list
     until we enter the same line (from the right end).
   */
  do
    {
      field_on_page = Sorted_Previous_Field(field_on_page);
    }
  while (field_on_page->frow != field->frow);

  return (field_on_page);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Right_Neighbor_Field(FIELD * field)
|
|   Description   :  Get the right neighbor of the field on the same line
|                    and the same page.
|
|   Return Values :  Pointer to right neighbor field.
+--------------------------------------------------------------------------*/
NCURSES_INLINE static FIELD *
Right_Neighbor_Field(FIELD *field)
{
  FIELD *field_on_page = field;

  /* See the comments on Left_Neighbor_Field to understand how it works */
  do
    {
      field_on_page = Sorted_Next_Field(field_on_page);
    }
  while (field_on_page->frow != field->frow);

  return (field_on_page);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Upper_Neighbor_Field(FIELD * field)
|
|   Description   :  Because of the row-major nature of sorting the fields,
|                    it is more difficult to define whats the upper neighbor
|                    field really means. We define that it must be on a
|                    'previous' line (cyclic order!) and is the rightmost
|                    field laying on the left side of the given field. If
|                    this set is empty, we take the first field on the line.
|
|   Return Values :  Pointer to the upper neighbor field.
+--------------------------------------------------------------------------*/
static FIELD *
Upper_Neighbor_Field(FIELD *field)
{
  FIELD *field_on_page = field;
  int frow = field->frow;
  int fcol = field->fcol;

  /* Walk back to the 'previous' line. The second term in the while clause
     just guarantees that we stop if we cycled through the line because
     there might be no 'previous' line if the page has just one line.
   */
  do
    {
      field_on_page = Sorted_Previous_Field(field_on_page);
    }
  while (field_on_page->frow == frow && field_on_page->fcol != fcol);

  if (field_on_page->frow != frow)
    {
      /* We really found a 'previous' line. We are positioned at the
         rightmost field on this line */
      frow = field_on_page->frow;

      /* We walk to the left as long as we are really right of the
         field. */
      while (field_on_page->frow == frow && field_on_page->fcol > fcol)
	field_on_page = Sorted_Previous_Field(field_on_page);

      /* If we wrapped, just go to the right which is the first field on
         the row */
      if (field_on_page->frow != frow)
	field_on_page = Sorted_Next_Field(field_on_page);
    }

  return (field_on_page);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static FIELD *Down_Neighbor_Field(FIELD * field)
|
|   Description   :  Because of the row-major nature of sorting the fields,
|                    its more difficult to define whats the down neighbor
|                    field really means. We define that it must be on a
|                    'next' line (cyclic order!) and is the leftmost
|                    field laying on the right side of the given field. If
|                    this set is empty, we take the last field on the line.
|
|   Return Values :  Pointer to the upper neighbor field.
+--------------------------------------------------------------------------*/
static FIELD *
Down_Neighbor_Field(FIELD *field)
{
  FIELD *field_on_page = field;
  int frow = field->frow;
  int fcol = field->fcol;

  /* Walk forward to the 'next' line. The second term in the while clause
     just guarantees that we stop if we cycled through the line because
     there might be no 'next' line if the page has just one line.
   */
  do
    {
      field_on_page = Sorted_Next_Field(field_on_page);
    }
  while (field_on_page->frow == frow && field_on_page->fcol != fcol);

  if (field_on_page->frow != frow)
    {
      /* We really found a 'next' line. We are positioned at the rightmost
         field on this line */
      frow = field_on_page->frow;

      /* We walk to the right as long as we are really left of the
         field. */
      while (field_on_page->frow == frow && field_on_page->fcol < fcol)
	field_on_page = Sorted_Next_Field(field_on_page);

      /* If we wrapped, just go to the left which is the last field on
         the row */
      if (field_on_page->frow != frow)
	field_on_page = Sorted_Previous_Field(field_on_page);
    }

  return (field_on_page);
}

/*----------------------------------------------------------------------------
  Inter-Field Navigation routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Inter_Field_Navigation(
|                                           int (* const fct) (FORM *),
|                                           FORM * form)
|
|   Description   :  Generic behavior for changing the current field, the
|                    field is left and a new field is entered. So the field
|                    must be validated and the field init/term hooks must
|                    be called.
|
|   Return Values :  E_OK                - success
|                    E_INVALID_FIELD     - field is invalid
|                    some other          - error from subordinate call
+--------------------------------------------------------------------------*/
static int
Inter_Field_Navigation(int (*const fct) (FORM *), FORM *form)
{
  int res;

  if (!_nc_Internal_Validation(form))
    res = E_INVALID_FIELD;
  else
    {
      Call_Hook(form, fieldterm);
      res = fct(form);
      Call_Hook(form, fieldinit);
    }
  return res;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Next_Field(FORM * form)
|
|   Description   :  Move to the next field on the current page of the form
|
|   Return Values :  E_OK                 - success
|                    != E_OK              - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Next_Field(FORM *form)
{
  T((T_CALLED("FN_Next_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Next_Field_On_Page(form->current)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Previous_Field(FORM * form)
|
|   Description   :  Move to the previous field on the current page of the
|                    form
|
|   Return Values :  E_OK                 - success
|                    != E_OK              - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Previous_Field(FORM *form)
{
  T((T_CALLED("FN_Previous_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Previous_Field_On_Page(form->current)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_First_Field(FORM * form)
|
|   Description   :  Move to the first field on the current page of the form
|
|   Return Values :  E_OK                 - success
|                    != E_OK              - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_First_Field(FORM *form)
{
  T((T_CALLED("FN_First_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Next_Field_On_Page(form->field[form->page[form->curpage].pmax])));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Last_Field(FORM * form)
|
|   Description   :  Move to the last field on the current page of the form
|
|   Return Values :  E_OK                 - success
|                    != E_OK              - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Last_Field(FORM *form)
{
  T((T_CALLED("FN_Last_Field(%p)"), form));
  returnCode(
	      _nc_Set_Current_Field(form,
				    Previous_Field_On_Page(form->field[form->page[form->curpage].pmin])));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Sorted_Next_Field(FORM * form)
|
|   Description   :  Move to the sorted next field on the current page
|                    of the form.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Sorted_Next_Field(FORM *form)
{
  T((T_CALLED("FN_Sorted_Next_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Sorted_Next_Field(form->current)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Sorted_Previous_Field(FORM * form)
|
|   Description   :  Move to the sorted previous field on the current page
|                    of the form.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Sorted_Previous_Field(FORM *form)
{
  T((T_CALLED("FN_Sorted_Previous_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Sorted_Previous_Field(form->current)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Sorted_First_Field(FORM * form)
|
|   Description   :  Move to the sorted first field on the current page
|                    of the form.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Sorted_First_Field(FORM *form)
{
  T((T_CALLED("FN_Sorted_First_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Sorted_Next_Field(form->field[form->page[form->curpage].smax])));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Sorted_Last_Field(FORM * form)
|
|   Description   :  Move to the sorted last field on the current page
|                    of the form.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Sorted_Last_Field(FORM *form)
{
  T((T_CALLED("FN_Sorted_Last_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Sorted_Previous_Field(form->field[form->page[form->curpage].smin])));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Left_Field(FORM * form)
|
|   Description   :  Get the field on the left of the current field on the
|                    same line and the same page. Cycles through the line.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Left_Field(FORM *form)
{
  T((T_CALLED("FN_Left_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Left_Neighbor_Field(form->current)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Right_Field(FORM * form)
|
|   Description   :  Get the field on the right of the current field on the
|                    same line and the same page. Cycles through the line.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Right_Field(FORM *form)
{
  T((T_CALLED("FN_Right_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Right_Neighbor_Field(form->current)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Up_Field(FORM * form)
|
|   Description   :  Get the upper neighbor of the current field. This
|                    cycles through the page. See the comments of the
|                    Upper_Neighbor_Field function to understand how
|                    'upper' is defined.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Up_Field(FORM *form)
{
  T((T_CALLED("FN_Up_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Upper_Neighbor_Field(form->current)));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int FN_Down_Field(FORM * form)
|
|   Description   :  Get the down neighbor of the current field. This
|                    cycles through the page. See the comments of the
|                    Down_Neighbor_Field function to understand how
|                    'down' is defined.
|
|   Return Values :  E_OK            - success
|                    != E_OK         - error from subordinate call
+--------------------------------------------------------------------------*/
static int
FN_Down_Field(FORM *form)
{
  T((T_CALLED("FN_Down_Field(%p)"), form));
  returnCode(_nc_Set_Current_Field(form,
				   Down_Neighbor_Field(form->current)));
}
/*----------------------------------------------------------------------------
  END of Field Navigation routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Helper routines for Page Navigation
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int _nc_Set_Form_Page(FORM * form,
|                                          int page,
|                                          FIELD * field)
|
|   Description   :  Make the given page number the current page and make
|                    the given field the current field on the page. If
|                    for the field NULL is given, make the first field on
|                    the page the current field. The routine acts only
|                    if the requested page is not the current page.
|
|   Return Values :  E_OK                - success
|                    != E_OK             - error from subordinate call
|                    E_BAD_ARGUMENT      - invalid field pointer
|                    E_SYSTEM_ERROR      - some severe basic error
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
_nc_Set_Form_Page(FORM *form, int page, FIELD *field)
{
  int res = E_OK;

  if ((form->curpage != page))
    {
      FIELD *last_field, *field_on_page;

      werase(Get_Form_Window(form));
      form->curpage = page;
      last_field = field_on_page = form->field[form->page[page].smin];
      do
	{
	  if (field_on_page->opts & O_VISIBLE)
	    if ((res = Display_Field(field_on_page)) != E_OK)
	      return (res);
	  field_on_page = field_on_page->snext;
	}
      while (field_on_page != last_field);

      if (field)
	res = _nc_Set_Current_Field(form, field);
      else
	/* N.B.: we don't encapsulate this by Inter_Field_Navigation(),
	   because this is already executed in a page navigation
	   context that contains field navigation
	 */
	res = FN_First_Field(form);
    }
  return (res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Next_Page_Number(const FORM * form)
|
|   Description   :  Calculate the page number following the current page
|                    number. This cycles if the highest page number is
|                    reached.
|
|   Return Values :  The next page number
+--------------------------------------------------------------------------*/
NCURSES_INLINE static int
Next_Page_Number(const FORM *form)
{
  return (form->curpage + 1) % form->maxpage;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Previous_Page_Number(const FORM * form)
|
|   Description   :  Calculate the page number before the current page
|                    number. This cycles if the first page number is
|                    reached.
|
|   Return Values :  The previous page number
+--------------------------------------------------------------------------*/
NCURSES_INLINE static int
Previous_Page_Number(const FORM *form)
{
  return (form->curpage != 0 ? form->curpage - 1 : form->maxpage - 1);
}

/*----------------------------------------------------------------------------
  Page Navigation routines
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Page_Navigation(
|                                               int (* const fct) (FORM *),
|                                               FORM * form)
|
|   Description   :  Generic behavior for changing a page. This means
|                    that the field is left and a new field is entered.
|                    So the field must be validated and the field init/term
|                    hooks must be called. Because also the page is changed,
|                    the forms init/term hooks must be called also.
|
|   Return Values :  E_OK                - success
|                    E_INVALID_FIELD     - field is invalid
|                    some other          - error from subordinate call
+--------------------------------------------------------------------------*/
static int
Page_Navigation(int (*const fct) (FORM *), FORM *form)
{
  int res;

  if (!_nc_Internal_Validation(form))
    res = E_INVALID_FIELD;
  else
    {
      Call_Hook(form, fieldterm);
      Call_Hook(form, formterm);
      res = fct(form);
      Call_Hook(form, forminit);
      Call_Hook(form, fieldinit);
    }
  return res;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int PN_Next_Page(FORM * form)
|
|   Description   :  Move to the next page of the form
|
|   Return Values :  E_OK                - success
|                    != E_OK             - error from subordinate call
+--------------------------------------------------------------------------*/
static int
PN_Next_Page(FORM *form)
{
  T((T_CALLED("PN_Next_Page(%p)"), form));
  returnCode(_nc_Set_Form_Page(form, Next_Page_Number(form), (FIELD *)0));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int PN_Previous_Page(FORM * form)
|
|   Description   :  Move to the previous page of the form
|
|   Return Values :  E_OK              - success
|                    != E_OK           - error from subordinate call
+--------------------------------------------------------------------------*/
static int
PN_Previous_Page(FORM *form)
{
  T((T_CALLED("PN_Previous_Page(%p)"), form));
  returnCode(_nc_Set_Form_Page(form, Previous_Page_Number(form), (FIELD *)0));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int PN_First_Page(FORM * form)
|
|   Description   :  Move to the first page of the form
|
|   Return Values :  E_OK              - success
|                    != E_OK           - error from subordinate call
+--------------------------------------------------------------------------*/
static int
PN_First_Page(FORM *form)
{
  T((T_CALLED("PN_First_Page(%p)"), form));
  returnCode(_nc_Set_Form_Page(form, 0, (FIELD *)0));
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int PN_Last_Page(FORM * form)
|
|   Description   :  Move to the last page of the form
|
|   Return Values :  E_OK              - success
|                    != E_OK           - error from subordinate call
+--------------------------------------------------------------------------*/
static int
PN_Last_Page(FORM *form)
{
  T((T_CALLED("PN_Last_Page(%p)"), form));
  returnCode(_nc_Set_Form_Page(form, form->maxpage - 1, (FIELD *)0));
}

/*----------------------------------------------------------------------------
  END of Field Navigation routines
  --------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Helper routines for the core form driver.
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  static int Data_Entry(FORM * form,int c)
|
|   Description   :  Enter character c into at the current position of the
|                    current field of the form.
|
|   Return Values :  E_OK              - success
|                    E_REQUEST_DENIED  - driver could not process the request
|                    E_SYSTEM_ERROR    -
+--------------------------------------------------------------------------*/
static int
Data_Entry(FORM *form, int c)
{
  FIELD *field = form->current;
  int result = E_REQUEST_DENIED;

  T((T_CALLED("Data_Entry(%p,%s)"), form, _tracechtype((chtype)c)));
  if ((field->opts & O_EDIT)
#if FIX_FORM_INACTIVE_BUG
      && (field->opts & O_ACTIVE)
#endif
    )
    {
      if ((field->opts & O_BLANK) &&
	  First_Position_In_Current_Field(form) &&
	  !(form->status & _FCHECK_REQUIRED) &&
	  !(form->status & _WINDOW_MODIFIED))
	werase(form->w);

      if (form->status & _OVLMODE)
	{
	  waddch(form->w, (chtype)c);
	}
      else
	/* no _OVLMODE */
	{
	  bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);

	  if (!(There_Is_Room ||
		((Single_Line_Field(field) && Growable(field)))))
	    RETURN(E_REQUEST_DENIED);

	  if (!There_Is_Room && !Field_Grown(field, 1))
	    RETURN(E_SYSTEM_ERROR);

	  winsch(form->w, (chtype)c);
	}

      if ((result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form)) == E_OK)
	{
	  bool End_Of_Field = (((field->drows - 1) == form->currow) &&
			       ((field->dcols - 1) == form->curcol));

	  form->status |= _WINDOW_MODIFIED;
	  if (End_Of_Field && !Growable(field) && (field->opts & O_AUTOSKIP))
	    result = Inter_Field_Navigation(FN_Next_Field, form);
	  else
	    {
	      if (End_Of_Field && Growable(field) && !Field_Grown(field, 1))
		result = E_SYSTEM_ERROR;
	      else
		{
#if USE_WIDEC_SUPPORT
		  /*
		   * We have just added a byte to the form field.  It may have
		   * been part of a multibyte character.  If it was, the
		   * addch_used field is nonzero and we should not try to move
		   * to a new column.
		   */
		  if (WINDOW_EXT(form->w, addch_used) == 0)
		    IFN_Next_Character(form);
#else
		  IFN_Next_Character(form);
#endif
		  result = E_OK;
		}
	    }
	}
    }
  RETURN(result);
}

/* Structure to describe the binding of a request code to a function.
   The member keycode codes the request value as well as the generic
   routine to use for the request. The code for the generic routine
   is coded in the upper 16 Bits while the request code is coded in
   the lower 16 bits.

   In terms of C++ you might think of a request as a class with a
   virtual method "perform". The different types of request are
   derived from this base class and overload (or not) the base class
   implementation of perform.
*/
typedef struct
{
  int keycode;			/* must be at least 32 bit: hi:mode, lo: key */
  int (*cmd) (FORM *);		/* low level driver routine for this key     */
}
Binding_Info;

/* You may see this is the class-id of the request type class */
#define ID_PN    (0x00000000)	/* Page navigation           */
#define ID_FN    (0x00010000)	/* Inter-Field navigation    */
#define ID_IFN   (0x00020000)	/* Intra-Field navigation    */
#define ID_VSC   (0x00030000)	/* Vertical Scrolling        */
#define ID_HSC   (0x00040000)	/* Horizontal Scrolling      */
#define ID_FE    (0x00050000)	/* Field Editing             */
#define ID_EM    (0x00060000)	/* Edit Mode                 */
#define ID_FV    (0x00070000)	/* Field Validation          */
#define ID_CH    (0x00080000)	/* Choice                    */
#define ID_Mask  (0xffff0000)
#define Key_Mask (0x0000ffff)
#define ID_Shft  (16)

/* This array holds all the Binding Infos */
/* *INDENT-OFF* */
static const Binding_Info bindings[MAX_FORM_COMMAND - MIN_FORM_COMMAND + 1] =
{
  { REQ_NEXT_PAGE    |ID_PN  ,PN_Next_Page},
  { REQ_PREV_PAGE    |ID_PN  ,PN_Previous_Page},
  { REQ_FIRST_PAGE   |ID_PN  ,PN_First_Page},
  { REQ_LAST_PAGE    |ID_PN  ,PN_Last_Page},

  { REQ_NEXT_FIELD   |ID_FN  ,FN_Next_Field},
  { REQ_PREV_FIELD   |ID_FN  ,FN_Previous_Field},
  { REQ_FIRST_FIELD  |ID_FN  ,FN_First_Field},
  { REQ_LAST_FIELD   |ID_FN  ,FN_Last_Field},
  { REQ_SNEXT_FIELD  |ID_FN  ,FN_Sorted_Next_Field},
  { REQ_SPREV_FIELD  |ID_FN  ,FN_Sorted_Previous_Field},
  { REQ_SFIRST_FIELD |ID_FN  ,FN_Sorted_First_Field},
  { REQ_SLAST_FIELD  |ID_FN  ,FN_Sorted_Last_Field},
  { REQ_LEFT_FIELD   |ID_FN  ,FN_Left_Field},
  { REQ_RIGHT_FIELD  |ID_FN  ,FN_Right_Field},
  { REQ_UP_FIELD     |ID_FN  ,FN_Up_Field},
  { REQ_DOWN_FIELD   |ID_FN  ,FN_Down_Field},

  { REQ_NEXT_CHAR    |ID_IFN ,IFN_Next_Character},
  { REQ_PREV_CHAR    |ID_IFN ,IFN_Previous_Character},
  { REQ_NEXT_LINE    |ID_IFN ,IFN_Next_Line},
  { REQ_PREV_LINE    |ID_IFN ,IFN_Previous_Line},
  { REQ_NEXT_WORD    |ID_IFN ,IFN_Next_Word},
  { REQ_PREV_WORD    |ID_IFN ,IFN_Previous_Word},
  { REQ_BEG_FIELD    |ID_IFN ,IFN_Beginning_Of_Field},
  { REQ_END_FIELD    |ID_IFN ,IFN_End_Of_Field},
  { REQ_BEG_LINE     |ID_IFN ,IFN_Beginning_Of_Line},
  { REQ_END_LINE     |ID_IFN ,IFN_End_Of_Line},
  { REQ_LEFT_CHAR    |ID_IFN ,IFN_Left_Character},
  { REQ_RIGHT_CHAR   |ID_IFN ,IFN_Right_Character},
  { REQ_UP_CHAR      |ID_IFN ,IFN_Up_Character},
  { REQ_DOWN_CHAR    |ID_IFN ,IFN_Down_Character},

  { REQ_NEW_LINE     |ID_FE  ,FE_New_Line},
  { REQ_INS_CHAR     |ID_FE  ,FE_Insert_Character},
  { REQ_INS_LINE     |ID_FE  ,FE_Insert_Line},
  { REQ_DEL_CHAR     |ID_FE  ,FE_Delete_Character},
  { REQ_DEL_PREV     |ID_FE  ,FE_Delete_Previous},
  { REQ_DEL_LINE     |ID_FE  ,FE_Delete_Line},
  { REQ_DEL_WORD     |ID_FE  ,FE_Delete_Word},
  { REQ_CLR_EOL      |ID_FE  ,FE_Clear_To_End_Of_Line},
  { REQ_CLR_EOF      |ID_FE  ,FE_Clear_To_End_Of_Field},
  { REQ_CLR_FIELD    |ID_FE  ,FE_Clear_Field},

  { REQ_OVL_MODE     |ID_EM  ,EM_Overlay_Mode},
  { REQ_INS_MODE     |ID_EM  ,EM_Insert_Mode},

  { REQ_SCR_FLINE    |ID_VSC ,VSC_Scroll_Line_Forward},
  { REQ_SCR_BLINE    |ID_VSC ,VSC_Scroll_Line_Backward},
  { REQ_SCR_FPAGE    |ID_VSC ,VSC_Scroll_Page_Forward},
  { REQ_SCR_BPAGE    |ID_VSC ,VSC_Scroll_Page_Backward},
  { REQ_SCR_FHPAGE   |ID_VSC ,VSC_Scroll_Half_Page_Forward},
  { REQ_SCR_BHPAGE   |ID_VSC ,VSC_Scroll_Half_Page_Backward},

  { REQ_SCR_FCHAR    |ID_HSC ,HSC_Scroll_Char_Forward},
  { REQ_SCR_BCHAR    |ID_HSC ,HSC_Scroll_Char_Backward},
  { REQ_SCR_HFLINE   |ID_HSC ,HSC_Horizontal_Line_Forward},
  { REQ_SCR_HBLINE   |ID_HSC ,HSC_Horizontal_Line_Backward},
  { REQ_SCR_HFHALF   |ID_HSC ,HSC_Horizontal_Half_Line_Forward},
  { REQ_SCR_HBHALF   |ID_HSC ,HSC_Horizontal_Half_Line_Backward},

  { REQ_VALIDATION   |ID_FV  ,FV_Validation},

  { REQ_NEXT_CHOICE  |ID_CH  ,CR_Next_Choice},
  { REQ_PREV_CHOICE  |ID_CH  ,CR_Previous_Choice}
};
/* *INDENT-ON* */

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int form_driver(FORM * form,int  c)
|
|   Description   :  This is the workhorse of the forms system. It checks
|                    to determine whether the character c is a request or
|                    data. If it is a request, the form driver executes
|                    the request and returns the result. If it is data
|                    (printable character), it enters the data into the
|                    current position in the current field. If it is not
|                    recognized, the form driver assumes it is an application
|                    defined command and returns E_UNKNOWN_COMMAND.
|                    Application defined command should be defined relative
|                    to MAX_FORM_COMMAND, the maximum value of a request.
|
|   Return Values :  E_OK              - success
|                    E_SYSTEM_ERROR    - system error
|                    E_BAD_ARGUMENT    - an argument is incorrect
|                    E_NOT_POSTED      - form is not posted
|                    E_INVALID_FIELD   - field contents are invalid
|                    E_BAD_STATE       - called from inside a hook routine
|                    E_REQUEST_DENIED  - request failed
|                    E_NOT_CONNECTED   - no fields are connected to the form
|                    E_UNKNOWN_COMMAND - command not known
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
form_driver(FORM *form, int c)
{
  const Binding_Info *BI = (Binding_Info *) 0;
  int res = E_UNKNOWN_COMMAND;

  T((T_CALLED("form_driver(%p,%d)"), form, c));

  if (!form)
    RETURN(E_BAD_ARGUMENT);

  if (!(form->field))
    RETURN(E_NOT_CONNECTED);

  assert(form->page);

  if (c == FIRST_ACTIVE_MAGIC)
    {
      form->current = _nc_First_Active_Field(form);
      RETURN(E_OK);
    }

  assert(form->current &&
	 form->current->buf &&
	 (form->current->form == form)
    );

  if (form->status & _IN_DRIVER)
    RETURN(E_BAD_STATE);

  if (!(form->status & _POSTED))
    RETURN(E_NOT_POSTED);

  if ((c >= MIN_FORM_COMMAND && c <= MAX_FORM_COMMAND) &&
      ((bindings[c - MIN_FORM_COMMAND].keycode & Key_Mask) == c))
    BI = &(bindings[c - MIN_FORM_COMMAND]);

  if (BI)
    {
      typedef int (*Generic_Method) (int (*const) (FORM *), FORM *);
      static const Generic_Method Generic_Methods[] =
      {
	Page_Navigation,	/* overloaded to call field&form hooks */
	Inter_Field_Navigation,	/* overloaded to call field hooks      */
	NULL,			/* Intra-Field is generic              */
	Vertical_Scrolling,	/* Overloaded to check multi-line      */
	Horizontal_Scrolling,	/* Overloaded to check single-line     */
	Field_Editing,		/* Overloaded to mark modification     */
	NULL,			/* Edit Mode is generic                */
	NULL,			/* Field Validation is generic         */
	NULL			/* Choice Request is generic           */
      };
      size_t nMethods = (sizeof(Generic_Methods) / sizeof(Generic_Methods[0]));
      size_t method = ((BI->keycode & ID_Mask) >> ID_Shft) & 0xffff;

      if ((method >= nMethods) || !(BI->cmd))
	res = E_SYSTEM_ERROR;
      else
	{
	  Generic_Method fct = Generic_Methods[method];

	  if (fct)
	    res = fct(BI->cmd, form);
	  else
	    res = (BI->cmd) (form);
	}
    }
#ifdef NCURSES_MOUSE_VERSION
  else if (KEY_MOUSE == c)
    {
      MEVENT event;
      WINDOW *win = form->win ? form->win : stdscr;
      WINDOW *sub = form->sub ? form->sub : win;

      getmouse(&event);
      if ((event.bstate & (BUTTON1_CLICKED |
			   BUTTON1_DOUBLE_CLICKED |
			   BUTTON1_TRIPLE_CLICKED))
	  && wenclose(win, event.y, event.x))
	{			/* we react only if the click was in the userwin, that means
				 * inside the form display area or at the decoration window.
				 */
	  int ry = event.y, rx = event.x;	/* screen coordinates */

	  res = E_REQUEST_DENIED;
	  if (mouse_trafo(&ry, &rx, FALSE))
	    {			/* rx, ry are now "curses" coordinates */
	      if (ry < sub->_begy)
		{		/* we clicked above the display region; this is
				 * interpreted as "scroll up" request
				 */
		  if (event.bstate & BUTTON1_CLICKED)
		    res = form_driver(form, REQ_PREV_FIELD);
		  else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
		    res = form_driver(form, REQ_PREV_PAGE);
		  else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
		    res = form_driver(form, REQ_FIRST_FIELD);
		}
	      else if (ry > sub->_begy + sub->_maxy)
		{		/* we clicked below the display region; this is
				 * interpreted as "scroll down" request
				 */
		  if (event.bstate & BUTTON1_CLICKED)
		    res = form_driver(form, REQ_NEXT_FIELD);
		  else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
		    res = form_driver(form, REQ_NEXT_PAGE);
		  else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
		    res = form_driver(form, REQ_LAST_FIELD);
		}
	      else if (wenclose(sub, event.y, event.x))
		{		/* Inside the area we try to find the hit item */
		  int i;

		  ry = event.y;
		  rx = event.x;
		  if (wmouse_trafo(sub, &ry, &rx, FALSE))
		    {
		      int min_field = form->page[form->curpage].pmin;
		      int max_field = form->page[form->curpage].pmax;

		      for (i = min_field; i <= max_field; ++i)
			{
			  FIELD *field = form->field[i];

			  if (Field_Is_Selectable(field)
			      && Field_encloses(field, ry, rx) == E_OK)
			    {
			      res = _nc_Set_Current_Field(form, field);
			      if (res == E_OK)
				res = _nc_Position_Form_Cursor(form);
			      if (res == E_OK
				  && (event.bstate & BUTTON1_DOUBLE_CLICKED))
				res = E_UNKNOWN_COMMAND;
			      break;
			    }
			}
		    }
		}
	    }
	}
      else
	res = E_REQUEST_DENIED;
    }
#endif /* NCURSES_MOUSE_VERSION */
  else if (!(c & (~(int)MAX_REGULAR_CHARACTER)))
    {
      /*
       * If we're using 8-bit characters, iscntrl+isprint cover the whole set.
       * But with multibyte characters, there is a third possibility, i.e.,
       * parts of characters that build up into printable characters which are
       * not considered printable.
       *
       * FIXME: the wide-character branch should also use Check_Char().
       */
#if USE_WIDEC_SUPPORT
      if (!iscntrl(UChar(c)))
#else
      if (isprint(UChar(c)) &&
	  Check_Char(form->current->type, c,
		     (TypeArgument *)(form->current->arg)))
#endif
	res = Data_Entry(form, c);
    }
  _nc_Refresh_Current_Field(form);
  RETURN(res);
}

/*----------------------------------------------------------------------------
  Field-Buffer manipulation routines.
  The effects of setting a buffer are tightly coupled to the core of the form
  driver logic. This is especially true in the case of growable fields.
  So I don't separate this into a separate module.
  --------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  int set_field_buffer(FIELD *field,
|                                         int buffer, char *value)
|
|   Description   :  Set the given buffer of the field to the given value.
|                    Buffer 0 stores the displayed content of the field.
|                    For dynamic fields this may grow the fieldbuffers if
|                    the length of the value exceeds the current buffer
|                    length. For buffer 0 only printable values are allowed.
|                    For static fields, the value needs not to be zero ter-
|                    minated. It is copied up to the length of the buffer.
|
|   Return Values :  E_OK            - success
|                    E_BAD_ARGUMENT  - invalid argument
|                    E_SYSTEM_ERROR  - system error
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
set_field_buffer(FIELD *field, int buffer, const char *value)
{
  FIELD_CELL *p;
  int res = E_OK;
  unsigned int i;
  unsigned int len;

#if USE_WIDEC_SUPPORT
  FIELD_CELL *widevalue = 0;
#endif

  T((T_CALLED("set_field_buffer(%p,%d,%s)"), field, buffer, _nc_visbuf(value)));

  if (!field || !value || ((buffer < 0) || (buffer > field->nbuf)))
    RETURN(E_BAD_ARGUMENT);

  len = Buffer_Length(field);

  if (buffer == 0)
    {
      for (i = 0; (value[i] != '\0') && (i < len); ++i)
	{
	  if (iscntrl(UChar(value[i])))
	    RETURN(E_BAD_ARGUMENT);
	}
    }

  if (Growable(field))
    {
      /* for a growable field we must assume zero terminated strings, because
         somehow we have to detect the length of what should be copied.
       */
      unsigned int vlen = strlen(value);

      if (vlen > len)
	{
	  if (!Field_Grown(field,
			   (int)(1 + (vlen - len) / ((field->rows + field->nrow)
						     * field->cols))))
	    RETURN(E_SYSTEM_ERROR);

	  /* in this case we also have to check, whether or not the remaining
	     characters in value are also printable for buffer 0. */
	  if (buffer == 0)
	    {
	      for (i = len; i < vlen; i++)
		if (iscntrl(UChar(value[i])))
		  RETURN(E_BAD_ARGUMENT);
	    }
	  len = vlen;
	}
    }

  p = Address_Of_Nth_Buffer(field, buffer);

#if USE_WIDEC_SUPPORT
  /*
   * Use addstr's logic for converting a string to an array of cchar_t's.
   * There should be a better way, but this handles nonspacing characters
   * and other special cases that we really do not want to handle here.
   */
  wclear(field->working);
  mvwaddstr(field->working, 0, 0, value);

  if ((widevalue = typeCalloc(FIELD_CELL, len + 1)) == 0)
    {
      RETURN(E_SYSTEM_ERROR);
    }
  else
    {
      mvwin_wchnstr(field->working, 0, 0, widevalue, (int)len);
      for (i = 0; i < len; ++i)
	{
	  if (CharEq(myZEROS, widevalue[i]))
	    {
	      while (i < len)
		p[i++] = myBLANK;
	      break;
	    }
	  p[i] = widevalue[i];
	}
      free(widevalue);
    }
#else
  for (i = 0; i < len; ++i)
    {
      if (value[i] == '\0')
	{
	  while (i < len)
	    p[i++] = myBLANK;
	  break;
	}
      p[i] = value[i];
    }
#endif

  if (buffer == 0)
    {
      int syncres;

      if (((syncres = Synchronize_Field(field)) != E_OK) &&
	  (res == E_OK))
	res = syncres;
      if (((syncres = Synchronize_Linked_Fields(field)) != E_OK) &&
	  (res == E_OK))
	res = syncres;
    }
  RETURN(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  char *field_buffer(const FIELD *field,int buffer)
|
|   Description   :  Return the address of the buffer for the field.
|
|   Return Values :  Pointer to buffer or NULL if arguments were invalid.
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(char *)
field_buffer(const FIELD *field, int buffer)
{
  char *result = 0;

  T((T_CALLED("field_buffer(%p,%d)"), field, buffer));

  if (field && (buffer >= 0) && (buffer <= field->nbuf))
    {
#if USE_WIDEC_SUPPORT
      FIELD_CELL *data = Address_Of_Nth_Buffer(field, buffer);
      unsigned need = 0;
      int size = Buffer_Length(field);
      int n;

      /* determine the number of bytes needed to store the expanded string */
      for (n = 0; n < size; ++n)
	{
	  if (!isWidecExt(data[n]))
	    {
	      mbstate_t state;
	      size_t next;

	      init_mb(state);
	      next = _nc_wcrtomb(0, data[n].chars[0], &state);
	      if (!isEILSEQ(next))
		need += next;
	    }
	}

      /* allocate a place to store the expanded string */
      if (field->expanded[buffer] != 0)
	free(field->expanded[buffer]);
      field->expanded[buffer] = typeMalloc(char, need + 1);

      /* expand the multibyte data */
      if ((result = field->expanded[buffer]) != 0)
	{
	  wclear(field->working);
	  mvwadd_wchnstr(field->working, 0, 0, data, size);
	  mvwinnstr(field->working, 0, 0, result, (int)need);
	}
#else
      result = Address_Of_Nth_Buffer(field, buffer);
#endif
    }
  returnPtr(result);
}

#if USE_WIDEC_SUPPORT

/* FIXME: see lib_get_wch.c */
#if HAVE_MBTOWC && HAVE_MBLEN
#define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
#define count_mbytes(buffer,length,state) mblen(buffer,length)
#define trans_mbytes(wch,buffer,length,state) \
	(int) mbtowc(&wch, buffer, length)
#elif HAVE_MBRTOWC && HAVE_MBRLEN
#define NEED_STATE
#define reset_mbytes(state) init_mb(state)
#define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
#define trans_mbytes(wch,buffer,length,state) \
	(int) mbrtowc(&wch, buffer, length, &state)
#else
make an error
#endif

/*---------------------------------------------------------------------------
| Convert a multibyte string to a wide-character string.  The result must be
| freed by the caller.
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(wchar_t *)
_nc_Widen_String(char *source, int *lengthp)
{
  wchar_t *result = 0;
  wchar_t wch;
  size_t given = strlen(source);
  size_t tries;
  int pass;
  int status;

#ifdef NEED_STATE
  mbstate_t state;
#endif

  for (pass = 0; pass < 2; ++pass)
    {
      unsigned need = 0;
      size_t passed = 0;

      while (passed < given)
	{
	  bool found = FALSE;

	  for (tries = 1, status = 0; tries <= (given - passed); ++tries)
	    {
	      int save = source[passed + tries];

	      source[passed + tries] = 0;
	      reset_mbytes(state);
	      status = trans_mbytes(wch, source + passed, tries, state);
	      source[passed + tries] = save;

	      if (status > 0)
		{
		  found = TRUE;
		  break;
		}
	    }
	  if (found)
	    {
	      if (pass)
		{
		  result[need] = wch;
		}
	      passed += status;
	      ++need;
	    }
	  else
	    {
	      if (pass)
		{
		  result[need] = source[passed];
		}
	      ++need;
	      ++passed;
	    }
	}

      if (!pass)
	{
	  if (!need)
	    break;
	  result = typeCalloc(wchar_t, need);

	  *lengthp = need;
	  if (result == 0)
	    break;
	}
    }

  return result;
}
#endif

/* frm_driver.c ends here */