File: macwin.c

package info (click to toggle)
nethack 3.4.3-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,512 kB
  • ctags: 16,957
  • sloc: ansic: 196,792; cpp: 7,083; sh: 6,785; yacc: 2,005; lex: 377; makefile: 120; awk: 89; sed: 11
file content (2650 lines) | stat: -rw-r--r-- 63,995 bytes parent folder | download | duplicates (12)
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
/*	SCCS Id: @(#)macwin.c	3.4	1996/01/15	*/
/* Copyright (c) Jon W{tte, Hao-Yang Wang, Jonathan Handler 1992. */
/* NetHack may be freely redistributed.  See license for details. */

#include "hack.h"
#include "func_tab.h"
#include "macwin.h"
#include "mactty.h"
#include "wintty.h"

#if !TARGET_API_MAC_CARBON
#include <LowMem.h>
#include <AppleEvents.h>
#include <Gestalt.h>
#include <TextUtils.h>
#include <DiskInit.h>
#include <ControlDefinitions.h>
#endif

NhWindow *theWindows = (NhWindow *) 0;
Cursor qdarrow;

/* Borrowed from the Mac tty port */
extern WindowPtr _mt_window;

/* Some useful #defines for the scroll bar width and height */
#define		SBARWIDTH	15
#define		SBARHEIGHT	15

/*
 * We put a TE on the message window for the "top line" queries.
 * top_line is the TE that holds both the query and the user's
 * response.  The first topl_query_len characters in top_line are
 * the query, the rests are the response.  topl_resp is the valid
 * response to a yn query, while topl_resp[topl_def_idx] is the
 * default response to a yn query.
 */
static TEHandle top_line = (TEHandle) nil;
static int	topl_query_len;
static int	topl_def_idx = -1;
static char	topl_resp[10] = "";

#define CHAR_ANY '\n'

/*
 * inSelect means we have a menu window up for selection or
 * something similar. It makes the window with win number ==
 * inSelect a movable modal (unfortunately without the border)
 * and clicking the close box forces an RET into the key
 * buffer. Don't forget to set inSelect to WIN_ERR when you're
 * done...
 */
static winid inSelect = WIN_ERR;

/*
 * The key queue ring buffer where Read is where to take from, 
 * Write is where next char goes and count is queue depth.
 */
static unsigned char keyQueue [QUEUE_LEN];
static int keyQueueRead = 0,
	keyQueueWrite = 0,
	keyQueueCount = 0;

static Boolean gClickedToMove = 0;	/* For ObscureCursor */

static Point clicked_pos;	/* For nh_poskey */
static int clicked_mod;
static Boolean cursor_locked = false;

static ControlActionUPP MoveScrollUPP;		/* scrolling callback, init'ed in InitMac */

void
lock_mouse_cursor(Boolean new_cursor_locked) {
	cursor_locked = new_cursor_locked;
}


/*
 * Add key to input queue, force means flush left and replace if full
 */
void
AddToKeyQueue (unsigned char ch, Boolean force) {
	if (keyQueueCount < QUEUE_LEN) {
		keyQueue [keyQueueWrite++] = ch;
		keyQueueCount++;
	}
	else if (force) {
		keyQueue [keyQueueWrite++] = ch;
		keyQueueRead++;
		if (keyQueueRead >= QUEUE_LEN)
			keyQueueRead = 0;
		keyQueueCount = QUEUE_LEN;
	}
	if (keyQueueWrite >= QUEUE_LEN)
		keyQueueWrite = 0;
}


/*
 * Get key from queue
 */
unsigned char
GetFromKeyQueue (void) {
	unsigned char ret;

	if (keyQueueCount) {
		ret = keyQueue [keyQueueRead++];
		keyQueueCount--;
		if (keyQueueRead >= QUEUE_LEN)
			keyQueueRead = 0;
	}
	else
		ret = 0;
	return ret;
}


/*
 * Cursor movement
 */
static RgnHandle gMouseRgn = (RgnHandle) 0;

/*
 * _Gestalt madness - we rely heavily on the _Gestalt glue, since we
 * don't check for the trap...
 */
MacFlags macFlags;

/*
 * The screen layouts on the small 512x342 screen need special cares.
 */
Boolean small_screen = 0;

#ifdef NHW_BASE
# undef NHW_BASE
#endif
#define NHW_BASE 0

static int FDECL(filter_scroll_key,(const int, NhWindow *));

static void FDECL(GeneralKey, (EventRecord *, WindowPtr));
static void FDECL(macKeyMenu, (EventRecord *, WindowPtr));
static void FDECL(macKeyText, (EventRecord *, WindowPtr));

static void FDECL(macClickMessage, (EventRecord *, WindowPtr));
static void FDECL(macClickTerm, (EventRecord *, WindowPtr));
static void FDECL(macClickMenu, (EventRecord *, WindowPtr));
static void FDECL(macClickText, (EventRecord *, WindowPtr));

static short FDECL(macDoNull, (EventRecord *, WindowPtr));
static short FDECL(macUpdateMessage, (EventRecord *, WindowPtr));
static short FDECL(macUpdateMenu, (EventRecord *, WindowPtr));
static short FDECL(GeneralUpdate, (EventRecord *, WindowPtr));

static void FDECL(macCursorTerm, (EventRecord *, WindowPtr, RgnHandle));
static void FDECL(GeneralCursor, (EventRecord *, WindowPtr, RgnHandle));

static void FDECL(DoScrollBar,(Point, short, ControlHandle, NhWindow *));
static pascal void FDECL(MoveScrollBar, (ControlHandle, short));

typedef void (*CbFunc) (EventRecord *, WindowPtr);
typedef short (*CbUpFunc) (EventRecord *, WindowPtr);
typedef void (*CbCursFunc) (EventRecord *, WindowPtr, RgnHandle);

#define NUM_FUNCS 6
static const CbFunc winKeyFuncs [NUM_FUNCS] = {
	GeneralKey, GeneralKey, GeneralKey, GeneralKey, macKeyMenu, macKeyText
};

static const CbFunc winClickFuncs [NUM_FUNCS] = {
	(CbFunc)macDoNull, macClickMessage, macClickTerm, macClickTerm, macClickMenu,
	macClickText
};

static const CbUpFunc winUpdateFuncs [NUM_FUNCS] = {
	macDoNull, macUpdateMessage, image_tty, image_tty,
	macUpdateMenu, GeneralUpdate
};

static const CbCursFunc winCursorFuncs [NUM_FUNCS] = {
	(CbCursFunc) macDoNull, GeneralCursor, macCursorTerm, macCursorTerm,
	GeneralCursor, GeneralCursor
};


static NhWindow *
GetNhWin(WindowPtr mac_win) {
	if (mac_win == _mt_window)	/* term window is still maintained by both systems, and */
		return theWindows;		/* WRefCon still refers to tty struct, so we have to map it */
	else {
		NhWindow *aWin = (NhWindow *)GetWRefCon (mac_win);
		if (aWin >= theWindows && aWin < &theWindows[NUM_MACWINDOWS])
			return aWin;
	}
	return ((NhWindow *) nil);
}


Boolean CheckNhWin (WindowPtr mac_win) {
	return GetNhWin (mac_win) != nil;
}


static pascal OSErr
AppleEventHandler (const AppleEvent* inAppleEvent, AppleEvent* outAEReply, long inRefCon) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(outAEReply,inRefCon)
#endif
	Size     actualSize;
	DescType typeCode;
	AEEventID EventID;
	OSErr    err;

	/* Get Event ID */
	err = AEGetAttributePtr (inAppleEvent, keyEventIDAttr, typeType, &typeCode,
								&EventID, sizeof (EventID), &actualSize);
	if (err == noErr) {
		switch (EventID) {
			default :
			case kAEOpenApplication :
				macFlags.gotOpen = 1;
				/* fall through */
			case kAEPrintDocuments :
				err = errAEEventNotHandled;
				break;
			case kAEQuitApplication :
				/* Flush key queue */
				keyQueueCount = keyQueueWrite = keyQueueRead = 0;
				AddToKeyQueue ('S', 1);
				break;
			case kAEOpenDocuments : {
				FSSpec      fss;
				FInfo fndrInfo;
				AEKeyword   keywd;
				AEDescList  docList;
				long     index, itemsInList;

				if((err = AEGetParamDesc(inAppleEvent, keyDirectObject, typeAEList, &docList)) != noErr ||
					(err = AECountItems(&docList, &itemsInList)) != noErr){
					if (err == errAEDescNotFound)
						itemsInList = 0;
					else
						break;
				}

				for(index = 1; index <= itemsInList; index++){
					err = AEGetNthPtr(&docList, index, typeFSS, &keywd, &typeCode, (Ptr)&fss,
								sizeof(FSSpec), &actualSize);
					if(noErr != err)
						break;

					err = FSpGetFInfo (&fss, &fndrInfo);
					if (noErr != err)
						break;

					if (fndrInfo.fdType != SAVE_TYPE) 
						continue;	/* only look at save files */

					process_openfile (fss.vRefNum, fss.parID, fss.name, fndrInfo.fdType);
					if (macFlags.gotOpen)
						break;	/* got our save file */
				}
				err = AEDisposeDesc(&docList);
				break;
			}
		}
	}			

	/* Check to see if all required parameters for this type of event are present */
	if (err == noErr) {
		err = AEGetAttributePtr (inAppleEvent, keyMissedKeywordAttr, 
						  		typeWildCard, &typeCode, NULL, 0, &actualSize);
		if (err == errAEDescNotFound)											 
			err = noErr;		/* got all the required parameters */
		else if (err == noErr)	/* missed a required parameter */
			err = errAEEventNotHandled;
	}

	return err;
}


short win_fonts [NHW_TEXT + 1];

void
InitMac(void) {
	short i;
	long l;
	Str255 volName;


#if !TARGET_API_MAC_CARBON
	if (LMGetDefltStack() < 50 * 1024L) {
		SetApplLimit ((void *) ((long) LMGetCurStackBase() - (50 * 1024L)));
	}
	MaxApplZone ();
	for (i = 0; i < 5; i ++)
		MoreMasters ();

	InitGraf (&qd.thePort);
	InitFonts ();
	InitWindows ();
	InitMenus ();
	InitDialogs (0L);
	TEInit ();
#endif

	memset (&macFlags, 0, sizeof(macFlags));
	if (!Gestalt (gestaltOSAttr, & l)) {
		macFlags.processes = (l & (1 << gestaltLaunchControl)) ? 1 : 0;
		macFlags.tempMem = (l & (1 << gestaltRealTempMemory)) ? 1 : 0;
		macFlags.hasDebugger = (l & (1 << gestaltSysDebuggerSupport)) ? 1 : 0;
	}
	if (!Gestalt (gestaltQuickdrawVersion, & l))
		macFlags.color = (l >= gestalt8BitQD) ? 1 : 0;

	if (!Gestalt (gestaltFindFolderAttr, & l))
		macFlags.folders = (l & (1 << gestaltFindFolderPresent)) ? 1 : 0;
		
	if (!Gestalt (gestaltHelpMgrAttr, & l))
		macFlags.help = (l & (1 << gestaltHelpMgrPresent)) ? 1 : 0;

	if (!Gestalt (gestaltFSAttr, & l))
		macFlags.fsSpec = (l & (1 << gestaltHasFSSpecCalls)) ? 1 : 0;

	if (!Gestalt (gestaltFontMgrAttr, & l))
		macFlags.trueType = (l & (1 << gestaltOutlineFonts)) ? 1 : 0;

	if (!Gestalt (gestaltAUXVersion, & l))
		macFlags.aux = (l >= 0x200) ? 1 : 0;

	if (!Gestalt (gestaltAliasMgrAttr, & l))
		macFlags.alias = (l & (1 << gestaltAliasMgrPresent)) ? 1 : 0;

	if (!Gestalt (gestaltStandardFileAttr, & l))
		macFlags.standardFile = (l & (1 << gestaltStandardFile58)) ? 1 : 0;

	gMouseRgn = NewRgn ();
	InitCursor();
	GetQDGlobalsArrow(&qdarrow);
	ObscureCursor ();
	
	MoveScrollUPP = NewControlActionUPP(MoveScrollBar);

	/* Set up base fonts for all window types */
	GetFNum ("\pHackFont", &i);
	if (i == 0)
		i = kFontIDMonaco;
	win_fonts [NHW_BASE] = win_fonts [NHW_MAP] = win_fonts [NHW_STATUS] = i;
	GetFNum ("\pPSHackFont", &i);
	if (i == 0)
		i = kFontIDGeneva;
	win_fonts [NHW_MESSAGE] = i;
	win_fonts [NHW_TEXT] = kFontIDGeneva;

	macFlags.hasAE = 0;
	if(!Gestalt(gestaltAppleEventsAttr, &l) && (l & (1L << gestaltAppleEventsPresent))){
		if (AEInstallEventHandler (kCoreEventClass, typeWildCard,
							NewAEEventHandlerUPP(AppleEventHandler),
							0,
							FALSE) == noErr)
			macFlags.hasAE = 1;
	}

#if TARGET_API_MAC_CARBON
	HGetVol(volName, &theDirs.dataRefNum, &theDirs.dataDirID);
#else
	/*
	 * We should try to get this data from a rsrc, in the profile file
	 * the user double-clicked...  This data should be saved with the
	 * save file in the resource fork, AND be saveable in "stationary"
	 */
	GetVol (volName, &theDirs.dataRefNum );
	GetWDInfo (theDirs.dataRefNum, &theDirs.dataRefNum, &theDirs.dataDirID, &l);
#endif
	if (volName [0] > 31) volName [0] = 31;
	for (l = 1; l <= volName [0]; l++) {
		if (volName [l] == ':') {
			volName [l] = 0;
			volName [0] = l - 1;
			break;
		}
	}
	BlockMove (volName, theDirs.dataName, l);
	BlockMove (volName, theDirs.saveName, l);
	BlockMove (volName, theDirs.levelName, l);
	theDirs.saveRefNum = theDirs.levelRefNum = theDirs.dataRefNum;
	theDirs.saveDirID = theDirs.levelDirID = theDirs.dataDirID;

	/* Create the "record" file, if necessary */
	check_recordfile("");
	return;
}


/*
 * Change default window fonts.
 */
short
set_tty_font_name (int window_type, char *font_name) {
	short fnum;
	Str255 new_font;
	
	if (window_type < NHW_BASE || window_type > NHW_TEXT)
		return general_failure;
		
	C2P (font_name, new_font);
	GetFNum (new_font, &(fnum));
	if (!fnum)
		return general_failure;
	win_fonts [window_type] = fnum;
	return noErr;
}


static void
DrawScrollbar (NhWindow *aWin)
{
	WindowPtr theWindow = aWin->its_window;
	Rect crect, wrect;
	Boolean vis;
	short val, lin, win_height;


	if (!aWin->scrollBar)
		return;
	GetControlBounds(aWin->scrollBar, &crect);
	GetWindowBounds(aWin->its_window, kWindowContentRgn, &wrect);
	OffsetRect(&wrect, -wrect.left, -wrect.top);
	win_height = wrect.bottom - wrect.top;

	if (crect.top != wrect.top - 1 ||
		 crect.left != wrect.right - SBARWIDTH) {
		MoveControl (aWin->scrollBar, wrect.right - SBARWIDTH, wrect.top - 1);
	}
	if (crect.bottom != wrect.bottom - SBARHEIGHT ||
		 crect.right != wrect.right + 1) {
		SizeControl (aWin->scrollBar, SBARWIDTH+1, win_height - SBARHEIGHT + 2);
	}
	vis = (win_height > (50 + SBARHEIGHT));
	if (vis != IsControlVisible(aWin->scrollBar)) {
		/* current status != control */
		if (vis)/* if visible, show */
			ShowControl (aWin->scrollBar);
		else	/* else hide */
			HideControl (aWin->scrollBar);
	}
	lin = aWin->y_size;
	if (aWin == theWindows + WIN_MESSAGE) {
		/* calculate how big scroll bar is for message window */
		lin -= (win_height - SBARHEIGHT) / aWin->row_height;
		if (lin < 0)
			lin = 0;		
		val = 0;			/* always have message scrollbar active */
	}
	else {
		/* calculate how big scroll bar is for other windows */
		lin -= win_height / aWin->row_height;
		if (lin < 0)
			lin = 0;
		if (lin) 	val = 0;	/* if there are 1+ screen lines, activate scrollbar */
		else		val = 255;	/* else grey it out */
	}
	SetControlMaximum (aWin->scrollBar, lin);
	HiliteControl (aWin->scrollBar, val);
	val = GetControlValue (aWin->scrollBar);
	if (val != aWin->scrollPos) {
		InvalWindowRect(theWindow, &wrect);
		aWin->scrollPos = val;
	}
}


#define MAX_HEIGHT 100
#define MIN_HEIGHT 50
#define MIN_WIDTH 300

/*
 * This function could be overloaded with any amount of intelligence...
 */
int
SanePositions (void)
{
#if TARGET_API_MAC_CARBON
	ConstrainWindowToScreen(_mt_window, kWindowStructureRgn,
		kWindowConstrainMoveRegardlessOfFit, NULL, NULL);
#else
	short left, top, width, height;
	int ix, numText = 0, numMenu = 0;
	int mbar_height = GetMBarHeight();
	BitMap qbitmap;
	Rect screenArea;
	WindowPtr theWindow;
	NhWindow *nhWin;


	screenArea = GetQDGlobalsScreenBits(&qbitmap)->bounds;
	OffsetRect (&screenArea, - screenArea.left, - screenArea.top);

/* Map Window */
	height = _mt_window->portRect.bottom - _mt_window->portRect.top;
	width = _mt_window->portRect.right - _mt_window->portRect.left;

	if (!RetrievePosition (kMapWindow, &top, &left)) {
		top = mbar_height + (small_screen ? 2 : 20);
		left = (screenArea.right - width) / 2;
	}
	MoveWindow (_mt_window, left, top, 1);

/* Message Window */
	if (!RetrievePosition (kMessageWindow, &top, &left)) {
		top += height;
		if (!small_screen)
			top += 20;
	}

	if (!RetrieveSize (kMessageWindow, top, left, &height, &width)) {
		height = screenArea.bottom - top - (small_screen ? 2-SBARHEIGHT : 2);
		if (height > MAX_HEIGHT) {
			height = MAX_HEIGHT;
		} else if (height < MIN_HEIGHT) {
			height = MIN_HEIGHT;
			width = MIN_WIDTH;
			left = screenArea.right - width;
			top = screenArea.bottom - MIN_HEIGHT;
		}
	}

/* Move these windows */
	nhWin = theWindows + WIN_MESSAGE;
	theWindow = nhWin->its_window;

	MoveWindow (theWindow, left, top, 1);
	SizeWindow (theWindow, width, height, 1);
	if (nhWin->scrollBar)
		DrawScrollbar (nhWin);

	/* Handle other windows */
	for (ix = 0; ix < NUM_MACWINDOWS; ix ++) {
		if (ix != WIN_STATUS && ix != WIN_MESSAGE && ix != WIN_MAP && ix != BASE_WINDOW) {
			theWindow = theWindows [ix].its_window;
			if (theWindow && ((WindowPeek) theWindow)->visible) {
				int shift;
				if (((WindowPeek)theWindow)->windowKind == WIN_BASE_KIND + NHW_MENU) {
					if (!RetrievePosition (kMenuWindow, &top, &left)) {
						top = mbar_height * 2;
						left = 2;
					}
					top += (numMenu * mbar_height);
					numMenu++;
					shift = 20;
				} else {
					if (!RetrievePosition (kTextWindow, &top, &left)) {
						top = mbar_height * 2;
						left = screenArea.right - 3 - 
							(theWindow->portRect.right - theWindow->portRect.left);
					}
					top += (numText * mbar_height);
					numText++;
					shift = -20;
				}
				while (top > screenArea.bottom - MIN_HEIGHT) {
					top -= screenArea.bottom - mbar_height * 2;
					left += shift;
				}
				MoveWindow (theWindow, left, top, 1);
			}
		}
	}
#endif
	return (0);
}


winid
mac_create_nhwindow (int kind) {
	int i;
	NhWindow *aWin;
	FontInfo fi;

	if (kind < NHW_BASE || kind > NHW_TEXT) {
		error ("cre_win: Invalid kind %d.", kind);
		return WIN_ERR;
	}

	for (i = 0; i < NUM_MACWINDOWS; i ++) {
		if (!theWindows [i].its_window)
			break;
	}
	if (i >= NUM_MACWINDOWS) {
		error ("cre_win: Win full; freeing extras");
		for (i = 0; i < NUM_MACWINDOWS; i ++) {
			if (IsWindowVisible(theWindows [i].its_window) || i == WIN_INVEN ||
				GetWindowKind(theWindows [i].its_window) != WIN_BASE_KIND + NHW_MENU &&
				GetWindowKind(theWindows [i].its_window) != WIN_BASE_KIND + NHW_TEXT)
				continue;
			mac_destroy_nhwindow(i);
			goto got1;
		}
		error ("cre_win: Out of ids!");
		return WIN_ERR;
	}

got1 :
	aWin = &theWindows [i];
	aWin->windowTextLen = 0L;
	aWin->scrollBar = (ControlHandle) 0;
	aWin->menuInfo = 0;
	aWin->menuSelected = 0;
	aWin->miLen = 0;
	aWin->miSize = 0;
	aWin->menuChar = 'a';
	
	dprintf ("cre_win: New kind %d", kind);

	if (kind == NHW_BASE || kind == NHW_MAP || kind == NHW_STATUS) {
		short x_sz, x_sz_p, y_sz, y_sz_p;
		if (kind != NHW_BASE) {
			if (i != tty_create_nhwindow(kind)) {
				dprintf ("cre_win: error creating kind %d", kind);
			}
			if (kind == NHW_MAP) {
				wins[i]->offy = 0;	/* the message box is in a separate window */
			}
		}
		aWin->its_window = _mt_window;
		get_tty_metrics(aWin->its_window, &x_sz, &y_sz, &x_sz_p, &y_sz_p,
					 &aWin->font_number, &aWin->font_size,
					 &aWin->char_width, &aWin->row_height);
		return i;
	}

	aWin->its_window = GetNewWindow (WIN_BASE_RES + kind, (WindowPtr) 0L, (WindowPtr) -1L);
	SetWindowKind(aWin->its_window, WIN_BASE_KIND + kind);
	SetWRefCon(aWin->its_window, (long) aWin);
	if (!(aWin->windowText = NewHandle (TEXT_BLOCK))) {
		error ("cre_win: NewHandle fail(%ld)", (long) TEXT_BLOCK);
		DisposeWindow (aWin->its_window);
		aWin->its_window = (WindowPtr) 0;
		return WIN_ERR;
	}
	aWin->x_size = aWin->y_size = 0;
	aWin->x_curs = aWin->y_curs = 0;
	aWin->drawn = TRUE;
	mac_clear_nhwindow (i);

	SetPortWindowPort(aWin->its_window);

	if (kind == NHW_MESSAGE) {
		aWin->font_number = win_fonts [NHW_MESSAGE];
		aWin->font_size = iflags.wc_fontsiz_message? iflags.wc_fontsiz_message :
			iflags.large_font ? 12 : 9;
		if (!top_line) {
			const Rect out_of_scr = {10000, 10000, 10100, 10100};
			TextFont(aWin->font_number);
			TextSize(aWin->font_size);
			TextFace(bold);
			top_line = TENew(&out_of_scr, &out_of_scr);
			TEActivate(top_line);
			TextFace(normal);
		}
	} else {
		aWin->font_number = win_fonts [NHW_TEXT];
		aWin->font_size = iflags.wc_fontsiz_text ? iflags.wc_fontsiz_text : 9;
	}

	TextFont (aWin->font_number); 
	TextSize (aWin->font_size);

	GetFontInfo (&fi);
	aWin->ascent_height = fi.ascent + fi.leading;
	aWin->row_height = aWin->ascent_height + fi.descent;
	aWin->char_width = fi.widMax;

	if (kind == NHW_MENU || kind == NHW_TEXT || kind == NHW_MESSAGE) {
		Rect r;

		GetWindowBounds(aWin->its_window, kWindowContentRgn, &r);
		r.right -= (r.left - 1);
		r.left = r.right - SBARWIDTH;
		r.bottom -= (r.top + SBARHEIGHT);
		r.top = -1;
		aWin->scrollBar = NewControl (aWin->its_window, &r, "\p", (r.bottom > r.top + 50), 0, 0, 0, 16, 0L);
		aWin->scrollPos = 0;
	}
	return i;
}


void
mac_init_nhwindows (int *argcp, char **argv)
{
#if !TARGET_API_MAC_CARBON
	Rect scr = (*GetGrayRgn())->rgnBBox;
	small_screen = scr.bottom - scr.top <= (iflags.large_font ? 12*40 : 9*40);
#endif
	Rect r;


	InitMenuRes ();

	theWindows = (NhWindow *) NewPtrClear (NUM_MACWINDOWS * sizeof (NhWindow));
	if (MemError())
		error("mac_init_nhwindows: Couldn't allocate memory for windows.");

	DimMenuBar ();

	tty_init_nhwindows(argcp, argv);
	iflags.window_inited = TRUE;

	/* Some ugly hacks to make both interfaces happy:
	 * Mac port uses both tty interface (for main map) and extra windows.  The winids need to
	 * be kept in synch for both interfaces to map.  Also, the "blocked" display_nhwindow case
	 * for the map automatically calls the tty interface for the message box, so some version
	 * of the message box has to exist in the tty world to prevent a meltdown, even though most
	 * messages are handled in mac window.
	 */
	mac_create_nhwindow(NHW_BASE);
	tty_create_nhwindow(NHW_MESSAGE);
	RetrievePosition(kMessageWindow, &r.top, &r.left);
	RetrieveSize(kMessageWindow, r.top, r.left, &r.bottom, &r.right);
	MoveWindow(theWindows[NHW_MESSAGE].its_window, r.left, r.top, false);
	SizeWindow(theWindows[NHW_MESSAGE].its_window, r.right, r.bottom, true);
	ConstrainWindowToScreen(theWindows[NHW_MESSAGE].its_window, kWindowStructureRgn,
		kWindowConstrainMoveRegardlessOfFit, NULL, NULL);
	return;
}


void
mac_clear_nhwindow (winid win) {
	long l;
	Rect r;
	NhWindow *aWin = &theWindows [win];
	WindowPtr theWindow = aWin->its_window;

	if (win < 0 || win >= NUM_MACWINDOWS || !theWindow) {
		error ("clr_win: Invalid win %d.", win);
		return;
	}
	if (theWindow == _mt_window) {
		tty_clear_nhwindow(win);
		return;
	}
	if (!aWin->drawn)
		return;

	SetPortWindowPort(theWindow);
	GetWindowBounds(theWindow, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);
	if (aWin->scrollBar)
		r.right -= SBARWIDTH;

	switch (GetWindowKind(theWindow) - WIN_BASE_KIND) {
	case NHW_MESSAGE :
		if (aWin->scrollPos == aWin->y_size - 1)	/* if no change since last clear */
			return;									/* don't bother with redraw */
		r.bottom -= SBARHEIGHT;
		for (l = 0; aWin->y_size > iflags.msg_history;) {
			const char cr = CHAR_CR;
			l = Munger(aWin->windowText, l, &cr, 1, nil, 0) + 1;
			--aWin->y_size;
		}
		if (l) {
			aWin->windowTextLen -= l;
			BlockMove(*aWin->windowText + l, *aWin->windowText, aWin->windowTextLen);
		}
		aWin->last_more_lin = aWin->y_size;
		aWin->save_lin	= aWin->y_size;
		aWin->scrollPos = aWin->y_size ? aWin->y_size - 1 : 0;
		break;
	case NHW_MENU:
		if (aWin->menuInfo) {
			DisposeHandle((Handle)aWin->menuInfo);
			aWin->menuInfo = NULL;
		}
		if (aWin->menuSelected) {
			DisposeHandle((Handle)aWin->menuSelected);
			aWin->menuSelected = NULL;
		}
		aWin->menuChar = 'a';
		aWin->miSelLen = 0;
		aWin->miLen = 0;
		aWin->miSize = 0;
		/* Fall-Through */
	default :
		SetHandleSize (aWin->windowText, TEXT_BLOCK);
		aWin->windowTextLen = 0L;
		aWin->x_size = 0;
		aWin->y_size = 0;
		aWin->scrollPos = 0;
		break;
	}
	if (aWin->scrollBar) {
		SetControlMaximum (aWin->scrollBar, aWin->y_size);
		SetControlValue(aWin->scrollBar, aWin->scrollPos);
	}
	aWin->y_curs = 0;
	aWin->x_curs = 0;
	aWin->drawn = FALSE;
	InvalWindowRect(theWindow, &r);
}


static Boolean
ClosingWindowChar(const int c) {
	return c == CHAR_ESC || c == CHAR_BLANK || c == CHAR_LF || c == CHAR_CR ||
			c == 'q';
}


static Boolean
in_topl_mode(void)
{
	Rect rect;


	GetWindowBounds(theWindows[WIN_MESSAGE].its_window, kWindowContentRgn, &rect);
	OffsetRect(&rect, -rect.left, -rect.top);
	return (WIN_MESSAGE != WIN_ERR && top_line &&
		(*top_line)->viewRect.left < rect.right);
}


#define BTN_IND 2
#define BTN_W	40
#define BTN_H	(SBARHEIGHT-3)

static void
topl_resp_rect(int resp_idx, Rect *r)
{
	Rect rect;


	GetWindowBounds(theWindows[WIN_MESSAGE].its_window, kWindowContentRgn, &rect);
	OffsetRect(&rect, -rect.left, -rect.top);
	r->left	  = (BTN_IND + BTN_W) * resp_idx + BTN_IND;
	r->right  = r->left + BTN_W;
	r->bottom = rect.bottom - 1;
	r->top	  = r->bottom - BTN_H;
	return;
}


void
enter_topl_mode(char *query) {
	if (in_topl_mode())
		return;

	putstr(WIN_MESSAGE, ATR_BOLD, query);

	topl_query_len = strlen(query);
	(*top_line)->selStart = topl_query_len;
	(*top_line)->selEnd = topl_query_len;
	(*top_line)->viewRect.left = 0;
	PtrToXHand(query, (*top_line)->hText, topl_query_len);
	TECalText(top_line);

	DimMenuBar();
	mac_display_nhwindow(WIN_MESSAGE, FALSE);
}


void
leave_topl_mode(char *answer) {
	unsigned char *ap, *bp;

	int ans_len = (*top_line)->teLength - topl_query_len;
	NhWindow *aWin = theWindows + WIN_MESSAGE;

	if (!in_topl_mode())
		return;

	/* Cap length of reply */
	if (ans_len >= BUFSZ)
		ans_len = BUFSZ-1;

	/* remove unprintables from the answer */
	for (ap = *(*top_line)->hText + topl_query_len, bp = answer; ans_len > 0; ans_len--, ap++) {
		if (*ap >= ' ' && *ap < 128) {
			*bp++ = *ap;
		}
	}
	*bp = 0;
	
	if (aWin->windowTextLen && (*aWin->windowText)[aWin->windowTextLen-1] == CHAR_CR) {
		-- aWin->windowTextLen;
		-- aWin->y_size;
	}
	putstr(WIN_MESSAGE, ATR_BOLD, answer);

	(*top_line)->viewRect.left += 10000;
	UndimMenuBar();
}

/*
 * TESetSelect flushes out all the pending key strokes.  I hate it.
 */
static void
topl_set_select(short selStart, short selEnd) {
	TEDeactivate(top_line);
	(*top_line)->selStart	= selStart;
	(*top_line)->selEnd	= selEnd;
	TEActivate(top_line);
}


static void
topl_replace(char *new_ans) {
	topl_set_select(topl_query_len, (*top_line)->teLength);
	TEDelete(top_line);
	TEInsert(new_ans, strlen(new_ans), top_line);
}


Boolean
topl_key(unsigned char ch, Boolean ext) {
	switch (ch) {
		case CHAR_ESC:
			topl_replace("\x1b");
		case CHAR_ENTER: case CHAR_CR: case CHAR_LF:
			return false;

		case 0x1f & 'P':
			mac_doprev_message();
			return true;
		case '\x1e'/* up arrow */:
			topl_replace ("");
			return true;
		case CHAR_BS: case '\x1c'/* left arrow */:
			if ((*top_line)->selEnd <= topl_query_len)
				return true;
			else if (ext) {
				topl_replace ("");
				return true;
			}
		default:
			TEKey(ch, top_line);
			if (ext) {
				int com_index = -1, oindex = 0;
				while(extcmdlist[oindex].ef_txt != (char *)0) {
					if(!strncmpi(*(*top_line)->hText + topl_query_len,
								 extcmdlist[oindex].ef_txt,
								 (*top_line)->teLength - topl_query_len)) {
						if(com_index == -1) /* No matches yet*/
							com_index = oindex;
						else /* More than 1 match */ {
							com_index = -2;
							break;
						}
					}
					oindex++;
				}
				if(com_index >= 0)
					topl_replace((char *) extcmdlist[com_index].ef_txt);
			}
			return true;
	}
}


static void
topl_flash_resp(int resp_idx) {
	unsigned long dont_care;
	Rect frame;
	SetPortWindowPort(theWindows[WIN_MESSAGE].its_window);
	topl_resp_rect(resp_idx, &frame);
	InsetRect(&frame, 1, 1);
	InvertRect(&frame);
	Delay(GetDblTime() / 2, &dont_care);
	InvertRect(&frame);
}


static void
topl_set_def(int new_def_idx) {
	Rect frame;
	SetPortWindowPort(theWindows[WIN_MESSAGE].its_window);
	topl_resp_rect(topl_def_idx, &frame);
	InvalWindowRect(theWindows[WIN_MESSAGE].its_window, &frame);
	topl_def_idx = new_def_idx;
	topl_resp_rect(new_def_idx, &frame);
	InvalWindowRect(theWindows[WIN_MESSAGE].its_window, &frame);
}


void
topl_set_resp(char *resp, char def) {
	char *loc;
	Rect frame;
	int r_len, r_len1;

	if (!resp) {
		const char any_str[2] = {CHAR_ANY, '\0'};
		resp = (char *) any_str;
		def = CHAR_ANY;
	}

	SetPortWindowPort(theWindows[WIN_MESSAGE].its_window);
	r_len1 = strlen(resp);
	r_len  = strlen(topl_resp);
	if (r_len < r_len1)
		r_len = r_len1;
	topl_resp_rect(0, &frame);
	frame.right = (BTN_IND + BTN_W) * r_len;
	InvalWindowRect(theWindows[WIN_MESSAGE].its_window, &frame);

	strcpy(topl_resp, resp);
	loc = strchr (resp, def);
	topl_def_idx = loc ? loc - resp : -1;
}


static char
topl_resp_key(char ch) {
	if (strlen(topl_resp) > 0) {
		char *loc = strchr(topl_resp, ch);

		if (!loc) {
			if (ch == '\x9'/* tab */) {
				topl_set_def(topl_def_idx<=0 ? strlen(topl_resp)-1 : topl_def_idx-1);
				ch = '\0';
			} else if (ch == CHAR_ESC) {
				loc = strchr(topl_resp, 'q');
				if (!loc) {
					loc = strchr(topl_resp, 'n');
					if (!loc && topl_def_idx >= 0)
						loc = topl_resp + topl_def_idx;
				}
			} else if (ch == (0x1f & 'P')) {
				mac_doprev_message();
				ch = '\0';
			} else if (topl_def_idx >= 0) {
				if (ch == CHAR_ENTER || ch == CHAR_CR || ch == CHAR_LF ||
					 ch == CHAR_BLANK || topl_resp[topl_def_idx] == CHAR_ANY)
					loc = topl_resp + topl_def_idx;

				else if (strchr(topl_resp, '#')) {
					if (digit(ch)) {
						topl_set_def(strchr(topl_resp, '#') - topl_resp);
						TEKey(ch, top_line);
						ch = '\0';

					} else if (topl_resp[topl_def_idx] == '#') {
						if (ch == '\x1e'/* up arrow */) {
							topl_set_select(topl_query_len, topl_query_len);
							ch = '\0';
						} else if (ch == '\x1d'/* right arrow */ ||
							   ch == '\x1f'/* down arrow */ ||
							   ch == CHAR_BS || ch == '\x1c'/* left arrow */ &&
							   (*top_line)->selEnd > topl_query_len) {
							TEKey(ch, top_line);
							ch = '\0';
						}
					}
				}
			}
		}

		if (loc) {
			topl_flash_resp(loc - topl_resp);
			if (*loc != CHAR_ANY)
				ch = *loc;
			TEKey(ch, top_line);
		}
	}

	return ch;
}


static void
adjust_window_pos(NhWindow *aWin, short width, short height)
{
	WindowRef theWindow = aWin->its_window;
#if TARGET_API_MAC_CARBON
	Rect r;


	GetWindowBounds(theWindow, kWindowContentRgn, &r);
	RetrieveWinPos(theWindow, &r.top, &r.left);
	MoveWindow(theWindow, r.left, r.top, false);
	SizeWindow(theWindow, width, height, true);
	ConstrainWindowToScreen(theWindow, kWindowStructureRgn,
		kWindowConstrainMoveRegardlessOfFit, NULL, NULL);
#else
	Rect scr_r = (*GetGrayRgn())->rgnBBox;
	const Rect win_ind = {2, 2, 3, 3};
	const short	min_w = theWindow->portRect.right - theWindow->portRect.left,
				max_w = scr_r.right - scr_r.left - win_ind.left - win_ind.right;
	Point pos;
	short max_h;

	SetPortWindowPort(theWindow);
	if (!RetrieveWinPos(theWindow, &pos.v, &pos.h)) {
		pos.v = 0;	/* take window's existing position */
		pos.h = 0;
		LocalToGlobal(&pos);
	}

	max_h = scr_r.bottom - win_ind.bottom - pos.v;
	if (height > max_h)		height = max_h;
	if (height < MIN_HEIGHT)	height = MIN_HEIGHT;
	if (width < min_w)		width = min_w;
	if (width > max_w)		width = max_w;
	SizeWindow(theWindow, width, height, true);

	if (pos.v + height + win_ind.bottom > scr_r.bottom)
		pos.v = scr_r.bottom - height - win_ind.bottom;
	if (pos.h + width + win_ind.right > scr_r.right)
		pos.h = scr_r.right	 - width - win_ind.right;
	MoveWindow(theWindow, pos.h, pos.v, false);
	if (aWin->scrollBar)	
		DrawScrollbar (aWin);
#endif
	return;
}


/*
 * display/select/update the window.
 * If f is true, this window should be "modal" - don't return
 * until presumed seen.
 */
void
mac_display_nhwindow (winid win, BOOLEAN_P f) {
	NhWindow *aWin = &theWindows [win];
	WindowPtr theWindow = aWin->its_window;

	if (win < 0 || win >= NUM_MACWINDOWS || !theWindow) {
		error ("disp_win: Invalid window %d.", win);
		return;
	}

	if (theWindow == _mt_window) {
		tty_display_nhwindow(win, f);
		return;
	}

	if (f && inSelect == WIN_ERR && win == WIN_MESSAGE) {
		topl_set_resp ((char *)0, 0);
		if (aWin->windowTextLen > 0 &&
			 (*aWin->windowText) [aWin->windowTextLen - 1] == CHAR_CR) {
			-- aWin->windowTextLen;
			-- aWin->y_size;
		}
		putstr (win, flags.standout ? ATR_INVERSE : ATR_NONE, " --More--");
	}

	if (!IsWindowVisible(theWindow)) {
		if (win != WIN_MESSAGE)
			adjust_window_pos(aWin, aWin->x_size + SBARWIDTH+1, aWin->y_size *aWin->row_height);

		SelectWindow (theWindow);
		ShowWindow (theWindow);
	}

	if (f && inSelect == WIN_ERR) {
		int ch;

		DimMenuBar();
		inSelect = win;
		do {
			ch = mac_nhgetch ();
		} while (!ClosingWindowChar (ch));
		inSelect = WIN_ERR;
		UndimMenuBar();

		if (win == WIN_MESSAGE)
			topl_set_resp ("", '\0');
		else
			HideWindow (theWindow);

	}
}


void
mac_destroy_nhwindow (winid win) {
	WindowPtr theWindow;
	NhWindow *aWin = &theWindows [win];
	int kind;

	if (win < 0 || win >= NUM_MACWINDOWS) {
		if (iflags.window_inited) error ("dest_win: Invalid win %d.", win);
		return;
	}
	theWindow = aWin->its_window;
	if (!theWindow) {
		error ("dest_win: Not allocated win %d.", win);
		return;
	}

	/*
	 * Check special windows.  The base window should never go away.
	 * Other "standard" windows should not go away unless we've exitted nhwindows.
	 */
	if (theWindow == _mt_window) {
		return;
	}
	if (win == WIN_INVEN || win == WIN_MESSAGE) {
		if (iflags.window_inited) {
			if (flags.tombstone && killer) {
				/* Prepare for the coming of the tombstone window. */
				win_fonts [NHW_TEXT] = kFontIDMonaco;
			}
			return;
		}
		if (win == WIN_MESSAGE)
			WIN_MESSAGE = WIN_ERR;
	}

	kind = GetWindowKind(theWindow) - WIN_BASE_KIND;

	if ((!IsWindowVisible(theWindow) || (kind != NHW_MENU && kind != NHW_TEXT))) {
		DisposeWindow (theWindow);
		if (aWin->windowText) {
			DisposeHandle(aWin->windowText);
		}
		aWin->its_window = (WindowPtr) 0;
		aWin->windowText = (Handle) 0;
	}
}


void
mac_number_pad (int pad) {
	iflags.num_pad = pad;
}


void
trans_num_keys(EventRecord *theEvent) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(theEvent)
#endif
/* KMH -- Removed this translation.
 * Number pad keys should always emit digit characters.
 * That's consistent with the default MacOS behavior.
 * The number_pad option controls how digits are interpreted.
 */
#if 0
	if (iflags.num_pad) {
		Handle h = GetResource('Nump', theEvent->modifiers & shiftKey ? 129 : 128);
		if (h) {
			short inkey = (theEvent->message & keyCodeMask), *ab = (short *)*h;
			int i = ab[0];
			for (; i; i--) {
				if (inkey == (ab[i] & keyCodeMask)) {
					theEvent->message = ab[i];
					break;
				}
			}
		}
	}
#endif
}


/*
 * Note; theWindow may very well be null here, since keyDown may call
 * it when theres no window !!!
 */
static void
GeneralKey (EventRecord *theEvent, WindowPtr theWindow) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(theWindow)
#endif
#if 0
	trans_num_keys (theEvent);
#endif
	AddToKeyQueue (topl_resp_key (theEvent->message & 0xff), TRUE);
}


/*
 * Routine used to select and de-select elements in a menu window, used by KeyMenu,
 * ClickMenu, and UpdateMenu.  Takes the NhWindow and a line ref relative to the scrollbar.
 */
static void ToggleMenuSelect (NhWindow *aWin, int line) {
	Rect r;


	GetWindowBounds(aWin->its_window, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);
	if (aWin->scrollBar)
		r.right -= SBARWIDTH;			
	r.top = line * aWin->row_height;
	r.bottom = r.top + aWin->row_height;

	LMSetHiliteMode((UInt8) (LMGetHiliteMode() & 0x7F));
	InvertRect(&r);
}

/*
 * Check to see if given item is selected, return index if it is
 */
static int
ListItemSelected (NhWindow *aWin, int item) {
	int		i;

	HLock ((char**)aWin->menuSelected);
	/* Find item in selection list */
	for (i = aWin->miSelLen - 1; i >= 0; i--) {
		if ((*aWin->menuSelected) [i] == item)
			break;
	}
	HUnlock ((char**)aWin->menuSelected);
	return i;
}

/*
 * Add item to selection list if it's not selected already
 * If it is selected already, remove it from the list.
 */
static void
ToggleMenuListItemSelected (NhWindow *aWin, short item) {
	int i = ListItemSelected (aWin, item);

	HLock ((char**)aWin->menuSelected);
	if (i < 0) {	/* not there, so add */
		(*aWin->menuSelected) [aWin->miSelLen] = item;
		aWin->miSelLen++;
	}
	else {			/* there, so remove */
		short *mi = &(*aWin->menuSelected)[i];
		aWin->miSelLen --;
		memcpy (mi, mi + 1, (aWin->miSelLen - i)*sizeof(short));
	}
	HUnlock ((char**)aWin->menuSelected);
}


/*
 * Find menu item in list given a line number on the window
 */
static short
ListCoordinateToItem (NhWindow *aWin, short Row) {
	int					i, item = -1;
	MacMHMenuItem *		mi;

	HLock ((char**)aWin->menuInfo);
	for (i = 0, mi = *aWin->menuInfo; i < aWin->miLen; i++, mi++) {
		if (mi->line == Row + aWin->scrollPos) {
			item = i;
			break;
		}
	}
	HUnlock ((char**)aWin->menuInfo);
	return item;
}


static void
macKeyMenu (EventRecord *theEvent, WindowPtr theWindow) {
	NhWindow *aWin = GetNhWin(theWindow);
	MacMHMenuItem *mi;
	int l, ch = theEvent->message & 0xff;

	if (aWin && aWin->menuInfo) {
		HLock ((char**)aWin->menuInfo);
		for (l = 0, mi = *aWin->menuInfo; l < aWin->miLen; l++, mi++) {
			if (mi->accelerator == ch) {
				ToggleMenuListItemSelected (aWin, l);
				if (mi->line >= aWin->scrollPos && mi->line <= aWin->y_size) {
					SetPortWindowPort(theWindow);
					ToggleMenuSelect (aWin, mi->line - aWin->scrollPos);
				}				
				/* Dismiss window if only picking one item */
				if (aWin->how != PICK_ANY)
					AddToKeyQueue(CHAR_CR, 1);
				break;
			}
		}
		HUnlock ((char**)aWin->menuInfo);
		/* add key if didn't find it in menu and not filtered */
		if (l == aWin->miLen && filter_scroll_key (ch, aWin))
			GeneralKey (theEvent, theWindow);
	}
}


static void
macClickMenu (EventRecord *theEvent, WindowRef theWindow) {
	Point p;
	NhWindow *aWin = GetNhWin(theWindow);
	Rect wrect;


	GetWindowBounds(theWindow, kWindowContentRgn, &wrect);
	OffsetRect(&wrect, -wrect.left, -wrect.top);
	if (aWin->scrollBar && IsControlVisible(aWin->scrollBar)) {
		short code;
		ControlHandle theBar;

		p = theEvent->where;
		GlobalToLocal (&p);
		code = FindControl (p, theWindow, &theBar);
		if (code) {
			DoScrollBar (p, code, theBar, aWin);
			return;
		}
	}
	if (inSelect != WIN_ERR && aWin->how != PICK_NONE) {
		short		currentRow = -1, previousRow = -1;
		short		previousItem = -1, item = -1;
		Boolean		majorSelectState, firstRow = TRUE;

		do {
#if !TARGET_API_MAC_CARBON
			SystemTask ();
#endif
			GetMouse (&p);
			currentRow = p.v / aWin->row_height;
			if (p.h < wrect.left || p.h > wrect.right ||
				p.v < 0 || p.v > wrect.bottom || currentRow >= aWin->y_size) {
				continue;	/* not in window range */
			}

			item = ListCoordinateToItem (aWin, currentRow);

			if (item != previousItem) {
				/* Implement typical Mac multiple-selection behavior
				 * (ie, not the UI implemented by the Finder)
				 */
				Boolean	itemIsSelected = (ListItemSelected (aWin,item) >= 0);

				if (firstRow) {
					/* this is first valid row, so major state is opposite of what this row is */
					majorSelectState = !itemIsSelected;
					firstRow = FALSE;
				}

				if (aWin->how == PICK_ONE && previousItem != -1) {
					/* if previous row was selected and we're only selecting one object,
					 * deselect previous row!
					 */
					ToggleMenuListItemSelected (aWin, previousItem);
					ToggleMenuSelect (aWin, previousRow);
					previousItem = -1;
				}

				if (item == -1)
					continue;	/* header line */
					
				if (majorSelectState != itemIsSelected) {
					ToggleMenuListItemSelected (aWin, item);
					ToggleMenuSelect (aWin, currentRow);
				}

				previousRow		= currentRow;
				previousItem	= item;
			}
		} while (StillDown ());

		/* Dismiss window if only picking one item */
		if (aWin->how == PICK_ONE)
			AddToKeyQueue(CHAR_CR, 1);
	}
}


static void
macKeyText (EventRecord *theEvent, WindowPtr theWindow) {
	NhWindow *aWin = GetNhWin (theWindow);
	char c = filter_scroll_key (theEvent->message & 0xff, aWin);
	if (c) {
		if (inSelect == WIN_ERR && ClosingWindowChar (c)) {
			HideWindow (theWindow);
			mac_destroy_nhwindow (aWin - theWindows);
		} else {
			GeneralKey (theEvent, theWindow);
		}
	}
}


static void
macClickText (EventRecord *theEvent, WindowPtr theWindow) {
	NhWindow *aWin = GetNhWin (theWindow);

	if (aWin->scrollBar && IsControlVisible(aWin->scrollBar)) {
		short code;
		Point p = theEvent->where;
		ControlHandle theBar;

		GlobalToLocal (&p);
		code = FindControl (p, theWindow, &theBar);
		if (code) {
			DoScrollBar (p, code, theBar, aWin);
		}
	}
}


static void
macClickMessage (EventRecord *theEvent, WindowPtr theWindow) {
	int r_idx = 0;
	Point mouse = theEvent->where;

	GlobalToLocal(&mouse);
	while (topl_resp[r_idx]) {
		Rect frame;
		topl_resp_rect(r_idx, &frame);
		InsetRect(&frame, 1, 1);
		if (PtInRect(mouse, &frame)) {
			Boolean in_btn = true;

			InvertRect(&frame);
			while (WaitMouseUp()) {
#if !TARGET_API_MAC_CARBON
				SystemTask();
#endif
				GetMouse(&mouse);
				if (PtInRect(mouse, &frame) != in_btn) {
					in_btn = !in_btn;
					InvertRect(&frame);
				}
			}
			if (in_btn) {
				InvertRect(&frame);
				AddToKeyQueue (topl_resp [r_idx], 1);
			}
			return;

		}
		++r_idx;
	}

	macClickText(theEvent, theWindow);
}


static void
macClickTerm (EventRecord *theEvent, WindowPtr theWindow) {
	NhWindow *nhw = GetNhWin(theWindow);
	Point where = theEvent->where;

	GlobalToLocal(&where);
	where.h = where.h / nhw->char_width + 1;
	where.v = where.v / nhw->row_height;
	clicked_mod = (theEvent->modifiers & shiftKey) ? CLICK_2 : CLICK_1;

	if (strchr(topl_resp, *click_to_cmd(where.h, where.v, clicked_mod)))
		nhbell();
	else {
#if !TARGET_API_MAC_CARBON
		if (cursor_locked)
			while (WaitMouseUp())
				SystemTask();
#endif

		gClickedToMove = TRUE;
		clicked_pos = where;
	}
}

static pascal void
MoveScrollBar (ControlHandle theBar, short part) {
	EventRecord fake;
	Rect r;
	RgnHandle rgn;
	int now, amtToScroll;
	WindowPtr theWin;
	NhWindow *winToScroll;
	
	if (!part)
		return;

	theWin = GetControlOwner(theBar);
	GetWindowBounds(theWin, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);
	winToScroll = (NhWindow*)(GetWRefCon(theWin));
	now = GetControlValue (theBar);

	if (part == kControlPageUpPart || part == kControlPageDownPart)	
		amtToScroll = (r.bottom - r.top) / winToScroll->row_height;
	else
		amtToScroll = 1;

	if (part == kControlPageUpPart || part == kControlUpButtonPart) {
		int bound = GetControlMinimum (theBar);
		if (now - bound < amtToScroll)
			amtToScroll = now - bound;
		amtToScroll = -amtToScroll;
	} else {
		int bound = GetControlMaximum (theBar);
		if (bound - now < amtToScroll)
			amtToScroll = bound - now;
	}
	
	if (!amtToScroll)
		return;

	SetControlValue (theBar, now + amtToScroll);
	winToScroll->scrollPos = now + amtToScroll;
	r.right -= SBARWIDTH;
	if (winToScroll == theWindows + WIN_MESSAGE)
		r.bottom -= SBARHEIGHT;
	rgn = NewRgn ();
	ScrollRect (&r, 0, -amtToScroll * winToScroll->row_height, rgn);
	if (rgn) {
		InvalWindowRgn(theWin, rgn);
		BeginUpdate(theWin);
	}
	winUpdateFuncs [GetWindowKind(theWin) - WIN_BASE_KIND] (&fake, theWin);
	if (rgn) {
		EndUpdate(theWin);
		DisposeRgn(rgn);
	}
}


static void
DoScrollBar (Point p, short code, ControlHandle theBar, NhWindow *aWin)
{
	ControlActionUPP func = NULL;
	Rect rect;

	if (code == kControlUpButtonPart || code == kControlPageUpPart ||
		code == kControlDownButtonPart || code == kControlPageDownPart)
		func = MoveScrollUPP;
	(void) TrackControl(theBar, p, func);
	if (!func) {
		if (aWin->scrollPos != GetControlValue (theBar)) {
			aWin->scrollPos = GetControlValue (theBar);
			GetWindowBounds(aWin->its_window, kWindowContentRgn, &rect);
			OffsetRect(&rect, -rect.left, -rect.top);
			InvalWindowRect(aWin->its_window, &rect);
		}
	}
}


static int
filter_scroll_key(const int ch, NhWindow *aWin) {
	if (aWin->scrollBar && GetControlValue(aWin->scrollBar) < GetControlMaximum(aWin->scrollBar)) {
		short part = 0;
		if (ch == CHAR_BLANK) {
			part = kControlPageDownPart;
		}
		else if (ch == CHAR_CR || ch == CHAR_LF) {
			part = kControlDownButtonPart;
		}
		if (part) {
			SetPortWindowPort(aWin->its_window);
			MoveScrollBar(aWin->scrollBar, part);
			return 0;
		}
	}
	return ch;
}


int
mac_doprev_message(void) {
	if (WIN_MESSAGE) {
		NhWindow *winToScroll = &theWindows[WIN_MESSAGE];
		mac_display_nhwindow(WIN_MESSAGE, FALSE);
		SetPortWindowPort(winToScroll->its_window);
		MoveScrollBar(winToScroll->scrollBar, kControlUpButtonPart);
	}	
	return 0;
}


static short
macDoNull (EventRecord *theEvent, WindowPtr theWindow) {
	return 0;
}


static void
draw_growicon_vert_only(WindowPtr wind)
{
	GrafPtr org_port;
	RgnHandle org_clip = NewRgn();
	Rect r;

	GetPort(&org_port);
	SetPortWindowPort(wind);
	GetClip(org_clip);
	GetWindowBounds(wind, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);
	r.left = r.right - SBARWIDTH;
	ClipRect(&r);
	DrawGrowIcon(wind);
	SetClip(org_clip);
	DisposeRgn(org_clip);
	SetPort(org_port);
}


static short
macUpdateMessage (EventRecord *theEvent, WindowPtr theWindow)
{
	RgnHandle org_clip = NewRgn(), clip = NewRgn();
	Rect r;
	NhWindow *aWin = GetNhWin(theWindow);
	int l;

	if (!theEvent)
		return 0;

	GetClip(org_clip);
	GetWindowBounds(theWindow, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);

	DrawControls(theWindow);
	DrawGrowIcon(theWindow);

	for (l = 0; topl_resp[l]; l++) {
		StringPtr name;
		unsigned char tmp[2];
		FontInfo font;
		Rect frame;
		topl_resp_rect(l, &frame);
		switch (topl_resp[l]) {
			case 'y':
				name = "\pyes";
				break;
			case 'n':
				name = "\pno";
				break;
			case 'N':
				name = "\pNone";
				break;
			case 'a':
				name = "\pall";
				break;
			case 'q':
				name = "\pquit";
				break;
			case CHAR_ANY:
				name = "\pany key";
				break;
			default:
				tmp[0] = 1;
				tmp[1] = topl_resp[l];
				name = tmp;
				break;
		}
		TextFont(kFontIDGeneva);
		TextSize(9);
		GetFontInfo(&font);
		MoveTo ((frame.left + frame.right - StringWidth(name)) / 2,
			(frame.top + frame.bottom + font.ascent-font.descent-font.leading-1) / 2);
		DrawString(name);
		PenNormal();
		if (l == topl_def_idx)
			PenSize(2, 2);
		FrameRoundRect(&frame, 4, 4);
	}

	r.right -= SBARWIDTH;
	r.bottom -= SBARHEIGHT;
	/* Clip to the portrect - scrollbar/growicon *before* adjusting the rect
		to be larger than the size of the window (!) */
	RectRgn(clip, &r);
	SectRgn(clip, org_clip, clip);
	if (r.right < MIN_RIGHT)
		r.right = MIN_RIGHT;
	r.top -= aWin->scrollPos * aWin->row_height;

#if 0
	/* If you enable this band of code (and disable the next band), you will get
	   fewer flickers but a slower performance while drawing the dot line. */
	{	RgnHandle dotl_rgn = NewRgn();
		Rect dotl;
		dotl.left	= r.left;
		dotl.right	= r.right;
		dotl.bottom = r.top + aWin->save_lin * aWin->row_height;
		dotl.top	= dotl.bottom - 1;
		FillRect(&dotl, &qd.gray);
		RectRgn(dotl_rgn, &dotl);
		DiffRgn(clip, dotl_rgn, clip);
		DisposeRgn(dotl_rgn);
		SetClip(clip);
	}
#endif

	if (in_topl_mode()) {
		RgnHandle topl_rgn = NewRgn();
		Rect topl_r = r;
		topl_r.top += (aWin->y_size - 1) * aWin->row_height;
		l = (*top_line)->destRect.right - (*top_line)->destRect.left;
		(*top_line)->viewRect = topl_r;
		(*top_line)->destRect = topl_r;
		if (l != topl_r.right - topl_r.left)
			TECalText(top_line);
		TEUpdate(&topl_r, top_line);
		RectRgn(topl_rgn, &topl_r);
		DiffRgn(clip, topl_rgn, clip);
		DisposeRgn(topl_rgn);
		SetClip(clip);
	}

	DisposeRgn(clip);

	TextFont (aWin->font_number);
	TextSize (aWin->font_size);
	HLock (aWin->windowText);
	TETextBox (*aWin->windowText, aWin->windowTextLen, &r, teJustLeft);
	HUnlock (aWin->windowText);

#if !TARGET_API_MAC_CARBON
	r.bottom = r.top + aWin->save_lin * aWin->row_height;
	r.top	 = r.bottom - 1;
	FillRect(&r, (void *) &qd.gray);
#endif

	SetClip(org_clip);
	DisposeRgn(org_clip);
	return 0;
}


static short 
macUpdateMenu (EventRecord *theEvent, WindowPtr theWindow) {
	NhWindow *aWin = GetNhWin (theWindow);
	int i, line;
	MacMHMenuItem *mi;
	
	GeneralUpdate (theEvent, theWindow);
	HLock ((char**)aWin->menuInfo);
	HLock ((char**)aWin->menuSelected);
	for (i = 0; i < aWin->miSelLen; i++) {
		mi = &(*aWin->menuInfo) [(*aWin->menuSelected) [i]];
		line = mi->line;
		if (line > aWin->scrollPos && line <= aWin->y_size)
			ToggleMenuSelect (aWin, line - aWin->scrollPos);
	}
	HUnlock ((char**)aWin->menuInfo);
	HUnlock ((char**)aWin->menuSelected);
	return 0;
}


static short
GeneralUpdate (EventRecord *theEvent, WindowPtr theWindow) {
	Rect r, r2;
	NhWindow *aWin = GetNhWin (theWindow);
	RgnHandle h;
	Boolean vis;


	if (!theEvent)
		return 0;

	GetWindowBounds(theWindow, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);
	r2 = r;
	r2.left = r2.right - SBARWIDTH;
	r2.right += 1;
	r2.top -= 1;
	vis = (r2.bottom > r2.top + 50);

	draw_growicon_vert_only(theWindow);
	DrawControls (theWindow);

	h = (RgnHandle) 0;
	if (vis && (h = NewRgn ())) {
		RgnHandle tmp = NewRgn ();
		if (!tmp) {
			DisposeRgn (h);
			h = (RgnHandle) 0;
		} else {
			GetClip (h);
			RectRgn (tmp, &r2);
			DiffRgn (h, tmp, tmp);
			SetClip (tmp);
			DisposeRgn (tmp);
		}
	}
	if (r.right < MIN_RIGHT)
		r.right = MIN_RIGHT;
	r.top -= aWin->scrollPos * aWin->row_height;
	r.right -= SBARWIDTH;
	HLock (aWin->windowText);
	TETextBox (*aWin->windowText, aWin->windowTextLen, &r, teJustLeft);
	HUnlock (aWin->windowText);
	if (h) {
		SetClip (h);
		DisposeRgn (h);
	}
	return 0;
}


static void
macCursorTerm (EventRecord *theEvent, WindowPtr theWindow, RgnHandle mouseRgn) {
	char *dir_bas, *dir;
	CursHandle ch;
	GrafPtr gp;
	NhWindow *nhw = GetNhWin (theWindow);
	Rect r = {0, 0, 1, 1};

	GetPort (&gp);
	SetPortWindowPort(theWindow);

	if (cursor_locked)
		dir = (char *)0;
	else {
		Point where = theEvent->where;

		GlobalToLocal (&where);
		dir_bas = iflags.num_pad ? (char *) ndir : (char *) sdir;
		dir = strchr (dir_bas, *click_to_cmd (where.h / nhw->char_width + 1 ,
							where.v / nhw->row_height, CLICK_1));
	}
	ch = GetCursor (dir ? dir - dir_bas + 513 : 512);
	if (ch) {
		HLock ((Handle) ch);
		SetCursor (*ch);
		HUnlock ((Handle) ch);

	} else {
		SetCursor(&qdarrow);
	}
	OffsetRect (&r, theEvent->where.h, theEvent->where.v);
	RectRgn (mouseRgn, &r);
	SetPort (gp);
}


static void
GeneralCursor (EventRecord *theEvent, WindowPtr theWindow, RgnHandle mouseRgn) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(theWindow)
#endif
	Rect r = {-1, -1, 2, 2};

	SetCursor(&qdarrow);
	OffsetRect (&r, theEvent->where.h, theEvent->where.v);
	RectRgn (mouseRgn, &r);
}


static void
HandleKey (EventRecord *theEvent) {
	WindowPtr theWindow = FrontWindow ();

	if (theEvent->modifiers & cmdKey) {
		if (theEvent->message & 0xff == '.') {
			/* Flush key queue */
			keyQueueCount = keyQueueWrite = keyQueueRead = 0;
			theEvent->message = '\033';
			goto dispatchKey;
		} else {
			UndimMenuBar ();
			DoMenuEvt (MenuKey (theEvent->message & 0xff));
		}
	} else {

dispatchKey :
		if (theWindow) {
			int kind = GetWindowKind(theWindow) - WIN_BASE_KIND;
			winKeyFuncs [kind] (theEvent, theWindow);
		} else {
			GeneralKey (theEvent, (WindowPtr) 0);
		}
	}
}


static void
WindowGoAway (EventRecord *theEvent, WindowPtr theWindow) {
	NhWindow *aWin = GetNhWin(theWindow);

	if (!theEvent || TrackGoAway (theWindow, theEvent->where)) {
		if (aWin - theWindows == BASE_WINDOW && !iflags.window_inited) {
			AddToKeyQueue ('\033', 1);
		} else {
			HideWindow (theWindow);
			if (aWin - theWindows != inSelect)
				mac_destroy_nhwindow (aWin - theWindows);
			else								/* if this IS the inSelect window put a close char */
				AddToKeyQueue (CHAR_CR, 1);		/* in queue to exit and maintain inSelect */
		}
	}
}


static void
HandleClick (EventRecord *theEvent) {
	int code;
	unsigned long l;
	WindowPtr theWindow;
	NhWindow *aWin;
	Rect r;
	Boolean not_inSelect;

	InsetRect(GetRegionBounds(GetGrayRgn(), &r), 4, 4);

	code = FindWindow (theEvent->where, &theWindow);
	aWin = GetNhWin (theWindow);
	not_inSelect = (inSelect == WIN_ERR || aWin - theWindows == inSelect);
	
	switch (code) {
	case inContent :
		if (not_inSelect) {
			int kind = GetWindowKind(theWindow) - WIN_BASE_KIND;
			winCursorFuncs [kind] (theEvent, theWindow, gMouseRgn);
			SelectWindow (theWindow);
			SetPortWindowPort(theWindow);
			winClickFuncs [kind] (theEvent, theWindow);
		} else {
			nhbell ();
		}
		break;

	case inDrag :
		if (not_inSelect) {
			SetCursor(&qdarrow);
			DragWindow (theWindow, theEvent->where, &r);
			SaveWindowPos(theWindow);
		} else {
			nhbell ();
		}
		break;

	case inGrow :
		if (not_inSelect) {
			SetCursor(&qdarrow);
			SetRect (&r, 80, 2 * aWin->row_height + 1, r.right, r.bottom);
			if (aWin == theWindows + WIN_MESSAGE)
				r.top += SBARHEIGHT;
			l = GrowWindow (theWindow, theEvent->where, &r);
			SizeWindow (theWindow, l & 0xffff, l >> 16, FALSE);
			SaveWindowSize(theWindow);
			SetPortWindowPort(theWindow);
			GetWindowBounds(theWindow, kWindowContentRgn, &r);
			OffsetRect(&r, -r.left, -r.top);
			InvalWindowRect(theWindow, &r);
			if (aWin->scrollBar) {
				DrawScrollbar (aWin);
			}
		} else {
			nhbell ();
		}
		break;

	case inGoAway :
		WindowGoAway(theEvent, theWindow);
		break;

	case inMenuBar :
		DoMenuEvt (MenuSelect (theEvent->where));
		break;

#if !TARGET_API_MAC_CARBON
	case inSysWindow :
		SystemClick(theEvent, theWindow);
#endif
	default :
		break;
	}
}


static void
HandleUpdate (EventRecord *theEvent) {
	WindowPtr theWindow = (WindowPtr) theEvent->message;
	NhWindow *aWin = GetNhWin (theWindow);
	Rect r;


	char existing_update_region = FALSE;
	Rect rect;
	
	if (theWindow == _mt_window) {
		existing_update_region = (get_invalid_region (theWindow, &rect) == noErr);
	}
	BeginUpdate (theWindow);
	SetPortWindowPort(theWindow);
	GetWindowBounds(theWindow, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);
	EraseRect(&r);
	winUpdateFuncs [GetWindowKind(theWindow) - WIN_BASE_KIND] 
				(theEvent, theWindow);

	if (theWindow == _mt_window && existing_update_region) {
		set_invalid_region (theWindow, &rect);
	}
	aWin->drawn = TRUE;
	EndUpdate (theWindow);
}


static void
DoOsEvt (EventRecord *theEvent) {
	WindowRef win;
	short code;

	if ((theEvent->message & 0xff000000) == 0xfa000000) {
		/* Mouse Moved */

		code = FindWindow (theEvent->where, &win);
		if (code != inContent) {
			Rect r = {-1, -1, 2, 2};

			SetCursor(&qdarrow);
			OffsetRect (&r, theEvent->where.h, theEvent->where.v);
			RectRgn (gMouseRgn, &r);
		} else {
			int kind = GetWindowKind(win) - WIN_BASE_KIND;
			if (kind >= 0 && kind <= NHW_TEXT) {
				winCursorFuncs [kind] (theEvent, win, gMouseRgn);
			}
		}
	}
}


void
HandleEvent (EventRecord *theEvent) {
	switch (theEvent->what) {
	case autoKey :
	case keyDown :
		HandleKey (theEvent);
		break;
	case updateEvt :
		HandleUpdate (theEvent);
		break;
	case mouseDown :
		HandleClick (theEvent);
		break;
#if !TARGET_API_MAC_CARBON
	case diskEvt :
		if ((theEvent->message & 0xffff0000) != 0) {
			Point p = {150, 150};
			(void) DIBadMount (p, theEvent->message);
		}
		break;
#endif
	case osEvt :
		DoOsEvt (theEvent);
		break;
	case kHighLevelEvent:
		AEProcessAppleEvent(theEvent);
	default :
		break;
	}
}


void
mac_get_nh_event(void) {
	EventRecord anEvent;

	/* KMH -- Don't proceed if the window system isn't set up */
	if (!iflags.window_inited)
		return;

	(void) WaitNextEvent (everyEvent, &anEvent, -1, gMouseRgn);
	HandleEvent(&anEvent);
}


int
mac_nhgetch(void) {
	int ch;
	long doDawdle;
	EventRecord anEvent;

	/* We want to take care of keys in the buffer as fast as
	 * possible
	 */
	if (keyQueueCount)
		doDawdle = 0L;
	else {
		long total, contig;
		static char warn = 0;

		doDawdle = (in_topl_mode() ? GetCaretTime () : 120L);
		/* Since we have time, check memory */
		PurgeSpace (&total, &contig);
		if (contig < 25000L || total < 50000L) {
			if (!warn) {
				pline ("Low Memory!");
				warn = 1;
			}
		} else {
			warn = 0;
		}
	}

	do {
		(void) WaitNextEvent (everyEvent, &anEvent, doDawdle, gMouseRgn);
		HandleEvent (&anEvent);
		ch = GetFromKeyQueue ();
	} while (!ch && !gClickedToMove);

	if (!gClickedToMove)
		ObscureCursor ();
	else
		gClickedToMove = 0;

#ifdef THINK_C
	if (ch == '\r') ch = '\n';
#endif

	return ch;
}


void
mac_delay_output(void) {
	long destTicks = TickCount () + 1;

	while (TickCount () < destTicks) {
		mac_get_nh_event ();
	}
}


#ifdef CLIPPING
static void
mac_cliparound (int x, int y) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(x,y)
#endif
	/* TODO */
}
#endif

void
mac_exit_nhwindows (const char *s) {
	clear_screen ();
	tty_exit_nhwindows (s);
	mac_destroy_nhwindow (WIN_MESSAGE);
	mac_destroy_nhwindow (WIN_INVEN);
}


/*
 * Don't forget to decrease in_putstr before returning...
 */
void
mac_putstr (winid win, int attr, const char *str) {
	long len, slen;
	NhWindow *aWin = &theWindows [win];
	static char in_putstr = 0;
	short newWidth, maxWidth;
	Rect r;
	char *src, *sline, *dst, ch;

	if (win < 0 || win >= NUM_MACWINDOWS || !aWin->its_window) {
		error ("putstr: Invalid win %d (Max %d).", win, NUM_MACWINDOWS, attr);
		return;
	}

	if (aWin->its_window == _mt_window) {
		tty_putstr(win, attr, str);
		return;
	}

	if (in_putstr > 3)
		return;

	in_putstr ++;
	slen = strlen (str);

	SetPortWindowPort(aWin->its_window);
	GetWindowBounds(aWin->its_window, kWindowContentRgn, &r);
	OffsetRect(&r, -r.left, -r.top);
	if (win == WIN_MESSAGE) {
		r.right  -= SBARWIDTH;
		r.bottom -= SBARHEIGHT;
		if (flags.page_wait && 
			aWin->last_more_lin <= aWin->y_size - (r.bottom - r.top) / aWin->row_height) {
			aWin->last_more_lin = aWin->y_size;
			mac_display_nhwindow(win, TRUE);
		}
	}

	/*
	 * A "default" text window - uses TETextBox
	 * We just add the text, without attributes for now
	 */
	len = GetHandleSize (aWin->windowText);
	while (aWin->windowTextLen + slen + 1 > len) {
		len = (len > 2048) ? (len + 2048) : (len * 2);
		SetHandleSize (aWin->windowText, len);
		if (MemError ()) {
			error ("putstr: SetHandleSize");
			aWin->windowTextLen = 0L;
			aWin->save_lin = 0;
			aWin->y_curs = 0;
			aWin->y_size = 0;
		}
	}
	
	len = aWin->windowTextLen;
	dst = *(aWin->windowText) + len;
	sline = src = (char *)str;
	maxWidth = newWidth = 0;
	for (ch = *src; ch; ch = *src) {
		if (ch == CHAR_LF)
			ch = CHAR_CR;
		*dst++ = ch;
		if (ch == CHAR_CR) {
			aWin->y_curs ++;
			aWin->y_size ++;
			aWin->x_curs = 0;
			newWidth = TextWidth (sline, 0, src - sline);
			if (newWidth > maxWidth) {
				maxWidth = newWidth;
			}
			sline = src+1;	/* keep track of where new line begins */
		}
		else
			aWin->x_curs ++;
		src++;
	}

	newWidth = TextWidth (sline, 0, src - sline);
	if (newWidth > maxWidth) {
		maxWidth = newWidth;
	}

	aWin->windowTextLen += slen;
	
	if (ch != CHAR_CR) {
		(*(aWin->windowText)) [len + slen] = CHAR_CR;
		aWin->windowTextLen ++;
		aWin->y_curs ++;
		aWin->y_size ++;
		aWin->x_curs = 0;
	}

	if (win == WIN_MESSAGE) {
		short min = aWin->y_size - (r.bottom - r.top) / aWin->row_height;
		if (aWin->scrollPos < min) {
			aWin->scrollPos = min;
			SetControlMaximum (aWin->scrollBar, aWin->y_size);
			SetControlValue(aWin->scrollBar, min);
		}
		InvalWindowRect(aWin->its_window, &r);
	}
	else	/* Message has a fixed width, other windows base on content */
		if (maxWidth > aWin->x_size)
			aWin->x_size = maxWidth;
	in_putstr --;
}


void
mac_curs (winid win, int x, int y) {
	NhWindow *aWin = &theWindows [win];

	if (aWin->its_window == _mt_window) {
		tty_curs(win, x, y);
		return;
	}

	SetPortWindowPort(aWin->its_window);
	MoveTo (x * aWin->char_width, (y * aWin->row_height) + aWin->ascent_height);
	aWin->x_curs = x;
	aWin->y_curs = y;
}


int
mac_nh_poskey (int *a, int *b, int *c) {
	int ch = mac_nhgetch();
	*a = clicked_pos.h;
	*b = clicked_pos.v;
	*c = clicked_mod;
	return ch;
}


void
mac_start_menu (winid win) {
	HideWindow (theWindows [win].its_window);
	mac_clear_nhwindow (win);
}


void
mac_add_menu (winid win, int glyph, const anything *any, CHAR_P menuChar, CHAR_P groupAcc, int attr, const char *inStr, int preselected) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(glyph)
#endif
	NhWindow *aWin = &theWindows [win];
	const char *str;
	char locStr[4+BUFSZ];
	MacMHMenuItem *item;

	if (!inStr) return;

	if (any->a_void != 0) {

#define kMenuSizeBump 26
		if (!aWin->miSize) {
			aWin->menuInfo = (MacMHMenuItem **)NewHandle(sizeof(MacMHMenuItem) * kMenuSizeBump);
			if (!aWin->menuInfo) {
				error("Can't alloc menu handle");
				return;
			}
			aWin->menuSelected = (short **)NewHandle(sizeof(short) * kMenuSizeBump);
			if (!aWin->menuSelected) {
				error("Can't alloc menu select handle");
				return;
			}
			aWin->miSize = kMenuSizeBump;
		}

		if (aWin->miLen >= aWin->miSize) {
			SetHandleSize((Handle)aWin->menuInfo, sizeof(MacMHMenuItem) * (aWin->miLen+kMenuSizeBump));
			if (MemError()) {
				error("Can't resize menu handle");
				return;
			}
			SetHandleSize((Handle)aWin->menuSelected, sizeof(short) * (aWin->miLen+kMenuSizeBump));
			if (MemError()) {
				error("Can't resize menu select handle");
				return;
			}
			aWin->miSize += kMenuSizeBump;
		}

		if (menuChar == 0) {	
			if (('a' <= aWin->menuChar && aWin->menuChar <= 'z') ||
				('A' <= aWin->menuChar && aWin->menuChar <= 'Z')) {
				menuChar = aWin->menuChar++;
				if (menuChar == 'z')
					aWin->menuChar = 'A';
			}
		}
		
		Sprintf(locStr, "%c - %s", (menuChar ? menuChar : ' '), inStr);
		str = locStr;
		HLock ((char**)aWin->menuInfo);
		HLock ((char**)aWin->menuSelected);
		(*aWin->menuSelected)[aWin->miLen] = preselected;
		item = &(*aWin->menuInfo)[aWin->miLen];
		aWin->miLen++;
		item->id = *any;
		item->accelerator = menuChar;
		item->groupAcc = groupAcc;
		item->line = aWin->y_size;
		HUnlock ((char**)aWin->menuInfo);
		HUnlock ((char**)aWin->menuSelected);
	} else
		str = inStr;

	putstr (win, attr, str);
}


/*
 * End a menu in this window, window must a type NHW_MENU.
 * str is a list of cancel characters (values that may be input)
 * morestr is a prompt to display, rather than the default.
 * str and morestr might be ignored by some ports.
 */
void
mac_end_menu (winid win, const char *morestr) {
	Str255 buf;
	NhWindow *aWin = &theWindows [win];

	buf [0] = 0;
	if (morestr) 
		C2P (morestr, buf);
	SetWTitle (aWin->its_window, buf);
}


int
mac_select_menu (winid win, int how, menu_item **selected_list) {
	int c;
	NhWindow *aWin = &theWindows [win];
	WindowPtr theWin = aWin->its_window;

	inSelect = win;

	mac_display_nhwindow (win, FALSE);

	aWin->how = (short) how;
	for (;;) {
		c = map_menu_cmd (mac_nhgetch());
		if (c == CHAR_ESC) {
			/* deselect everything */
			aWin->miSelLen = 0;
			break;
		} else if (ClosingWindowChar(c)) {
			break;
		} else {
			nhbell();
		}
	}

	HideWindow (theWin);

	if (aWin->miSelLen) {
		menu_item *mp;
		MacMHMenuItem *mi;
		*selected_list = mp = (menu_item *) alloc(aWin->miSelLen * sizeof(menu_item));
		HLock ((char**)aWin->menuInfo);
		HLock ((char**)aWin->menuSelected);
		for (c = 0; c < aWin->miSelLen; c++) {
			mi = &(*aWin->menuInfo)[(*aWin->menuSelected) [c]];
			mp->item = mi->id;
			mp->count = -1L;
			mp++;
		}
		HUnlock ((char**)aWin->menuInfo);
		HUnlock ((char**)aWin->menuSelected);
	} else
		*selected_list = 0;

	inSelect = WIN_ERR;

	return aWin->miSelLen;
}

#include "dlb.h"

static void
mac_display_file (name, complain)
const char *name;	/* not ANSI prototype because of boolean parameter */
boolean complain;
{
	Ptr buf;
	int win;
	dlb *fp = dlb_fopen(name, "r");
	
	if (fp) {
		long l = dlb_fseek(fp, 0, SEEK_END);
		(void) dlb_fseek(fp, 0, 0L);
		buf = NewPtr(l+1);
		if (buf) {
			l = dlb_fread(buf, 1, l, fp);
			if (l > 0) {
				buf[l] = '\0';
				win = create_nhwindow(NHW_TEXT);
				if (WIN_ERR == win) {
					if (complain) error ("Cannot make window.");
				} else {
					putstr(win, 0, buf);
					display_nhwindow(win, FALSE);
				}
			}
			DisposePtr(buf);
		}
		dlb_fclose(fp);
	} else if (complain)
		error("Cannot open %s.", name);
}


void
port_help () {
	display_file (PORT_HELP, TRUE);
}


static void
mac_unimplemented (void) {
}


static void
mac_suspend_nhwindows (const char *foo) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(foo)
#endif
	/*	Can't really do that :-)		*/
}


int
try_key_queue (char *bufp) {
	if (keyQueueCount) {
		char ch;
		for (ch = GetFromKeyQueue(); ; ch = GetFromKeyQueue()) {
			if (ch == CHAR_LF || ch == CHAR_CR)
				ch = 0;
			*bufp++ = ch;
			if (ch == 0)
				break;
		}
		return 1;
	}
	return 0;
}

/* Interface definition, for windows.c */
struct window_procs mac_procs = {
	"mac",
	WC_COLOR | WC_HILITE_PET |
	WC_FONT_MAP | WC_FONT_MENU | WC_FONT_MESSAGE | WC_FONT_STATUS | WC_FONT_TEXT |
	WC_FONTSIZ_MAP | WC_FONTSIZ_MENU | WC_FONTSIZ_MESSAGE | WC_FONTSIZ_STATUS | WC_FONTSIZ_TEXT,
	0L,
	mac_init_nhwindows,
	mac_unimplemented,	/* see macmenu.c:mac_askname() for player selection */
	mac_askname,
	mac_get_nh_event,
	mac_exit_nhwindows,
	mac_suspend_nhwindows,
	mac_unimplemented,
	mac_create_nhwindow,
	mac_clear_nhwindow,
	mac_display_nhwindow,
	mac_destroy_nhwindow,
	mac_curs,
	mac_putstr,
	mac_display_file,
	mac_start_menu,
	mac_add_menu,
	mac_end_menu,
	mac_select_menu,
	genl_message_menu,
	mac_unimplemented,
	mac_get_nh_event,
	mac_get_nh_event,
#ifdef CLIPPING
	mac_cliparound,
#endif
#ifdef POSITIONBAR
	donull,
#endif
	tty_print_glyph,
	tty_raw_print,
	tty_raw_print_bold,
	mac_nhgetch,
	mac_nh_poskey,
	tty_nhbell,
	mac_doprev_message,
	mac_yn_function,
	mac_getlin,
	mac_get_ext_cmd,
	mac_number_pad,
	mac_delay_output,
#ifdef CHANGE_COLOR
	tty_change_color,
	tty_change_background,
	set_tty_font_name,
	tty_get_color_string,
#endif
/* other defs that really should go away (they're tty specific) */
	0, //    mac_start_screen,
	0, //    mac_end_screen,
	genl_outrip,
	genl_preference_update,
};

/*macwin.c*/