File: pmbitchx.c

package info (click to toggle)
ircii-pana 1%3A1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,048 kB
  • ctags: 14,567
  • sloc: ansic: 130,654; sql: 6,041; makefile: 4,313; cpp: 1,270; tcl: 1,230; sh: 638; java: 151
file content (3742 lines) | stat: -rw-r--r-- 93,301 bytes parent folder | download | duplicates (2)
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
/* pmbitchx.c Written by: Brian Smith (NuKe) and rosmo for BitchX */


#include <sys/stat.h>
/* These headers from the Warp 4 toolkit */
#include <os2/mciapi.h>
#include <os2/mcios2.h>
#include <io.h>
#include <stddef.h>
/* We now require dynamic windows */
#include <dw.h>
#include "window.h"
#include "gui.h"
#include "server.h"
#include "hook.h"
#include "list.h"
#include "commands.h"
#include "hash2.h"
#include "status.h"

/* Window data, located at QWP_USER */
typedef struct {
	HVPS    hvps;
	Screen *screen;
} MYWINDATA, *PMYWINDATA;

int	VIO_staticx=200;
int	VIO_staticy=100;
int	VIO_font_width=6;
int	VIO_font_height=10;

int cxvs, cxsb, cysb, cytb;

/* These are for $lastclickline() function */
char *lastclicklinedata = NULL;
int lastclickcol, lastclickrow;

/* These are for the context menu */
HWND contextmenu;
int contextx, contexty;

HAB	mainhab = 0;
HMQ	mainhmq = 0;

HAB	hab = 0;		 /* handle to the anchor block */
HMQ	hmq = 0;		 /* handle to the message queue */
HWND	hwndClient = 0;		 /* handle to the client */
HWND	hwndFrame = 0; 		 /* handle to the frame window */
HWND	hwndMenu = 0;            /* handle to the menu */
HWND    hwndLeft = 0, hwndRight = 0; /* For nicklist */
HWND    hwndscroll = 0, hwndnickscroll = 0; /* scrollerbars */
HWND    MDIClient = 0,
        MDIFrame = 0;            /* MDI handles */
QMSG	qmsg = { 0 };		 /* message-queue structure */
HDC	hdc = 0;		 /* handle to the device context */
HVPS	hvps = 0,
        mainHvps = 0,         /* handle to the AVIO presentation space */
	    hvpsnick = 0;
static HEV sem = 0;
VIOMODEINFO viomode;
FILE	*debug;
static unsigned char output_buf[256];
int     output_tail = 0;
LONG    menuY;
clock_t flush_counter;
int     pmthread = -1;

HMTX    mutex = 0;

ULONG  cx, cy;
int    just_resized = 0, dont_resize = 0, no_resize;
Screen *focus_screen, *just_resized_screen;
extern MenuStruct *morigin;

/* Used by window_menu_stub() */
ULONG   menuroot=4000;
ULONG   tmpmenuid=3000;

Screen *cursor_screen = NULL;

/* Used by popupmsg */
extern char *msgtext;

/* Used by the scrollers */
int newscrollerpos=0, lastscrollerpos=0, lastscrollerwindow=-1, lastpos=0;

/* Used by file dialog to make it non blocking */
char *codeptr, *paramptr;

/* Used by properties notebook */
static HWND hwndPage1;
static HWND hwndPage2;
static HWND hwndPage3;
static HWND hwndPage4;
static HWND hwndPage5;
int p1e, p2e, p3e, p4e, inproperties=0;
ChannelList *chan = NULL;
Window *nb_window=NULL;
FONTMETRICS fm;
int in_fontdialog=0;

IrcVariable *return_irc_var(int nummer);
IrcVariable *return_fset_var(int nummer);
CSetArray *return_cset_var(int nummer);
WSetArray *return_wset_var(int nummer);

int fontstart=0;

HMODULE hmod;
ULONG EXPENTRY (*DLL_mciPlayFile)(HWND, PSZ, ULONG, PSZ, HWND) = NULL;
ULONG EXPENTRY (*DLL_mciGetErrorString)(ULONG, PSZ, USHORT) = NULL;
ULONG EXPENTRY (*DLL_mciSendString)(PSZ, PSZ,USHORT, HWND, USHORT) = NULL;


#include "input.h"
#define         WIDTH   10

/* needed for rclick */
#include "keys.h"
unsigned long menucmd;

int guiipc[2];

int new_menuref(MenuRef **root, int refnum, HWND menuhandle, int menuid)
{
	MenuRef *new = new_malloc(sizeof(MenuRef));

	if(new)
	{
		new->refnum = refnum;
		new->menuhandle = menuhandle;
		new->menuid = menuid;
		new->next = NULL;

		if (!*root)
			*root = new;
		else
		{
			MenuRef *prev = NULL, *tmp = *root;
			while(tmp)
			{
				prev = tmp;
				tmp = tmp->next;
			}
			if(prev)
				prev->next = new;
			else
				*root = new;
		}
		return TRUE;
	}
	else
		return FALSE;
}

int remove_menuref(MenuRef **root, int refnum)
{
	MenuRef *prev = NULL, *tmp = *root;

	while(tmp)
	{
		if(tmp->refnum == refnum)
		{
			if(!prev)
			{
				new_free(&tmp);
                                *root = NULL;
                                return 0;
			}
			else
			{
				prev->next = tmp->next;
                                new_free(&tmp);
                                return 0;
			}
		}
		tmp = tmp->next;
	}
	return 0;
}

MenuRef *find_menuref(MenuRef *root, int refnum)
{
	MenuRef *tmp = root;

	while(tmp)
	{
		if(tmp->refnum == refnum)
			return tmp;
		tmp = tmp->next;
	}
	return NULL;
}


void gui_settitle(char *titletext, Screen *os2win) {
	WinSetWindowText(os2win->hwndFrame, titletext);
}

void aflush() {
	HMTX h = mutex;

	/* flush */
	if (output_tail > 0)
	{
		DosOpenMutexSem(NULL, &h);
		DosRequestMutexSem(h, SEM_INDEFINITE_WAIT);

		VioWrtTTY((PCH)output_buf, output_tail, (output_screen ? output_screen->hvps : hvps));
		output_tail = 0;
		DosReleaseMutexSem(h);
		DosCloseMutexSem(h);
	}
}

void sendevent(unsigned int event, unsigned int window)
{
	unsigned int evbuf[2];

	if(guiipc[1])
	{
		evbuf[0] = event;
		evbuf[1] = window;

		write(guiipc[1], (void *)evbuf, sizeof(unsigned int)*2);
	}
	return;
}

void flush_thread(void *param)
{
	clock_t foo;

	while (1 & 1)
	{
		foo = clock();
		if ((foo - flush_counter) > 10)
		{ if (output_tail > 0) { aflush(); } }
		DosSleep(100L);
	}
}

void cursor_off(HVPS this_hvps)
{
	VIOCURSORINFO bleah;

	VioGetCurType(&bleah, this_hvps);
	bleah.attr = -1;
	VioSetCurType(&bleah, this_hvps);
}

void cursor_thread(void)
{
	VIOCURSORINFO bleah;
	HVPS hvps;;

	while(1 & 1)
	{
		DosSleep(500L);
		if(cursor_screen && (hvps = cursor_screen->hvps))
		{
			VioGetCurType(&bleah, hvps);
			bleah.attr = 0;
			VioSetCurType(&bleah, hvps);
		}
		DosSleep(500L);
		if(cursor_screen && (hvps = cursor_screen->hvps))
		{
			VioGetCurType(&bleah, hvps);
			bleah.attr = -1;
			VioSetCurType(&bleah, hvps);
		}
	}
}

void aputc(int c) {
	HMTX h;

	DosOpenMutexSem(NULL, &h);
	DosRequestMutexSem(h, SEM_INDEFINITE_WAIT);

	/* buffer */
	if (output_tail < 256)
	{
		output_buf[output_tail] = (unsigned char) c;
		output_tail++;
		flush_counter = clock();
	}

	/* flush */
	if (output_tail == 256 || c == '\n' || c == '\r')
	{
		VioWrtTTY((PCH)output_buf, output_tail, (output_screen ? output_screen->hvps : hvps));
		output_tail = 0;
	}

	DosReleaseMutexSem(h);
	DosCloseMutexSem(h);
}

int gui_read(Screen *screen, char *buffer, int maxbufsize) {
	int i;	

	if (strlen(screen->aviokbdbuffer)> maxbufsize) {
		for(i=0;i<=strlen(screen->aviokbdbuffer);i++)
		{
			if (i<maxbufsize) {
				buffer[i]=screen->aviokbdbuffer[i];
			}
			else if(i==maxbufsize) {
				buffer[i]=0;
				screen->aviokbdbuffer[0]=screen->aviokbdbuffer[i];
			}
			else {
				screen->aviokbdbuffer[i-maxbufsize]=screen->aviokbdbuffer[i];
			}
		}
	} else {
		strcpy(buffer,screen->aviokbdbuffer);
		screen->aviokbdbuffer[0]='\0';
	}
	return strlen(buffer);
}

void aprintf(char *format, ...) {
	va_list args;
	char putbuf[AVIO_BUFFER+1], bluf[AVIO_BUFFER];
	USHORT  i, o;

	va_start(args, format);
	vsprintf(putbuf, format, args);
	va_end(args);

	i = o = 0;
	while (putbuf[i] != '\n' && putbuf[i] != 0) {
		i++;
		if (putbuf[i] == '\n' || putbuf[i] == 0) {
			strcpy(bluf, "");
			strncat(bluf, &putbuf[o], i - o);
			if (putbuf[i] != 0) strcat(bluf, "\r\n");
			VioWrtTTY((PCH)bluf,
				  strlen(bluf),
				  (output_screen ? output_screen->hvps : hvps));
			i++; o = i;
		}
	}
}

MRESULT EXPENTRY FontDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
	ULONG Remfonts = 0, Fonts = 0, i, i2;
	static PFONTMETRICS fonts;
	char buf[256];
	ULONG maxX, maxY, len;
	SWP menupos;
	unsigned char *scr;
	char fontsize[10];
	static Screen *changescreen, *savedscreen = NULL;

	switch (msg) {
	case WM_DESTROY:
		free(fonts);
	break;
	case WM_INITDLG:
		savedscreen = current_window ? current_window->screen : main_screen;
		VioQueryFonts(&Remfonts,
			      NULL,
			      0,
			      &Fonts,
			      "System VIO",
			      VQF_PUBLIC,
			      hvps);

		fonts = malloc(sizeof(FONTMETRICS) * Remfonts);

		Fonts = Remfonts;
		VioQueryFonts(&Remfonts,
			      fonts,
			      sizeof(FONTMETRICS),
			      &Fonts,
			      "System VIO",
			      VQF_PUBLIC,
			      hvps);

		for (i = 0; i < Fonts; i++)
		{
			if(fonts[i].lAveCharWidth != 5)
			{
				sprintf(buf, "%s %ux%u", fonts[i].szFacename, (unsigned int)fonts[i].lMaxBaselineExt, (unsigned int)fonts[i].lAveCharWidth);
				WinSendMsg(WinWindowFromID(hwnd, 101),
						   LM_INSERTITEM,
						   MPFROMSHORT(LIT_END),
						   MPFROMP(buf));
			}
			else
				fontstart=i;
		}
		fontstart++;
		break;
	case WM_COMMAND:
        switch (COMMANDMSG(&msg)->cmd)
	{
	case DID_CANCEL:
		WinDismissDlg(hwnd, 0);
		break;
	case DID_OK:
		i = (ULONG)WinSendMsg(WinWindowFromID(hwnd, LB_FONTLIST),
				      LM_QUERYSELECTION,
				      MPFROMSHORT(LIT_CURSOR),
				      0);

		if(i == LIT_NONE)
		{
			WinDismissDlg(hwnd, 0);
			return (MRESULT)0;
		}

		i+=fontstart;

		i2 = (ULONG)WinSendMsg(WinWindowFromID(hwnd, CB_DEFAULT),
				       BM_QUERYCHECK,
				       0,
				       0);
		if(i2)
		{
			sprintf(fontsize, "%dx%d", (int)fonts[i].lAveCharWidth, (int)fonts[i].lMaxBaselineExt);
			set_string_var(DEFAULT_FONT_VAR, fontsize);
		}

		i2 = (ULONG)WinSendMsg(WinWindowFromID(hwnd, CB_CHANGEFORALL),
				       BM_QUERYCHECK,
				       0, 0);

		if(i2)
			changescreen = screen_list;
		else
			changescreen = savedscreen;

		while(changescreen)
		{
			if(changescreen->alive)
			{
				len = (changescreen->co + 1) * changescreen->li * 2;
				scr = malloc(len);
				VioReadCellStr(scr, (PUSHORT)&len, 0, 0, changescreen->hvps);

				changescreen->VIO_font_width = fonts[i].lAveCharWidth;
				changescreen->VIO_font_height = fonts[i].lMaxBaselineExt;

				co = changescreen->co;
				li = changescreen->li;

				cx = (co - 1) * changescreen->VIO_font_width;
				cy = li * changescreen->VIO_font_height;

				maxX = WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
				maxY = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);

				/* No point in making bigger windows than the screen */
				if (cx > maxX)
					changescreen->co = (maxX / changescreen->VIO_font_width) -1;

				if (cy > maxY)
					changescreen->li = (maxY / changescreen->VIO_font_height) -1;

				/* Recalculate in case we modified the values */
				cx = (co - 1) * changescreen->VIO_font_width;
				cy = li * changescreen->VIO_font_height;

				if(changescreen->nicklist)
					cx += (changescreen->VIO_font_width * changescreen->nicklist) + cxvs;

				if(changescreen->hwndMenu==(HWND)NULL)
					WinSetWindowPos(changescreen->hwndFrame, HWND_TOP, 0, 0, cx + (cxsb*2) + cxvs, cy + (cysb*2)+cytb, SWP_SIZE);
				else
				{
					WinQueryWindowPos(changescreen->hwndMenu, &menupos);
					WinSetWindowPos(changescreen->hwndFrame, HWND_TOP, 0, 0, cx + (cxsb*2) + cxvs, cy + (cxsb*2)+cytb + menupos.cy, SWP_SIZE);
				}
				VioSetDeviceCellSize(changescreen->VIO_font_height, changescreen->VIO_font_width, changescreen->hvps);
				VioSetDeviceCellSize(changescreen->VIO_font_height, changescreen->VIO_font_width, changescreen->hvpsnick);

				VioWrtCellStr(scr, len, 0, 0, changescreen->hvps);
				free(scr);
			}

			if(i2)
				changescreen = changescreen->next;
			else
				changescreen = NULL;
		}
		WinDismissDlg(hwnd, 0);
		break;
	default: break;
	}
	return((MRESULT)0);
	break;
	}
	return (WinDefDlgProc(hwnd, msg, mp1, mp2));
}

void font_dialog_stub(Screen *screen)
{
	HAB hnbab;
	HMQ hnbmq;

	hnbab = WinInitialize(0);
	hnbmq = WinCreateMsgQueue(hnbab, 0);

	WinDlgBox(HWND_DESKTOP, screen->hwndFrame, FontDlgProc, NULLHANDLE, FONTDIALOG, NULL);
	in_fontdialog=0;

	WinDestroyMsgQueue(hnbmq);
	WinTerminate(hnbab);
}

void gui_font_dialog(Screen *screen)
{
	if(in_fontdialog)
		return;

	in_fontdialog=1;
	_beginthread((void *)font_dialog_stub, NULL, 0xFFFF, (void *)screen);
}

void set_right_window(HWND hwnd)
{
	Screen *this_screen = NULL;
	static Screen *last_screen = NULL;

	this_screen = (Screen *)WinQueryWindowPtr(WinQueryWindow(hwnd, QW_PARENT), 0);
	if (!this_screen)
		this_screen = (Screen *)WinQueryWindowPtr(hwnd, 0);
	if (!this_screen)
		this_screen = output_screen;

	if (this_screen != last_screen)
		make_window_current(this_screen->current_window);

	last_screen = this_screen;
}

int mesg(char *format, ...) {
	va_list args;
	char outbuf[256];

	va_start(args, format);
	vsprintf(outbuf, format, args);
	va_end(args);

	WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, outbuf, "PMBitchX", 0, MB_OK | MB_INFORMATION | MB_MOVEABLE);

	return strlen(outbuf);
}

/* Routines dealing with menus */
HWND newsubmenu(MenuStruct *menutoadd, HWND location, int pulldown)
{
	HWND tmphandle;
	MenuList *tmp;
	MENUITEM miSubMenu;
	HWND hwndSubmenu;
	ULONG itemmask, flag = 0L, id = 0L;

	if(menutoadd->sharedhandle && WinIsWindow(hab, menutoadd->sharedhandle))
		return menutoadd->sharedhandle;

	if(pulldown)
	{
		flag = MS_ACTIONBAR;
        id = FID_MENU;
	}

	tmphandle=WinCreateWindow(location,
							  WC_MENU,
							  NULL,
							  flag,
							  0,0,0,0,
							  location,
							  HWND_TOP,
							  id,
							  NULL,
							  NULL);

	/* Go through all menuitems adding items and submenus (and subitems) as neccessary */

	tmp = menutoadd->menuorigin;
	while(tmp!=NULL)
	{
		itemmask=0;
		hwndSubmenu=(HWND)NULLHANDLE;

		if(tmp->menutype & GUISUBMENU)
		{
			if(tmp->menutype & GUISHARED)
			{
				MenuStruct *tmpmenu = findmenu(tmp->submenu);

				if(!tmpmenu->sharedhandle || !WinIsWindow(hab, tmpmenu->sharedhandle))
				{
					hwndSubmenu=tmpmenu->sharedhandle=(HWND)newsubmenu(tmpmenu, HWND_OBJECT, FALSE);

					/* By mikh's recommendation :) */
					WinSetWindowULong(hwndSubmenu, QWL_STYLE,
									  WinQueryWindowULong(hwndSubmenu, QWL_STYLE) & ~MS_ACTIONBAR);
				}
				else
			hwndSubmenu=(HWND)tmpmenu->sharedhandle;
			}
			else
				hwndSubmenu=(HWND)newsubmenu((MenuStruct *)findmenu(tmp->submenu), tmphandle, FALSE);
		}
		miSubMenu.iPosition=MIT_END;
		miSubMenu.afStyle=MIS_TEXT;
		if(tmp->menutype & GUISEPARATOR)
			miSubMenu.afStyle = MIS_SEPARATOR;
		if(tmp->menutype & GUIBRKMENUITEM)
			miSubMenu.afStyle |= MIS_BREAKSEPARATOR;
		if(tmp->menutype & GUIBRKSUBMENU)
			miSubMenu.afStyle |= MIS_BREAKSEPARATOR;
		if(tmp->menutype & GUIIAMENUITEM)
			itemmask |= MIA_DISABLED;
		if(tmp->menutype & GUICHECKEDMENUITEM)
			itemmask |= MIA_CHECKED;
		if(tmp->menutype & GUINDMENUITEM)
			itemmask |= MIA_NODISMISS;

		miSubMenu.afAttribute=0;
		miSubMenu.id=tmp->menuid;
		miSubMenu.hwndSubMenu=hwndSubmenu;
		miSubMenu.hItem=NULLHANDLE;

		WinSendMsg((HWND)tmphandle,
				   MM_INSERTITEM,
				   MPFROMP(&miSubMenu),
				   MPFROMP(tmp->name));

		if(tmp->refnum > 0)
			new_menuref(&menutoadd->root, tmp->refnum, tmphandle, tmp->menuid);
		/* Set other options */
		if(itemmask != 0)
			WinSendMsg((HWND)tmphandle, MM_SETITEMATTR, MPFROM2SHORT(tmp->menuid, TRUE), MPFROM2SHORT(itemmask, itemmask));
		if(tmp->menutype & GUIDEFMENUITEM)
		{
			WinSetWindowBits((HWND)tmphandle, QWL_STYLE, MS_CONDITIONALCASCADE,MS_CONDITIONALCASCADE );
			/* Set cascade menu default */
			WinSendMsg((HWND)tmphandle, MM_SETDEFAULTITEMID, MPFROMSHORT(tmp->menuid), NULL );
		}

		tmp=tmp->next;
	}
	return tmphandle;
}

/* This is so I can create a menu easily from elsewhere in the code */

HWND menucreatestub(char *addthismenu, HWND location, int pulldown)
{
	MenuStruct *menutoadd;

	if((menutoadd = (MenuStruct *)findmenu(addthismenu))==NULL)
	{
		say("Menu not found.");
		return 0;
	}

	if(!menutoadd->menuorigin)
	{
		say("Cannot create blank menu.");
		return 0;
	}
	if(menutoadd->sharedhandle && WinIsWindow(hab, menutoadd->sharedhandle))
		return menutoadd->sharedhandle;
	else
        return newsubmenu(menutoadd, location, pulldown);
}

void detach_shared(Screen *this_screen)
{
	int itemid = (int)WinSendMsg(this_screen->hwndMenu, MM_ITEMIDFROMPOSITION, (MPARAM)0, (MPARAM)NULL);
	/* Set any owners of shared menus to HWND_OBJECT so the won't get destroyed - thanks mikh */
	if(this_screen->menu && WinIsWindow(hab, this_screen->hwndMenu))
	{
		MenuStruct *tmpmenu = findmenu(this_screen->menu);

        if(tmpmenu)
	{
		MenuList *current = tmpmenu->menuorigin;

		if(tmpmenu->sharedhandle && WinIsWindow(hab, tmpmenu->sharedhandle))
			WinSetOwner(tmpmenu->sharedhandle, HWND_OBJECT);
		else
		{
			while(current)
			{
				MenuStruct *submenu;

				if((current->menutype & GUISHARED) && (submenu = findmenu(current->submenu)))
				{
					if(submenu->sharedhandle && WinIsWindow(hab, submenu->sharedhandle))
						WinSetOwner(submenu->sharedhandle, HWND_OBJECT);
				}
				current = current->next;
			}
		}
	}
	}
	while(itemid != -1)
	{
		/* This may cause memory leaks... unless they are all shared  (menu.bx is) */
		WinSendMsg(this_screen->hwndMenu, MM_REMOVEITEM, MPFROM2SHORT(itemid, FALSE), NULL);
		itemid = (int)WinSendMsg(this_screen->hwndMenu, MM_ITEMIDFROMPOSITION, (MPARAM)0, (MPARAM)NULL);
	}
}

void window_menu_stub(Screen *menuscreen, char *addthismenu)
{
	int menustate=0, newmenustate=0;

	tmpmenuid=3000;

	if(!menuscreen)
		return;

	if(menuscreen->hwndMenu && WinIsWindow(hab, menuscreen->hwndMenu))
	{
		menustate=1;
		detach_shared(menuscreen);
		WinDestroyWindow(menuscreen->hwndMenu);
        menuscreen->hwndMenu = NULLHANDLE;
		new_free(&menuscreen->menu);
	}

	/* Create toplevel menu */

	aflush();
	if(addthismenu && strcmp(addthismenu, "-delete")!=0)
	{
		if((menuscreen->hwndMenu=(HWND)menucreatestub(addthismenu, menuscreen->hwndFrame, TRUE))!=(HWND)NULL)
		newmenustate=1;
	}
	else
        menuscreen->hwndMenu=(HWND)NULL;

	if(newmenustate==1)
		menuscreen->menu=m_strdup(addthismenu);
	else
		menuscreen->menu=NULL;

	/* Tell the Window to resize because of menu */
	WinSendMsg(menuscreen->hwndFrame, WM_UPDATEFRAME, 0, 0);

	if(menustate != newmenustate)   
		WinSendMsg(menuscreen->hwndFrame, WM_USER, 0, 0);
	new_free(&addthismenu);
}

void pm_seticon(Screen *screen)
{
	static HPOINTER icon = 0;

	if(!screen)
		return;

	/* For some reason the icon gets unset when spawning */
    if(!icon)
		icon = WinLoadPointer(HWND_DESKTOP,NULLHANDLE,IDM_MAINMENU);

	WinSendMsg(screen->hwndFrame, WM_SETICON, (MPARAM)icon, 0);
}

void pm_mdi_on(void)
{
	Screen *tmp = screen_list;
	HSWITCH hswitch;
	SWCNTRL swcntrl;
	SWP swpw, swpf;
	PID pid;
	ULONG width, height, x, y;

	WinSetParent(MDIFrame, HWND_DESKTOP, TRUE);

	WinQueryWindowProcess(MDIFrame, &pid, NULL);
 
	/* initialize switch structure */
	swcntrl.hwnd = MDIFrame;
	swcntrl.hwndIcon = NULLHANDLE;
	swcntrl.hprog = NULLHANDLE;
	swcntrl.idProcess = pid;
	swcntrl.idSession = 0;
	swcntrl.uchVisibility = SWL_VISIBLE;
	swcntrl.fbJump = SWL_JUMPABLE;
	strcpy(swcntrl.szSwtitle,irc_version);
 
	hswitch = WinCreateSwitchEntry(mainhab, &swcntrl);

	width = (ULONG)((((float)WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN)))*0.75);
	height = (ULONG)((((float)WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN)))*0.75);
	x = (ULONG)((WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN)-width)/2);
	y = (ULONG)((WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN)-height)/2);

	WinSetWindowPos(MDIFrame, 0, x, y, width, height, SWP_MOVE | SWP_SIZE | SWP_SHOW);
	WinShowWindow(MDIFrame, TRUE);

	WinQueryWindowPos(MDIClient, &swpf);
	WinSetWindowULong(MDIFrame, QWP_USER, swpf.cy);

	while(tmp)
	{

		if(tmp->alive)
		{
			WinQueryWindowPos(tmp->hwndFrame, &swpw);
			hswitch = WinQuerySwitchHandle(tmp->hwndFrame, 0);
			if(hswitch)
			{
				WinQuerySwitchEntry(hswitch, &swcntrl);
				swcntrl.uchVisibility = SWL_INVISIBLE;
				WinChangeSwitchEntry(hswitch, &swcntrl);
			}
			WinSetParent(tmp->hwndFrame, MDIClient, TRUE);
			WinSetWindowPos(tmp->hwndFrame, NULLHANDLE, swpw.x, swpw.y - (WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN) - swpf.cy), 0, 0, SWP_MOVE);
		}
		tmp = tmp->next;
	}
}

void pm_mdi_off(void)
{
	Screen *tmp = screen_list;
	HSWITCH hswitch;
	SWCNTRL swcntrl;
	SWP swpw, swpf;

	WinQueryWindowPos(MDIClient, &swpf);

	hswitch = WinQuerySwitchHandle(MDIFrame, 0);
	if(hswitch)
		WinRemoveSwitchEntry(hswitch);

	while(tmp)
	{
		if(tmp->alive)
		{
			WinQueryWindowPos(tmp->hwndFrame, &swpw);
			WinSetParent(tmp->hwndFrame, HWND_DESKTOP, TRUE);
			WinSetWindowPos(tmp->hwndFrame, NULLHANDLE, swpw.x, swpw.y + (WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN) - swpf.cy), 0, 0, SWP_MOVE);
			hswitch = WinQuerySwitchHandle(tmp->hwndFrame, 0);
			if(hswitch)
			{
				WinQuerySwitchEntry(hswitch, &swcntrl);
				swcntrl.uchVisibility = SWL_VISIBLE;
				WinChangeSwitchEntry(hswitch, &swcntrl);
			}
		}
		tmp = tmp->next;
	}

	WinShowWindow(MDIFrame, FALSE);
	WinSetParent(MDIFrame, HWND_OBJECT, TRUE);
}

int test_callback(HWND window, void *data)
{
	dw_window_destroy((HWND)data);
	return -1;
}

void pm_aboutbox(char *about_text)
{
	HWND mainwindow, text, stext, logo,
	     okbutton, buttonbox, lbbox;

	ULONG flStyle = FCF_SYSMENU | FCF_TITLEBAR |
		FCF_SHELLPOSITION | FCF_TASKLIST | FCF_DLGBORDER | FCF_SIZEBORDER | FCF_MINMAX;

	mainwindow = dw_window_new(HWND_DESKTOP, "About PMBitchX", flStyle);

	dw_window_set_icon(mainwindow, IDM_MAINMENU);

	lbbox = dw_box_new(BOXVERT, 10);

	dw_box_pack_start(mainwindow, lbbox, 0, 0, TRUE, TRUE, 0);

	stext = dw_text_new("PMBitchX (c) 2002 Colten Edwards, Brian Smith", 0);

	dw_window_set_style(stext, DT_VCENTER, DT_VCENTER);
	dw_window_set_style(stext, DT_CENTER, DT_CENTER);

	dw_box_pack_start(lbbox, stext, 130, 20, TRUE, FALSE, 10);

	buttonbox = dw_box_new(BOXHORZ, 0);

	dw_box_pack_start(lbbox, buttonbox, 0, 0, TRUE, FALSE, 0);

	dw_window_set_color(buttonbox, CLR_PALEGRAY, CLR_PALEGRAY);

	logo = dw_bitmap_new(0);

	dw_window_set_bitmap(logo, IDM_LOGO);

    dw_box_pack_start(buttonbox, 0, 52, 52, TRUE, FALSE, 5);
	dw_box_pack_start(buttonbox, logo, 52, 52, FALSE, FALSE, 5);
    dw_box_pack_start(buttonbox, 0, 52, 52, TRUE, FALSE, 5);

	text = dw_text_new(about_text, 0);

	dw_window_set_style(text, DT_CENTER, DT_CENTER);
	dw_window_set_style(text, DT_WORDBREAK, DT_WORDBREAK);

	dw_box_pack_start(lbbox, text, 130, 120, TRUE, TRUE, 10);

	buttonbox = dw_box_new(BOXHORZ, 0);

	dw_box_pack_start(lbbox, buttonbox, 0, 0, TRUE, FALSE, 0);

	okbutton = dw_button_new("Ok", 1001L);

    dw_box_pack_start(buttonbox, 0, 50, 30, TRUE, FALSE, 5);
	dw_box_pack_start(buttonbox, okbutton, 50, 30, FALSE, FALSE, 5);
    dw_box_pack_start(buttonbox, 0, 50, 30, TRUE, FALSE, 5);

	dw_window_set_usize(mainwindow, 435, 340);

	dw_window_show(mainwindow);

	dw_signal_connect(okbutton, "clicked", DW_SIGNAL_FUNC(test_callback), (void *)mainwindow);
	dw_signal_connect(mainwindow, "delete_event", DW_SIGNAL_FUNC(test_callback), (void *)mainwindow);
}

void pm_resize(Screen *this_screen)
{
	if(this_screen->co < 20) this_screen->old_co=this_screen->co=20;
	if(this_screen->li < 10) this_screen->old_li=this_screen->li=10;
	if(this_screen->co > 199) this_screen->old_co=this_screen->co=199;
	if(this_screen->li > 99) this_screen->old_li=this_screen->li=99;
	cx = this_screen->co * this_screen->VIO_font_width;
	cy = this_screen->li * this_screen->VIO_font_height;

	co = this_screen->co; li = this_screen->li;

	/* Recalculate some stuff that was done in input.c previously */
	this_screen->input_line = this_screen->li-1;

	this_screen->input_zone_len = this_screen->co - (WIDTH * 2);
	if (this_screen->input_zone_len < 10)
		this_screen->input_zone_len = 10;		/* Take that! */

	this_screen->input_start_zone = WIDTH;
	this_screen->input_end_zone = this_screen->co - WIDTH;
}

void drawnicklist(Screen *this_screen)
{
	NickList *n, *list = NULL;
	ChannelList *cptr;
	char minibuffer[NICKNAME_LEN+1], spaces[100];
	int nickcount=0, k;

	if(!this_screen)
		return;

	memset(spaces, ' ', 100);

	if(this_screen->current_window && this_screen->current_window->current_channel)
	{
		cptr = lookup_channel(this_screen->current_window->current_channel, this_screen->current_window->server, 0);
		list = sorted_nicklist(cptr, get_int_var(NICKLIST_SORT_VAR));

		if(this_screen->spos<0)
			this_screen->spos=0;
		for(n = list; n; n = n->next)
		{
			if(nickcount >= this_screen->spos && nickcount < this_screen->spos + this_screen->li)
			{
				minibuffer[0]=' ';
				minibuffer[1]='\0';
				if(nick_isvoice(n))
					strcpy(minibuffer, "+");
				if(nick_isop(n))
					strcpy(minibuffer, "@");
				if(nick_isircop(n))
				{
					if(minibuffer[0] == ' ')
						strcpy(minibuffer, "*");
					else
						strcat(minibuffer, "*");
				}
				strcat(minibuffer, n->nick);
				for(k=strlen(minibuffer);k<(this_screen->nicklist+1);k++)
					minibuffer[k]=' ';
				minibuffer[k]='\0';
				VioSetCurPos(nickcount-this_screen->spos, 0, this_screen->hvpsnick);
				if(this_screen->mpos == nickcount)
					VioWrtTTY("\e[0;1;37;44m", 12, this_screen->hvpsnick);
				VioWrtTTY(minibuffer, strlen(minibuffer), this_screen->hvpsnick);
				VioWrtTTY("\e[0m", 4, this_screen->hvpsnick);
			}
			nickcount++;
		}
	}

	if(nickcount < this_screen->li)
	{
		for(k=nickcount;k<this_screen->li;k++)
		{
			VioSetCurPos(k, 0, this_screen->hvpsnick);
			VioWrtTTY(spaces, NICKNAME_LEN, this_screen->hvpsnick);
		}
	}
	else if (this_screen->spos > nickcount-this_screen->li)
	{
		this_screen->spos=nickcount-this_screen->li;
		drawnicklist(this_screen);
	}

	WinSendMsg(this_screen->hwndnickscroll, SBM_SETSCROLLBAR, (MPARAM)this_screen->spos, MPFROM2SHORT(0, nickcount - this_screen->li));
	WinSendMsg(this_screen->hwndnickscroll, SBM_SETTHUMBSIZE, MPFROM2SHORT(this_screen->li, nickcount), (MPARAM)NULL);
    if(list)
		clear_sorted_nicklist(&list);
}

void pm_focus(char key)
{
	int count = 1, page = (int)(key - '0');
    Screen *tmp = screen_list;

	if(key == '0')
		page = 10;

	while(tmp)
	{
		if(count == page)
			WinSetFocus(HWND_DESKTOP, tmp->hwndClient);

		count++;
		tmp = tmp->next;
	}
}

MRESULT EXPENTRY FrameWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
	PMYWINDATA wd;
	HPS hps;
	Screen *this_screen = NULL;

	wd = (PMYWINDATA)WinQueryWindowPtr(hwnd, QWP_USER);

    if(wd)
		this_screen = wd->screen;

	switch (msg) {
	case WM_BUTTON1MOTIONSTART:
	case WM_BUTTON2MOTIONSTART:
	case WM_BUTTON1UP:
	case WM_BUTTON2UP:
	case WM_BUTTON1DOWN:
	case WM_BUTTON2DOWN:
	case WM_SETFOCUS:
	case WM_COMMAND:
	case WM_CONTEXTMENU:
	case WM_CHAR:
		{
			if(this_screen)
				return GenericWndProc(this_screen->hwndClient, msg, mp1, mp2);
			else
				return NULL;
		}
		break;
	case WM_PAINT:
			hps = WinBeginPaint(hwnd, hvps, NULL);
			if (this_screen)
			{
					VioShowPS(this_screen->li+1,
							  this_screen->co+1,
							  0,
							  this_screen->hvps);
					if(this_screen->nicklist)
							VioShowPS(this_screen->li+1,
									  this_screen->nicklist + 1,
									  0,
									  this_screen->hvpsnick);
			}
			WinEndPaint(hps);
			return (WinDefAVioWindowProc(hwnd, msg, (ULONG)mp1, (ULONG)mp2));
	}
	return (MRESULT)WinDefWindowProc(hwnd, msg, mp1, mp2);
}

MRESULT EXPENTRY GenericWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{

	HPS   hps;
	int tmp, statusstart;
	SHORT x, y, i;
	SWP swp, menupos;
	static HVPS hvps;
	Screen *this_screen = NULL;
	PMYWINDATA wd;
	static int sx, sy, tx, ty;
	static unsigned char *mark = NULL;
	ULONG len;
	POINTL ptl;
	static PVOID shared;
	TRACKINFO ti;
	APIRET rc;
	char *selectionbuffer;
	VIOCURSORINFO bleah;

	wd = (PMYWINDATA)WinQueryWindowPtr(hwnd, QWP_USER);
	if (!wd)
		wd = (PMYWINDATA)WinQueryWindowPtr(WinWindowFromID(hwnd, FID_CLIENT), QWP_USER);

	if (wd)
	{
		hvps = wd->hvps;
		this_screen = wd->screen;
		if (!this_screen) { wd->screen = main_screen; this_screen = wd->screen; }
	}

	switch (msg) {
		/* rosmo's awesome copy functions */
	case WM_BUTTON1MOTIONSTART:
		if(this_screen)
	{
		WinQueryPointerPos(HWND_DESKTOP, &ptl);
		WinMapWindowPoints(HWND_DESKTOP, hwnd, &ptl, 1);

		WinQueryWindowPos(hwnd, &swp);
		ti.rclTrack.xLeft   = ti.rclTrack.xRight = (int)(((int)(ptl.x/this_screen->VIO_font_width))*this_screen->VIO_font_width)+1;
		ti.rclTrack.yBottom = ti.rclTrack.yTop   = (int)(((int)(ptl.y/this_screen->VIO_font_height)+1)*this_screen->VIO_font_height)+1;
		ti.rclTrack.xLeft--;
		ti.rclTrack.yBottom--;

		WinQueryWindowRect(this_screen->hwndLeft, &ti.rclBoundary);
		ti.cxBorder	= ti.cyBorder = 1;
		ti.cxGrid 	= this_screen->VIO_font_width;
		ti.cyGrid 	= this_screen->VIO_font_height;
		ti.cxKeyboard = 0;
		ti.cyKeyboard = 0;
		ti.ptlMinTrackSize.x = 0;
		ti.ptlMinTrackSize.y = 0;
		ti.ptlMaxTrackSize.x = swp.cx + this_screen->VIO_font_width;
		ti.ptlMaxTrackSize.y = swp.cy + this_screen->VIO_font_height;
		ti.fs = TF_BOTTOM | TF_RIGHT;

		/* Allocate a buffer to store the entire screen so it won't change
		 during selection - NuKe */

		selectionbuffer = new_malloc((this_screen->li * this_screen->co) + 1);

		for(i=0;i<this_screen->li;i++)
			VioReadCharStr(&selectionbuffer[i*this_screen->co], (PUSHORT)&this_screen->co, i, 0, hvps);

		if (WinTrackRect(hwnd, NULLHANDLE, &ti))
		{
			sx = (int)(ti.rclTrack.xLeft/this_screen->VIO_font_width);
			ty = this_screen->li - (int)(ti.rclTrack.yBottom/this_screen->VIO_font_height);
			tx = (int)(ti.rclTrack.xRight/this_screen->VIO_font_width);
			sy = this_screen->li - (int)(ti.rclTrack.yTop/this_screen->VIO_font_height);

			mark = new_malloc((ty - sy) * (tx - sx + 2) + 1); i = 0;
			for (y = sy; y < ty; y++)
			{
				len = tx - sx;
				memcpy(&mark[i], &selectionbuffer[(y * this_screen->co) + sx], len);
				i += len - 1;
				/* get rid of trailing white space */
				while(mark[i] == ' ' && i > 0)
					i--;
				mark[i + 1] = '\r';
				mark[i + 2] = '\n';
				i += 2;
			}


			mark[i] = '\0';

			/* Ok, we got the stuff - now place it in the clipboard */
			WinOpenClipbrd(hab); /* Open clipboard */
			WinEmptyClipbrd(hab); /* Empty clipboard */

			/* Ok, clipboard wants giveable unnamed shared memory */

			shared = NULL;
			rc = DosAllocSharedMem(&shared,
					       NULL,
					       i,
					       OBJ_GIVEABLE | PAG_COMMIT | PAG_READ | PAG_WRITE);

			if (rc == 0)
			{
				memcpy(shared, mark, i);

				WinSetClipbrdData(hab,
						  (ULONG)shared,
						  CF_TEXT,
						  CFI_POINTER);
			}

			WinCloseClipbrd(hab); /* Close clipboard */

			new_free(&selectionbuffer);
			new_free(&mark);
		}
	}
		break;
	case WM_SETFOCUS:
        if (SHORT1FROMMP(mp2)==TRUE)
	{
		/* If we have a shared menu attached to the window make it owned by the
		 * current window so it opens properly
		 */
		cursor_screen = this_screen;

		if(this_screen && this_screen->current_window)
		{
			if(this_screen->menu)
			{
				MenuStruct *tmpmenu = findmenu(this_screen->menu);

				if(tmpmenu)
				{
					MenuList *current = tmpmenu->menuorigin;

					if(tmpmenu->sharedhandle && WinIsWindow(hab, tmpmenu->sharedhandle))
						WinSetOwner(tmpmenu->sharedhandle, this_screen->hwndClient);
					else
					{
						while(current)
						{
							HWND hwndmenu;
							MenuStruct *submenu;

							if((current->menutype & GUISHARED) && (submenu = findmenu(current->submenu)))
							{
								if(submenu->sharedhandle && WinIsWindow(hab, submenu->sharedhandle) && (hwndmenu = WinWindowFromID(this_screen->hwndFrame, FID_MENU)))
									WinSetOwner(submenu->sharedhandle, hwndmenu);
							}
							current = current->next;
						}
					}
				}
			}
            WinSetCp(hmq, this_screen->codepage);
            WinSetCp(mainhmq, this_screen->codepage);
			/* Tell the main thread to set this window as current */
			sendevent(EVFOCUS, this_screen->current_window->refnum);
		}
	}
	else
	{
		cursor_screen = NULL;
		if(this_screen)
		{
			VioGetCurType(&bleah, this_screen->hvps);
			bleah.attr = -1;
			VioSetCurType(&bleah, this_screen->hvps);
		}
	}
	break;
	case WM_COMMAND:
		menucmd=COMMANDMSG(&msg)->cmd;
		sendevent(EVMENU, this_screen->current_window->refnum);
		return((MRESULT)0);
		break;
	case WM_CLOSE:
#ifdef WINDOW_CREATE
		kill_screen(this_screen);
#else
		irc_exit(1, NULL, "%s rocks your world!", VERSION);
#endif
		break;
	case WM_PAINT:
		hps = WinBeginPaint(hwnd, hvps, NULL);
		if (this_screen)
		{
			VioShowPS(this_screen->li+1,
				  this_screen->co+1,
				  0,
				  this_screen->hvps);
                        if(this_screen->nicklist)
				VioShowPS(this_screen->li+1,
					  this_screen->nicklist + 1,
					  0,
					  this_screen->hvpsnick);
		}
		WinEndPaint(hps);
		return (WinDefAVioWindowProc(hwnd, msg, (ULONG)mp1, (ULONG)mp2));
	case WM_CHAR:

		if((SHORT1FROMMP(mp1) & KC_KEYUP)) break;
		if(strlen(this_screen->aviokbdbuffer)==254) {
			DosBeep(1000, 200);
				 } else {
			if(SHORT1FROMMP(mp2) != 0) {
				tmp=strlen(this_screen->aviokbdbuffer);
				if (CHAR1FROMMP(mp2) == 224) {
					this_screen->aviokbdbuffer[tmp]='\e';
					this_screen->aviokbdbuffer[tmp+1]=CHAR2FROMMP(mp2);
					this_screen->aviokbdbuffer[tmp+2]=0;
				} else {
					/* Fake a SIGINT on CTRL-C */
#if 0
					if((SHORT1FROMMP(mp1) & KC_CTRL) && SHORT1FROMMP(mp2) == 99)
						raise(SIGINT);
#endif
					if((SHORT1FROMMP(mp1) & KC_ALT) && SHORT1FROMMP(mp2) >= '0' && SHORT1FROMMP(mp2) <= '9')
						pm_focus((char)SHORT1FROMMP(mp2));
					else if ((SHORT1FROMMP(mp1) & KC_CTRL) && (SHORT1FROMMP(mp2) > 96) && (SHORT1FROMMP(mp2) < 122))
						this_screen->aviokbdbuffer[tmp]=SHORT1FROMMP(mp2)-96;
					else
						this_screen->aviokbdbuffer[tmp]=SHORT1FROMMP(mp2);
					this_screen->aviokbdbuffer[tmp+1]=0;
				}
			}
				 }
		/* Force select() to exit */
		sendevent(EVNONE, 0);
		break;

	case WM_BUTTON1UP:
	case WM_BUTTON2UP:
	case WM_BUTTON3UP:
	case WM_BUTTON1DBLCLK:
	case WM_BUTTON2DBLCLK:
	case WM_BUTTON3DBLCLK:
		{
			/* Save the server, and restore it when we are done. */
			int save_server = from_server;

			WinQueryPointerPos(HWND_DESKTOP, &ptl);
			WinMapWindowPoints(HWND_DESKTOP, this_screen->hwndClient, &ptl, 1);
			contextx=ptl.x;contexty=ptl.y;
			lastclickrow=this_screen->li - ((int)(ptl.y/this_screen->VIO_font_height)) - 1;
			lastclickcol=((int)(ptl.x/this_screen->VIO_font_width));
			last_input_screen=output_screen=this_screen;
			make_window_current(this_screen->current_window);

			from_server = current_window->server;

			if(this_screen->nicklist && lastclickcol > this_screen->co)
			{
				USHORT len = this_screen->nicklist + 1;

				lastclickcol = 0;
				if(lastclicklinedata != NULL)
				{
					VioReadCharStr((PCH)lastclicklinedata,(PUSHORT)&len,lastclickrow,0,this_screen->hvpsnick);
					lastclicklinedata[len] = 0;
				}
				if(lastclicklinedata[0] == ' ')
					memmove(lastclicklinedata, &lastclicklinedata[1], strlen(lastclicklinedata));

				this_screen->mpos = lastclickrow + this_screen->spos;
				drawnicklist(this_screen);

				switch(msg)
				{
				case WM_BUTTON1UP:
					wm_process(NICKLISTLCLICK);
					break;
				case WM_BUTTON2UP:
					wm_process(NICKLISTRCLICK);
					break;
				case WM_BUTTON3UP:
					wm_process(NICKLISTMCLICK);
					break;
				case WM_BUTTON1DBLCLK:
					wm_process(NICKLISTLDBLCLICK);
					break;
				case WM_BUTTON2DBLCLK:
					wm_process(NICKLISTRDBLCLICK);
					break;
				case WM_BUTTON3DBLCLK:
					wm_process(NICKLISTMDBLCLICK);
					break;
				}

				from_server = save_server;

				return NULL;
			}

			if(lastclicklinedata != NULL)
				VioReadCharStr((PCH)lastclicklinedata,(PUSHORT)&this_screen->co,lastclickrow,0,this_screen->hvps);
			statusstart=this_screen->li - 2;

			/* I was hoping there was an easier way but....I gave up */

			if(this_screen->window_list_end->status_lines == 0 && this_screen->window_list_end->double_status == 1 && this_screen->window_list_end->status_split == 1)
				statusstart=statusstart-1;
			if(this_screen->window_list_end->status_lines == 1 && this_screen->window_list_end->double_status == 0 && this_screen->window_list_end->status_split == 0)
				statusstart=statusstart-1;
			if(this_screen->window_list_end->status_lines == 1 && this_screen->window_list_end->double_status == 1 && this_screen->window_list_end->status_split == 1)
				statusstart=statusstart-1;
			if(this_screen->window_list_end->status_lines == 1 && this_screen->window_list_end->double_status == 1 && this_screen->window_list_end->status_split == 0)
				statusstart=statusstart-2;
			if(lastclickrow <= (current_window->screen->li - 2) && lastclickrow >= statusstart)
			{
				switch(msg)
				{
				case WM_BUTTON1UP:
					wm_process(STATUSLCLICK);
					break;
				case WM_BUTTON2UP:
					wm_process(STATUSRCLICK);
					break;
				case WM_BUTTON3UP:
					wm_process(STATUSMCLICK);
					break;
				case WM_BUTTON1DBLCLK:
					wm_process(STATUSLDBLCLICK);
					break;
				case WM_BUTTON2DBLCLK:
					wm_process(STATUSRDBLCLICK);
					break;
				case WM_BUTTON3DBLCLK:
					wm_process(STATUSMDBLCLICK);
					break;
				}
			}
			else
			{
				switch(msg)
				{
				case WM_BUTTON1UP:
					wm_process(LCLICK);
					break;
				case WM_BUTTON2UP:
					wm_process(RCLICK);
					break;
				case WM_BUTTON3UP:
					wm_process(MCLICK);
					break;
				case WM_BUTTON1DBLCLK:
					wm_process(LDBLCLICK);
					break;
				case WM_BUTTON2DBLCLK:
					wm_process(RDBLCLICK);
					break;
				case WM_BUTTON3DBLCLK:
					wm_process(MDBLCLICK);
					break;
				}
			}

			from_server = save_server;
		}
		break;
	case WM_VSCROLL:
		if(SHORT1FROMMP(mp1) == 1000)
		{   /* Channel is being scrolled */
			switch(SHORT2FROMMP(mp2))
			{
			case SB_LINEUP:
				sendevent(EVSUP, this_screen->current_window->refnum);
				break;
			case SB_LINEDOWN:
				sendevent(EVSDOWN, this_screen->current_window->refnum);
				break;
			case SB_PAGEUP:
				sendevent(EVSUPPG, this_screen->current_window->refnum);
				break;
			case SB_PAGEDOWN:
                        sendevent(EVSDOWNPG, this_screen->current_window->refnum);
			break;
			case SB_SLIDERTRACK:
				newscrollerpos=SHORT1FROMMP(mp2);
				if(newscrollerpos<0)
					newscrollerpos=0;
				if(newscrollerpos>get_int_var(SCROLLBACK_VAR))
					newscrollerpos=get_int_var(SCROLLBACK_VAR);
				sendevent(EVSTRACK, this_screen->current_window->refnum);
				break;
			}
		} else if(SHORT1FROMMP(mp1) == 1001)
		{   /* Nicklist is scrolling */
			switch(SHORT2FROMMP(mp2))
			{
			case SB_LINEUP:
				this_screen->spos--;
				break;
			case SB_LINEDOWN:
				this_screen->spos++;
				break;
			case SB_PAGEUP:
				this_screen->spos -= this_screen->li;
				break;
			case SB_PAGEDOWN:
				this_screen->spos += this_screen->li;
				break;
			case SB_SLIDERPOSITION:
			case SB_SLIDERTRACK:
				this_screen->spos=SHORT1FROMMP(mp2);
				break;
			}
			drawnicklist(this_screen);
		}
		break;
	case WM_USER:
		dont_resize=1;
		if(this_screen->hwndMenu==(HWND)NULL)
		{
			WinSetWindowPos(this_screen->hwndFrame, HWND_TOP, 0, 0, (this_screen->VIO_font_width * this_screen->nicklist) + cx + (cxsb*2) + (cxvs+(this_screen->nicklist ? cxvs : 0)), cy + (cysb*2) + cytb + 1, SWP_SIZE);
		}
		else
		{
			WinQueryWindowPos(this_screen->hwndMenu, &menupos);
			if(menupos.cy<3)
				WinSetWindowPos(this_screen->hwndFrame, HWND_TOP, 0, 0, (this_screen->VIO_font_width * this_screen->nicklist) + cx + (cxvs+(this_screen->nicklist ? cxvs : 0)) + (cxsb*2), cy + (cysb*2)+cytb + WinQuerySysValue(HWND_DESKTOP, SV_CYMENU), SWP_SIZE);
			else
				WinSetWindowPos(this_screen->hwndFrame, HWND_TOP, 0, 0, (this_screen->VIO_font_width * this_screen->nicklist) + cx + (cxvs+(this_screen->nicklist ? cxvs : 0)) + (cxsb*2), cy + (cysb*2)+cytb + menupos.cy, SWP_SIZE);
		}
		drawnicklist(this_screen);
		sendevent(EVREFRESH, this_screen->current_window->refnum);
		break;
	case WM_USER+2:
		pm_new_window((Screen *)mp1, (Window *)mp2);
		break;
	case WM_USER+3:
		if(mp1)
			pm_mdi_on();
		else
			pm_mdi_off();
		break;
	case WM_USER+4:
		gui_kill_window((Screen *)mp1);
		break;
	case WM_USER+5:
        pm_aboutbox((char *)mp1);
        break;
	case WM_USER+6:
		dont_resize=1;
		window_menu_stub((Screen *)mp1, (char *)mp2);
		break;
	case 0x041e:
		if(just_resized == 1 && this_screen == just_resized_screen)
		{
			WinPostMsg(hwnd, WM_USER, 0, 0);
			just_resized=0;
		}
		break;
	case 0x041f:
		if(just_resized == 1 && this_screen == just_resized_screen)
		{
			WinPostMsg(hwnd, WM_USER, 0, 0);
			  just_resized=0;
		}
		break;
	case WM_MINMAXFRAME:
		{
			PSWP pSwp = PVOIDFROMMP(mp1);

			if(pSwp->fl & SWP_MAXIMIZE)
			{
				dont_resize=1;
				WinPostMsg(hwnd, WM_USER, 0, 0);
			}
		}
		break;
	case WM_SIZE:
		if (!this_screen) return((MRESULT)0);

		x=SHORT1FROMMP(mp2); y=SHORT2FROMMP(mp2);
		if(x && y) {
			int nx, ny, ncx, ncy;
			HDC hdc;

			this_screen->old_co = this_screen->co=((int)((x-(cxvs+(this_screen->nicklist ? cxvs : 0)))/this_screen->VIO_font_width)) - this_screen->nicklist;
			this_screen->old_li = this_screen->li=((int)(y/this_screen->VIO_font_height));

			nx = 0;
			ny = 0;
			ncx = (x-(cxvs+(this_screen->nicklist ? cxvs : 0))) - (this_screen->nicklist * this_screen->VIO_font_width);
			ncy = y;


			VioAssociate((HDC)NULL, this_screen->hvps);
			WinSetWindowPos(this_screen->hwndLeft, NULLHANDLE, nx, ny-((this_screen->VIO_font_height*VIO_staticy)-ncy), ncx, ncy+abs((this_screen->VIO_font_height*VIO_staticy)-ncy), SWP_MOVE | SWP_SIZE);
			hdc = WinOpenWindowDC(this_screen->hwndLeft);
			VioAssociate(hdc, this_screen->hvps);

			nx += ncx;
			ncx = cxvs;

			WinSetWindowPos(this_screen->hwndscroll, NULLHANDLE, nx, ny, ncx, ncy, SWP_MOVE | SWP_SIZE);

			if(this_screen->nicklist)
			{
				nx += ncx;
				ncx = this_screen->nicklist * this_screen->VIO_font_width;

				VioAssociate((HDC)NULL, this_screen->hvpsnick);
				WinSetWindowPos(this_screen->hwndRight, NULLHANDLE, nx, ny-((this_screen->VIO_font_height*VIO_staticy)-ncy), ncx, ncy+abs((this_screen->VIO_font_height*VIO_staticy)-ncy), SWP_MOVE | SWP_SIZE);
				hdc = WinOpenWindowDC(this_screen->hwndRight);
				VioAssociate(hdc, this_screen->hvpsnick);

				nx += ncx;
				ncx =  cxvs;

				WinSetWindowPos(this_screen->hwndnickscroll, NULLHANDLE, nx, ny, ncx, ncy, SWP_MOVE | SWP_SIZE);
			}

			pm_resize(this_screen);

			recalculate_windows(this_screen);
			make_window_current(this_screen->current_window);
			if(dont_resize == 1)
				dont_resize = 0;
			else
			{
				just_resized_screen=this_screen;
				just_resized=1;
			}
		}
		WinDefAVioWindowProc(hwnd, msg, (ULONG)mp1, (ULONG)mp2);
		break;
	}
	return WinDefWindowProc(hwnd, msg, mp1, mp2);
}

MRESULT EXPENTRY MDIWndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
{
	switch( msg )
	{
	case WM_PAINT:
		{
		HPS    hps;
		RECTL  rc;

		hps = WinBeginPaint( hwnd, 0L, &rc );
		WinFillRect(hps, &rc, CLR_DARKGRAY);
		WinEndPaint( hps );
		break;
		}
	case WM_SIZE:
		{
			Screen *tmp = screen_list;
			ULONG oldheight = WinQueryWindowULong(MDIFrame, QWP_USER);

			if(!SHORT1FROMMP(mp2) && !SHORT2FROMMP(mp2))
				return NULL;

			if(oldheight && (oldheight-SHORT2FROMMP(mp2)))
			{
				while(tmp)
				{
					if(tmp->alive)
					{
						SWP swp;

						WinQueryWindowPos(tmp->hwndFrame, &swp);
						WinSetWindowPos(tmp->hwndFrame,0, swp.x, swp.y - (oldheight-SHORT2FROMMP(mp2)), 0, 0, SWP_MOVE);
					}
					tmp = tmp->next;
				}
			}
			WinSetWindowULong(MDIFrame, QWP_USER, SHORT2FROMMP(mp2));
		}
		break;
	case WM_CLOSE:
		irc_exit(1, NULL, "%s exiting", VERSION);
		break;
	default:
		return WinDefWindowProc( hwnd, msg, mp1, mp2 );
	}
	return (MRESULT)FALSE;
}

void avio_exit(void) {
	VioAssociate((HDC)NULL, (output_screen ? output_screen->hvps : hvps));
	VioDestroyPS((output_screen ? output_screen->hvps : hvps));
	WinDestroyWindow(hwndFrame);
	WinDestroyMsgQueue(hmq);
	WinTerminate(hab);
	DosExit(EXIT_PROCESS, 0);
}

void avio_init() {

	HEV	 mySem = sem;
	PMYWINDATA mywindata;

	ULONG flStyle = FCF_MINMAX | FCF_SYSMENU | FCF_TITLEBAR |
		FCF_SIZEBORDER | FCF_SHELLPOSITION | FCF_TASKLIST | FCF_ICON;

	pmthread = *_threadid;

	DosOpenEventSem(NULL, (PHEV)&mySem);

	hab = WinInitialize(0);
	hmq = WinCreateMsgQueue(hab, 0);

	cxvs = WinQuerySysValue(HWND_DESKTOP, SV_CXVSCROLL);
	cxsb = WinQuerySysValue(HWND_DESKTOP, SV_CXSIZEBORDER);
	cysb = WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER);
	cytb = WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR);

	if (!WinRegisterClass(hab, "AVIO", GenericWndProc, CS_SIZEREDRAW, 32))
		DosExit(EXIT_PROCESS, 1);
	WinRegisterClass(hab, "BXMDI", MDIWndProc, CS_SIZEREDRAW | CS_CLIPCHILDREN, 0);

	MDIFrame = WinCreateStdWindow(HWND_DESKTOP,
				      WS_CLIPCHILDREN,
				      &flStyle,
				      "BXMDI",
				      irc_version,
				      WS_CLIPCHILDREN,
				      NULLHANDLE,
				      IDM_MAINMENU,
				      &MDIClient);

	hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
				       0,
				       &flStyle,
				       "AVIO",
				       irc_version,
				       0L,
				       NULLHANDLE,
				       IDM_MAINMENU,
				       &hwndClient);

	hwndLeft = WinCreateWindow(hwndClient,
				   WC_FRAME,
				   NULL,
				   WS_VISIBLE,
				   0,0,2000,1000,
				   hwndClient,
				   HWND_TOP,
				   0L,
				   NULL,
				   NULL);

	hwndRight = WinCreateWindow(hwndClient,
				    WC_FRAME,
				    NULL,
				    WS_VISIBLE,
				    0,0,2000,1000,
				    hwndClient,
				    HWND_TOP,
				    0L,
				    NULL,
				    NULL);

	hwndscroll = WinCreateWindow(hwndClient,
				     WC_SCROLLBAR,
				     NULL,
				     WS_VISIBLE | SBS_VERT,
				     0,0,100,100,
				     hwndClient,
				     HWND_TOP,
				     1000L,
				     NULL,
				     NULL);
	hwndnickscroll = WinCreateWindow(hwndClient,
					 WC_SCROLLBAR,
					 NULL,
					 WS_VISIBLE | SBS_VERT,
					 0,0,100,100,
					 hwndClient,
					 HWND_TOP,
					 1001L,
					 NULL,
					 NULL);
	hwndMenu=(HWND)NULL;

	current_term->TI_cols = co = 81;
	current_term->TI_lines = li = 25;
	cx = (co+14) * VIO_font_width;
	cy = li * VIO_font_height;

	WinSetParent(MDIFrame, HWND_OBJECT, FALSE);
	/* Setup main window */
	hdc = WinOpenWindowDC(hwndLeft);
	VioCreatePS(&hvps, VIO_staticy, VIO_staticx, 0,
		    1, 0);
	VioAssociate(hdc, hvps);

	/* Setup nicklist */
	hdc = WinOpenWindowDC(hwndRight);
	VioCreatePS(&hvpsnick, VIO_staticy, VIO_staticx, 0,
		    1, 0);
	VioAssociate(hdc, hvpsnick);

	cursor_off(hvpsnick);

	VIO_font_width=6;VIO_font_height=10;

	/* Set font size */
	VioSetDeviceCellSize(VIO_font_height, VIO_font_width, hvps);
	VioSetDeviceCellSize(VIO_font_height, VIO_font_width, hvpsnick);

	DosPostEventSem(mySem);
	DosCloseEventSem(mySem);

	WinSendMsg(hwndscroll, SBM_SETSCROLLBAR, (MPARAM)get_int_var(SCROLLBACK_VAR), MPFROM2SHORT(0, get_int_var(SCROLLBACK_VAR)));

	DosCreateMutexSem(NULL, &mutex, 0, FALSE);

	lastclicklinedata = new_malloc(500);

	mywindata = new_malloc(sizeof(MYWINDATA));
	memset(mywindata, '\0', sizeof(MYWINDATA));
	mywindata->hvps  = hvps;
	mywindata->screen = NULL;
	WinSetWindowPtr(hwndClient, QWP_USER, mywindata);
	WinSetWindowPtr(hwndLeft, QWP_USER, mywindata);
	WinSetWindowPtr(hwndRight, QWP_USER, mywindata);

	WinSubclassWindow(hwndLeft, FrameWndProc);
	WinSubclassWindow(hwndRight, FrameWndProc);

	WinSendMsg(hwndnickscroll, SBM_SETSCROLLBAR, (MPARAM)0, MPFROM2SHORT(0, li));
	WinSendMsg(hwndnickscroll, SBM_SETTHUMBSIZE, MPFROM2SHORT(li, 0), (MPARAM)NULL);

	setmode(guiipc[0], O_BINARY);
	setmode(guiipc[1], O_BINARY);

	while (WinGetMsg(hab, &qmsg, (HWND)NULL, 0, 0))
	    WinDispatchMsg(hab, &qmsg);

}

void load_default_font(Screen *font_screen)
{
	char *fontsize, *height, *width;
	int t, cy, cx;
	SWP     menupos;

	if((fontsize=get_string_var(DEFAULT_FONT_VAR))!=NULL)
	{
		width=new_malloc(10);
		strcpy(width, fontsize);
		t=0;
		while(width[t]!='x' && t<9)
			t++;
		height=&width[t+1];
		width[t]=0;
		VIO_font_width=atoi(width);
		VIO_font_height=atoi(height);
		new_free(&width);
		if (VIO_font_width < 6 || VIO_font_height < 8)
		{
			VIO_font_width=6;VIO_font_height=10;
		}
	}
	else
	{
		VIO_font_width=6;VIO_font_height=10;
	}

	font_screen->VIO_font_width=VIO_font_width;
	font_screen->VIO_font_height=VIO_font_height;

	if(VIO_font_width != 6 || VIO_font_height != 10)
	{
		cx = (font_screen->co - 1) * font_screen->VIO_font_width;
		cy = font_screen->li * font_screen->VIO_font_height;
		cx += font_screen->VIO_font_width * font_screen->nicklist;
		if(font_screen->nicklist)
			cx += cxvs;

		VioSetDeviceCellSize(font_screen->VIO_font_height, font_screen->VIO_font_width, font_screen->hvps);			  /* Set the font size */
		VioSetDeviceCellSize(font_screen->VIO_font_height, font_screen->VIO_font_width, font_screen->hvpsnick);
		if(font_screen->hwndMenu==(HWND)NULL)
			WinSetWindowPos(font_screen->hwndFrame, HWND_TOP, 0, 0, cx+(cxsb*2)+1, cy+(cysb+cytb), SWP_SIZE);
		else
		{
			WinQueryWindowPos(font_screen->hwndMenu, &menupos);
			WinSetWindowPos(font_screen->hwndFrame, HWND_TOP, 0, 0, cx+(cxsb*2)+cxvs, cy+(cysb*2)+cytb+menupos.cy, SWP_SIZE);
		}
	}
}

/* Individual Page Procs */
MRESULT EXPENTRY NotebookPage1DlgProc(HWND hWnd, ULONG msg, MPARAM mp1,	MPARAM mp2)
{
	char szBuffer[1024];
	int i;
	char *ptr;

	switch (msg)
	{
		/* Perform dialog initialization	*/
	case WM_INITDLG:
		WinSetDlgItemText(hWnd,	EF_ENTRY1, "");
		p1e=LIT_NONE;
		break;
		/* Process control selections */
	case WM_CONTROL:
		switch(SHORT1FROMMP(mp1))
		{
		case SETS1:
			if(p1e!=LIT_NONE)
			{
				switch(return_irc_var(p1e)->type)
				{
				case BOOL_TYPE_VAR:
					i = (ULONG)WinSendMsg(WinWindowFromID(hWnd, CB_ONOFF1),
							      BM_QUERYCHECK,
							      0,
							      0);
					set_int_var(p1e, i);
					break;
				case CHAR_TYPE_VAR:
					WinQueryDlgItemText(hWnd, EF_ENTRY1,	1024L, szBuffer);
					set_string_var(p1e, szBuffer);
					break;
				case STR_TYPE_VAR:
					WinQueryDlgItemText(hWnd, EF_ENTRY1,	1024L, szBuffer);
					set_string_var(p1e, szBuffer);
					break;
				case INT_TYPE_VAR:
					WinQueryDlgItemText(hWnd, EF_ENTRY1,	1024L, szBuffer);
					set_int_var(p1e, my_atol((char *)&szBuffer));
					break;
				}
			}
			p1e = (ULONG)WinSendMsg(WinWindowFromID(hWnd, SETS1),
						LM_QUERYSELECTION,
						MPFROMSHORT(LIT_CURSOR),
						0);
			if(p1e!=LIT_NONE)
			{
				switch(return_irc_var(p1e)->type)
				{
				case BOOL_TYPE_VAR:
					WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF1), TRUE);
					WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY1), FALSE);
					i = get_int_var(p1e);
					WinSendMsg(WinWindowFromID(hWnd, CB_ONOFF1),BM_SETCHECK,MPFROMSHORT(i),0);
					WinSetDlgItemText(hWnd,	EF_ENTRY1, "");
					break;
				case INT_TYPE_VAR:
					WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF1), FALSE);
					WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY1), TRUE);
					sprintf(szBuffer, "%d", get_int_var(p1e));
					WinSetDlgItemText(hWnd,	EF_ENTRY1, szBuffer);
					break;
				case STR_TYPE_VAR:
					WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF1), FALSE);
					WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY1), TRUE);
					ptr=get_string_var(p1e);
					if(ptr && *ptr)
						strcpy(szBuffer, ptr);
					else
						strcpy(szBuffer, empty_string);
					WinSetDlgItemText(hWnd,	EF_ENTRY1, szBuffer);
					break;
				case CHAR_TYPE_VAR:
					WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF1), FALSE);
					WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY1), TRUE);
					ptr = get_string_var(p1e);
					if(ptr && *ptr)
						strcpy(szBuffer, ptr);
					else
						strcpy(szBuffer, empty_string);
					WinSetDlgItemText(hWnd,	EF_ENTRY1, szBuffer);
					break;
				}
			}
			break;
		}
		break;
		/* Process push	button selections */
	case WM_COMMAND:
		switch ( SHORT1FROMMP(mp1) )
		{
		case DID_OK:
			if(p1e!=LIT_NONE)
			{
				switch(return_irc_var(p1e)->type)
				{
				case BOOL_TYPE_VAR:
					i = (ULONG)WinSendMsg(WinWindowFromID(hWnd, CB_ONOFF1),BM_QUERYCHECK,0,0);
					set_int_var(p1e, i);
					break;
				case CHAR_TYPE_VAR:
					WinQueryDlgItemText(hWnd, EF_ENTRY1,	1024L, szBuffer);
					set_string_var(p1e, szBuffer);
					break;
				case STR_TYPE_VAR:
					WinQueryDlgItemText(hWnd, EF_ENTRY1,	1024L, szBuffer);
					set_string_var(p1e, szBuffer);
					break;
				case INT_TYPE_VAR:
					WinQueryDlgItemText(hWnd, EF_ENTRY1,	1024L, szBuffer);
					set_int_var(p1e, my_atol((char *)&szBuffer));
					break;
				}
			}
			break;
		}

		break;
		/* Close requested, exit dialogue */
	case WM_CLOSE:
		WinDismissDlg(hWnd, FALSE);
		break;

		/* Pass	through	unhandled messages */
	default:
		return(WinDefDlgProc(hWnd, msg, mp1, mp2));
	}
	return(0L);
}

MRESULT	EXPENTRY NotebookPage2DlgProc(HWND hWnd, ULONG msg, MPARAM mp1,	MPARAM mp2)

{
	 char szBuffer[1024];
	 int i;
	 char *ptr;
 
	 switch (msg)
	 {
		 /* Perform dialog initialization */
	 case WM_INITDLG :
		 WinSetDlgItemText(hWnd,	EF_ENTRY2, "");
		 if(nb_window && nb_window->current_channel)
			 chan = (ChannelList *) find_in_list((List **)get_server_channels(from_server), nb_window->current_channel, 0);
		 else
			 chan = NULL;
		 p2e=LIT_NONE;
		 break;
		 /* Process control selections */
	 case WM_CONTROL:
		 switch(SHORT1FROMMP(mp1))
		 {
		 case SETS2:
			 if(p2e!=LIT_NONE && chan)
			 {
				 switch(return_cset_var(p2e)->type)
				 {
				 case BOOL_TYPE_VAR:
					 i = (ULONG)WinSendMsg(WinWindowFromID(hWnd, CB_ONOFF2),
							       BM_QUERYCHECK,
							       0,
							       0);
					 set_cset_int_var(chan->csets, p2e, i);
					 break;
				 case CHAR_TYPE_VAR:
					 WinQueryDlgItemText(hWnd, EF_ENTRY2,	1024L, szBuffer);
					 set_cset_str_var(chan->csets, p2e, szBuffer);
					 break;
				 case STR_TYPE_VAR:
					 WinQueryDlgItemText(hWnd, EF_ENTRY2,	1024L, szBuffer);
					 set_cset_str_var(chan->csets, p2e, szBuffer);
					 break;
				 case INT_TYPE_VAR:
					 WinQueryDlgItemText(hWnd, EF_ENTRY2,	1024L, szBuffer);
					 set_cset_int_var(chan->csets, p2e, my_atol((char *)&szBuffer));
					 break;
				 }
			 }
			 p2e = (ULONG)WinSendMsg(WinWindowFromID(hWnd, SETS2),
						 LM_QUERYSELECTION,
						 MPFROMSHORT(LIT_CURSOR),
						 0);
			 if(p2e!=LIT_NONE && chan)
			 {
				 switch(return_cset_var(p2e)->type)
				 {
				 case BOOL_TYPE_VAR:
					 WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF2), TRUE);
					 WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY2), FALSE);
					 i = get_cset_int_var(chan->csets, p2e);
					 WinSendMsg(WinWindowFromID(hWnd, CB_ONOFF2),
						    BM_SETCHECK,
						    MPFROMSHORT(i),
						    0);
					 WinSetDlgItemText(hWnd,	EF_ENTRY2, empty_string);
					 break;
				 case INT_TYPE_VAR:
					 WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF2), FALSE);
					 WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY2), TRUE);
					 sprintf(szBuffer, "%d", get_cset_int_var(chan->csets, p2e));
					 WinSetDlgItemText(hWnd,	EF_ENTRY2, szBuffer);
					 break;
				 case STR_TYPE_VAR:
					 WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF2), FALSE);
					 WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY2), TRUE);
					 ptr=get_cset_str_var(chan->csets, p2e);
					 if(ptr && *ptr)
						 strcpy(szBuffer, ptr);
					 else
						 strcpy(szBuffer, empty_string);
					 WinSetDlgItemText(hWnd,	EF_ENTRY2, szBuffer);
					 break;
				 case CHAR_TYPE_VAR:
					 WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF2), FALSE);
					 WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY2), TRUE);
					 ptr = get_cset_str_var(chan->csets, p2e);
					 if(ptr && *ptr)
						 strcpy(szBuffer, ptr);
					 else
						 strcpy(szBuffer, empty_string);
					 WinSetDlgItemText(hWnd,	EF_ENTRY2, szBuffer);
					 break;
				 }
			 }
			 break;
		 }
		 break;
		 /* Process push	button selections */
	 case WM_COMMAND:
	 switch (SHORT1FROMMP(mp1))
	 {
	 case DID_OK	:
		 if(p2e!=LIT_NONE && chan)
		 {
			 switch(return_cset_var(p2e)->type)
			 {
			 case BOOL_TYPE_VAR:
				 i = (ULONG)WinSendMsg(WinWindowFromID(hWnd, CB_ONOFF2),
						       BM_QUERYCHECK,
						       0,
					   0);
				 set_cset_int_var(chan->csets, p2e, i);
				 break;
			 case CHAR_TYPE_VAR:
				 WinQueryDlgItemText(hWnd, EF_ENTRY2,	1024L, szBuffer);
				 set_cset_str_var(chan->csets, p2e, szBuffer);
				 break;
			 case STR_TYPE_VAR:
				 WinQueryDlgItemText(hWnd, EF_ENTRY2,	1024L, szBuffer);
				 set_cset_str_var(chan->csets, p2e, szBuffer);
				 break;
			 case INT_TYPE_VAR:
				 WinQueryDlgItemText(hWnd, EF_ENTRY2,	1024L, szBuffer);
				 set_cset_int_var(chan->csets, p2e, my_atol((char *)&szBuffer));
				 break;
			 }
		 }
		 break;
	 }

	 break;
	 /* Close requested, exit dialogue */
	 case WM_CLOSE:
	 WinDismissDlg(hWnd, FALSE);
	 break;

	 /* Pass through unhandled messages */
	 default:
	     return(WinDefDlgProc(hWnd, msg, mp1, mp2));
	 }
	 return(0L);
}

MRESULT	EXPENTRY NotebookPage3DlgProc(HWND hWnd, ULONG msg, MPARAM mp1,	MPARAM mp2)
{
	char szBuffer[1024]; /* String Buffer */
	char *ptr;

	switch ( msg )
	{
		/* Perform dialog initialization */
	case WM_INITDLG :
		WinSetDlgItemText(hWnd,	EF_ENTRY3, empty_string);
		p3e=LIT_NONE;
		break;
		/* Process control selections */
	case WM_CONTROL:
		switch(SHORT1FROMMP(mp1))
		{
		case SETS3:
			if(p3e!=LIT_NONE)
			{
				WinQueryDlgItemText(hWnd, EF_ENTRY3,	1024L, szBuffer);
				fset_string_var(p3e, szBuffer);
			}
			p3e = (ULONG)WinSendMsg(WinWindowFromID(hWnd, SETS3),
						LM_QUERYSELECTION,
						MPFROMSHORT(LIT_CURSOR),
							  0);
			if(p3e!=LIT_NONE)
			{
				WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF3), FALSE);
				WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY3), TRUE);
				ptr=fget_string_var(p3e);
				if(ptr && *ptr)
					strcpy(szBuffer, ptr);
				else
					strcpy(szBuffer, empty_string);
				WinSetDlgItemText(hWnd,	EF_ENTRY3, szBuffer);
			}
			break;
		}
		break;
		/* Process push	button selections */
	case WM_COMMAND:
		switch ( SHORT1FROMMP(mp1) )
		{
		case DID_OK	:
			if(p3e!=LIT_NONE)
			{
				WinQueryDlgItemText(hWnd, EF_ENTRY3, 1024L, szBuffer);
				fset_string_var(p3e, szBuffer);
			}
			break;
		}

		break;
	   /* Close requested, exit dialogue */
	case WM_CLOSE:
		WinDismissDlg(hWnd, FALSE);
		break;

		/* Pass	through	unhandled messages */
	default:
		return(WinDefDlgProc(hWnd, msg, mp1, mp2));
	}
	return(0L);
}

MRESULT	EXPENTRY NotebookPage4DlgProc(HWND hWnd, ULONG msg, MPARAM mp1,	MPARAM mp2)

{
	char szBuffer[1024]; /* String Buffer */
	char *ptr;

	switch (msg)
	{
		/* Perform dialog initialization */
	case WM_INITDLG :
		WinSetDlgItemText(hWnd,	EF_ENTRY4, empty_string);
		p4e=LIT_NONE;
		break;
		/* Process control selections */
	case WM_CONTROL :
		switch(SHORT1FROMMP(mp1))
		{
		case SETS4:
			if(p4e!=LIT_NONE && nb_window)
			{
				WinQueryDlgItemText(hWnd, EF_ENTRY4,	1024L, szBuffer);
				set_wset_string_var(nb_window->wset, p4e, szBuffer);
			}
			p4e = (ULONG)WinSendMsg(WinWindowFromID(hWnd, SETS4),
						LM_QUERYSELECTION,
						MPFROMSHORT(LIT_CURSOR),
						0);
			if(p4e!=LIT_NONE && nb_window)
			{
				WinEnableWindow(WinWindowFromID(hWnd, CB_ONOFF4), FALSE);
				WinEnableWindow(WinWindowFromID(hWnd, EF_ENTRY4), TRUE);
				ptr=get_wset_string_var(nb_window->wset, p4e);
				if(ptr && *ptr)
					strcpy(szBuffer, ptr);
				else
					strcpy(szBuffer, empty_string);
				WinSetDlgItemText(hWnd,	EF_ENTRY4, szBuffer);
			}
			break;
		}
		break;
		/* Process push	button selections */
	case WM_COMMAND :
		switch ( SHORT1FROMMP(mp1) )
		{
		case DID_OK	:
			if(p4e!=LIT_NONE && nb_window)
			{
				WinQueryDlgItemText(hWnd, EF_ENTRY4,	1024L, szBuffer);
				set_wset_string_var(nb_window->wset, p4e, szBuffer);
			}
			break;
		}

		break;
		/* Close requested, exit dialogue */
	case WM_CLOSE :
		WinDismissDlg(hWnd, FALSE);
		break;

		/* Pass	through	unhandled messages */
	default :
		return(WinDefDlgProc(hWnd, msg, mp1, mp2));
	}
	return(0L);
}



MRESULT	EXPENTRY NotebookPage5DlgProc(HWND hWnd, ULONG msg, MPARAM mp1,	MPARAM mp2)
{
	switch (msg)
	{
	case	WM_CHAR	:
		break;
		/* Perform dialog initialization */
	case	WM_INITDLG :
		break;
		/* Process control selections */
	case	WM_CONTROL :
		switch ( SHORT2FROMMP(mp1) )
		{
		}
		break;
		/* Process push	button selections */
	case	WM_COMMAND :
		switch ( SHORT1FROMMP(mp1) )
		{
		case DID_OK	:
			break;
		}
		break;
		/* Close requested, exit dialogue */
	case	WM_CLOSE :
		WinDismissDlg(hWnd, FALSE);
		break;

		/* Pass	through	unhandled messages */
	default :
		return(WinDefDlgProc(hWnd, msg, mp1, mp2));
	}
	return(0L);
}

MRESULT	EXPENTRY NotebookDlgProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
	HPS   hPS;			   /* Temporary	Presentation Space */
	HWND  hwndNBK;     		   /* Notebook Window Handle */
	PVOID ppb;			   /* Dialogue Template	Pointer */
	RECTL rcl;			   /* Rectangle	Holder */
	ULONG ulPageID;    		   /* Inserted Page ID */
	int t;
	char szBuffer[200];

	switch ( msg )
	{
		/* Perform dialog initialization */
	case WM_INITDLG:

		inproperties=1;

		nb_window=current_window;

		GpiQueryFontMetrics(hPS = WinGetPS(hWnd), sizeof(FONTMETRICS), &fm);

		if(nb_window->current_channel)
			sprintf(szBuffer, "PMBitchX Properties for %s", nb_window->current_channel);
		else
			strcpy(szBuffer, "PMBitchX Properties");

		hwndNBK = WinWindowFromID(hWnd, ID_PROP);

		WinSetWindowText(WinWindowFromID(hWnd, FID_TITLEBAR), szBuffer);

		/* Page 1 - Sets */

		ulPageID = (ULONG)WinSendMsg(hwndNBK, BKM_INSERTPAGE, 0L,
					     MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE |	BKA_MAJOR), BKA_LAST));

		WinSendMsg(hwndNBK, BKM_SETSTATUSLINETEXT,
			   MPFROMLONG(ulPageID), MPFROMP("Page 1 of 5"));

		WinSendMsg(hwndNBK, BKM_SETTABTEXT,
			   MPFROMLONG(ulPageID), MPFROMP("~Sets"));

		DosGetResource((HMODULE)NULL, RT_DIALOG, NBKP_SETS1, (PPVOID)&ppb);
		hwndPage1 = WinCreateDlg(HWND_DESKTOP, (HWND)NULL, (PFNWP)NotebookPage1DlgProc,
					 (PDLGTEMPLATE)ppb, NULL);
		DosFreeResource((PVOID)ppb);
		WinSendMsg(hwndNBK, BKM_SETPAGEWINDOWHWND,
			   MPFROMLONG(ulPageID), MPFROMHWND(hwndPage1));

		/* Page 2 - Csets */

		ulPageID = (ULONG)WinSendMsg(hwndNBK, BKM_INSERTPAGE, 0L,
					     MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE | BKA_MAJOR), BKA_LAST));

		WinSendMsg(hwndNBK, BKM_SETSTATUSLINETEXT,
			   MPFROMLONG(ulPageID), MPFROMP("Page 2 of 5"));

		WinSendMsg(hwndNBK, BKM_SETTABTEXT,
			   MPFROMLONG(ulPageID), MPFROMP("~Csets"));

		DosGetResource((HMODULE)NULL, RT_DIALOG, NBKP_SETS2, (PPVOID)&ppb);
		hwndPage2 = WinCreateDlg(HWND_DESKTOP, (HWND)NULL, (PFNWP)NotebookPage2DlgProc,
					 (PDLGTEMPLATE)ppb, NULL);
		DosFreeResource((PVOID)ppb);
		WinSendMsg(hwndNBK, BKM_SETPAGEWINDOWHWND,
			   MPFROMLONG(ulPageID), MPFROMHWND(hwndPage2));

		/* Page 3 - Fsets */

		ulPageID = (ULONG)WinSendMsg(hwndNBK, BKM_INSERTPAGE, 0L,
					     MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE | BKA_MAJOR), BKA_LAST));

		WinSendMsg(hwndNBK, BKM_SETSTATUSLINETEXT,
			   MPFROMLONG(ulPageID), MPFROMP("Page 3 of 5"));

		WinSendMsg(hwndNBK, BKM_SETTABTEXT,
			   MPFROMLONG(ulPageID), MPFROMP("~Fsets"));

		DosGetResource((HMODULE)NULL, RT_DIALOG, NBKP_SETS3, (PPVOID)&ppb);
		hwndPage3 = WinCreateDlg(HWND_DESKTOP, (HWND)NULL, (PFNWP)NotebookPage3DlgProc,
					 (PDLGTEMPLATE)ppb, NULL);
		DosFreeResource((PVOID)ppb);
		WinSendMsg(hwndNBK, BKM_SETPAGEWINDOWHWND,
			   MPFROMLONG(ulPageID), MPFROMHWND(hwndPage3));

		/* Page 4 - Wsets */

		ulPageID = (ULONG)WinSendMsg(hwndNBK, BKM_INSERTPAGE, 0L,
                                     MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE | BKA_MAJOR), BKA_LAST));

		WinSendMsg(hwndNBK, BKM_SETSTATUSLINETEXT,
			   MPFROMLONG(ulPageID), MPFROMP("Page 4 of 5"));

		WinSendMsg(hwndNBK, BKM_SETTABTEXT,
			   MPFROMLONG(ulPageID), MPFROMP("~Wsets"));

		DosGetResource((HMODULE)NULL, RT_DIALOG, NBKP_SETS4, (PPVOID)&ppb);
		hwndPage4 = WinCreateDlg(HWND_DESKTOP, (HWND)NULL, (PFNWP)NotebookPage4DlgProc,
					 (PDLGTEMPLATE)ppb, NULL);
		DosFreeResource((PVOID)ppb);
		WinSendMsg(hwndNBK, BKM_SETPAGEWINDOWHWND,
			   MPFROMLONG(ulPageID), MPFROMHWND(hwndPage4));

		/* Page 5 - OS/2 Settings */

		ulPageID = (ULONG)WinSendMsg(hwndNBK, BKM_INSERTPAGE, 0L,
					     MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE | BKA_MAJOR), BKA_LAST));

		WinSendMsg(hwndNBK, BKM_SETSTATUSLINETEXT,
			   MPFROMLONG(ulPageID), MPFROMP("Page 5 of 5"));

		WinSendMsg(hwndNBK, BKM_SETTABTEXT,
			   MPFROMLONG(ulPageID), MPFROMP("~OS/2 Settings"));

		DosGetResource((HMODULE)NULL, RT_DIALOG, NBKP_OSSETTINGS, (PPVOID)&ppb);
		hwndPage5 = WinCreateDlg(HWND_DESKTOP, (HWND)NULL, (PFNWP)NotebookPage4DlgProc,
					 (PDLGTEMPLATE)ppb, NULL);
		DosFreeResource((PVOID)ppb);
		WinSendMsg(hwndNBK, BKM_SETPAGEWINDOWHWND,
			   MPFROMLONG(ulPageID), MPFROMHWND(hwndPage5));

		rcl.xLeft = rcl.yBottom = 0L;
		rcl.xRight = rcl.yTop = 400L;

		WinDrawText(hPS = WinGetPS(hWnd), -1, " Second Page ", &rcl, CLR_BLACK, CLR_BLACK,
			    DT_LEFT | DT_BOTTOM | DT_QUERYEXTENT);
		WinReleasePS(hPS);
		WinSendMsg(hwndNBK, BKM_SETDIMENSIONS,
			   MPFROM2SHORT((SHORT)(rcl.xRight - rcl.xLeft),	(SHORT)(fm.lMaxBaselineExt * 2)),
			   MPFROMSHORT(BKA_MAJORTAB));

		/* Set the dimension of the notebook buttons	*/

		WinSendMsg(hwndNBK, BKM_SETDIMENSIONS, MPFROM2SHORT(21, 21),
			   MPFROMLONG(BKA_PAGEBUTTON));

		/* Set the background colours to that of the dialog */

		WinSendMsg(hwndNBK, BKM_SETNOTEBOOKCOLORS, MPFROMLONG(SYSCLR_DIALOGBACKGROUND),
			   MPFROMLONG(BKA_BACKGROUNDPAGECOLORINDEX));
		WinSendMsg(hwndNBK, BKM_SETNOTEBOOKCOLORS, MPFROMLONG(SYSCLR_DIALOGBACKGROUND),
			   MPFROMLONG(BKA_BACKGROUNDMAJORCOLORINDEX));

		for(t=0; t<NUMBER_OF_VARIABLES; t++)
		{
			WinSendMsg(WinWindowFromID(hwndPage1, SETS1),
				   LM_INSERTITEM,
				   MPFROMSHORT(LIT_END),
				   MPFROMP(return_irc_var(t)->name));
		}

		for(t=0; t<NUMBER_OF_CSETS; t++)
		{
			WinSendMsg(WinWindowFromID(hwndPage2, SETS2),
				   LM_INSERTITEM,
				   MPFROMSHORT(LIT_END),
				   MPFROMP(return_cset_var(t)->name));
		}

		for(t=0; t<NUMBER_OF_FSET; t++)
		{
			WinSendMsg(WinWindowFromID(hwndPage3, SETS3),
				   LM_INSERTITEM,
				   MPFROMSHORT(LIT_END),
				   MPFROMP(return_fset_var(t)->name));
		}

		for(t=0; t<NUMBER_OF_WSETS; t++)
		{
			WinSendMsg(WinWindowFromID(hwndPage4, SETS4),
				   LM_INSERTITEM,
				   MPFROMSHORT(LIT_END),
				   MPFROMP(return_wset_var(t)->name));
		}

		break;
	case WM_COMMAND :
		switch (SHORT1FROMMP(mp1))
		{
		case DID_OK	:
			WinSendMsg(hwndPage1, WM_COMMAND,
				   MPFROMLONG(DID_OK), MPFROMLONG(CMDSRC_OTHER));
			WinSendMsg(hwndPage2, WM_COMMAND,
				   MPFROMLONG(DID_OK), MPFROMLONG(CMDSRC_OTHER));
			WinSendMsg(hwndPage3, WM_COMMAND,
				   MPFROMLONG(DID_OK), MPFROMLONG(CMDSRC_OTHER));
			WinSendMsg(hwndPage4, WM_COMMAND,
				   MPFROMLONG(DID_OK), MPFROMLONG(CMDSRC_OTHER));
			inproperties=0;
			WinDismissDlg(hWnd, TRUE);
			break;
		}
		break;
		/* Close requested, exit dialogue */
	case WM_CLOSE :
		inproperties=0;
		WinDismissDlg(hWnd, FALSE);
		break;

		/* Pass	through	unhandled messages */
	default :
		return(WinDefDlgProc(hWnd, msg, mp1, mp2));
	}
	return(0L);
}

void properties_notebook(void)
{
	HAB hnbab;
	HMQ hnbmq;

	hnbab = WinInitialize(0);
	hnbmq = WinCreateMsgQueue(hnbab, 0);
	if(inproperties==0)
		WinDlgBox(HWND_DESKTOP, HWND_DESKTOP, NotebookDlgProc, NULLHANDLE, PROPNBK, NULL);

	WinDestroyMsgQueue(hnbmq);
	WinTerminate(hnbab);
}

void msgbox(void)
{
	HAB hmbab;
	HMQ hmbmq;

	hmbab = WinInitialize(0);
	hmbmq = WinCreateMsgQueue(hmbab, 0);

	WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, msgtext, "PMBitchX", 0, MB_OK | MB_INFORMATION | MB_MOVEABLE);

	new_free(&msgtext);

	WinDestroyMsgQueue(hmbmq);
	WinTerminate(hmbab);
}

void pm_clrscr(Screen *tmp)
{
	VioScrollUp(0, 0, -1, -1, -1, (PBYTE)&default_pair, tmp->hvps);
}

void gui_font_set(char *fontsize, Screen *screen)
{
	char *height, *width;
	int t;

	width=new_malloc(10);
	strcpy(width, fontsize);
	t=0;
	while(width[t]!='x' && t<9)
		t++;
	height=&width[t+1];
	width[t]=0;
	VIO_font_width=atoi(width);
	VIO_font_height=atoi(height);
	new_free(&width);
	if (VIO_font_width < 6 || VIO_font_height < 8)
	{
		VIO_font_width=6;VIO_font_height=10;
	}

	if(VioSetDeviceCellSize(VIO_font_height, VIO_font_width, screen->hvps)!=NO_ERROR)
	{
 		VIO_font_width = screen->VIO_font_width;
		VIO_font_height = screen->VIO_font_height;
	}
	else
	{
		VioSetDeviceCellSize(VIO_font_height, VIO_font_width, screen->hvpsnick);
		screen->VIO_font_width = VIO_font_width;
		screen->VIO_font_height = VIO_font_height;
	}

}

void gui_font_init(void)
{
	char *fontsize;
	int cy, cx, containerh, codepage = 0;

	VIO_font_width=6;VIO_font_height=10;
	main_screen->VIO_font_width=VIO_font_width;
	main_screen->VIO_font_height=VIO_font_height;

	if((fontsize=get_string_var(DEFAULT_FONT_VAR))!=NULL)
		gui_font_set(fontsize, main_screen);

	main_screen->nicklist = get_int_var(NICKLIST_VAR);

	main_screen->co = 84; main_screen->li = 25;
	cx = (main_screen->co - 1) * main_screen->VIO_font_width;
	cy = main_screen->li * main_screen->VIO_font_height;
	cx += main_screen->VIO_font_width * main_screen->nicklist;
	if(main_screen->nicklist)
		cx += cxvs;

	WinSetWindowPos(main_screen->hwndFrame, HWND_TOP, 0, 0, 0 ,0, SWP_HIDE);

	if(get_int_var(MDI_VAR))
	{
		SWP swp;

		WinQueryWindowPos(MDIClient, &swp);

		containerh = swp.cy;
		WinSetParent(main_screen->hwndFrame, MDIClient, FALSE);
	}
	else
	{
		containerh = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
		WinSetParent(main_screen->hwndFrame, HWND_DESKTOP, FALSE);
	}

	if(main_screen->hwndMenu==(HWND)NULL)
		WinSetWindowPos(main_screen->hwndFrame, HWND_TOP, 32 * main_screen->screennum, containerh - ((32 * main_screen->screennum) + cy + (cysb*2)+cytb), cx+cxsb, cy+(cysb*2)+WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR), SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ACTIVATE | SWP_ZORDER);
	else
		WinSetWindowPos(main_screen->hwndFrame, HWND_TOP, 32 * main_screen->screennum, containerh - ((32 * main_screen->screennum) + cy + (cysb*2)+cytb), cx+cxsb, cy+(cysb*2)+WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR) + WinQuerySysValue(HWND_DESKTOP, SV_CYMENU) + 1, SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ACTIVATE | SWP_ZORDER);

	if((codepage = get_int_var(DEFAULT_CODEPAGE_VAR)))
	{
		VioSetCp(0, codepage, main_screen->hvps);
		VioSetCp(0, codepage, main_screen->hvpsnick);
		WinSetCp(hmq, codepage);
		WinSetCp(mainhmq, codepage);
	}

	DosSleep(1000);

	/*if(!get_int_var(SCROLLERBARS_VAR))
	 WinSetParent();*/
}

/* This section is for portability considerations */
void gui_init(void)
{
	LONG add = 100;
	ULONG new;

	/* Create an unnamed semaphore */
	DosCreateEventSem(NULL,
					  (PHEV)&sem,
					  DC_SEM_SHARED,
					  FALSE);

	li = 25;
	co = 80;

#ifdef SOUND
	if(DosLoadModule(NULL, 0, "MCIAPI", &hmod) == NO_ERROR)
	{
		if(DosQueryProcAddr(hmod, 0, "mciPlayFile", (PFN *)&DLL_mciPlayFile) != NO_ERROR)
			DLL_mciPlayFile = NULL;
		if(DosQueryProcAddr(hmod, 0, "mciSendString", (PFN *)&DLL_mciSendString) != NO_ERROR)
			DLL_mciSendString = NULL;
		if(DosQueryProcAddr(hmod, 0, "mciGetErrorString", (PFN *)&DLL_mciGetErrorString) != NO_ERROR)
			DLL_mciGetErrorString = NULL;
	}
#endif

	DosSetRelMaxFH(&add, &new);
	_beginthread((void *)avio_init, NULL, (4*0xFFFF), main_screen );
	_beginthread((void *)flush_thread, NULL, 0xFFFF, &hvps);
	_beginthread((void *)cursor_thread, NULL, 0xFFFF, NULL);

	DosWaitEventSem(sem, SEM_INDEFINITE_WAIT);
	DosCloseEventSem(sem);


	default_pair[0] = ' ';
	default_pair[1] = 7;
}

void gui_clreol(void)
{
	USHORT row, col;
	char tmpbuf[201];

	aflush();
	if(!output_screen)
		return;

	VioGetCurPos((PUSHORT)&row, (PUSHORT)&col, output_screen->hvps);
	if((output_screen->co - col + 1) > 0)
	{
		memset(tmpbuf, ' ', 200);
		tmpbuf[201] = 0;
		VioWrtTTY((PCH)tmpbuf, (output_screen->co - col) + 1, output_screen->hvps);
		VioSetCurPos((USHORT)row, (USHORT)col, output_screen->hvps);
	}
}

void gui_gotoxy(int col, int row)
{
	aflush();
	VioSetCurPos((USHORT)row, (USHORT)col, output_screen ? output_screen->hvps : hvps);
}

void gui_clrscr(void)
{
	Screen *tmp;

	aflush();
	for (tmp = screen_list; tmp; tmp = tmp->next)
        if(tmp->alive)
			pm_clrscr(tmp);
}

void gui_left(int num)
{
	unsigned int row,col;
	aflush();
	VioGetCurPos((PUSHORT)&row, (PUSHORT)&col, output_screen ? output_screen->hvps : hvps);
	if(col>num)
		col=col-num;
	else
		col=0;
	VioSetCurPos((USHORT)row, (USHORT)col, output_screen ? output_screen->hvps : hvps);
}

void gui_right(int num)
{
	unsigned int row,col;
	aflush();
	VioGetCurPos((PUSHORT)&row, (PUSHORT)&col, output_screen->hvps);
	if(output_screen->co > num)
		col=col+num;
	else
		col=output_screen->co;
	VioSetCurPos((USHORT)row, (USHORT)col, output_screen->hvps);
}

void gui_scroll(int top, int bot, int n)
{
	aflush();
	if (n > 0) VioScrollUp(top, 0, bot, output_screen->co, n, (PBYTE)&pair, output_screen->hvps);
	else if (n < 0) { n = -n; VioScrollDn(top, 0, bot, output_screen->co, n, (PBYTE)&pair, output_screen->hvps); }
	return;
}

void gui_flush(void)
{
	aflush();
}

void gui_puts(unsigned char *buffer)
{
	int i;
	for (i = 0; i < strlen(buffer); i++) aputc(buffer[i]);
	aputc('\n'); aputc('\r');
}

void gui_new_window(Screen *new, Window *win)
{
	WinSendMsg(main_screen->hwndClient, WM_USER+2, MPFROMP(new), MPFROMP(win));
}

void pm_new_window(Screen *new, Window *win)
{
	int	cx,
	cy;
	HDC	hdc;
	ULONG	flStyle = FCF_MINMAX | FCF_SYSMENU | FCF_TITLEBAR | FCF_SIZEBORDER | FCF_ICON;
	PMYWINDATA	windata;
	MenuStruct *menutoadd;
	char    *defmenu, *fontsize, *width, *height;
	int     t, containerh, codepage = 0;

	windata = new_malloc(sizeof(MYWINDATA));
	memset(windata, '\0', sizeof(MYWINDATA));

	if(!get_int_var(MDI_VAR))
		flStyle |= FCF_TASKLIST;

	new->hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
					    0,
					    &flStyle,
					    "AVIO",
					    irc_version,
					    0L,
					    NULLHANDLE,
					    IDM_MAINMENU,
					    &new->hwndClient);

	WinSetWindowPtr(new->hwndClient, QWP_USER, NULL);

	new->hwndLeft = WinCreateWindow(new->hwndClient,
					WC_FRAME,
					NULL,
					WS_VISIBLE,
					0,0,2000,1000,
					new->hwndClient,
					HWND_TOP,
					0L,
					NULL,
					NULL);

	new->hwndRight = WinCreateWindow(new->hwndClient,
					 WC_FRAME,
					 NULL,
					 WS_VISIBLE,
					 0,0,2000,1000,
					 new->hwndClient,
					 HWND_TOP,
					 0L,
					 NULL,
					 NULL);

	new->hwndscroll = WinCreateWindow(new->hwndClient,
					  WC_SCROLLBAR,
					  NULL,
					  WS_VISIBLE | SBS_VERT,
					  0,0,100,100,
					  new->hwndClient,
					  HWND_TOP,
					  1000L,
					  NULL,
					  NULL);

	new->hwndnickscroll = WinCreateWindow(new->hwndClient,
					      WC_SCROLLBAR,
					      NULL,
					      WS_VISIBLE | SBS_VERT,
					      0,0,100,100,
					      new->hwndClient,
					      HWND_TOP,
					      1001L,
					      NULL,
					      NULL);

	if (main_screen)
	{
		new->co = main_screen->co;
		new->li = main_screen->li;
	}
	else
	{
		new->co = 81;
		new->li = 25;
	}

	if (main_screen)
	{
		new->VIO_font_width  = main_screen->VIO_font_width;
		new->VIO_font_height = main_screen->VIO_font_height;
	}
	else
	{
		new->VIO_font_width  = VIO_font_width;
		new->VIO_font_height = VIO_font_height;
	}

	/* Get the default font and set the new window with it */

	if((fontsize=get_string_var(DEFAULT_FONT_VAR))!=NULL)
	{
		width=new_malloc(10);
		strcpy(width, fontsize);
		t=0;
		while(width[t]!='x' && t<9)
			t++;
		height=&width[t+1];
		width[t]=0;
		VIO_font_width=atoi(width);
		VIO_font_height=atoi(height);
		new_free(&width);
		if (VIO_font_width < 6 || VIO_font_height < 8)
		{
			VIO_font_width=6;VIO_font_height=10;
		}
	}
	else
	{
		VIO_font_width=6;VIO_font_height=10;
	}

	new->VIO_font_width  = VIO_font_width;
	new->VIO_font_height = VIO_font_height;

	/* Get the default menu and add it to the new window */

	defmenu=get_string_var(DEFAULT_MENU_VAR);
	if(defmenu && *defmenu)
	{
		if((menutoadd = (MenuStruct *)findmenu(defmenu))==NULL)
			say("Menu not found.");
		else if(menutoadd->menuorigin==NULL)
			say("Cannot create blank menu.");
		else
		{
			new->hwndMenu=newsubmenu((MenuStruct *)menutoadd, new->hwndFrame, TRUE);
			new->menu=m_strdup(defmenu);
		}
	}
	else
		new->hwndMenu=(HWND)NULL;

	cx = new->co * new->VIO_font_width;
	cy = new->li * new->VIO_font_height;
	new->nicklist = get_int_var(NICKLIST_VAR);
	cx += new->nicklist * new->VIO_font_width;
	if(new->nicklist)
		cx += cxvs;

	hdc = WinOpenWindowDC(new->hwndLeft);

	/* VIO_staticx and VIO_staticy */
	VioCreatePS(&new->hvps, VIO_staticy, VIO_staticx, 0, 1, 0);
	VioAssociate(hdc, new->hvps);

	hdc = WinOpenWindowDC(new->hwndRight);
	VioCreatePS(&new->hvpsnick, VIO_staticy, VIO_staticx, 0, 1, 0);
	VioAssociate(hdc, new->hvpsnick);

	cursor_off(new->hvpsnick);

	VioSetDeviceCellSize(new->VIO_font_height, new->VIO_font_width, new->hvps);
	VioSetDeviceCellSize(new->VIO_font_height, new->VIO_font_width, new->hvpsnick);

	windata->hvps   = new->hvps;
	windata->screen = new;
	WinSendMsg(new->hwndscroll, SBM_SETSCROLLBAR, (MPARAM)get_int_var(SCROLLBACK_VAR), MPFROM2SHORT(0, get_int_var(SCROLLBACK_VAR)));
	WinSetWindowPtr(new->hwndClient, QWP_USER, windata);
	WinSetWindowPtr(new->hwndLeft, QWP_USER, windata);
	WinSetWindowPtr(new->hwndRight, QWP_USER, windata);

	WinSubclassWindow(new->hwndLeft, FrameWndProc);
	WinSubclassWindow(new->hwndRight, FrameWndProc);

	WinSetWindowPos(new->hwndFrame, HWND_TOP, 0, 0, 0 ,0, SWP_HIDE);

	WinSendMsg(new->hwndnickscroll, SBM_SETSCROLLBAR, (MPARAM)0, MPFROM2SHORT(0, new->li));
	WinSendMsg(new->hwndnickscroll, SBM_SETTHUMBSIZE, MPFROM2SHORT(new->li, 0), (MPARAM)NULL);

	if(get_int_var(MDI_VAR))
	{
		SWP swp;

		WinQueryWindowPos(MDIClient, &swp);

		containerh = swp.cy;
		WinSetParent(new->hwndFrame, MDIClient, FALSE);
	}
	else
	{
		containerh = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
		WinSetParent(new->hwndFrame, HWND_DESKTOP, FALSE);
	}

	if(new->hwndMenu==(HWND)NULL)
		WinSetWindowPos(new->hwndFrame, HWND_TOP, 32 * new->screennum, containerh - ((32 * new->screennum) + cy + (cysb*2)+ cytb), cx+(cxsb*2) + cxvs, cy + (cysb*2)+ WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR), SWP_SIZE | SWP_ZORDER | SWP_ACTIVATE | SWP_SHOW | SWP_MOVE);
	else
		WinSetWindowPos(new->hwndFrame, HWND_TOP, 32 * new->screennum, containerh - ((32 * new->screennum) + cy + (cysb*2)+ cytb), cx+(cxsb*2) + cxvs, cy + (cysb*2)+ WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR) + /*menupos.cy*/ WinQuerySysValue(HWND_DESKTOP, SV_CYMENU) + 1, SWP_SIZE | SWP_ZORDER | SWP_ACTIVATE | SWP_SHOW | SWP_MOVE);

	if((codepage = get_int_var(DEFAULT_CODEPAGE_VAR)))
	{
		VioSetCp(0, codepage, new->hvps);
		VioSetCp(0, codepage, new->hvpsnick);
		WinSetCp(hmq, codepage);
		WinSetCp(mainhmq, codepage);
	}

}

void gui_kill_window(Screen *killscreen)
{
	PMYWINDATA wd = (PMYWINDATA)WinQueryWindowPtr(killscreen->hwndClient, QWP_USER);
	
	if(pmthread == *_threadid)
	{
		if(killscreen->hwndMenu && WinIsWindow(hab, killscreen->hwndMenu))
			detach_shared(killscreen);
		VioAssociate((HDC)NULL, killscreen->hvps);
		VioDestroyPS(killscreen->hvps);
		killscreen->hvps = 0;
		VioAssociate((HDC)NULL, killscreen->hvpsnick);
		VioDestroyPS(killscreen->hvpsnick);
		killscreen->hvpsnick = 0;
		WinDestroyWindow(killscreen->hwndFrame);
	} else {
		WinSendMsg(killscreen->hwndFrame, WM_USER+4, (MPARAM)killscreen, NULL);
		return;
	}
	if(wd)
		new_free(&wd);
}

void gui_msgbox(void)
{
	_beginthread((void *)msgbox, NULL, 0xFFFF, NULL);
}

void gui_popupmenu(char *menuname)
{
	if (contextmenu && WinIsWindow(hab, contextmenu))
		WinDestroyWindow(contextmenu);
	contextmenu=(HWND)menucreatestub(menuname, last_input_screen->hwndFrame, FALSE);
	WinPopupMenu(last_input_screen->hwndFrame, last_input_screen->hwndFrame, contextmenu, contextx, contexty, 0, PU_KEYBOARD | PU_MOUSEBUTTON1 | PU_VCONSTRAIN | PU_HCONSTRAIN);
}

/* These are support routines for gui_file_dialog() */
MRESULT EXPENTRY FileFilterProc(HWND hwnd, ULONG message, MPARAM mp1, MPARAM mp2 )
{
	return WinDefFileDlgProc( hwnd, message, mp1, mp2 ) ;
}

/* Ripped from functions.c because I couldn't get it to link from there */
BUILT_IN_FUNCTION(fencode)
{
	char	*result;
	int	i = 0;

	result = (char *)new_malloc(strlen((char *)input) * 2 + 1);
	while (*input)
	{
        result[i++] = (*input >> 4) + 0x41;
	result[i++] = (*input & 0x0f) + 0x41;
	input++;
	}
	result[i] = '\0';

	return result;		/* DONT USE RETURN_STR HERE! */
}

void pm_file_dialog(char *data[7])
{
	/* 0 type
	 1 path
	 2 title
	 3 ok
	 4 apply
	 5 szButton
	 6 code
	 */
	HAB hfdab;
	HMQ hfdmq;

	FILEDLG fild;
	char *okbut, *title, *tmp, *tmp2;
	int z;

	HWND hwndFile;

	hfdab = WinInitialize(0);
	hfdmq = WinCreateMsgQueue(hfdab, 0);

	convert_dos(data[1]);
	okbut = m_strdup(data[5]);
	title = m_strdup(data[2]);

	fild.cbSize = sizeof(FILEDLG);
	fild.fl = /*FDS_HELPBUTTON |*/ FDS_CENTER | FDS_OPEN_DIALOG;
	fild.pszTitle = title;
	fild.pszOKButton = okbut;
	fild.ulUser = 0L;
	fild.pfnDlgProc = (PFNWP)FileFilterProc;
	fild.lReturn = 0L;
	fild.lSRC = 0L;
	fild.hMod = 0;
	fild.x = 0;
	fild.y = 0;
	fild.pszIType       = (PSZ)NULL;
	fild.papszITypeList = (PAPSZ)NULL;
	fild.pszIDrive      = (PSZ)NULL;
	fild.papszIDriveList= (PAPSZ)NULL;
	fild.sEAType        = (SHORT)0;
	fild.papszFQFilename= (PAPSZ)NULL;
	fild.ulFQFCount     = 0L;
	strcpy(fild.szFullFile, data[1]);

	tmp = m_strdup(data[6]);

	for(z=0;z<7;z++)
	{
		if(data[z])
			new_free(&data[z]);
	}

	hwndFile = WinFileDlg(HWND_DESKTOP, HWND_DESKTOP, &fild);
	if(hwndFile)
	{
		switch(fild.lReturn)
		{
		case DID_OK:
			paramptr = m_strdup("OK ");
			convert_unix(fild.szFullFile);
			tmp2 = fencode(NULL, fild.szFullFile);
			malloc_strcat(&paramptr, tmp2);
			new_free(&tmp2);
			break;
		case DID_CANCEL:
			paramptr = m_strdup("CANCEL");
			break;
		default:
			paramptr = m_strdup(empty_string);
			break;
		}

		new_free(&title);
		new_free(&okbut);

		codeptr = tmp;
		sendevent(EVFILE, current_window->refnum);
	}

	WinDestroyMsgQueue(hfdmq);
	WinTerminate(hfdab);
}

void gui_file_dialog(char *type, char *path, char *title, char *ok, char *apply, char *code, char *szButton)
{
	char *data[7];

	if(!type)
		data[0] = m_strdup(empty_string);
    else
		data[0] = m_strdup(type);
	if(!path)
		data[1] = m_strdup(empty_string);
    else
		data[1] = m_strdup(path);
	if(!title)
		data[2] = m_strdup(empty_string);
    else
		data[2] = m_strdup(title);
	if(!ok)
		data[3] = m_strdup(empty_string);
    else
		data[3] = m_strdup(ok);
	if(!apply)
		data[4] = m_strdup(empty_string);
    else
		data[4] = m_strdup(apply);
	if(!szButton)
		data[5] = m_strdup(empty_string);
    else
		data[5] = m_strdup(szButton);
	if(!code)
		data[6] = m_strdup(empty_string);
    else
		data[6] = m_strdup(code);
	_beginthread((void *)pm_file_dialog, NULL, 0xFFFF, data);
}


void gui_properties_notebook(void)
{
	_beginthread((void *)properties_notebook, NULL, 0xFFFF, NULL);
}

#define current_screen      last_input_screen
#define INPUT_BUFFER        current_screen->input_buffer
#define ADD_TO_INPUT(x)     strmcat(INPUT_BUFFER, (x), INPUT_BUFFER_SIZE);

void gui_paste(char *args)
{
	ULONG  fmtInfo;
	APIRET rc;
	PSZ    pszClipText;
	char   *oclip, *clip, *bit;
	int    line = 0, i = 0;
	char   *channel = NULL;
	int topic = 0;
	int smartpaste = 0;
	char smart[512];
	int smartsize = 80;
	int input_line = 0;

	from_server = current_window->server;

	/* Parse arguments */
	if (!args || !*args)
		channel = get_current_channel_by_refnum(0);
	else
	{
		char *t;
		while (args && *args)
		{
			t = next_arg(args, &args);
			if (*t == '-')
			{
				if (!my_strnicmp(t, "-topic", strlen(t)))
				{
					topic = 1;
				}
				else if (!my_strnicmp(t, "-smart", strlen(t)))
				{
					/* Smartpaste */
					smartpaste = 1;
				}
				else if (!my_strnicmp(t, "-input", strlen(t)))
				{
					/* Smartpaste */
					input_line = 1;
				}
			}
			else
				channel = t;
		}
	}
	if (!channel)
		channel = get_current_channel_by_refnum(0);


	WinOpenClipbrd(hab);

	rc = WinQueryClipbrdFmtInfo(hab,
				    CF_TEXT,
				    &fmtInfo);
	if (rc) /* Text data in clipboard */
	{
		pszClipText = (PSZ)WinQueryClipbrdData(hab, CF_TEXT); /* Query data handle */
		oclip = clip = malloc(strlen(pszClipText) + 1); line = 0;
		while ((*clip++ = *pszClipText++)); /* Copy to own memory */

		if (!smartpaste)
		{
			/* Ordinary paste */
			clip = oclip; bit = strtok(clip, "\n\r");
			while (bit)
			{
				if (input_line)
				{
					ADD_TO_INPUT(bit);
					update_input(UPDATE_FROM_CURSOR);
				} else
					if (!topic)
					{
						if(current_window->query_nick)
						{
							if (do_hook(PASTE_LIST, "%s %s", current_window->query_nick, bit))
								send_text(current_window->query_nick, bit, NULL, 1, 0);
						}
						else
						{
							if (do_hook(PASTE_LIST, "%s %s", channel, bit))
								send_text(channel, bit, NULL, 1, 0);
						}
					} else
					{
						send_to_server("TOPIC %s :%s", channel, bit);
						break;
					}
				line++;
				bit = strtok(NULL, "\n\r");
				if (input_line && bit)
					send_line(0, NULL);

			}
		} else
		{
			/* Rosmo's Incredibly Dull SmartPaste:
			 Fits as much as possible into a 80-nick_length buffer, stripping
			 spaces from beginning and end and then pasting */

			if (!topic)
				smartsize = 512;
			else
				smartsize = 128; /* 'tis correct? */

			clip = oclip;
			while (*clip && *clip == ' ') clip++; /* Strip spaces from the start */

			*smart = '\0';
			while (*clip)
			{
				if (input_line && *smart)
					send_line(0, NULL);

				*smart = '\0';

				i = 0;
				while (*clip && i < smartsize)
				{
					if (*clip != '\n')
					{
						if (*clip != '\r') strncat(smart, clip, 1);
					}
					else
					{
						strcat(smart, " "); clip++; i++;
						while (*clip && *clip == ' ') clip++; /* Strip spaces */
						clip--;
					}
					clip++; i++;
				}

				if (strcmp(smart, "") != 0) /* If our smart buffer has stuff, send it! */
				{
					if (input_line)
					{
						ADD_TO_INPUT(smart);
						update_input(UPDATE_FROM_CURSOR);
					} else
						if (!topic)
						{
							if(current_window->query_nick)
							{
								if (do_hook(PASTE_LIST, "%s %s", current_window->query_nick, smart))
									send_text(current_window->query_nick, smart, NULL, 1, 0);
							}
							else
							{
								if (do_hook(PASTE_LIST, "%s %s", channel, smart))
									send_text(channel, smart, NULL, 1, 0);
							}
						}
						else
						{
							send_to_server("TOPIC %s :%s", channel, smart);
							break;
						}
				}
			}
		}

		free(oclip); /* Free */
	}
	WinCloseClipbrd(hab);
}

void gui_setfocus(Screen *screen)
{
	WinSetFocus(HWND_DESKTOP, screen->hwndClient);
}

void gui_scrollerchanged(Screen *screen, int position)
{
	WinSendMsg(screen->hwndscroll, SBM_SETSCROLLBAR, (MPARAM)position, MPFROM2SHORT(0, get_int_var(SCROLLBACK_VAR)));
}

void gui_query_window_info(Screen *screen, char *fontinfo, int *x, int *y, int *cx, int *cy)
{

	SWP wpos;

	if(screen)
	{
		WinQueryWindowPos(screen->hwndFrame, &wpos);
		*x = wpos.x;
		*y = wpos.y;
		*cx = wpos.cx;
		*cy = wpos.cy;
	}
	else
	{
		*x=*y=*cx=*cy=0;
	}
	sprintf(fontinfo, "%dx%d", screen ? screen->VIO_font_width : 0, screen ? screen->VIO_font_height : 0);
}

void play_sound(char *filename)
{
#ifdef SOUND
	struct stat tmpstat;
	HAB soundhab = 0;
	HMQ soundhmq = 0;

	soundhab = WinInitialize(0);
	soundhmq = WinCreateMsgQueue(soundhab, 0);

	convert_dos(filename);
	if((stat(filename,&tmpstat))==0)
		DLL_mciPlayFile((HWND)NULL, filename, 0, 0, 0);

	free(filename);

	WinDestroyMsgQueue(soundhmq);
	WinTerminate(soundhab);
#endif
}

void gui_play_sound(char *filename)
{
#ifdef SOUND
	char *freeme = strdup(filename);

	if(!DLL_mciPlayFile)
		return;

	_beginthread((void *)play_sound, NULL, 0xFFFF, (char *)freeme);
#endif
}

int gui_send_mci_string(char *mcistring, char *retstring)
{
#ifdef SOUND
	if(DLL_mciSendString)
		return DLL_mciSendString((PSZ)mcistring, (PSZ)retstring, 500, 0, 0);
	else
#endif
	return 0;
}

void gui_get_sound_error(int errnum, char *errstring)
{
#ifdef SOUND
	if(DLL_mciGetErrorString)
		DLL_mciGetErrorString(errnum, (PSZ)errstring, 500);
#endif
}

void gui_menu(Screen *screen, char *addmenu)
{
	/* Finish creating the menu in the Window thread */
	WinSendMsg(screen->hwndClient, WM_USER+6, (MPARAM)screen, (MPARAM)addmenu);
}

int gui_isset(Screen *screen, fd_set *rd, int what)
{
	if (screen->aviokbdbuffer[0] != 0)
		return TRUE;
	else
		return FALSE;

}

int gui_putc(int c)
{
	aputc(c);
	return 1;
}

void gui_exit(void)
{
#ifdef SOUND
	DosFreeModule(hmod);
#endif
	avio_exit();
}

void gui_screen(Screen *new)
{
	new->hvps = hvps;
	new->hvpsnick = hvpsnick;
	new->VIO_font_width = VIO_font_width;
	new->VIO_font_height = VIO_font_height;
	new->hwndFrame = hwndFrame;
	new->hwndClient = hwndClient;
	new->hwndLeft = hwndLeft;
	new->hwndRight = hwndRight;
	new->hwndscroll = hwndscroll;
	new->hwndnickscroll = hwndnickscroll;
	new->menu=(HWND)NULL;
	new->old_li=new->li;
	new->old_co=new->co;
	new->nicklist = get_int_var(NICKLIST_VAR);


	main_screen = new;
}

void gui_resize(Screen *new)
{
	pm_resize(new);
}

int gui_screen_width(void)
{
	return WinQuerySysValue(HWND_DESKTOP,SV_CXSCREEN);
}

int gui_screen_height(void)
{
	return WinQuerySysValue(HWND_DESKTOP,SV_CYSCREEN);
}

void gui_setwindowpos(Screen *screen, int x, int y, int cx, int cy, int top, int bottom, int min, int max, int restore, int activate, int size, int position)
{
	ULONG flags = 0;
	HWND pos = HWND_BOTTOM;

	if(top || bottom)
		flags |= SWP_ZORDER;

	if(top)
		pos = HWND_TOP;

	if(min)
		flags |= SWP_MINIMIZE;

	if(max)
		flags |= SWP_MAXIMIZE;

	if(restore)
		flags |= SWP_RESTORE;

	if(activate)
		flags |= SWP_ACTIVATE;

	if(size)
		flags |= SWP_SIZE;

	if(position)
		flags |= SWP_MOVE;

	WinSetWindowPos(screen->hwndFrame, pos, x, y, cx, cy, flags);
}

void BX_gui_mutex_lock(void)
{
}

void BX_gui_mutex_unlock(void)
{
}

int gui_setmenuitem(char *menuname, int refnum, char *what, char *param)
{
	MenuRef *tmp = NULL;
	MenuStruct *thismenu = findmenu(menuname);

	if(thismenu)
		tmp = find_menuref(thismenu->root, refnum);

	if(!tmp)
		return FALSE;

	if(my_stricmp(what, "check")==0)
	{
		if(my_atol(param))
			WinSendMsg(tmp->menuhandle, MM_SETITEMATTR, MPFROM2SHORT(tmp->menuid, TRUE), MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
		else
			WinSendMsg(tmp->menuhandle, MM_SETITEMATTR, MPFROM2SHORT(tmp->menuid, TRUE), MPFROM2SHORT(MIA_CHECKED, 0));
	}
	else if(my_stricmp(what, "text")==0)
	{
		WinSendMsg(tmp->menuhandle, MM_SETITEMTEXT, MPFROMSHORT(tmp->menuid), MPFROMP(param));
	}
	else
		return FALSE;

	return TRUE;

}

void gui_remove_menurefs(char *menu)
{
	MenuStruct *cleanmenu = findmenu(menu);

	if(cleanmenu)
	{
		MenuList *tmplist = cleanmenu->menuorigin;

		while(tmplist)
		{
			if(tmplist->refnum > 0)
				remove_menuref(&cleanmenu->root, tmplist->refnum);
			if(tmplist->menutype == GUISUBMENU || tmplist->menutype == GUIBRKSUBMENU)
				gui_remove_menurefs(tmplist->submenu);
			tmplist = tmplist->next;
		}
	}
}

void gui_mdi(Window *window, char *text, int value)
{
	WinSendMsg(main_screen->hwndClient, WM_USER+3, (MPARAM)value, 0);
}

void gui_update_nicklist(char *channel)
{
	if(channel)
	{
		Window *this_window;
		ChannelList *cptr = lookup_channel(channel, from_server, 0);

		if(cptr)
		{
			this_window = get_window_by_refnum(cptr->refnum);
			if(this_window && this_window->screen)
				drawnicklist(this_window->screen);
		}
	}
	else
	{
		Screen *tmp = screen_list;

		while(tmp)
		{
            if(tmp->alive)
				drawnicklist(tmp);
			tmp = tmp->next;
		}
	}
}

void gui_nicklist_width(int width, Screen *this_screen)
{
	SWP swp;
	int ocx = this_screen->VIO_font_width * this_screen->nicklist;
	int ncx = this_screen->VIO_font_width * width;

	if(ocx)
		ocx += cxvs;
	if(width)
		ncx += cxvs;

	if(this_screen->nicklist && !width)
	{
		WinSetParent(this_screen->hwndRight, HWND_OBJECT, FALSE);
		WinSetParent(this_screen->hwndnickscroll, HWND_OBJECT, FALSE);
	}
	if(!this_screen->nicklist && width)
	{
		WinSetParent(this_screen->hwndRight, this_screen->hwndClient, FALSE);
		WinSetParent(this_screen->hwndnickscroll, this_screen->hwndClient, FALSE);
	}
		this_screen->nicklist = width;
	WinQueryWindowPos(this_screen->hwndFrame, &swp);
	WinSetWindowPos(this_screen->hwndFrame, HWND_TOP, swp.x, swp.y, swp.cx + (ncx - ocx), swp.cy, SWP_SIZE);
}

void gui_startup(int argc, char *argv[])
{
	mainhab = WinInitialize(0);
	mainhmq = WinCreateMsgQueue(mainhab, 0);

	pipe(guiipc);
	new_open(guiipc[0]);

	dw_init(FALSE, argc, argv);
}

void gui_about_box(char *about_text)
{
	WinSendMsg(main_screen->hwndFrame, WM_USER+5, MPFROMP(about_text), NULL);
}

void gui_activity(int color)
{
	HWND hwnd;
    ULONG ulColor;

	if(target_window && target_window->screen)
		hwnd = target_window->screen->hwndFrame;
	else
		return;

	switch(color)
	{
	case COLOR_ACTIVE:
		ulColor = CLR_RED;
		break;
	case COLOR_HIGHLIGHT:
		ulColor = CLR_BLUE;
		break;
	default:
		ulColor = CLR_PALEGRAY;
        break;
	}

#if 0
	WinSetPresParam(hwnd,
					PP_FOREGROUNDCOLORINDEX,
					sizeof(ulColor),
					&ulColor);
#endif
}

void gui_setfileinfo(char *filename, char *nick, int server)
{
	const unsigned fea2listsize = 6000;
	char *pData, buffer[200], *s, *t, *fullname, *tmp = NULL;
	EAOP2 eaop2;
	PFEA2 pFEA2;
	extern char	*time_format;
	APIRET rc;

	malloc_sprintf(&tmp, "%s/%s", get_string_var(DCC_DLDIR_VAR), filename);
	fullname = expand_twiddle(tmp);
	convert_dos(fullname);
	new_free(&tmp);

	s = get_server_network(server);

	time_format = "%c";
	update_clock(RESET_TIME);
	t = update_clock(GET_TIME);

	sprintf(buffer, "%s@%s %s", nick, s ? s : "IRC", t ? t : "clock problem");

	time_format = NULL;
	update_clock(RESET_TIME);

	eaop2.fpGEA2List = 0;
	eaop2.fpFEA2List = (PFEA2LIST)malloc(fea2listsize);
	pFEA2 = &eaop2.fpFEA2List->list[0];

	pFEA2->fEA = 0;
    /* .COMMENTS is 9 characters long */
	pFEA2->cbName = 9;

	/* space for the type and length field. */
	pFEA2->cbValue = strlen(buffer)+2*sizeof(USHORT);

	strcpy(pFEA2->szName, ".COMMENTS");
	pData = pFEA2->szName+pFEA2->cbName+1;
	/* data begins at first byte after the name */

	*(USHORT*)pData = EAT_ASCII;             /* type */
	*((USHORT*)pData+1) = strlen(buffer);  /* length */
	strcpy(pData+2*sizeof(USHORT), buffer);/* content */

	pFEA2->oNextEntryOffset = 0;

	eaop2.fpFEA2List->cbList = ((PCHAR)pData+2*sizeof(USHORT)+
									 pFEA2->cbValue)-((PCHAR)eaop2.fpFEA2List);

	rc = DosSetPathInfo(fullname,
						FIL_QUERYEASIZE,
						&eaop2,
						sizeof(eaop2),
						0);

	free((void *)eaop2.fpFEA2List);
}

void gui_setfd(fd_set *rd)
{
	/* Set the GUI IPC pipe readable for select() */
	FD_SET(guiipc[0], rd);
}