summaryrefslogtreecommitdiff
path: root/hammer/op_output.cpp
blob: c5c9d8be59c3e3dd967270bc99af539522071e40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements a dialog for editing the input/output connections of a
//			list of entities. The connections are displayed in a grid control,
//			each row of the grid control representing a connection that is
//			common to all entities being edited. For example, given these two ents:
//
//			Button01
//				OnDamaged Sound01 PlaySound 0 0
//				OnPressed Door01 Open 0 0
//
//			Button02
//				OnPressed Door01 Open 0 0
//
//			If these two entities were selected, the grid control would show:
//
//				OnPressed Door01 Open 0 0
//
//			because it is the only connection that is common to both entities.
//			Editing an entry in the grid control modifies the corresponding 
//			connection in all selected entities.
//
// TODO: persist sort column index, sort directions, and column sizes
// TODO: implement an external mode, where the grid shows all connections to unselected ents
//
//=============================================================================//

#include "stdafx.h"
#include "GlobalFunctions.h"
#include "MapDoc.h"
#include "MapEntity.h"
#include "MapWorld.h"
#include "ObjectProperties.h"
#include "OP_Output.h"
#include "ToolManager.h"
#include "MainFrm.h"
#include "utlrbtree.h"
#include "options.h"
#include ".\op_output.h"

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>


#pragma warning( disable : 4355 )


#define ICON_CONN_BAD		0
#define ICON_CONN_GOOD		1
#define ICON_CONN_BAD_GREY	2
#define ICON_CONN_GOOD_GREY	3


const char *PARAM_STRING_NONE = "<none>";


//
// Column indices for the list control.
//
const int ICON_COLUMN			= 0;
const int OUTPUT_NAME_COLUMN	= 1;
const int TARGET_NAME_COLUMN	= 2;
const int INPUT_NAME_COLUMN		= 3;
const int PARAMETER_COLUMN		= 4;
const int DELAY_COLUMN			= 5;
const int ONLY_ONCE_COLUMN		= 6;

IMPLEMENT_DYNCREATE(COP_Output, CObjectPage)


BEGIN_MESSAGE_MAP(COP_Output, CObjectPage)
	//{{AFX_MSG_MAP(COP_Output)
	ON_BN_CLICKED(IDC_ADD,		OnAdd)
	ON_BN_CLICKED(IDC_DELETE,	OnDelete)
	ON_BN_CLICKED(IDC_COPY,		OnCopy)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_PASTE,	OnPaste)
	ON_BN_CLICKED(IDC_MARK,		OnMark)
	ON_BN_CLICKED(IDC_PICK_ENTITY, OnPickEntity)
	ON_BN_CLICKED(IDC_PICK_ENTITY_PARAM,	OnPickEntityParam)
	ON_CBN_SELCHANGE(IDC_EDIT_CONN_INPUT,	OnSelChangeInput)
	ON_CBN_EDITUPDATE(IDC_EDIT_CONN_INPUT,	OnEditUpdateInput)
	ON_CBN_SELCHANGE(IDC_EDIT_CONN_OUTPUT,	OnSelChangeOutput)
	ON_CBN_EDITUPDATE(IDC_EDIT_CONN_OUTPUT, OnEditUpdateOutput)
	ON_CBN_SELCHANGE(IDC_EDIT_CONN_PARAM,	OnSelChangeParam)
	ON_CBN_EDITUPDATE(IDC_EDIT_CONN_PARAM,	OnEditUpdateParam)
	ON_EN_CHANGE(IDC_EDIT_CONN_DELAY,		OnEditDelay)
	ON_BN_CLICKED(IDC_EDIT_CONN_FIRE_ONCE,	OnFireOnce)
	ON_BN_CLICKED(IDC_SHOWHIDDENTARGETS,	OnShowHiddenTargetsAsBroken)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//	ON_CBN_SELCHANGE(IDC_EDIT_CONN_TARGET,	OnSelChangeTarget)
//	ON_CBN_EDITUPDATE(IDC_EDIT_CONN_TARGET, OnEditUpdateTarget)


//
// Static data.
//
CEntityConnectionList *COP_Output::m_pConnectionBuffer = new CEntityConnectionList;
CImageList *COP_Output::m_pImageList = NULL;


//-----------------------------------------------------------------------------
// Returns true if any of the target entities in the connection list are visible.
//-----------------------------------------------------------------------------
static bool AreAnyTargetEntitiesVisible( CEntityConnectionList *pList )
{
	for ( int i=0; i < pList->Count(); i++ )
	{
		if ( pList->Element(i)->AreAnyTargetEntitiesVisible() )
			return true;
	}
	return false;
}


//-----------------------------------------------------------------------------
// Purpose: Compares by delays. Used as a secondary sort by all other columns.
//-----------------------------------------------------------------------------
static int CALLBACK ListCompareDelaysSecondary(COutputConnection *pOutputConn1, COutputConnection *pOutputConn2, SortDirection_t eDirection)
{
	CEntityConnectionList *pConnList1 = pOutputConn1->m_pConnList;
	CEntityConnectionList *pConnList2 = pOutputConn2->m_pConnList;
	CEntityConnection *pConn1 = pConnList1->Element(0);
	CEntityConnection *pConn2 = pConnList2->Element(0);
	return CEntityConnection::CompareDelaysSecondary(pConn1,pConn2,eDirection);
}


//-----------------------------------------------------------------------------
// Purpose: Compares by delays, does a secondary compare by output name.
//-----------------------------------------------------------------------------
static int CALLBACK ListCompareDelays(COutputConnection *pOutputConn1, COutputConnection *pOutputConn2, SortDirection_t eDirection)
{
	CEntityConnectionList *pConnList1 = pOutputConn1->m_pConnList;
	CEntityConnectionList *pConnList2 = pOutputConn2->m_pConnList;
	CEntityConnection *pConn1 = pConnList1->Element(0);
	CEntityConnection *pConn2 = pConnList2->Element(0);
	return CEntityConnection::CompareDelays(pConn1,pConn2,eDirection);
}


//-----------------------------------------------------------------------------
// Purpose: Compares by output name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
static int CALLBACK ListCompareOutputNames(COutputConnection *pOutputConn1, COutputConnection *pOutputConn2, SortDirection_t eDirection)
{
	CEntityConnectionList *pConnList1 = pOutputConn1->m_pConnList;
	CEntityConnectionList *pConnList2 = pOutputConn2->m_pConnList;
	CEntityConnection *pConn1 = pConnList1->Element(0);
	CEntityConnection *pConn2 = pConnList2->Element(0);
	return CEntityConnection::CompareOutputNames(pConn1,pConn2,eDirection);
}


//-----------------------------------------------------------------------------
// Purpose: Compares by input name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
static int CALLBACK ListCompareInputNames(COutputConnection *pOutputConn1, COutputConnection *pOutputConn2, SortDirection_t eDirection)
{
	CEntityConnectionList *pConnList1 = pOutputConn1->m_pConnList;
	CEntityConnectionList *pConnList2 = pOutputConn2->m_pConnList;
	CEntityConnection *pConn1 = pConnList1->Element(0);
	CEntityConnection *pConn2 = pConnList2->Element(0);
	return	(CEntityConnection::CompareInputNames(pConn1,pConn2,eDirection));
}


//-----------------------------------------------------------------------------
// Purpose: Compares by source name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
static int CALLBACK ListCompareSourceNames(COutputConnection *pOutputConn1, COutputConnection *pOutputConn2, SortDirection_t eDirection)
{
	CEntityConnectionList *pConnList1 = pOutputConn1->m_pConnList;
	CEntityConnectionList *pConnList2 = pOutputConn2->m_pConnList;
	CEntityConnection *pConn1 = pConnList1->Element(0);
	CEntityConnection *pConn2 = pConnList2->Element(0);
	return	(CEntityConnection::CompareSourceNames(pConn1,pConn2,eDirection));
}


//-----------------------------------------------------------------------------
// Purpose: Compares by target name, does a secondary compare by delay.
//-----------------------------------------------------------------------------
static int CALLBACK ListCompareTargetNames(COutputConnection *pOutputConn1, COutputConnection *pOutputConn2, SortDirection_t eDirection)
{
	CEntityConnectionList *pConnList1 = pOutputConn1->m_pConnList;
	CEntityConnectionList *pConnList2 = pOutputConn2->m_pConnList;
	CEntityConnection *pConn1 = pConnList1->Element(0);
	CEntityConnection *pConn2 = pConnList2->Element(0);
	return	(CEntityConnection::CompareTargetNames(pConn1,pConn2,eDirection));
}


//-----------------------------------------------------------------------------
// Purpose: Called by the entity picker tool when an entity is picked. This
//			stuffs the entity name into the smartedit control.
//-----------------------------------------------------------------------------
void COP_OutputPickEntityTarget::OnNotifyPickEntity(CToolPickEntity *pTool)
{
	//
	// Update the edit control text with the entity name. This text will be
	// stuffed into the local keyvalue storage in OnChangeSmartControl.
	//
	CMapEntityList Full;
	CMapEntityList Partial;
	pTool->GetSelectedEntities(Full, Partial);
	CMapEntity *pEntity = Full.Element(0);
	if (pEntity)
	{
		const char *pszName = pEntity->GetKeyValue("targetname");
		if (!pszName)
		{
			pszName = "";
		}

		switch ( m_nDlgItem )
		{
			case IDC_EDIT_CONN_TARGET:
			{
				// FIXME: this should be called automatically, but it isn't
				m_pDlg->m_ComboTarget.SelectItem(pszName);
				break;
			}

			case IDC_EDIT_CONN_PARAM:
			{
				// FIXME: this should be called automatically, but it isn't
				m_pDlg->GetDlgItem(m_nDlgItem)->SetWindowText(pszName);
				m_pDlg->OnEditUpdateParam();
				break;
			}
			
			default:
			{
				m_pDlg->GetDlgItem(m_nDlgItem)->SetWindowText(pszName);
				break;
			}
		}
	}

	m_pDlg->StopPicking();
}


//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
COP_Output::COP_Output(void)
	: CObjectPage(COP_Output::IDD), m_ComboTarget( this )
{
	m_bIgnoreTextChanged = false;
	m_pObjectList = NULL;
	m_pEditObjectRuntimeClass = RUNTIME_CLASS(editCMapClass);
	m_nSortColumn = OUTPUT_NAME_COLUMN;
	m_pMapEntityList = NULL;
	m_fDelay = 0;
	m_bPickingEntities = false;

	bSkipEditControlRefresh = false;
	//
	// All columns initially sort in ascending order.
	//
	for (int i = 0; i < OUTPUT_LIST_NUM_COLUMNS; i++)
	{
		m_eSortDirection[i] = Sort_Ascending;
	}

	m_PickEntityTarget.AttachEntityDlg(this);
}


//-----------------------------------------------------------------------------
// Purpose: Destructor.
//-----------------------------------------------------------------------------
COP_Output::~COP_Output(void)
{
}


void COP_Output::OnTextChanged( const char *pText )
{
	if ( m_bIgnoreTextChanged )
		return;
	
	// Updating the listbox data, will trigger the edit
	// controls to update.  They don't need to be
	bSkipEditControlRefresh = true;

	// Target has changed so we need to update for list of inputs
	// that are valid for this target
	FillInputList();
	FilterInputList();

	m_ComboInput.SetWindowText(m_strInput);

	UpdateEditedTargets();
}


//------------------------------------------------------------------------------
// Purpose: Updates the validity flag on the given item in the list control
// Input  : nItem - 
//------------------------------------------------------------------------------
void COP_Output::UpdateItemValidity(int nItem)
{
	COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
	CEntityConnectionList *pConnectionList = pOutputConn->m_pConnList;
	bool bShared = (m_EntityList.Count() == pConnectionList->Count());

	bool bShowHiddenTargets = ShouldShowHiddenTargets();

	int nIcon;
	if (ValidateConnections(pOutputConn, bShowHiddenTargets))
	{
		if ( !bShowHiddenTargets && !AreAnyTargetEntitiesVisible( pConnectionList ) )
			nIcon = ICON_CONN_GOOD_GREY;
		else if ( bShared )
			nIcon = ICON_CONN_GOOD;
		else
			nIcon = ICON_CONN_GOOD_GREY;
		
		pOutputConn->m_bIsValid = true;
	}
	else
	{
		nIcon = (bShared ? ICON_CONN_BAD : ICON_CONN_BAD_GREY);
		pOutputConn->m_bIsValid = false;
	}
	m_ListCtrl.SetItem(nItem,0,LVIF_IMAGE, 0, nIcon, 0, 0, 0 );
}


//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void COP_Output::UpdateValidityButton(void)
{
	CObjectProperties *pParent = (CObjectProperties*) GetParent();

	// Get status of all connections
	int nItemCount = m_ListCtrl.GetItemCount();	

	if (nItemCount == 0)
	{
		pParent->SetOutputButtonState(CONNECTION_NONE);
		return;
	}

	for (int nItem = 0; nItem < nItemCount; nItem++)
	{
		COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
		if (!pOutputConn->m_bIsValid)
		{
			pParent->SetOutputButtonState(CONNECTION_BAD);
			return;
		}
	}
	pParent->SetOutputButtonState(CONNECTION_GOOD);
}


//------------------------------------------------------------------------------
// Purpose: Return true if all connections entries are valid for the given
//			 output connection.  Return false otherwise
//------------------------------------------------------------------------------
bool COP_Output::ValidateConnections(COutputConnection *pOutputConn, bool bVisibilityCheck)
{
	int nCount = pOutputConn->m_pConnList->Count();
	for (int i = 0; i < nCount; i++)
	{
		CEntityConnection *pConnection = pOutputConn->m_pConnList->Element(i);
		if (pConnection != NULL)
		{
			// Check validity of output for the list of entities
			if (!CEntityConnection::ValidateOutput(pOutputConn->m_pEntityList,pConnection->GetOutputName()))
			{
				return false;
			}

			// Check validity of target entity (is it in the map?)
			if (!CEntityConnection::ValidateTarget(m_pMapEntityList, bVisibilityCheck, pConnection->GetTargetName()))
			{
				return false;
			}

			// Check validity of input
			if (!CEntityConnection::ValidateInput(pConnection->GetTargetName(), pConnection->GetInputName(), bVisibilityCheck))
			{
				return false;
			}
		}
	}
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pEntity - 
//			bFirst - 
//-----------------------------------------------------------------------------
void COP_Output::AddEntityConnections(CMapEntity *pEntity, bool bFirst)
{
	m_ListCtrl.SetRedraw(FALSE);

	//
	// The first entity simply adds its connections to the list.
	//
	int nConnCount = pEntity->Connections_GetCount();
	for (int i = 0; i < nConnCount; i++)
	{
		CEntityConnection *pConnection = pEntity->Connections_Get(i);
		if (pConnection != NULL)
		{
			// First check if the connection already exists, if so just add to it
			bool bFound = false;
			int nItemCount = m_ListCtrl.GetItemCount();	

			if (nItemCount > 0)
			{
				for (int nItem = nItemCount - 1; nItem >= 0; nItem--)
				{
					COutputConnection		*pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
					CEntityConnectionList	*pConnList	 = pOutputConn->m_pConnList;
					CEntityConnection		*pTestConn   = pConnList->Element(0);
					if (pTestConn->CompareConnection(pConnection))
					{
						// Don't consolidate duplicate connections in the same entity
						// Show them twice so the user will see
						if ( pOutputConn->m_pEntityList->Find(pEntity) == -1)
						{
							pConnList->AddToTail(pConnection);
							pOutputConn->m_pEntityList->AddToTail(pEntity);
							bFound = true;
							break;
						}

					}
				}
			}
			
			if (!bFound)
			{
				m_ListCtrl.SetItemCount(nItemCount + 1);

				m_ListCtrl.InsertItem(LVIF_IMAGE, nItemCount, "", 0, 0, ICON_CONN_GOOD, 0);

				m_ListCtrl.SetItemText(nItemCount, OUTPUT_NAME_COLUMN, pConnection->GetOutputName());
				m_ListCtrl.SetItemText(nItemCount, TARGET_NAME_COLUMN, pConnection->GetTargetName());
				m_ListCtrl.SetItemText(nItemCount, INPUT_NAME_COLUMN, pConnection->GetInputName());

				// Build the string for the delay.
				float fDelay = pConnection->GetDelay();
				char szTemp[MAX_PATH];
				sprintf(szTemp, "%.2f", fDelay);
				m_ListCtrl.SetItemText(nItemCount, DELAY_COLUMN, szTemp);

				// Fire once
				m_ListCtrl.SetItemText(nItemCount, ONLY_ONCE_COLUMN, (pConnection->GetTimesToFire() == EVENT_FIRE_ALWAYS) ? "No" : "Yes");
				m_ListCtrl.SetItemText(nItemCount, PARAMETER_COLUMN, pConnection->GetParam());

				
				// Set list ctrl data 
				COutputConnection* pOutputConn	= new COutputConnection;
				pOutputConn->m_pConnList		= new CEntityConnectionList;
				pOutputConn->m_pEntityList		= new CMapEntityList;
				pOutputConn->m_pConnList->AddToTail(pConnection);
				pOutputConn->m_pEntityList->AddToTail(pEntity);
				pOutputConn->m_bOwnedByAll		= true;
				m_ListCtrl.SetItemData(nItemCount, (DWORD)pOutputConn);
				
				nItemCount++;
			}
		}
	}

	m_ListCtrl.SetRedraw(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pDX - 
//-----------------------------------------------------------------------------
void COP_Output::DoDataExchange(CDataExchange *pDX)
{
	CObjectPage::DoDataExchange(pDX);

	//{{AFX_DATA_MAP(COP_Output)
	DDX_Control(pDX, IDC_LIST, m_ListCtrl);
	DDX_Text(pDX, IDC_EDIT_CONN_DELAY, m_fDelay);
	DDX_CBString(pDX, IDC_EDIT_CONN_OUTPUT, m_strOutput);
	DDX_CBString(pDX, IDC_EDIT_CONN_TARGET, m_strTarget);
	DDX_CBString(pDX, IDC_EDIT_CONN_INPUT, m_strInput);
	DDX_CBString(pDX, IDC_EDIT_CONN_PARAM, m_strParam);
	DDX_Check(pDX, IDC_EDIT_CONN_FIRE_ONCE, m_bFireOnce);
	DDX_Control(pDX, IDC_SHOWHIDDENTARGETS, m_ctlShowHiddenTargetsAsBroken);
	DDX_Control(pDX, IDC_ADD, m_AddControl);
	DDX_Control(pDX, IDC_PASTE, m_PasteControl);
	DDX_Control(pDX, IDC_DELETE, m_DeleteControl);

	//}}AFX_DATA_MAP
}


bool COP_Output::ShouldShowHiddenTargets()
{
	return (Options.general.bShowHiddenTargetsAsBroken == TRUE);
}


//------------------------------------------------------------------------------
// Purpose: Enables or Disables all edit controls
// Input  : bValue - 
//------------------------------------------------------------------------------
void COP_Output::EnableEditControls(bool bValue)
{
	m_ComboOutput.EnableWindow(bValue);
	EnableTarget(bValue);
	m_ComboInput.EnableWindow(bValue);

	CButton *pButton = (CButton *)GetDlgItem(IDC_EDIT_CONN_FIRE_ONCE);
	pButton->EnableWindow(bValue);

	CEdit *pDelayEdit = (CEdit *)GetDlgItem(IDC_EDIT_CONN_DELAY);
	pDelayEdit->EnableWindow(bValue);

	CComboBox *pParamCombo = (CComboBox *)GetDlgItem(IDC_EDIT_CONN_PARAM);
	pParamCombo->EnableWindow(bValue);
	GetDlgItem(IDC_PICK_ENTITY_PARAM)->EnableWindow( bValue );

	// Clear any values
	if (!bValue)
	{
		m_ComboTarget.ForceEditControlText( "" );
		m_ComboInput.SetWindowText("");
		m_ComboOutput.SetWindowText("");
		pParamCombo->SetCurSel(0);
		pDelayEdit->SetWindowText("0.0");
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pMapEntityList - 
//-----------------------------------------------------------------------------
void COP_Output::SetMapEntityList(const CMapEntityList *pMapEntityList)
{
	m_pMapEntityList = pMapEntityList;
	FillTargetList();
}


//------------------------------------------------------------------------------
// Purpose: Updates data displayed in edit controls
//------------------------------------------------------------------------------
void COP_Output::UpdateEditControls(void)
{
	//
	// Build a list of connections to edit.
	//
	m_EditList.RemoveAll();

	m_AddControl.EnableWindow( ( m_bCanEdit ? TRUE : FALSE ) );
	m_PasteControl.EnableWindow( ( m_bCanEdit ? TRUE : FALSE ) );
	m_DeleteControl.EnableWindow( ( m_bCanEdit ? TRUE : FALSE ) );

	// If nothing is selected, disable edit controls
	if (!m_ListCtrl.IsWindowEnabled() || m_ListCtrl.GetSelectedCount() == 0)
	{
		EnableEditControls(false);
		return;
	}

	for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
	{

		if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
		{
			COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
			m_EditList.AddVectorToTail(*pOutputConn->m_pConnList);
		}
	}

	if (m_EditList.Count() > 0)
	{
		SetConnection(&m_EditList);

		FillOutputList();
		FillInputList();

		// We must ignore the text changed event here or else it'll set all selected outputs to the same value.
		m_bIgnoreTextChanged = true;
		m_ComboTarget.SelectItem(m_strTarget);
		m_bIgnoreTextChanged = false;
		
		m_ComboInput.SetWindowText(m_strInput);
		m_ComboOutput.SetWindowText(m_strOutput);
		m_CheckBoxFireOnce.SetCheck(m_bFireOnce);

		CEdit *pDelayEdit = ( CEdit* )GetDlgItem( IDC_EDIT_CONN_DELAY );
		char szTemp[MAX_PATH];
		sprintf(szTemp, "%.2f", m_fDelay);
		pDelayEdit->SetWindowText(szTemp);
		
		CComboBox* pParamEdit = ( CComboBox* )GetDlgItem( IDC_EDIT_CONN_PARAM );
		pParamEdit->SetWindowText(m_strParam);

		FilterInputList();

		//
		// Update the UI state based on our current data.
		//
		char szBuf[MAX_IO_NAME_LEN];

		CClassOutput *pOutput = GetOutput(szBuf, sizeof(szBuf));
		UpdateCombosForSelectedOutput(pOutput);

		CClassInput *pInput = GetInput(szBuf, sizeof(szBuf));
		UpdateCombosForSelectedInput(pInput);

		//CMapEntityList *pTarget = GetTarget(szBuf, sizeof(szBuf));
		//UpdateCombosForSelectedTarget(pTarget);
	}

	if ( m_bCanEdit == false )
	{
		EnableEditControls( false );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Adds a connection to all entities being edited.
//-----------------------------------------------------------------------------
void COP_Output::OnAdd(void)
{
	FOR_EACH_OBJ( m_EntityList, pos)
	{
		CMapEntity *pEntity = m_EntityList.Element(pos);
		if (pEntity != NULL)
		{
			CEntityConnection *pConnection = new CEntityConnection;
			pEntity->Connections_Add(pConnection);
		}
	}

	UpdateConnectionList();

	// Set selection to new item, and move the focus to the output combo
	// so they can just start editing.
	int nCount = m_ListCtrl.GetItemCount();
	SetSelectedItem(nCount - 1);
	m_ListCtrl.EnsureVisible(nCount - 1, FALSE);
	GetDlgItem(IDC_EDIT_CONN_OUTPUT)->SetFocus();
}


//------------------------------------------------------------------------------
// Purpose: Clear copy buffer
//------------------------------------------------------------------------------
void COP_Output::EmptyCopyBuffer(void)
{
	// Delete any old connections
	int nConnCount = m_pConnectionBuffer->Count();
	for (int i = 0; i < nConnCount; i++)
	{
		CEntityConnection *pConnection = m_pConnectionBuffer->Element(i);
		if (pConnection != NULL)
		{
			delete pConnection;
		}
	}
	m_pConnectionBuffer->RemoveAll();
	
}


//-----------------------------------------------------------------------------
// Purpose: Copies list of selected connections into copy buffer
//-----------------------------------------------------------------------------
void COP_Output::OnCopy(void)
{
	EmptyCopyBuffer();

	if (m_ListCtrl.GetSelectedCount() != 0)
	{
		int nCount = m_ListCtrl.GetItemCount();
		if (nCount > 0)
		{
			for (int nItem = nCount - 1; nItem >= 0; nItem--)
			{
				if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
				{
					//
					// Each item in the list control is a list of identical connections that are contained
					// in multiple entities. Add each selected connection to the selected entities.
					//
					COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
					CEntityConnectionList *pConnList = pOutputConn->m_pConnList;
					if (pConnList != NULL)
					{
						CEntityConnection *pConnection = pConnList->Element(0);
						if (pConnection)
						{
							CEntityConnection *pNewConnection = new CEntityConnection;
							*pNewConnection = *pConnection;
							m_pConnectionBuffer->AddToTail(pNewConnection);
						}
					}
				}
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Adds a connection to all entities being edited.
//-----------------------------------------------------------------------------
void COP_Output::OnPaste(void)
{
	// Early out
	if (!m_pConnectionBuffer->Count())
	{
		return;
	}

	CUtlVector<CEntityConnection *> NewConnections;

	// Add connections from copy buffer to all selected entities
	FOR_EACH_OBJ( m_EntityList, pos )
	{
		CMapEntity *pEntity = m_EntityList.Element(pos);
		if (pEntity != NULL)
		{
			int nConnCount = m_pConnectionBuffer->Count();
			for (int i = 0; i < nConnCount; i++)
			{
				CEntityConnection *pConnection = m_pConnectionBuffer->Element(i);
				if (pConnection != NULL)
				{
					CEntityConnection *pNewConnection = new CEntityConnection;
					*pNewConnection = *pConnection;
					pEntity->Connections_Add(pNewConnection);

					NewConnections.AddToTail(pNewConnection);
				}
			}
		}
	}
	UpdateConnectionList();
	SortListByColumn(m_nSortColumn, m_eSortDirection[m_nSortColumn]);
	SetSelectedConnections(NewConnections);
	GetDlgItem(IDC_EDIT_CONN_OUTPUT)->SetFocus();
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void COP_Output::OnPickEntity(void)
{
	CButton *pButton = (CButton *)GetDlgItem(IDC_PICK_ENTITY);
	Assert(pButton != NULL);

	if (pButton != NULL)
	{
		if (pButton->GetCheck())
		{
			//
			// Activate the entity picker tool.
			//
			m_bPickingEntities = true;
			m_PickEntityTarget.AttachDlgItem( IDC_EDIT_CONN_TARGET );
			CToolPickEntity *pTool = (CToolPickEntity *)ToolManager()->GetToolForID(TOOL_PICK_ENTITY);
			pTool->Attach(&m_PickEntityTarget);
			ToolManager()->SetTool(TOOL_PICK_ENTITY);
			GetDlgItem(IDC_PICK_ENTITY_PARAM)->EnableWindow( false );
		}
		else
		{
			StopPicking();
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void COP_Output::OnPickEntityParam(void)
{
	CButton *pButton = (CButton *)GetDlgItem(IDC_PICK_ENTITY_PARAM);
	Assert(pButton != NULL);

	if (pButton != NULL)
	{
		if (pButton->GetCheck())
		{
			//
			// Activate the entity picker tool.
			//
			m_bPickingEntities = true;
			m_PickEntityTarget.AttachDlgItem( IDC_EDIT_CONN_PARAM );
			CToolPickEntity *pTool = (CToolPickEntity *)ToolManager()->GetToolForID(TOOL_PICK_ENTITY);
			pTool->Attach(&m_PickEntityTarget);
			ToolManager()->SetTool(TOOL_PICK_ENTITY);
			GetDlgItem(IDC_PICK_ENTITY)->EnableWindow( false );
		}
		else
		{
			StopPicking();
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Deletes all selected items from the connection list, and removes the
//			corresponding connections from the list of entities being edited.
//-----------------------------------------------------------------------------
void COP_Output::OnDelete(void)
{
	if (m_ListCtrl.GetSelectedCount() != 0)
	{
		int nCount		= m_ListCtrl.GetItemCount();
		int nLastItem	= 0;
		if (nCount > 0)
		{
			for (int nItem = nCount - 1; nItem >= 0; nItem--)
			{
				if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
				{
					//
					// Each item in the list control is a list of identical connections that are contained
					// in multiple entities. Since we don't store the containing entity along with the connection,
					// just try to remove all the connections in the list from all the selected entities.
					//
					COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
					CEntityConnectionList *pConnList = pOutputConn->m_pConnList;
					m_ListCtrl.DeleteItem(nItem);

					if (pConnList != NULL)
					{
						int nConnCount = pConnList->Count();
						for (int nConn = 0; nConn < nConnCount; nConn++)
						{
							CEntityConnection *pConnection = pConnList->Element(nConn);
							if (pConnection != NULL)
							{
								//
								// Remove the connection from all entities being edited.
								//
								FOR_EACH_OBJ( m_EntityList, pos )
								{
									CMapEntity *pEntity = m_EntityList.Element(pos);
									if (pEntity != NULL)
									{
										pEntity->Connections_Remove(pConnection);
									}
								}

								//								
								// Remove the connection from the upstream list of all entities it targets.
								//
								CMapEntityList *pTargetList = pConnection->GetTargetEntityList();
								if ( pTargetList )
								{
									FOR_EACH_OBJ( *pTargetList, pos2 )
									{
										CMapEntity *pEntity = pTargetList->Element( pos2 );
										pEntity->Upstream_Remove( pConnection );
									}
								}
							}
							
							delete pConnection;
						}
						
						delete pConnList;
					}
					// Keep track of last item so can set selection focus
					nLastItem = nItem;
				}
			}
		}

		// Set selection focus as point of deletion or on last item 
		int nNumItems = m_ListCtrl.GetItemCount()-1;
		if (nLastItem > nNumItems)
		{
			nLastItem = nNumItems;
		}
		SetSelectedItem(nLastItem);
		UpdateValidityButton();
	}
}


//------------------------------------------------------------------------------
// Purpose : Take the user to the output page of the selected entity that
//			 targets me.  
// Input   :
// Output  :
//------------------------------------------------------------------------------
void COP_Output::OnMark(void)
{
	int			nCount	= m_ListCtrl.GetItemCount();
	CMapDoc*	pDoc	= CMapDoc::GetActiveMapDoc();

	CEntityConnection *pConnection = NULL; 

	if (nCount > 0 && pDoc)
	{
		CMapObjectList Select;

		for (int nItem = nCount - 1; nItem >= 0; nItem--)
		{
			if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
			{
				COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
				pConnection = pOutputConn->m_pConnList->Element(0);

				CMapDoc *pDocActive = CMapDoc::GetActiveMapDoc();
				if ( pDocActive != NULL)
				{
					CMapEntityList Found;
					pDocActive->FindEntitiesByName(Found, m_ListCtrl.GetItemText(nItem, TARGET_NAME_COLUMN), false);

					FOR_EACH_OBJ( Found, pos )
					{
						CMapEntity *pEntity = Found.Element(pos);
						Select.AddToTail(pEntity);
					}
				}
			}
		}
		if (Select.Count()>0)
		{
			pDoc->SelectObjectList(&Select);

			// (a bit squirly way of doing this)
			if ( Select.Count()==1 )
				GetMainWnd()->pObjectProperties->SetPageToInput(pConnection);
		
			pDoc->Center2DViewsOnSelection();
		}
		else
		{
			MessageBox("No entities were found with that targetname.", "No entities found", MB_ICONINFORMATION | MB_OK);
			return;
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Sets up the list view columns, initial sort column.
//-----------------------------------------------------------------------------
BOOL COP_Output::OnInitDialog(void)
{
	CObjectPage::OnInitDialog();

	m_ComboOutput.SubclassDlgItem(IDC_EDIT_CONN_OUTPUT, this);
	m_ComboInput.SubclassDlgItem(IDC_EDIT_CONN_INPUT, this);
	m_ComboTarget.SubclassDlgItem(IDC_EDIT_CONN_TARGET, this);
	m_CheckBoxFireOnce.SubclassDlgItem(IDC_EDIT_CONN_FIRE_ONCE, this);

	m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP);
	
	m_ListCtrl.InsertColumn(ICON_COLUMN, "", LVCFMT_CENTER, 20);
	m_ListCtrl.InsertColumn(OUTPUT_NAME_COLUMN, "My Output", LVCFMT_LEFT, 70);
	m_ListCtrl.InsertColumn(TARGET_NAME_COLUMN, "Target Entity", LVCFMT_LEFT, 70);
	m_ListCtrl.InsertColumn(INPUT_NAME_COLUMN, "Target Input", LVCFMT_LEFT, 70);
	m_ListCtrl.InsertColumn(DELAY_COLUMN, "Delay", LVCFMT_LEFT, 70);
	m_ListCtrl.InsertColumn(ONLY_ONCE_COLUMN, "Only Once", LVCFMT_LEFT, 70);
	m_ListCtrl.InsertColumn(PARAMETER_COLUMN, "Parameter", LVCFMT_LEFT, 70);

	UpdateConnectionList();

	SetSortColumn(m_nSortColumn, m_eSortDirection[m_nSortColumn]);

	// Force an update of the column header text so that the sort indicator is shown.
	UpdateColumnHeaderText(m_nSortColumn, true, m_eSortDirection[m_nSortColumn]);

	ResizeColumns();

	m_strLastParam.Empty();

	// Select the first item in the combo box
	SetSelectedItem(0);
	   
	// Create image list.  Is deleted automatically when listctrl is deleted
	if (!m_pImageList)
	{
		CWinApp *pApp = AfxGetApp();
		m_pImageList = new CImageList();
		Assert(m_pImageList != NULL);    // serious allocation failure checking
		m_pImageList->Create(16, 16, TRUE,   1, 0);
		m_pImageList->Add(pApp->LoadIcon( IDI_OUTPUTBAD ));
		m_pImageList->Add(pApp->LoadIcon( IDI_OUTPUT ));
		m_pImageList->Add(pApp->LoadIcon( IDI_OUTPUTBAD_GREY ));
		m_pImageList->Add(pApp->LoadIcon( IDI_OUTPUT_GREY ));

	}
	m_ListCtrl.SetImageList(m_pImageList, LVSIL_SMALL );

	// Apply the eyedropper image to the picker buttons.
	CButton *pButton = (CButton *)GetDlgItem(IDC_PICK_ENTITY);
	if (pButton)
	{
		CWinApp *pApp = AfxGetApp();
		HICON hIcon = pApp->LoadIcon(IDI_EYEDROPPER);
		pButton->SetIcon(hIcon);
	}

	pButton = (CButton *)GetDlgItem(IDC_PICK_ENTITY_PARAM);
	if (pButton)
	{
		CWinApp *pApp = AfxGetApp();
		HICON hIcon = pApp->LoadIcon(IDI_EYEDROPPER);
		pButton->SetIcon(hIcon);
	}

	CAnchorDef anchorDefs[] = 
	{
		CAnchorDef( IDC_LIST, k_eSimpleAnchorAllSides ),
		CAnchorDef( IDC_OUTPUTS_STATIC_PANEL, k_eAnchorLeft, k_eAnchorBottom, k_eAnchorRight, k_eAnchorBottom ),
		CAnchorDef( IDC_OUTPUT_LABEL, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_TARGETS_LABEL, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_VIA_INPUT_LABEL, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_PARAMETER_LABEL, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_DELAY_LABEL, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_EDIT_CONN_DELAY, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_EDIT_CONN_FIRE_ONCE, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_EDIT_CONN_PARAM, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_EDIT_CONN_INPUT, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_EDIT_CONN_TARGET, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_EDIT_CONN_OUTPUT, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_PICK_ENTITY, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_PICK_ENTITY_PARAM, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_MARK, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_ADD, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_COPY, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_PASTE, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_DELETE, k_eSimpleAnchorBottomSide ),
		CAnchorDef( IDC_SHOWHIDDENTARGETS, k_eSimpleAnchorBottomRight )		
	};
	m_AnchorMgr.Init( GetSafeHwnd(), anchorDefs, ARRAYSIZE( anchorDefs ) );

	// Set the last state this was at.
	m_ctlShowHiddenTargetsAsBroken.SetCheck( ShouldShowHiddenTargets() );
	return(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : wParam - 
//			lParam - 
//			pResult - 
// Output : Returns TRUE on success, FALSE on failure.
//-----------------------------------------------------------------------------
BOOL COP_Output::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT *pResult)
{
	NMHDR *pnmh = (NMHDR *)lParam;

	if (pnmh->idFrom == IDC_LIST)
	{
		switch (pnmh->code)
		{
			case LVN_COLUMNCLICK:
			{
				NMLISTVIEW *pnmv = (NMLISTVIEW *)lParam;
				if (pnmv->iSubItem < OUTPUT_LIST_NUM_COLUMNS)
				{
					SortDirection_t eSortDirection = m_eSortDirection[pnmv->iSubItem];

					//
					// If they clicked on the current sort column, reverse the sort direction.
					//
					if (pnmv->iSubItem == m_nSortColumn)
					{
						if (m_eSortDirection[m_nSortColumn] == Sort_Ascending)
						{
							eSortDirection = Sort_Descending;
						}
						else
						{
							eSortDirection = Sort_Ascending;
						}
					}
					
					//
					// Update the sort column and sort the list.
					//
					SetSortColumn(pnmv->iSubItem, eSortDirection);
				}

				return(TRUE);
			}

			case NM_DBLCLK:
			{
				OnMark();
				return(TRUE);
			}

			case LVN_ITEMCHANGED:
			{
				NMLISTVIEW *pnmv = (NMLISTVIEW *)lParam;
				if ( ( pnmv->uNewState & LVIS_SELECTED ) != ( pnmv->uOldState & LVIS_SELECTED ) )
				{
					// Listbox selection has changed so update edit controls
					if (!bSkipEditControlRefresh)
					{
						UpdateEditControls();
					}
					bSkipEditControlRefresh = false;

					// Forget the saved param, because it was for a different I/O connection.
					m_strLastParam.Empty();
				}
				
				return(TRUE);
			}
		}
	}

	return(CObjectPage::OnNotify(wParam, lParam, pResult));
}


//-----------------------------------------------------------------------------
// Purpose: Empties the contents of the connections list control, freeing the
//			connection list hanging off of each row.
//-----------------------------------------------------------------------------
void COP_Output::RemoveAllEntityConnections(void)
{
	m_ListCtrl.SetRedraw(FALSE);

	int nCount = m_ListCtrl.GetItemCount();
	if (nCount > 0)
	{
		for (int nItem = nCount - 1; nItem >= 0; nItem--)
		{
			COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
			CEntityConnectionList *pConnList = pOutputConn->m_pConnList;
			CMapEntityList *pEntityList = pOutputConn->m_pEntityList;

			m_ListCtrl.DeleteItem(nItem);

			delete pOutputConn;
			delete pConnList;
			delete pEntityList;
		}
	}

	m_ListCtrl.SetRedraw(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : Mode - 
//			pData - 
//-----------------------------------------------------------------------------
void COP_Output::UpdateData( int Mode, PVOID pData, bool bCanEdit )
{
	__super::UpdateData( Mode, pData, bCanEdit );

	if (!IsWindow(m_hWnd))
	{
		return;
	}

	switch (Mode)
	{
		case LoadFirstData:
		{
//			m_ListCtrl.DeleteAllItems();
//			UpdateConnectionList();
			break;
		}

		case LoadData:
		{
//			m_ListCtrl.DeleteAllItems();
//			UpdateConnectionList();
//			SetSelectedItem(0);
			break;
		}

		case LoadFinished:
		{
			m_ListCtrl.DeleteAllItems();
			UpdateConnectionList();
			SetSelectedItem(0);
			SortListByColumn(m_nSortColumn, m_eSortDirection[m_nSortColumn]);
		}
	}

	UpdateEditControls();
}


//------------------------------------------------------------------------------
// Purpose: Generates list of map entites that are being edited from the
//			 m_pObject list
//------------------------------------------------------------------------------
void COP_Output::UpdateEntityList(void)
{
	// Clear old entity list
	m_EntityList.RemoveAll();

	if (m_pObjectList != NULL)
	{
		FOR_EACH_OBJ( *m_pObjectList, pos )
		{
			CMapClass *pObject = m_pObjectList->Element(pos);
	
			if ((pObject != NULL) && (pObject->IsMapClass(MAPCLASS_TYPE(CMapEntity))) )
			{
				CMapEntity *pEntity = (CMapEntity *)pObject;
				m_EntityList.AddToTail(pEntity);
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : nColumn - 
//			eDirection - 
//-----------------------------------------------------------------------------
void COP_Output::SetSortColumn(int nColumn, SortDirection_t eDirection)
{
	Assert(nColumn < OUTPUT_LIST_NUM_COLUMNS);

	//
	// If the sort column changed, update the old sort column header text.
	//
	if (m_nSortColumn != nColumn)
	{
		UpdateColumnHeaderText(m_nSortColumn, false, eDirection);
	}

	//
	// If the sort column or direction changed, update the new sort column header text.
	//
	if ((m_nSortColumn != nColumn) || (m_eSortDirection[m_nSortColumn] != eDirection))
	{
		UpdateColumnHeaderText(nColumn, true, eDirection);
	}

	m_nSortColumn = nColumn;
	m_eSortDirection[m_nSortColumn] = eDirection;

	SortListByColumn(m_nSortColumn, m_eSortDirection[m_nSortColumn]);
}


//-----------------------------------------------------------------------------
// Purpose: Sorts the outputs list by column.
// Input  : nColumn - Index of column by which to sort.
//-----------------------------------------------------------------------------
void COP_Output::SortListByColumn(int nColumn, SortDirection_t eDirection)
{
	PFNLVCOMPARE pfnSort = NULL;

	switch (nColumn)
	{
		case ONLY_ONCE_COLUMN:
		{
			//No Sort
			break;
		}

		case PARAMETER_COLUMN:
		{
			//No Sort
			break;
		}

		case OUTPUT_NAME_COLUMN:
		{
			pfnSort = (PFNLVCOMPARE)ListCompareOutputNames;
			break;
		}

		case TARGET_NAME_COLUMN:
		{
			pfnSort = (PFNLVCOMPARE)ListCompareTargetNames;
			break;
		}

		case INPUT_NAME_COLUMN:
		{
			pfnSort = (PFNLVCOMPARE)ListCompareInputNames;
			break;
		}

		case DELAY_COLUMN:
		{
			pfnSort = (PFNLVCOMPARE)ListCompareDelays;
			break;
		}

		default:
		{
			Assert(FALSE);
			break;
		}
	}

	if (pfnSort != NULL)
	{
		m_ListCtrl.SortItems(pfnSort, (DWORD)eDirection);
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void COP_Output::ResizeColumns(void)
{
	if (m_ListCtrl.GetItemCount() > 0)
	{
		m_ListCtrl.SetColumnWidth(OUTPUT_NAME_COLUMN, LVSCW_AUTOSIZE);
		m_ListCtrl.SetColumnWidth(TARGET_NAME_COLUMN, LVSCW_AUTOSIZE);
		m_ListCtrl.SetColumnWidth(INPUT_NAME_COLUMN, LVSCW_AUTOSIZE);
		m_ListCtrl.SetColumnWidth(DELAY_COLUMN, LVSCW_AUTOSIZE_USEHEADER);
		m_ListCtrl.SetColumnWidth(ONLY_ONCE_COLUMN, LVSCW_AUTOSIZE_USEHEADER);
		m_ListCtrl.SetColumnWidth(PARAMETER_COLUMN, LVSCW_AUTOSIZE);
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void COP_Output::UpdateConnectionList(void)
{	
	// Get list of all entities in the world
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	Assert(pDoc != NULL);
	if (!pDoc)
		return;

	CMapWorld *pWorld = pDoc->GetMapWorld();
	Assert(pWorld != NULL); // dvs: I've seen pWorld be NULL on app shutdown, not sure why we ended up here though
	if (!pWorld)
		return;

	SetMapEntityList(pWorld->EntityList_GetList());

	UpdateEntityList();
	RemoveAllEntityConnections();

	bool bFirst = true;

	FOR_EACH_OBJ( m_EntityList, pos )
	{
		CMapEntity *pEntity = m_EntityList.Element(pos);
		if (pEntity != NULL)
		{
			AddEntityConnections(pEntity, bFirst);
			bFirst = false;
		}
	}
	
	// Update validity flag on all items
	for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
	{
		UpdateItemValidity(nItem);
	}
	UpdateValidityButton();

	ResizeColumns();
}


//------------------------------------------------------------------------------
// Purpose: Set the selected item in the listbox by index.
// Input  : nSelectItem - 
//------------------------------------------------------------------------------
void COP_Output::SetSelectedItem(int nSelectItem)
{
	m_ListCtrl.SetRedraw(FALSE);

	// Set selected item to be active and all others to false
	int nItemCount = m_ListCtrl.GetItemCount();	
	for (int nItem = 0; nItem < nItemCount; nItem++)
	{
		if (nItem == nSelectItem)
		{
			m_ListCtrl.SetItemState(nItem, (unsigned int)LVIS_SELECTED, (unsigned int)LVIS_SELECTED);
		}
		else
		{
			m_ListCtrl.SetItemState(nItem, (unsigned int)~LVIS_SELECTED, (unsigned int)LVIS_SELECTED);
		}
	}

	m_ListCtrl.SetRedraw(TRUE);

	// Selected item has changed so update edit controls
	UpdateEditControls();
}


//------------------------------------------------------------------------------
// Purpose: Set the selected item in the listbox
// Input  : pConnection
//------------------------------------------------------------------------------
void COP_Output::SetSelectedConnection(CEntityConnection *pConnection)
{
	m_ListCtrl.SetRedraw(FALSE);

	// Set selected item to be active and all others to false
	int nItemCount = m_ListCtrl.GetItemCount();	
	for (int nItem = 0; nItem < nItemCount; nItem++)
	{
		COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
		CEntityConnectionList *pTestList = pOutputConn->m_pConnList;

		if (pTestList->Element(0) == pConnection)
		{
			m_ListCtrl.SetItemState(nItem,LVIS_SELECTED,LVIS_SELECTED);
		}
		else
		{
			m_ListCtrl.SetItemState(nItem, (unsigned int)~LVIS_SELECTED, (unsigned int)LVIS_SELECTED);
		}
	}

	m_ListCtrl.SetRedraw(TRUE);

	// Selected item has changed so update edit controls
	UpdateEditControls();
}


//-----------------------------------------------------------------------------
// Purpose: Selects the list box entries that correspond to the connections in
//			the given list.
//-----------------------------------------------------------------------------
void COP_Output::SetSelectedConnections(CEntityConnectionList &List)
{
	m_ListCtrl.SetRedraw(FALSE);

	int nConnCount = List.Count();

	int nItemCount = m_ListCtrl.GetItemCount();
	for (int nItem = 0; nItem < nItemCount; nItem++)
	{
		COutputConnection *pOutputConn = (COutputConnection *)m_ListCtrl.GetItemData(nItem);
		CEntityConnectionList *pConnList = pOutputConn->m_pConnList;

		// See if this row's list holds any of the connections in the given list.
		bool bFound = false;
		for (int nConn = 0; nConn < nConnCount; nConn++)
		{
			CEntityConnection *pConn = List.Element(nConn);
			if (pConnList->Find(pConn) != -1)
			{
				bFound = true;
				break;
			}
		}

		m_ListCtrl.SetItemState(nItem, bFound ? LVIS_SELECTED : ~LVIS_SELECTED, LVIS_SELECTED);
	}

	m_ListCtrl.SetRedraw(TRUE);

	UpdateEditControls();
}


//-----------------------------------------------------------------------------
// Purpose: Adds or removes the little 'V' or '^' sort indicator as appropriate.
// Input  : nColumn - Index of column to update.
//			bSortColumn - true if this column is the sort column, false if not.
//			eDirection - Direction of sort, Sort_Ascending or Sort_Descending.
//-----------------------------------------------------------------------------
void COP_Output::UpdateColumnHeaderText(int nColumn, bool bIsSortColumn, SortDirection_t eDirection)
{
	char szHeaderText[MAX_PATH];

	LVCOLUMN Column;
	memset(&Column, 0, sizeof(Column));
	Column.mask = LVCF_TEXT;
	Column.pszText = szHeaderText;
	Column.cchTextMax = sizeof(szHeaderText);
	m_ListCtrl.GetColumn(nColumn, &Column);

	int nMarker = 0;

	if (szHeaderText[0] != '\0')
	{
		nMarker = strlen(szHeaderText) - 1;
		char chMarker = szHeaderText[nMarker];

		if ((chMarker == '>') || (chMarker == '<'))
		{
			nMarker -= 2;
		}
		else
		{
			nMarker++;
		}
	}

	if (bIsSortColumn)
	{
		if (nMarker != 0)
		{
			szHeaderText[nMarker++] = ' ';
			szHeaderText[nMarker++] = ' ';
		}

		szHeaderText[nMarker++] = (eDirection == Sort_Ascending) ? '>' : '<';
	}

	szHeaderText[nMarker] = '\0';

	m_ListCtrl.SetColumn(nColumn, &Column);
}


//-----------------------------------------------------------------------------
// Purpose: Called when our window is being destroyed.
//-----------------------------------------------------------------------------
void COP_Output::OnDestroy(void)
{
	m_ListCtrl.EnableWindow(false);
	RemoveAllEntityConnections();
}


//------------------------------------------------------------------------------
// Purpose:
//------------------------------------------------------------------------------
void COP_Output::UpdateEditedFireOnce(void)
{
	// Get new delay
	CButton *pButton = ( CButton* )GetDlgItem( IDC_EDIT_CONN_FIRE_ONCE );

	if (pButton->IsWindowEnabled())
	{
		int nChecked = (pButton->GetState()&0x0003);  // Checked state

		// Update the connections
		int nConnCount = m_EditList.Count();
		for (int nConn = 0; nConn < nConnCount; nConn++)
		{
			CEntityConnection *pConnection = m_EditList.Element(nConn);
			if (pConnection != NULL)
			{
				pConnection->SetTimesToFire(nChecked?1:EVENT_FIRE_ALWAYS);
			}
		}

		// Update the list box
		for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
		{
			if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
			{
				m_ListCtrl.SetItemText(nItem, ONLY_ONCE_COLUMN, nChecked ? "Yes" : "No");
			}
		}
		ResizeColumns();
	}
}


//------------------------------------------------------------------------------
// Purpose:
//------------------------------------------------------------------------------
void COP_Output::UpdateEditedDelays(void)
{
	// Get new delay
	CEdit *pDelayEdit = ( CEdit* )GetDlgItem( IDC_EDIT_CONN_DELAY );

	if (pDelayEdit->IsWindowEnabled())
	{
		char strDelay[MAX_IO_NAME_LEN];
		pDelayEdit->GetWindowText(strDelay, sizeof(strDelay));
		float flDelay = atof(strDelay);

		// Update the connections
		int nConnCount = m_EditList.Count();
		for (int nConn = 0; nConn < nConnCount; nConn++)
		{
			CEntityConnection *pConnection = m_EditList.Element(nConn);
			if (pConnection != NULL)
			{
				pConnection->SetDelay(flDelay);
			}
		}

		// Update the list box
		for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
		{
			if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
			{
				m_ListCtrl.SetItemText(nItem, DELAY_COLUMN, strDelay);	
			}
		}
		ResizeColumns();
	}
}


//------------------------------------------------------------------------------
// Purpose: Parameters have changed.  Update connections and listbox
//------------------------------------------------------------------------------
void COP_Output::UpdateEditedParams(void)
{
	CComboBox *pParamEdit = ( CComboBox* )GetDlgItem( IDC_EDIT_CONN_PARAM );

	if (pParamEdit->IsWindowEnabled())
	{
		char strParam[MAX_IO_NAME_LEN];
		pParamEdit->GetWindowText(strParam, sizeof(strParam));
		if (!strcmp(strParam, PARAM_STRING_NONE))
		{
			strParam[0] = '\0';
		}

		// Update the connections
		int nConnCount = m_EditList.Count();
		for (int nConn = 0; nConn < nConnCount; nConn++)
		{
			CEntityConnection *pConnection = m_EditList.Element(nConn);
			if (pConnection != NULL)
			{
				pConnection->SetParam(strParam);
			}
		}

		// Update the list box
		for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
		{
			if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
			{
				m_ListCtrl.SetItemText(nItem, PARAMETER_COLUMN, strParam);	
			}
		}
		ResizeColumns();
	}
}


//------------------------------------------------------------------------------
// Purpose: Inputs have changed.  Update connections and listbox
//------------------------------------------------------------------------------
void COP_Output::UpdateEditedInputs(void)
{
	// Get the new name
	char strInput[MAX_IO_NAME_LEN];
	GetInput(strInput, sizeof(strInput));

	// Update the connections
	int nConnCount = m_EditList.Count();
	for (int nConn = 0; nConn < nConnCount; nConn++)
	{
		CEntityConnection *pConnection = m_EditList.Element(nConn);
		if (pConnection != NULL)
		{
			pConnection->SetInputName(strInput);
		}
	}

	// Update the list box
	for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
	{
		if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
		{
			m_ListCtrl.SetItemText(nItem, INPUT_NAME_COLUMN, strInput);	
			UpdateItemValidity(nItem);
		}
	}
	UpdateValidityButton();
	ResizeColumns();
}


//------------------------------------------------------------------------------
// Purpose: Outputs have changed.  Update connections and listbox
//------------------------------------------------------------------------------
void COP_Output::UpdateEditedOutputs()
{
	// Get the new name
	char strOutput[MAX_IO_NAME_LEN];
	GetOutput(strOutput, sizeof(strOutput));

	// Update the connections
	int nConnCount = m_EditList.Count();
	for (int nConn = 0; nConn < nConnCount; nConn++)
	{
		CEntityConnection *pConnection = m_EditList.Element(nConn);
		if (pConnection != NULL)
		{
			pConnection->SetOutputName(strOutput);
		}
	}

	// Update the list box
	for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
	{
		if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
		{
			m_ListCtrl.SetItemText(nItem, OUTPUT_NAME_COLUMN, strOutput);
			UpdateItemValidity(nItem);
		}
	}
	UpdateValidityButton();
	ResizeColumns();
}


//------------------------------------------------------------------------------
// Purpose: Targets have changed.  Update connections and listbox
//------------------------------------------------------------------------------
void COP_Output::UpdateEditedTargets(void)
{
	// Get the new target name
	char strTarget[MAX_IO_NAME_LEN];
	GetTarget(strTarget, sizeof(strTarget));

	// Update the connections
	int nConnCount = m_EditList.Count();
	for (int nConn = 0; nConn < nConnCount; nConn++)
	{
		CEntityConnection *pConnection = m_EditList.Element(nConn);
		if (pConnection != NULL)
		{
			pConnection->SetTargetName(strTarget);
		}
	}

	// Update the list box
	for (int nItem = 0; nItem < m_ListCtrl.GetItemCount(); nItem++)
	{
		if (m_ListCtrl.GetItemState(nItem, LVIS_SELECTED) & LVIS_SELECTED)
		{
			m_ListCtrl.SetItemText(nItem, TARGET_NAME_COLUMN, strTarget);	
			UpdateItemValidity(nItem);
		}
	}
	UpdateValidityButton();
	ResizeColumns();
}


//-----------------------------------------------------------------------------
// Purpose: Enables or diables the target combo box and the eyedropper button.
//-----------------------------------------------------------------------------
void COP_Output::EnableTarget(bool bEnable)
{
	m_ComboTarget.EnableWindow(bEnable);
	GetDlgItem(IDC_PICK_ENTITY)->EnableWindow(bEnable);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pConnection - 
//-----------------------------------------------------------------------------
void COP_Output::SetConnection(CEntityConnectionList *pConnectionList)
{
	Assert(pConnectionList != NULL);
	
	// Fill edit boxes.  Disable for multiple connections have incompatible data
	bool		bFirst		= true;
	CButton*	pFireEdit	= ( CButton* )GetDlgItem( IDC_EDIT_CONN_FIRE_ONCE );
	CEdit*		pDelayEdit	= ( CEdit* )GetDlgItem( IDC_EDIT_CONN_DELAY );
	CComboBox*	pParamEdit	= ( CComboBox* )GetDlgItem( IDC_EDIT_CONN_PARAM );

	m_ComboOutput.EnableWindow(true);
	EnableTarget(true);
	m_ComboInput.EnableWindow(true);
	pFireEdit->EnableWindow(true);
	pDelayEdit->EnableWindow(true);
	pParamEdit->EnableWindow(true);  
	GetDlgItem(IDC_PICK_ENTITY_PARAM)->EnableWindow( false );
	m_bEntityParamTarget = false;

	int nConnCount = pConnectionList->Count();
	for (int nConn = 0; nConn < nConnCount; nConn++)
	{
		CEntityConnection *pConnection = (CEntityConnection *)pConnectionList->Element(nConn);
		if (pConnection == NULL)
			continue;

		// Fill in output name, disable for non-compatible connections
		if (m_ComboOutput.IsWindowEnabled())
		{
			if (bFirst)
			{
				m_strOutput = pConnection->GetOutputName();
			}
			else if (m_strOutput != pConnection->GetOutputName())
			{
				m_strOutput.Empty();
				m_ComboOutput.EnableWindow(false);
			}
		}

		// Fill in target name, disable for non-compatible connections
		if (m_ComboTarget.IsWindowEnabled())
		{
			if (bFirst)
			{
				m_strTarget = pConnection->GetTargetName();
			}
			else if (m_strTarget != pConnection->GetTargetName())
			{
				m_strTarget.Empty();
				EnableTarget(false);
			}
		}

		// Fill in input name, disable for non-compatible connections
		if (m_ComboInput.IsWindowEnabled())
		{
			if (bFirst)
			{
				m_strInput = pConnection->GetInputName();
			}
			else if (m_strInput != pConnection->GetInputName())
			{
				m_strInput.Empty();
				m_ComboInput.EnableWindow(false);
			}
		}

		// Fill in parameters, disable for non-compatible connections
		if (pParamEdit->IsWindowEnabled())
		{
			if (bFirst)
			{
				m_strParam = pConnection->GetParam();
				m_bNoParamEdit = false;
			}
			else if (m_strParam != pConnection->GetParam())
			{
				m_strParam.Empty();
				pParamEdit->EnableWindow(false);
				GetDlgItem(IDC_PICK_ENTITY_PARAM)->EnableWindow( false );
				m_bNoParamEdit = true;
			}
		}

		// Fill in delay, disable for non-compatible connections
		if (pDelayEdit->IsWindowEnabled())
		{
			if (bFirst)
			{
				m_fDelay = pConnection->GetDelay();
			}
			else if (m_fDelay != pConnection->GetDelay())
			{
				m_fDelay = 0;
				pDelayEdit->EnableWindow(false);
			}
		}

		// Set fire once flag, disable for non-compatible connections
		if (pFireEdit->IsWindowEnabled())
		{
			if (bFirst)
			{
				m_bFireOnce = (pConnection->GetTimesToFire() == -1) ? false : true;
			}
			else if (m_bFireOnce != pConnection->GetTimesToFire())
			{
				m_bFireOnce = false;
				pFireEdit->EnableWindow(false);
			}
		}

		bFirst = false;
	}

	// Put a <none> in param box if no param
	if (strlen(m_strParam) == 0)
	{
		m_strParam = PARAM_STRING_NONE;
	}
}


//-----------------------------------------------------------------------------
// Purpose: Adds all of an entity's outputs from its class definition to the
//			outputs combo box.
// Input  : pEntity - Entity whose outputs are to be added to the combo box.
//-----------------------------------------------------------------------------
void COP_Output::AddEntityOutputs(CMapEntity *pEntity)
{
	GDclass *pClass = pEntity->GetClass();
	if (pClass != NULL)
	{
		int nCount = pClass->GetOutputCount();
		for (int i = 0; i < nCount; i++)
		{
			CClassOutput *pOutput = pClass->GetOutput(i);
			int nIndex = m_ComboOutput.AddString(pOutput->GetName());
			if (nIndex >= 0)
			{
				m_ComboOutput.SetItemDataPtr(nIndex, pOutput);
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void COP_Output::FillInputList(void)
{
	if (!m_pMapEntityList)
	{
		return;
	}

	//
	// Add all entity inputs to the inputs combo box.
	//
	m_ComboInput.SetRedraw(FALSE);
	m_ComboInput.ResetContent();

	// CUtlVector<GDclass*> classCache;
	CUtlRBTree<int,int> classCache;
	SetDefLessFunc( classCache );
	
	FOR_EACH_OBJ( *m_pMapEntityList, pos )
	{
		CMapEntity *pEntity = m_pMapEntityList->Element(pos);
		Assert(pEntity != NULL);

		if (pEntity == NULL)
			continue;
		
		//
		// Get the entity's class, which contains the list of inputs that this entity exposes.
		//
		GDclass *pClass = pEntity->GetClass();

        if (pClass == NULL)
			continue;

		// check if class was already added
		if ( classCache.Find( (int)pClass ) != -1 )
			continue;

		classCache.Insert( (int)pClass );
			
		//
		// Add this class' inputs to the list.
		//
		int nCount = pClass->GetInputCount();
		for (int i = 0; i < nCount; i++)
		{
			CClassInput *pInput = pClass->GetInput(i);
			bool bAddInput = true;

			//
			// Don't add the input to the combo box if another input with the same name
			// and type is already there.
			//
			int nIndex = m_ComboInput.FindStringExact(-1, pInput->GetName());
			if (nIndex != CB_ERR)
			{
				CClassInput *pExistingInput = (CClassInput *)m_ComboInput.GetItemDataPtr(nIndex);
				if (pExistingInput->GetType() == pInput->GetType())
				{
					bAddInput = false;
				}
			}

			if (bAddInput)
			{
				nIndex = m_ComboInput.AddString(pInput->GetName());
				if (nIndex >= 0)
				{
					m_ComboInput.SetItemDataPtr(nIndex, pInput);
				}
			}
		}
	}

	m_ComboInput.SetRedraw(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: Fills the list of outputs with outputs common to all the selected entities.
//-----------------------------------------------------------------------------
void COP_Output::FillOutputList(void)
{
	if ( m_EntityList.Count() == 0 )
	{
		return;
	}

	//
	// Determine what the currently selected output is (if any).
	//
	CClassOutput *pSelectedOutput;
	int nOutput = m_ComboOutput.GetCurSel();
	if (nOutput != CB_ERR)
	{
		pSelectedOutput = (CClassOutput *)m_ComboOutput.GetItemDataPtr(nOutput);
	}
	else
	{
		pSelectedOutput = NULL;
	}

	//
	// Add the entity outputs to the outputs combo box.
	//
	m_ComboOutput.SetRedraw(FALSE);
	m_ComboOutput.ResetContent();

	bool bFirst = true;
	
	FOR_EACH_OBJ( m_EntityList, pos )
	{
		CMapEntity *pEntity = m_EntityList.Element(pos);

		if (bFirst)
		{
			//
			// The first entity adds its outputs to the list.
			//
			AddEntityOutputs(pEntity);
			bFirst = false;
		}
		else
		{
			//
			// All subsequent entities filter the output list.
			//
			FilterEntityOutputs(pEntity);	
		}
	}

	if (m_ComboOutput.GetCount() == 0)
	{
		m_ComboOutput.EnableWindow(false);
	}

	m_ComboOutput.SetRedraw(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: Fills the list of targets with entities that have "targetname" keys.
//-----------------------------------------------------------------------------
void COP_Output::FillTargetList(void)
{
	m_bIgnoreTextChanged = true;
	m_ComboTarget.SetEntityList(m_pMapEntityList);
	m_bIgnoreTextChanged = false;
}


//-----------------------------------------------------------------------------
// Purpose: Removes all outputs from the outputs combo box that are NOT present
//			in the given entity's output list. Used when multiple entities are
//			selected into the Entity Properties dialog.
// Input  : pEntity - Entity to use for filter.
//-----------------------------------------------------------------------------
void COP_Output::FilterEntityOutputs(CMapEntity *pEntity)
{
	//
	// Make sure that this entity has a valid class to use for filtering.
	//
	GDclass *pClass = pEntity->GetClass();
	if (pClass == NULL)
	{
		return;
	}

	//
	// Remove any outputs from the combo box that are not in the class.
	//
	char szText[MAX_PATH];

	int nCount = m_ComboOutput.GetCount();
	if (nCount > 0)
	{
		for (int i = nCount - 1; i >= 0; i--)
		{
			if (m_ComboOutput.GetLBText(i, szText) != CB_ERR)
			{
				if (pClass->FindOutput(szText) == NULL)
				{
					m_ComboOutput.DeleteString(i);
				}
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void COP_Output::FilterOutputList(void)
{
	// dvs: Possibly unnecessary. For example, if they choose an input, then
	// choose an incompatible output, the input will become red to indicate
	// the incompatibilty. So maybe the outputs can always contain the set of
	// all outputs common to the selected entities.
}


//-----------------------------------------------------------------------------
// Purpose: Filters the list of inputs based on the current selected target.
//-----------------------------------------------------------------------------
void COP_Output::FilterInputList(void)
{
	char szTarget[MAX_ENTITY_NAME_LEN];
	CMapEntityList *pTargets = GetTarget(szTarget, sizeof(szTarget));

	if (pTargets != NULL)
	{
		//
		// Remove all items from the inputs combo that:
		//
		// 1) Are not compatible with the currently selected output, OR
		// 2) Are not found in the currently selected targets list.
		//
		int nCount = m_ComboInput.GetCount();
		if (nCount > 0)
		{
			for (int i = nCount - 1; i >= 0; i--)
			{
				CClassInput *pInput = (CClassInput *)m_ComboInput.GetItemDataPtr(i);
				if (!MapEntityList_HasInput(pTargets, pInput->GetName(), pInput->GetType()))
				{
					m_ComboInput.DeleteString(i);
				}
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void COP_Output::FilterTargetList(void)
{
#if 0 // Not used...
	char szInput[MAX_IO_NAME_LEN];
	CClassInput *pInput = GetInput(szInput, sizeof(szInput));

	//
	// Remove all items from the targets combo that:
	//
	// 1) Do not have the selected input name OR
	// 2) Do not have inputs that are compatible with the selected output.
	//
	int nCount = m_ComboTarget.GetCount();
	if (nCount > 0)
	{
		for (int i = nCount - 1; i >= 0; i--)
		{
			CMapEntityList *pTargets = (CMapEntityList *)m_ComboTarget.GetItemDataPtr(i);
						
			if (!MapEntityList_HasInput(pTargets, pInput->GetName(), pInput->GetType()))
			{
				m_ComboTarget.DeleteString(i);
			}
		}
	}
#endif
}


//-----------------------------------------------------------------------------
// Purpose: Returns the currently selected input, NULL if unknown.
// Input  : szInput - Receives the text in the Input combo edit control.
//			nSize - Size of buffer pointed to by szInput.
//-----------------------------------------------------------------------------
CClassInput *COP_Output::GetInput(char *szInput, int nSize)
{
	szInput[0] = '\0';

	int nCurSel = m_ComboInput.GetCurSel();
	if (nCurSel == CB_ERR)
	{
		if (m_ComboInput.GetWindowText(szInput, nSize) > 0)
		{
			nCurSel = m_ComboInput.FindStringExact(-1, szInput);
		}
	}

	CClassInput *pInput = NULL;
	if (nCurSel != CB_ERR)
	{
		m_ComboInput.GetLBText(nCurSel, szInput);
		pInput = (CClassInput *)m_ComboInput.GetItemDataPtr(nCurSel);
	}

	return(pInput);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the currently selected output, NULL if unknown.
// Input  : szOutput - Receives the text in the Output combo edit control.
//			nSize - Size of buffer pointed to by szOutput.
//-----------------------------------------------------------------------------
CClassOutput *COP_Output::GetOutput(char *szOutput, int nSize)
{
	szOutput[0] = '\0';

	int nCurSel = m_ComboOutput.GetCurSel();
	if (nCurSel == CB_ERR)
	{
		if (m_ComboOutput.GetWindowText(szOutput, nSize) > 0)
		{
			nCurSel = m_ComboOutput.FindStringExact(-1, szOutput);
		}
	}

	CClassOutput *pOutput = NULL;
	if (nCurSel != CB_ERR)
	{
		m_ComboOutput.GetLBText(nCurSel, szOutput);
		pOutput = (CClassOutput *)m_ComboOutput.GetItemDataPtr(nCurSel);
	}

	return(pOutput);
}


//-----------------------------------------------------------------------------
// Purpose: Returns the currently selected target list, NULL if unknown.
// Input  : szTarget - Receives the text in the Target combo edit control.
//			nSize - Size of buffer pointed to by szTarget.
//-----------------------------------------------------------------------------
CMapEntityList *COP_Output::GetTarget(char *szTarget, int nSize)
{
	szTarget[0] = '\0';

	CString str = m_ComboTarget.GetCurrentItem();
	Q_strncpy( szTarget, str, nSize );

	return m_ComboTarget.GetSubEntityList( szTarget );
}


//-----------------------------------------------------------------------------
// Purpose: Called when the contents of the delay edit box change.
//-----------------------------------------------------------------------------
void COP_Output::OnEditDelay(void)
{
	UpdateEditedDelays();
}


//-----------------------------------------------------------------------------
// Purpose: Called when the contents of the target combo edit box change.
//-----------------------------------------------------------------------------
void COP_Output::OnFireOnce(void)
{
	UpdateEditedFireOnce();
}


//-----------------------------------------------------------------------------
// Purpose: Called when they change the "Show Hidden Targets" checkbox.
//-----------------------------------------------------------------------------
void COP_Output::OnShowHiddenTargetsAsBroken()
{
	// Remember the last state of this checkbox.
	Options.general.bShowHiddenTargetsAsBroken = (m_ctlShowHiddenTargetsAsBroken.GetCheck() != FALSE);
	
	// Refresh.
	int nCount = m_ListCtrl.GetItemCount();
	for ( int i=0; i < nCount; i++ )
	{
		UpdateItemValidity( i );
	}
	//UpdateConnectionList();
}


//-----------------------------------------------------------------------------
// Purpose: React to the input combo box being changed
//-----------------------------------------------------------------------------
void COP_Output::InputChanged(void)
{
	// Updating the listbox data, will trigger the edit
	// controls to update.  They don't need to be
	bSkipEditControlRefresh = true;

	char szInput[MAX_IO_NAME_LEN];
	CClassInput *pInput = GetInput(szInput, sizeof(szInput));
	UpdateCombosForSelectedInput(pInput);
	UpdateEditedInputs();
}


//-----------------------------------------------------------------------------
// Purpose: Called when selection of input combo box chages
//-----------------------------------------------------------------------------
void COP_Output::OnSelChangeInput(void)
{
	InputChanged();
}


//-----------------------------------------------------------------------------
// Purpose: Called when the contents of the input combo edit box change.
//-----------------------------------------------------------------------------
void COP_Output::OnEditUpdateInput(void)
{
	InputChanged();
}


//------------------------------------------------------------------------------
// Purpose: React to the output combo box being changed
//------------------------------------------------------------------------------
void COP_Output::OutputChanged(void)
{
	// Updating the listbox data, will trigger the edit
	// controls to update.  They don't need to be
	bSkipEditControlRefresh = true;

	char szOutput[MAX_IO_NAME_LEN];
	CClassOutput *pOutput = GetOutput(szOutput, sizeof(szOutput));
	UpdateCombosForSelectedOutput(pOutput);
	UpdateEditedOutputs();
}


//-----------------------------------------------------------------------------
// Purpose: Called when selection of output combo box chages
//-----------------------------------------------------------------------------
void COP_Output::OnSelChangeOutput(void)
{
	OutputChanged();
}


//-----------------------------------------------------------------------------
// Purpose: Called when the contents of the output combo edit box change.
//-----------------------------------------------------------------------------
void COP_Output::OnEditUpdateOutput(void)
{
	OutputChanged();
}


//-----------------------------------------------------------------------------
// Purpose: Called when selection of parameter combo box chages 
//-----------------------------------------------------------------------------
void COP_Output::OnSelChangeParam(void)
{
	// If user picked <none> selection (the only valid one) clear window text
	CComboBox *pParamEdit = ( CComboBox* )GetDlgItem( IDC_EDIT_CONN_PARAM );
	if (pParamEdit->GetCurSel() != CB_ERR)
	{
		pParamEdit->SetWindowText("");
	}

	UpdateEditedParams();
}


//-----------------------------------------------------------------------------
// Purpose: Called when the contents of the parameter combo edit box change.
//-----------------------------------------------------------------------------
void COP_Output::OnEditUpdateParam(void)
{
	UpdateEditedParams();
}


//-----------------------------------------------------------------------------
// Purpose: Updates the dialog based on the currently selected input.
// Input  : pInput - Pointer to the input that is selected, NULL if none or
//			ambiguous/unresolved.
//-----------------------------------------------------------------------------
void COP_Output::UpdateCombosForSelectedInput(CClassInput *pInput)
{
	// Enable / Disable param box based on input type if allowed
	if (!m_bNoParamEdit)
	{
		CComboBox *pParamCombo = (CComboBox *)GetDlgItem(IDC_EDIT_CONN_PARAM);
		bool bEnable = ((!pInput) || (pInput && (pInput->GetType() != iotVoid)));
		if (!bEnable)
		{
			// Save the param so we can restore it if they switch right back.
			CString strTemp;
			pParamCombo->GetWindowText(strTemp);
			if (strTemp.Compare(PARAM_STRING_NONE))
			{
				m_strLastParam = strTemp;
			}

			// Switch back to <none> if we're disabling the parameter combo.
			pParamCombo->SetCurSel(0);
		}
		else if (!m_strLastParam.IsEmpty())
		{
			pParamCombo->SetWindowText(m_strLastParam);
		}

		UpdateEditedParams();
		pParamCombo->EnableWindow(bEnable);
		m_bEntityParamTarget = pInput && (pInput->GetType() == iotEHandle);
		GetDlgItem(IDC_PICK_ENTITY_PARAM)->EnableWindow( m_bEntityParamTarget );
	}

	if (pInput != NULL)
	{
		//
		// Known input, render it in black.
		//
		m_ComboInput.SetTextColor(RGB(0, 0, 0));
	}
	else
	{
		//
		// Unknown input, render it in red.
		//
		m_ComboInput.SetTextColor(RGB(255, 0, 0));
	}
	m_ComboInput.RedrawWindow();
}


//-----------------------------------------------------------------------------
// Purpose: Updates the dialog based on the currently selected output.
// Input  : pOutput - Pointer to the output that is selected, NULL if none or
//			ambiguous/unresolved.
//-----------------------------------------------------------------------------
void COP_Output::UpdateCombosForSelectedOutput(CClassOutput *pOutput)
{
	if (pOutput != NULL)
	{
		//
		// Known output, render it in black.
		//
		m_ComboOutput.SetTextColor(RGB(0, 0, 0));
	}
	else
	{
		//
		// Unknown output, render it in red.
		//
		m_ComboOutput.SetTextColor(RGB(255, 0, 0));
	}
	m_ComboOutput.RedrawWindow();
}


//-----------------------------------------------------------------------------
// Purpose: Stops entity picking.
//-----------------------------------------------------------------------------
void COP_Output::StopPicking(void)
{
	if (m_bPickingEntities)
	{
		m_bPickingEntities = false;
		ToolManager()->SetTool(TOOL_POINTER);

		CButton *pButton = (CButton *)GetDlgItem(IDC_PICK_ENTITY);
		if (pButton)
		{
			pButton->SetCheck(0);
		}

		pButton = (CButton *)GetDlgItem(IDC_PICK_ENTITY_PARAM);
		if (pButton)
		{
			pButton->SetCheck(0);
		}

		if ( m_ComboTarget.IsWindowEnabled() )
		{
			GetDlgItem(IDC_PICK_ENTITY)->EnableWindow( true );
		}

		CComboBox* pParamEdit = ( CComboBox* )GetDlgItem( IDC_EDIT_CONN_PARAM );
		if ( pParamEdit->IsWindowEnabled() )
		{
			GetDlgItem(IDC_PICK_ENTITY_PARAM)->EnableWindow( m_bEntityParamTarget );
		}
	}
}

void COP_Output::OnSize( UINT nType, int cx, int cy )
{
	m_AnchorMgr.OnSize();
}