[c8d76d]: src / ui / Windows / PWTreeCtrl.cpp  Maximize  Restore  History

Download this file

2408 lines (2062 with data), 73.2 kB

   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
/*
* Copyright (c) 2003-2014 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/*
* Use of CInfoDisplay to replace MS's broken ToolTips support.
* Based on CInfoDisplay class taken from Asynch Explorer by
* Joseph M. Newcomer [MVP]; http://www.flounder.com
* Additional enhancements to the use of this code have been made to
* allow for delayed showing of the display and for a limited period.
*/
#include "stdafx.h"
#include "PWTreeCtrl.h"
#include "DboxMain.h"
#include "ThisMfcApp.h"
#include "DDSupport.h"
#include "InfoDisplay.h"
#include "SecString.h"
#include "SMemFile.h"
#include "GeneralMsgBox.h"
#include "core/ItemData.h"
#include "core/Util.h"
#include "core/Pwsprefs.h"
#include "os/debug.h"
#include <algorithm>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Hover time of 1.5 seconds before expanding a group during D&D
#define HOVERTIME 1500
extern const wchar_t GROUP_SEP = L'.';
extern const wchar_t *GROUP_SEP2 = L".";
// following header for D&D data passed over OLE:
// Process ID of sender (to determine if src == tgt)
// Type of data
// Length of actual payload, in bytes.
static const char *OLE_HDR_FMT = "%08x%02x%08x";
static const int OLE_HDR_LEN = 18;
/*
* Following classes are used to "Fake" multiple inheritance:
* Ideally, CPWTreeCtrl should derive from CTreeCtrl, COleDropTarget
* and COleDropSource. However, since m'soft, in their infinite
* wisdom, couldn't get this common use-case straight,
* we use the following classes as proxies: CPWTreeCtrl
* has a member var for each, registers said member appropriately
* for D&D, and member calls parent's method to do the grunt work.
*/
class CPWTDropTarget : public COleDropTarget
{
public:
CPWTDropTarget(CPWTreeCtrl *parent) : m_tree(*parent) {}
DROPEFFECT OnDragEnter(CWnd* pWnd , COleDataObject* pDataObject,
DWORD dwKeyState, CPoint point)
{return m_tree.OnDragEnter(pWnd, pDataObject, dwKeyState, point);}
DROPEFFECT OnDragOver(CWnd* pWnd , COleDataObject* pDataObject,
DWORD dwKeyState, CPoint point)
{return m_tree.OnDragOver(pWnd, pDataObject, dwKeyState, point);}
void OnDragLeave(CWnd*)
{m_tree.OnDragLeave();}
BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject,
DROPEFFECT dropEffect, CPoint point)
{return m_tree.OnDrop(pWnd, pDataObject, dropEffect, point);}
private:
CPWTreeCtrl &m_tree;
};
class CPWTDropSource : public COleDropSource
{
public:
CPWTDropSource(CPWTreeCtrl *parent) : m_tree(*parent) {}
virtual SCODE QueryContinueDrag(BOOL bEscapePressed, DWORD dwKeyState)
{
// To prevent processing in multiple calls to CStaticDataSource::OnRenderGlobalData
// Only process the request if data has been dropped.
SCODE sCode = COleDropSource::QueryContinueDrag(bEscapePressed, dwKeyState);
if (sCode == DRAGDROP_S_DROP) {
pws_os::Trace(L"CStaticDropSource::QueryContinueDrag - dropped\n");
m_tree.EndDrop();
}
return sCode;
}
virtual SCODE GiveFeedback(DROPEFFECT dropEffect)
{return m_tree.GiveFeedback(dropEffect);}
private:
CPWTreeCtrl &m_tree;
};
class CPWTDataSource : public COleDataSource
{
public:
CPWTDataSource(CPWTreeCtrl *parent, COleDropSource *ds)
: m_tree(*parent), m_DropSource(ds) {}
DROPEFFECT StartDragging(CLIPFORMAT cpfmt, LPCRECT rClient)
{
DelayRenderData(cpfmt);
DelayRenderData(CF_UNICODETEXT);
DelayRenderData(CF_TEXT);
m_tree.m_cfdropped = 0;
//pws_os::Trace(L"CPWTDataSource::StartDragging - calling DoDragDrop\n");
DROPEFFECT de = DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE,
rClient, m_DropSource);
// Cleanup:
// Standard processing is for the recipient to do this!!!
if (de == DROPEFFECT_NONE) {
if (m_tree.m_hgDataALL != NULL) {
//pws_os::Trace(L"CPWTDataSource::StartDragging - Unlock/Free m_hgDataALL\n");
LPVOID lpData = GlobalLock(m_tree.m_hgDataALL);
SIZE_T memsize = GlobalSize(m_tree.m_hgDataALL);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_tree.m_hgDataALL);
GlobalFree(m_tree.m_hgDataALL);
m_tree.m_hgDataALL = NULL;
}
if (m_tree.m_hgDataTXT != NULL) {
//pws_os::Trace(L"CPWTDataSource::StartDragging - Unlock/Free m_hgDataTXT\n");
LPVOID lpData = GlobalLock(m_tree.m_hgDataTXT);
SIZE_T memsize = GlobalSize(m_tree.m_hgDataTXT);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_tree.m_hgDataTXT);
GlobalFree(m_tree.m_hgDataTXT);
m_tree.m_hgDataTXT = NULL;
}
if (m_tree.m_hgDataUTXT != NULL) {
//pws_os::Trace(L"CPWTDataSource::StartDragging - Unlock/Free m_hgDataUTXT\n");
LPVOID lpData = GlobalLock(m_tree.m_hgDataUTXT);
SIZE_T memsize = GlobalSize(m_tree.m_hgDataUTXT);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_tree.m_hgDataUTXT);
GlobalFree(m_tree.m_hgDataUTXT);
m_tree.m_hgDataUTXT = NULL;
}
}
return de;
}
virtual BOOL OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
{return m_tree.OnRenderGlobalData(lpFormatEtc, phGlobal);}
private:
CPWTreeCtrl &m_tree;
COleDropSource *m_DropSource;
};
/**
* Implementation of CPWTreeCtrl begins here
*/
CPWTreeCtrl::CPWTreeCtrl()
: m_isRestoring(false), m_bWithinThisInstance(true),
m_bMouseInWindow(false), m_nHoverNDTimerID(0), m_nShowNDTimerID(0),
m_hgDataALL(NULL), m_hgDataTXT(NULL), m_hgDataUTXT(NULL),
m_bFilterActive(false), m_bUseHighLighting(false)
{
// Register a clipboard format for column drag & drop.
// Note that it's OK to register same format more than once:
// "If a registered format with the specified name already exists,
// a new format is not registered and the return value identifies the existing format."
CString cs_CPF(MAKEINTRESOURCE(IDS_CPF_TVDD));
m_tcddCPFID = (CLIPFORMAT)RegisterClipboardFormat(cs_CPF);
ASSERT(m_tcddCPFID != 0);
// instantiate "proxy" objects for D&D.
// The members are currently pointers mainly to hide
// their implementation from the header file. If this changes,
// e.g., if we make them nested classes, then they should
// be non-pointers.
m_DropTarget = new CPWTDropTarget(this);
m_DropSource = new CPWTDropSource(this);
m_DataSource = new CPWTDataSource(this, m_DropSource);
}
CPWTreeCtrl::~CPWTreeCtrl()
{
// see comment in constructor re these member variables
delete m_DropTarget;
delete m_DropSource;
delete m_DataSource;
}
BEGIN_MESSAGE_MAP(CPWTreeCtrl, CTreeCtrl)
//{{AFX_MSG_MAP(CPWTreeCtrl)
ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT, OnBeginLabelEdit)
ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndLabelEdit)
ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBeginDrag)
ON_NOTIFY_REFLECT(TVN_BEGINRDRAG, OnBeginDrag)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnExpandCollapse)
ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelectionChanged)
ON_NOTIFY_REFLECT(TVN_DELETEITEM, OnDeleteItem)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_WM_DESTROY()
ON_WM_TIMER()
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CPWTreeCtrl::Initialize()
{
// This should really be in OnCreate(), but for some reason,
// it was never called.
m_DropTarget->Register(this);
}
void CPWTreeCtrl::ActivateND(const bool bActivate)
{
m_bShowNotes = bActivate;
if (!m_bShowNotes) {
m_bMouseInWindow = false;
}
}
void CPWTreeCtrl::OnDestroy()
{
CImageList *pimagelist = GetImageList(TVSIL_NORMAL);
if (pimagelist != NULL) {
pimagelist->DeleteImageList();
delete pimagelist;
}
m_DropTarget->Revoke();
}
void CPWTreeCtrl::OnPaint()
{
CTreeCtrl::OnPaint();
app.GetMainDlg()->SaveGUIStatusEx(DboxMain::iTreeOnly);
}
void CPWTreeCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
{
CTreeCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
app.GetMainDlg()->SaveGUIStatusEx(DboxMain::iTreeOnly);
}
BOOL CPWTreeCtrl::PreTranslateMessage(MSG* pMsg)
{
// When an item is being edited make sure the edit control
// receives certain important key strokes
if (GetEditControl()) {
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return TRUE; // DO NOT process further
}
// Process User's AutoType shortcut
if (app.GetMainDlg()->CheckPreTranslateAutoType(pMsg))
return TRUE;
// Process User's Delete shortcut
if (app.GetMainDlg()->CheckPreTranslateDelete(pMsg))
return TRUE;
// Process user's Rename shortcut
if (app.GetMainDlg()->CheckPreTranslateRename(pMsg)) {
// Send via main window to ensure it isn't an Edit in place
HTREEITEM hItem = GetSelectedItem();
CItemData *pci = (CItemData *)GetItemData(hItem);
app.GetMainDlg()->SendMessage(WM_COMMAND,
pci == NULL ? ID_MENUITEM_RENAMEENTRY : ID_MENUITEM_RENAMEGROUP);
return TRUE;
}
// Let the parent class do its thing
return CTreeCtrl::PreTranslateMessage(pMsg);
}
SCODE CPWTreeCtrl::GiveFeedback(DROPEFFECT )
{
app.GetMainDlg()->ResetIdleLockCounter();
return DRAGDROP_S_USEDEFAULTCURSORS;
}
DROPEFFECT CPWTreeCtrl::OnDragEnter(CWnd* , COleDataObject* pDataObject,
DWORD dwKeyState, CPoint )
{
// Is it ours?
if (!pDataObject->IsDataAvailable(m_tcddCPFID, NULL))
return DROPEFFECT_NONE;
m_TickCount = GetTickCount();
POINT p, hs;
CImageList* pil = CImageList::GetDragImage(&p, &hs);
if (pil != NULL) {
pws_os::Trace(L"CPWTreeCtrl::OnDragEnter() hide cursor\n");
while (ShowCursor(FALSE) >= 0)
;
}
m_bWithinThisInstance = true;
return ((dwKeyState & MK_CONTROL) == MK_CONTROL) ?
DROPEFFECT_COPY : DROPEFFECT_MOVE;
}
DROPEFFECT CPWTreeCtrl::OnDragOver(CWnd* pWnd , COleDataObject* pDataObject,
DWORD dwKeyState, CPoint point)
{
// Is it ours?
if (!pDataObject->IsDataAvailable(m_tcddCPFID, NULL))
return DROPEFFECT_NONE;
CPWTreeCtrl *pDestTreeCtrl = (CPWTreeCtrl *)pWnd;
HTREEITEM hHitItem(NULL);
POINT p, hs;
CImageList* pil = CImageList::GetDragImage(&p, &hs);
if (pil != NULL) pil->DragMove(point);
// Can't use a timer, as the WM_TIMER msg is very low priority and
// would not get processed during the drag processing.
// Implement expand if hovering over a group
if (m_TickCount != 0 && (GetTickCount() - m_TickCount >= HOVERTIME)) {
m_TickCount = GetTickCount();
if (m_hitemHover != NULL && !pDestTreeCtrl->IsLeaf(m_hitemHover)) {
pDestTreeCtrl->SelectItem(m_hitemHover);
pDestTreeCtrl->Expand(m_hitemHover, TVE_EXPAND);
}
}
hHitItem = pDestTreeCtrl->HitTest(point);
// Are we hovering over the same entry?
if (hHitItem == NULL || hHitItem != m_hitemHover) {
// No - reset hover item and ticking
m_hitemHover = hHitItem;
m_TickCount = GetTickCount();
}
if (hHitItem != NULL) {
// Highlight the item under the mouse anyway
if (pil != NULL) pil->DragLeave(this);
pDestTreeCtrl->SelectDropTarget(hHitItem);
m_hitemDrop = hHitItem;
if (pil != NULL) pil->DragEnter(this, point);
}
CRect rectClient;
pWnd->GetClientRect(&rectClient);
pWnd->ClientToScreen(rectClient);
pWnd->ClientToScreen(&point);
// Scroll Tree control depending on mouse position
int iMaxV = GetScrollLimit(SB_VERT);
int iPosV = GetScrollPos(SB_VERT);
const int SCROLL_BORDER = 10;
int nScrollDir = -1;
if ((point.y > rectClient.bottom - SCROLL_BORDER) && (iPosV != iMaxV))
nScrollDir = SB_LINEDOWN;
else if ((point.y < rectClient.top + SCROLL_BORDER) && (iPosV != 0))
nScrollDir = SB_LINEUP;
if (nScrollDir != -1) {
int nScrollPos = pWnd->GetScrollPos(SB_VERT);
WPARAM wParam = MAKELONG(nScrollDir, nScrollPos);
pWnd->SendMessage(WM_VSCROLL, wParam);
}
int iPosH = GetScrollPos(SB_HORZ);
int iMaxH = GetScrollLimit(SB_HORZ);
nScrollDir = -1;
if ((point.x < rectClient.left + SCROLL_BORDER) && (iPosH != 0))
nScrollDir = SB_LINELEFT;
else if ((point.x > rectClient.right - SCROLL_BORDER) && (iPosH != iMaxH))
nScrollDir = SB_LINERIGHT;
if (nScrollDir != -1) {
int nScrollPos = pWnd->GetScrollPos(SB_VERT);
WPARAM wParam = MAKELONG(nScrollDir, nScrollPos);
pWnd->SendMessage(WM_HSCROLL, wParam);
}
DROPEFFECT dropeffectRet;
// If we're dragging between processes, default is to COPY, Ctrl key
// changes this to MOVE.
// If we're dragging in the same process, default is to MOVE, Ctrl
// key changes this to COPY
if (pil == NULL)
dropeffectRet = ((dwKeyState & MK_CONTROL) == MK_CONTROL) ?
DROPEFFECT_MOVE : DROPEFFECT_COPY;
else
dropeffectRet = ((dwKeyState & MK_CONTROL) == MK_CONTROL) ?
DROPEFFECT_COPY : DROPEFFECT_MOVE;
return dropeffectRet;
}
void CPWTreeCtrl::OnDragLeave()
{
m_TickCount = 0;
m_bWithinThisInstance = false;
//We leave or window, so we need to clean DropTarget selection
SelectDropTarget(NULL);
// ShowCursor's semantics are VERY odd - RTFM
pws_os::Trace(L"CPWTreeCtrl::OnDragLeave() show cursor\n");
while (ShowCursor(TRUE) < 0)
;
}
void CPWTreeCtrl::SetUpFont()
{
Fonts::GetInstance()->SetUpFont(this, Fonts::GetInstance()->GetCurrentFont());
}
void CPWTreeCtrl::OnBeginLabelEdit(NMHDR *pNotifyStruct, LRESULT *pLResult)
{
NMTVDISPINFO *ptvinfo = (NMTVDISPINFO *)pNotifyStruct;
*pLResult = TRUE; // TRUE cancels label editing
// Check IsInRename to prevent unintentional Edit in place
if (app.GetMainDlg()->IsDBReadOnly() || (!app.GetMainDlg()->IsInRename() &&
!app.GetMainDlg()->IsInAddGroup()))
return;
m_bEditLabelCompleted = false;
/*
Allowed formats:
1. title
If preference ShowUsernameInTree is set:
2. title [username]
If preferences ShowUsernameInTree and ShowPasswordInTree are set:
3. title [username] {password}
Neither Title, Username or Password may contain square or curly brackes to be
edited in place and visible.
*/
HTREEITEM ti = ptvinfo->item.hItem;
PWSprefs *prefs = PWSprefs::GetInstance();
if (IsLeaf(ti)) {
DWORD_PTR itemData = GetItemData(ti);
ASSERT(itemData != NULL);
CItemData *pci = (CItemData *)itemData;
// Don't allow edit if a protected entry
if (pci->IsProtected())
return;
CSecString currentTitle, currentUser, currentPassword;
// We cannot allow in-place edit if these fields contain braces!
currentTitle = pci->GetTitle();
if (currentTitle.FindOneOf(L"[]{}") != -1)
return;
currentUser = pci->GetUser();
if (prefs->GetPref(PWSprefs::ShowUsernameInTree) &&
currentUser.FindOneOf(L"[]{}") != -1)
return;
currentPassword = pci->GetPassword();
if (prefs->GetPref(PWSprefs::ShowPasswordInTree) &&
currentPassword.FindOneOf(L"[]{}") != -1)
return;
}
// In case we have to revert:
m_eLabel = CSecString(GetItemText(ti));
// Allow in-place editing
*pLResult = FALSE;
}
static bool splitLeafText(const wchar_t *lt, StringX &newTitle,
StringX &newUser, StringX &newPassword)
{
bool bPasswordSet(false);
newTitle = newUser = newPassword = L"";
CString cs_leafText(lt);
cs_leafText.Trim();
if (cs_leafText.IsEmpty())
return false;
// Check no duplicate braces
int OpenSquareBraceIndex = cs_leafText.Find(L'[');
if (OpenSquareBraceIndex >= 0)
if (cs_leafText.Find(L'[', OpenSquareBraceIndex + 1) != -1)
return false;
int CloseSquareBraceIndex = cs_leafText.Find(L']');
if (CloseSquareBraceIndex >= 0)
if (cs_leafText.Find(L']', CloseSquareBraceIndex + 1) != -1)
return false;
int OpenCurlyBraceIndex = cs_leafText.Find(L'{');
if (OpenCurlyBraceIndex >= 0)
if (cs_leafText.Find(L'{', OpenCurlyBraceIndex + 1) != -1)
return false;
int CloseCurlyBraceIndex = cs_leafText.Find(L'}');
if (CloseCurlyBraceIndex >= 0)
if (cs_leafText.Find(L'}', CloseCurlyBraceIndex + 1) != -1)
return false;
// Check we have both open and close brackets
if (OpenSquareBraceIndex >= 0 && CloseSquareBraceIndex == -1)
return false;
if (OpenSquareBraceIndex == -1 && CloseSquareBraceIndex >= 0)
return false;
if (OpenCurlyBraceIndex >= 0 && CloseCurlyBraceIndex == -1)
return false;
if (OpenCurlyBraceIndex == -1 && CloseCurlyBraceIndex >= 0)
return false;
// Check we are in the right order - open before close
if (OpenSquareBraceIndex >= 0 && CloseSquareBraceIndex < OpenSquareBraceIndex)
return false;
if (OpenCurlyBraceIndex >= 0 && CloseCurlyBraceIndex < OpenCurlyBraceIndex)
return false;
// Check we are in the right order - square before curly
if (OpenSquareBraceIndex >= 0 && OpenCurlyBraceIndex >= 0 &&
OpenCurlyBraceIndex < OpenSquareBraceIndex)
return false;
if (OpenSquareBraceIndex == -1 && OpenCurlyBraceIndex == -1) {
// title
newTitle = cs_leafText;
return true;
}
if (OpenSquareBraceIndex >= 0 && OpenCurlyBraceIndex == -1) {
// title [user]
newTitle = cs_leafText.Left(OpenSquareBraceIndex);
Trim(newTitle);
newUser = cs_leafText.Mid(OpenSquareBraceIndex + 1,
CloseSquareBraceIndex - OpenSquareBraceIndex - 1);
Trim(newUser);
goto final_check;
}
if (OpenSquareBraceIndex == -1 && OpenCurlyBraceIndex >= 0) {
// title {password}
newTitle = cs_leafText.Left(OpenCurlyBraceIndex);
Trim(newTitle);
newPassword = cs_leafText.Mid(OpenCurlyBraceIndex + 1,
CloseCurlyBraceIndex - OpenCurlyBraceIndex - 1);
Trim(newPassword);
bPasswordSet = true;
goto final_check;
}
if (OpenSquareBraceIndex >= 0 && OpenCurlyBraceIndex >= 0) {
// title [user] {password}
newTitle = cs_leafText.Left(OpenSquareBraceIndex);
Trim(newTitle);
newUser = cs_leafText.Mid(OpenSquareBraceIndex + 1,
CloseSquareBraceIndex - OpenSquareBraceIndex - 1);
Trim(newUser);
newPassword = cs_leafText.Mid(OpenCurlyBraceIndex + 1,
CloseCurlyBraceIndex - OpenCurlyBraceIndex - 1);
Trim(newPassword);
bPasswordSet = true;
goto final_check;
}
return false; // Should never get here!
final_check:
bool bRC(true);
if (newTitle.empty())
bRC = false;
if (bPasswordSet && newPassword.empty())
bRC = false;
return bRC;
}
void CPWTreeCtrl::OnSelectionChanged(NMHDR *pNotifyStruct, LRESULT *pLResult)
{
*pLResult = 0;
NMTREEVIEW *pNMTreeView = (NMTREEVIEW *)pNotifyStruct;
// Don't bother if no entries or not via the keyboard/mouse (check this first
// as more likely than no entries).
if (pNMTreeView->action != TVC_BYKEYBOARD || GetCount() == 0)
return;
app.GetMainDlg()->OnItemSelected(pNotifyStruct, pLResult, true);
}
void CPWTreeCtrl::OnDeleteItem(NMHDR *pNotifyStruct, LRESULT *pLResult)
{
*pLResult = 0;
// Clear pointer to CItemData of item being deleted so no other
// routine has a problem with an invalid value.
HTREEITEM hItem = ((NMTREEVIEW *)pNotifyStruct)->itemOld.hItem;
if (hItem != NULL)
SetItemData(hItem, 0);
}
void CPWTreeCtrl::OnEndLabelEdit(NMHDR *pNotifyStruct, LRESULT *pLResult)
{
if (app.GetMainDlg()->IsDBReadOnly())
return; // don't edit in read-only mode
// Initial verification performed in OnBeginLabelEdit - so some events may not get here!
// Only items visible will be changed - e.g. if password is not shown and the user
// puts a new password in the new dispay text, it will be ignored.
/* Allowed formats:
1. title
If preference ShowUsernameInTree is set:
2. title [username]
If preferences ShowUsernameInTree and ShowPasswordInTree are set:
3. title [username] {password}
There can only be one of each:
open square brace
close square brace
open curly brace
close curly brace
If pos_xtb = position of x = open/close, t = square/curly brackes, then
pos_osb < pos_csb < pos_ocb < pos_ccb
Title and Password are mandatory fields within the PWS database and so, if specified,
these fields cannot be empty.
*/
CommandInterface *pcore = (CommandInterface *)app.GetCore();
MultiCommands *pmulticmds = MultiCommands::Create(pcore);
NMTVDISPINFO *ptvinfo = (NMTVDISPINFO *)pNotifyStruct;
HTREEITEM ti = ptvinfo->item.hItem;
if (ptvinfo->item.pszText == NULL || // NULL if edit cancelled,
ptvinfo->item.pszText[0] == L'\0') { // empty if text deleted - not allowed
// If called from AddGroup, user cancels EditLabel - save it
// (Still called "New Group" or the changed name if that already existed)
if (app.GetMainDlg()->IsInAddGroup()) {
// m_eLabel is the old name but need to get the path
StringX sxPath = GetGroup(ti);
pmulticmds->Add(DBEmptyGroupsCommand::Create(pcore, sxPath,
DBEmptyGroupsCommand::EG_ADD));
// Do it
app.GetMainDlg()->Execute(pmulticmds);
app.GetMainDlg()->ResetInAddGroup();
*pLResult = TRUE;
}
return;
}
// Now do the rename
ptvinfo->item.mask = TVIF_TEXT;
SetItem(&ptvinfo->item);
const StringX sxNewText = ptvinfo->item.pszText;
// Set up what we need
StringX sxNewTitle, sxNewUser, sxNewPassword; // For Leaf
StringX sxOldPath, sxNewPath; // For Node
CString prefix; // For Node
PWSprefs *prefs = PWSprefs::GetInstance();
bool bShowUsernameInTree = prefs->GetPref(PWSprefs::ShowUsernameInTree);
bool bShowPasswordInTree = prefs->GetPref(PWSprefs::ShowPasswordInTree);
bool bIsLeaf = IsLeaf(ptvinfo->item.hItem);
CItemData *pci(NULL);
if (bIsLeaf) { // Leaf
pci = (CItemData *)GetItemData(ti);
ASSERT(pci != NULL);
if (!splitLeafText(sxNewText.c_str(), sxNewTitle, sxNewUser, sxNewPassword)) {
// errors in user's input - restore text and refresh display
goto bad_exit;
}
StringX sxGroup = pci->GetGroup();
if ((sxNewTitle != pci->GetTitle() || sxNewUser != pci->GetUser()) &&
app.GetMainDlg()->Find(sxGroup, sxNewTitle, sxNewUser) != app.GetMainDlg()->End()) {
CGeneralMsgBox gmb;
CSecString temp;
if (sxGroup.empty()) {
if (sxNewUser.empty())
temp.Format(IDS_ENTRYEXISTS3, sxNewTitle.c_str());
else
temp.Format(IDS_ENTRYEXISTS2, sxNewTitle.c_str(), sxNewUser.c_str());
} else {
if (sxNewUser.empty())
temp.Format(IDS_ENTRYEXISTS1, sxGroup.c_str(), sxNewTitle.c_str());
else
temp.Format(IDS_ENTRYEXISTS, sxGroup.c_str(), sxNewTitle.c_str(),
sxNewUser.c_str());
}
gmb.AfxMessageBox(temp);
goto bad_exit;
}
if (sxNewUser.empty())
sxNewUser = pci->GetUser();
if (sxNewPassword.empty())
sxNewPassword = pci->GetPassword();
StringX treeDispString = sxNewTitle;
if (bShowUsernameInTree) {
treeDispString += L" [";
treeDispString += sxNewUser;
treeDispString += L"]";
if (bShowPasswordInTree) {
treeDispString += L" {";
treeDispString += sxNewPassword;
treeDispString += L"}";
}
}
// Update corresponding Tree mode text with the new display text (ie: in case
// the username was removed and had to be normalized back in). Note that
// we cannot do "SetItemText(ti, treeDispString)" here since Windows will
// automatically overwrite and update the item text with the contents from
// the "ptvinfo->item.pszText" buffer.
PWSUtil::strCopy(ptvinfo->item.pszText, ptvinfo->item.cchTextMax,
treeDispString.c_str(), ptvinfo->item.cchTextMax);
ptvinfo->item.pszText[ptvinfo->item.cchTextMax - 1] = L'\0';
// update corresponding List mode text - but only those visible in Tree
DisplayInfo *pdi = (DisplayInfo *)pci->GetDisplayInfo();
ASSERT(pdi != NULL);
int lindex = pdi->list_index;
if (sxNewTitle != pci->GetTitle()) {
app.GetMainDlg()->UpdateListItemTitle(lindex, sxNewTitle);
}
if (bShowUsernameInTree) {
if(sxNewUser != pci->GetUser()) {
app.GetMainDlg()->UpdateListItemUser(lindex, sxNewUser);
}
if (bShowPasswordInTree && sxNewPassword != pci->GetPassword()) {
app.GetMainDlg()->UpdateListItemPassword(lindex, sxNewPassword);
}
}
} else { // Node
/* TO DO - Deal with username in display when group renamed! */
// Check that pathname with node name doesn't already exist.
// (We could do fancy merging if so, but for now we'll reject the rename)
// Check is simple - just see if there's a sibling GROUP name sxNewText
HTREEITEM hParent = GetParentItem(ti);
HTREEITEM hSibling = GetChildItem(hParent);
do {
if (hSibling != ti && !IsLeaf(hSibling)) {
CString cs_SiblingText = GetItemText(hSibling);
if (cs_SiblingText == sxNewText.c_str())
goto bad_exit;
}
hSibling = GetNextSiblingItem(hSibling);
} while (hSibling != NULL);
// If we made it here, then name's unique.
// PR2407325: If the user edits a group name so that it has
// a GROUP_SEP, all hell breaks loose.
// Right Thing (tm) would be to parse and create subgroups as
// needed, but this is too hard (for now), so we'll just reject
// any group name that has one or more GROUP_SEP.
if (sxNewText.find(GROUP_SEP) != StringX::npos) {
SetItemText(ti, m_eLabel);
goto bad_exit;
} else {
// Update all leaf children with new path element
// prefix is path up to and NOT including renamed node
HTREEITEM parent, current = ti;
do {
parent = GetParentItem(current);
if (parent == NULL)
break;
current = parent;
if (!prefix.IsEmpty())
prefix = GROUP_SEP + prefix;
prefix = GetItemText(current) + prefix;
} while (1);
if (prefix.IsEmpty()) {
sxOldPath = (LPCWSTR)m_eLabel;
sxNewPath = sxNewText;
} else {
sxOldPath = StringX(prefix) + StringX(GROUP_SEP2) + (LPCWSTR)m_eLabel;
sxNewPath = StringX(prefix) + StringX(GROUP_SEP2) + sxNewText;
}
if (app.GetMainDlg()->IsEmptyGroup(sxOldPath))
pmulticmds->Add(DBEmptyGroupsCommand::Create(pcore, sxOldPath, sxNewPath));
else
app.GetMainDlg()->UpdateGroupNamesInMap(sxOldPath, sxNewPath);
} // good group name (no GROUP_SEP)
} // !IsLeaf
if (bIsLeaf) {
// Update Leaf
if (sxNewTitle != pci->GetTitle()) {
pmulticmds->Add(UpdateEntryCommand::Create(pcore, *pci,
CItemData::TITLE, sxNewTitle));
}
if (bShowUsernameInTree) {
if (sxNewUser != pci->GetUser()) {
pmulticmds->Add(UpdateEntryCommand::Create(pcore, *pci,
CItemData::USER, sxNewUser));
}
if (bShowPasswordInTree && sxNewPassword != pci->GetPassword()) {
pmulticmds->Add(UpdateEntryCommand::Create(pcore, *pci,
CItemData::PASSWORD, sxNewPassword));
}
}
} else {
// Group
if (app.GetMainDlg()->IsInAddGroup()) {
// Add the group
pmulticmds->Add(DBEmptyGroupsCommand::Create(pcore, sxNewPath,
DBEmptyGroupsCommand::EG_ADD));
app.GetMainDlg()->ResetInAddGroup();
*pLResult = TRUE;
} else {
// We refresh the view
Command *pcmd1 = UpdateGUICommand::Create(pcore,
UpdateGUICommand::WN_UNDO,
UpdateGUICommand::GUI_REFRESH_TREE);
pmulticmds->Add(pcmd1);
// Update Group
pmulticmds->Add(RenameGroupCommand::Create(pcore, sxOldPath, sxNewPath));
// We refresh the view
Command *pcmd2 = UpdateGUICommand::Create(pcore,
UpdateGUICommand::WN_EXECUTE_REDO,
UpdateGUICommand::GUI_REFRESH_TREE);
pmulticmds->Add(pcmd2);
}
}
if (pmulticmds->GetSize() > 0)
app.GetMainDlg()->Execute(pmulticmds);
else
delete pmulticmds;
// Mark database as modified
app.GetMainDlg()->SetChanged(DboxMain::Data);
app.GetMainDlg()->ChangeOkUpdate();
// put edited text in right order by sorting
SortTree(GetParentItem(ti));
// OK
*pLResult = TRUE;
m_bEditLabelCompleted = true;
if (bIsLeaf)
app.GetMainDlg()->RefreshViews();
return;
bad_exit:
// Refresh display to show old text - if we don't no one else will
app.GetMainDlg()->RefreshViews();
// restore text
*pLResult = FALSE;
}
bool CPWTreeCtrl::IsChildNodeOf(HTREEITEM hitemChild, HTREEITEM hitemSuspectedParent) const
{
do {
if (hitemChild == hitemSuspectedParent)
break;
} while ((hitemChild = GetParentItem(hitemChild)) != NULL);
return (hitemChild != NULL);
}
bool CPWTreeCtrl::IsLeaf(HTREEITEM hItem) const
{
// ItemHasChildren() won't work in the general case
int i, dummy;
BOOL status = GetItemImage(hItem, i, dummy);
ASSERT(status);
return (i != GROUP && i!= EMPTY_GROUP);
}
// Returns the number of children of this group
int CPWTreeCtrl::CountChildren(HTREEITEM hStartItem) const
{
// Walk the Tree!
int num = 0;
if (hStartItem != NULL && ItemHasChildren(hStartItem)) {
HTREEITEM hChildItem = GetChildItem(hStartItem);
while (hChildItem != NULL) {
if (ItemHasChildren(hChildItem)) {
num += CountChildren(hChildItem);
} else {
num++;
}
hChildItem = GetNextSiblingItem(hChildItem);
}
}
return num;
}
void CPWTreeCtrl::DeleteWithParents(HTREEITEM hItem)
{
// We don't want nodes that have no children to remain
HTREEITEM parent;
do {
StringX sxPath = (LPCWSTR)GetGroup(hItem);
parent = GetParentItem(hItem);
DeleteItem(hItem);
if (ItemHasChildren(parent))
break;
app.GetMainDlg()->m_mapGroupToTreeItem.erase(sxPath);
hItem = parent;
} while (parent != TVI_ROOT && parent != NULL);
}
// If passed an entry, return the full path leading up to a given item, but
// not including the name of the item itself.
// If passed a Group, return full path including this group.
CString CPWTreeCtrl::GetGroup(HTREEITEM hItem)
{
CString retval(L""), nodeText;
if (hItem == TVI_ROOT)
return retval;
while (hItem != NULL) {
nodeText = GetItemText(hItem);
if (!retval.IsEmpty())
nodeText += GROUP_SEP;
retval = nodeText + retval;
hItem = GetParentItem(hItem);
}
return retval;
}
static StringX GetFirstPathElem(StringX &sxPath)
{
// Get first path element and chop it off, i.e., if
// path = "a.b.c.d"
// will return "a" and path will be "b.c.d"
// path = "a..b.c.d"
// will return "a." and path will be "b.c.d"
// (assuming GROUP_SEP is '.')
StringX sxElement;
size_t dotPos = sxPath.find_first_of(GROUP_SEP);
size_t len=sxPath.length();
if (dotPos == StringX::npos){
sxElement = sxPath;
sxPath = L"";
} else {
while ((dotPos < len) && (sxPath[dotPos] == GROUP_SEP)) {// look for consecutive dots
dotPos++;
}
if (dotPos < len) {
sxElement = sxPath.substr(0, dotPos-1);
sxPath = sxPath.substr(dotPos);
}
else { // trailing dots
sxElement = sxPath;
sxPath = L"";
}
}
return sxElement;
}
bool CPWTreeCtrl::ExistsInTree(HTREEITEM &node, const CSecString &s, HTREEITEM &si) const
{
// returns true iff s is a direct descendant of node
HTREEITEM ti = GetChildItem(node);
while (ti != NULL) {
const CSecString itemText = GetItemText(ti);
if (itemText == s)
if (!IsLeaf(ti)) { // A non-node doesn't count
si = ti;
return true;
}
ti = GetNextItem(ti, TVGN_NEXT);
}
return false;
}
HTREEITEM CPWTreeCtrl::AddGroup(const CString &group, bool &bAlreadyExists)
{
// Add a group at the end of path
HTREEITEM ti = TVI_ROOT;
HTREEITEM si;
bAlreadyExists = true;
if (!group.IsEmpty()) {
StringX sxPath = group;
StringX sxTemp, sxPath2Root(L"");
do {
sxTemp = GetFirstPathElem(sxPath);
if (sxPath2Root.empty())
sxPath2Root = sxTemp;
else
sxPath2Root += GROUP_SEP2 + sxTemp;
if (!ExistsInTree(ti, sxTemp, si)) {
ti = InsertItem(sxTemp.c_str(), ti, TVI_SORT);
SetItemImage(ti, CPWTreeCtrl::GROUP, CPWTreeCtrl::GROUP);
bAlreadyExists = false;
} else
ti = si;
app.GetMainDlg()->m_mapGroupToTreeItem[sxPath2Root] = ti;
} while (!sxPath.empty());
}
if (app.GetMainDlg()->IsEmptyGroup(StringX(group)))
SetItemImage(ti, CPWTreeCtrl::EMPTY_GROUP, CPWTreeCtrl::EMPTY_GROUP);
return ti;
}
bool CPWTreeCtrl::MoveItem(MultiCommands *pmulticmds, HTREEITEM hitemDrag, HTREEITEM hitemDrop)
{
TV_INSERTSTRUCT tvstruct;
wchar_t sztBuffer[260]; // max visible
tvstruct.item.hItem = hitemDrag;
tvstruct.item.cchTextMax = _countof(sztBuffer) - 1;
tvstruct.item.pszText = sztBuffer;
tvstruct.item.mask = (TVIF_CHILDREN | TVIF_HANDLE | TVIF_IMAGE |
TVIF_PARAM | TVIF_SELECTEDIMAGE | TVIF_TEXT);
GetItem(&tvstruct.item); // get information of the dragged element
LPARAM itemData = tvstruct.item.lParam;
tvstruct.hParent = hitemDrop;
if (PWSprefs::GetInstance()->GetPref(PWSprefs::ExplorerTypeTree))
tvstruct.hInsertAfter = TVI_LAST;
else
tvstruct.hInsertAfter = TVI_SORT;
tvstruct.item.mask = TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
HTREEITEM hNewItem = InsertItem(&tvstruct);
if (itemData != 0) { // Non-NULL itemData implies Leaf
CItemData *pci = (CItemData *)itemData;
DisplayInfo *pdi = (DisplayInfo *)pci->GetDisplayInfo();
ASSERT(pdi != NULL);
ASSERT(pdi->list_index >= 0);
// Update Group
CSecString path, elem;
HTREEITEM p, q = hNewItem;
do {
p = GetParentItem(q);
if (p != NULL) {
elem = CSecString(GetItemText(p));
if (!path.IsEmpty())
elem += GROUP_SEP;
path = elem + path;
q = p;
} else
break;
} while (1);
// Get information from current selected entry
CSecString ci_user = pci->GetUser();
CSecString ci_title0 = pci->GetTitle();
CSecString ci_title = app.GetMainDlg()->GetUniqueTitle(path, ci_title0, ci_user, IDS_DRAGNUMBER);
// Update list field with new group
pmulticmds->Add(UpdateEntryCommand::Create(app.GetCore(), *pci,
CItemData::GROUP, path));
app.GetMainDlg()->UpdateListItemGroup(pdi->list_index, (LPCWSTR)path);
if (ci_title.Compare(ci_title0) != 0) {
pmulticmds->Add(UpdateEntryCommand::Create(app.GetCore(), *pci,
CItemData::TITLE, ci_title));
}
// Update tree label
SetItemText(hNewItem, MakeTreeDisplayString(*pci));
// Update list field with new title
app.GetMainDlg()->UpdateListItemTitle(pdi->list_index, (LPCWSTR)ci_title);
// Update DisplayInfo record associated with ItemData
pdi->tree_item = hNewItem;
} // leaf processing
HTREEITEM hFirstChild;
while ((hFirstChild = GetChildItem(hitemDrag)) != NULL) {
MoveItem(pmulticmds, hFirstChild, hNewItem); // recursively move all the items
}
// We are moving it - so now delete original from TreeCtrl
// Why can't MoveItem & CopyItem be the same except for the following line?!?
// For one, if we don't delete, we'll get into an infinite recursion.
// For two, entry type may change on Copy or bases will get extra dependents
DeleteItem(hitemDrag);
return true;
}
bool CPWTreeCtrl::CopyItem(HTREEITEM hitemDrag, HTREEITEM hitemDrop,
const CSecString &prefix)
{
DWORD_PTR itemData = GetItemData(hitemDrag);
if (itemData == 0) { // we're dragging a group
HTREEITEM hChild = GetChildItem(hitemDrag);
while (hChild != NULL) {
CopyItem(hChild, hitemDrop, prefix);
hChild = GetNextItem(hChild, TVGN_NEXT);
}
} else { // we're dragging a leaf
CItemData *pci = (CItemData *)itemData;
CItemData ci_temp(*pci); // copy construct a duplicate
// Update Group: chop away prefix, replace
CSecString oldPath(ci_temp.GetGroup());
if (!prefix.IsEmpty()) {
oldPath = oldPath.Right(oldPath.GetLength() - prefix.GetLength() - 1);
}
// with new path
CSecString path, elem;
path = CSecString(GetItemText(hitemDrop));
HTREEITEM p, q = hitemDrop;
do {
p = GetParentItem(q);
if (p != NULL) {
elem = CSecString(GetItemText(p));
if (!path.IsEmpty())
elem += GROUP_SEP;
path = elem + path;
q = p;
} else
break;
} while (1);
CSecString newPath;
if (path.IsEmpty())
newPath = oldPath;
else {
newPath = path;
if (!oldPath.IsEmpty())
newPath += GROUP_SEP + oldPath;
}
// Get information from current selected entry
CSecString ci_user = pci->GetUser();
CSecString ci_title0 = pci->GetTitle();
CSecString ci_title = app.GetMainDlg()->GetUniqueTitle(newPath, ci_title0,
ci_user, IDS_DRAGNUMBER);
ci_temp.CreateUUID(); // Copy needs its own UUID
ci_temp.SetGroup(newPath);
ci_temp.SetTitle(ci_title);
ci_temp.SetDisplayInfo(new DisplayInfo);
Command *pcmd(NULL);
CItemData::EntryType temp_et = ci_temp.GetEntryType();
switch (temp_et) {
case CItemData::ET_ALIASBASE:
case CItemData::ET_SHORTCUTBASE:
// An alias or shortcut can only have one base
ci_temp.SetNormal();
// Deliberate fall-thru
case CItemData::ET_NORMAL:
pcmd = AddEntryCommand::Create(app.GetCore(), ci_temp);
break;
case CItemData::ET_ALIAS:
ci_temp.SetPassword(CSecString(L"[Alias]"));
// Get base of original alias and make this copy point to it
pcmd = AddEntryCommand::Create(app.GetCore(), ci_temp,
app.GetMainDlg()->GetBaseEntry(pci)->GetUUID());
break;
case CItemData::ET_SHORTCUT:
ci_temp.SetPassword(CSecString(L"[Shortcut]"));
// Get base of original shortcut and make this copy point to it
pcmd = AddEntryCommand::Create(app.GetCore(), ci_temp,
app.GetMainDlg()->GetBaseEntry(pci)->GetUUID());
break;
default:
ASSERT(0);
}
app.GetMainDlg()->Execute(pcmd);
} // leaf handling
return true;
}
BOOL CPWTreeCtrl::OnDrop(CWnd * , COleDataObject *pDataObject,
DROPEFFECT dropEffect, CPoint point)
{
// We need to cancel DropTarget (SelectDropTarget(NULL)) selection
// before every return, otherwise next mouse/keybord selection
// will be treated as drop target selections
// Is it ours?
if (!pDataObject->IsDataAvailable(m_tcddCPFID, NULL)) {
SelectDropTarget(NULL);
return FALSE;
}
m_TickCount = 0;
pws_os::Trace(L"CPWTreeCtrl::OnDrop() show cursor\n");
while (ShowCursor(TRUE) < 0)
;
POINT p, hs;
CImageList* pil = CImageList::GetDragImage(&p, &hs);
// pil will be NULL if we're the target of inter-process D&D
if (pil != NULL) {
pil->DragLeave(this);
pil->EndDrag();
pil->DeleteImageList();
}
if (app.GetMainDlg()->IsDBReadOnly()) {
SelectDropTarget(NULL);
return FALSE; // don't drop in read-only mode
}
if (!pDataObject->IsDataAvailable(m_tcddCPFID, NULL)) {
SelectDropTarget(NULL);
return FALSE;
}
UINT uFlags;
HTREEITEM hitemDrop = HitTest(point, &uFlags);
bool bForceRoot(false);
switch (uFlags) {
case TVHT_ABOVE: case TVHT_BELOW: case TVHT_TOLEFT: case TVHT_TORIGHT:
SelectDropTarget(NULL);
return FALSE;
case TVHT_NOWHERE:
if (hitemDrop == NULL) {
// Treat as drop in root
hitemDrop = GetRootItem();
bForceRoot = true;
}
else {
SelectDropTarget(NULL);
return FALSE;
}
break;
case TVHT_ONITEM: case TVHT_ONITEMBUTTON: case TVHT_ONITEMICON:
case TVHT_ONITEMINDENT: case TVHT_ONITEMLABEL: case TVHT_ONITEMRIGHT:
case TVHT_ONITEMSTATEICON:
if (hitemDrop == NULL){
SelectDropTarget(NULL);
return FALSE;
}
break;
default:
SelectDropTarget(NULL);
return FALSE;
}
BOOL retval(FALSE);
// On Drop of data from one tree to another
HGLOBAL hGlobal = pDataObject->GetGlobalData(m_tcddCPFID);
BYTE *pData = (BYTE *)GlobalLock(hGlobal);
ASSERT(pData != NULL);
SIZE_T memsize = GlobalSize(hGlobal);
if (memsize < OLE_HDR_LEN) // OLE_HDR_FMT
goto exit;
// iDDType = D&D type FROMTREE/FROMTREE_R or
// for column D&D only FROMCC, FROMHDR
// lBufLen = Length of D&D data appended to this data
unsigned long lPid;
int iDDType;
long lBufLen;
#if (_MSC_VER >= 1400)
sscanf_s((char *)pData, OLE_HDR_FMT, &lPid, &iDDType, &lBufLen);
#else
sscanf((char *)pData, OLE_HDR_FMT, &lPid, &iDDType, &lBufLen);
#endif
pData += OLE_HDR_LEN; // so ProcessData won't sweat
// NULL-ness of pil is also a good indicator of intra/inter-ness
// alternately, we can also raise an m_flag in OnBeginDrag.
// However, plugging the process ID in the header
// is the most direct and straightforward way to do this,
// and probably the most robust...
m_bWithinThisInstance = (lPid == GetCurrentProcessId());
// Check if it is from another TreeCtrl (left or right mouse drag)?
// - we don't accept drop from anything else
if (iDDType != FROMTREE_L && iDDType != FROMTREE_R && iDDType != FROMTREE_RSC)
goto exit;
if (iDDType == FROMTREE_R || iDDType == FROMTREE_RSC) {
CMenu menu;
if (menu.LoadMenu(IDR_POPRIGHTDRAG)) {
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
ClientToScreen(&point);
pPopup->SetDefaultItem(GetKeyState(VK_CONTROL) < 0 ?
ID_MENUITEM_COPYHERE : ID_MENUITEM_MOVEHERE);
if (!m_bWithinThisInstance || iDDType != FROMTREE_RSC)
pPopup->EnableMenuItem(ID_MENUITEM_RCREATESHORTCUT, MF_BYCOMMAND | MF_GRAYED);
DWORD dwcode = pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON |
TPM_NONOTIFY | TPM_RETURNCMD,
point.x, point.y, this);
pPopup->DestroyMenu();
switch (dwcode) {
case ID_MENUITEM_COPYHERE:
dropEffect = DROPEFFECT_COPY;
break;
case ID_MENUITEM_MOVEHERE:
dropEffect = DROPEFFECT_MOVE;
break;
case ID_MENUITEM_RCREATESHORTCUT:
{
// Shortcut group from drop point, title & user from drag entry
CSecString cs_group, cs_title, cs_user;
CItemData *pci;
DWORD_PTR itemData;
itemData = GetItemData(m_hitemDrop);
if (itemData == NULL) {
// Dropping on a group
cs_group = CSecString(GetGroup(m_hitemDrop));
} else {
// Dropping on an entry
pci = (CItemData *)itemData;
cs_group = pci->GetGroup();
}
itemData = GetItemData(m_hitemDrag);
ASSERT(itemData != NULL);
pci = (CItemData *)itemData;
cs_title.Format(IDS_SCTARGET, pci->GetTitle().c_str());
cs_user = pci->GetUser();
// If there is a matching entry in our list, generate unique one
if (app.GetMainDlg()->Find(cs_group, cs_title, cs_user) != app.GetMainDlg()->End()) {
cs_title = app.GetMainDlg()->GetUniqueTitle(cs_group, cs_title, cs_user, IDS_DRAGNUMBER);
}
StringX sxNewDBPrefsString(L"");
app.GetMainDlg()->CreateShortcutEntry(pci, cs_group, cs_title, cs_user, sxNewDBPrefsString);
retval = TRUE;
SelectItem(NULL); // Deselect
goto exit;
}
case ID_MENUITEM_CANCEL:
default:
SelectItem(NULL); // Deselect
goto exit;
}
}
}
if (hitemDrop == NULL && GetCount() == 0) {
// Dropping on to an empty database
CSecString DropGroup (L"");
ProcessData(pData, lBufLen, DropGroup);
SelectItem(GetRootItem());
retval = TRUE;
goto exit;
}
if (IsLeaf(hitemDrop) || bForceRoot)
hitemDrop = GetParentItem(hitemDrop);
if (m_bWithinThisInstance) {
// from me! - easy
HTREEITEM parent = GetParentItem(m_hitemDrag);
if (m_hitemDrag != hitemDrop &&
!IsChildNodeOf(hitemDrop, m_hitemDrag) &&
parent != hitemDrop) {
// drag operation allowed
if (dropEffect == DROPEFFECT_MOVE) {
MultiCommands *pmulticmds = MultiCommands::Create(app.GetCore());
MoveItem(pmulticmds, m_hitemDrag, hitemDrop);
// Make sure that the folder to which drag is performed will
// be removed from the vector of empty groups
StringX sxGroup(GetGroup(hitemDrop));
if (app.GetMainDlg()->IsEmptyGroup(sxGroup)) {
pmulticmds->Add(DBEmptyGroupsCommand::Create(app.GetCore(), sxGroup,
DBEmptyGroupsCommand::EG_DELETE));
}
app.GetMainDlg()->Execute(pmulticmds);
} else
if (dropEffect == DROPEFFECT_COPY) {
CopyItem(m_hitemDrag, hitemDrop, GetPrefix(m_hitemDrag));
SortTree(hitemDrop);
}
SelectItem(hitemDrop);
retval = TRUE;
} else {
// drag failed or cancelled, revert to last selected
SelectItem(m_hitemDrag);
goto exit;
}
} else { // from someone else!
// Now add it
CSecString DropGroup = CSecString(GetGroup(hitemDrop));
ProcessData((BYTE *)pData, lBufLen, DropGroup);
SelectItem(hitemDrop);
retval = TRUE;
}
SortTree(TVI_ROOT);
app.GetMainDlg()->FixListIndexes();
GetParent()->SetFocus();
exit:
SelectDropTarget(NULL);
GlobalUnlock(hGlobal);
if (retval == TRUE) {
app.GetMainDlg()->SetChanged(DboxMain::Data);
app.GetMainDlg()->ChangeOkUpdate();
if (app.GetMainDlg()->IsFilterActive())
app.GetMainDlg()->RefreshViews();
}
return retval;
}
void CPWTreeCtrl::OnBeginDrag(NMHDR *pNotifyStruct, LRESULT *pLResult)
{
// This sets the whole D&D mechanism in motion...
if (pNotifyStruct->code == TVN_BEGINDRAG)
m_DDType = FROMTREE_L; // Left mouse D&D
else
m_DDType = FROMTREE_R; // Right mouse D&D
CPoint ptAction;
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNotifyStruct;
*pLResult = 0L;
GetCursorPos(&ptAction);
ScreenToClient(&ptAction);
m_hitemDrag = pNMTreeView->itemNew.hItem;
m_hitemDrop = NULL;
SelectItem(m_hitemDrag);
// Bug in MS TreeCtrl and CreateDragImage. During Drag, it doesn't show
// the entry's text as well as the drag image if the font is not MS Sans Serif !!!!
SetFont(Fonts::GetInstance()->GetDragFixFont(), false);
CImageList *pil = CreateDragImage(m_hitemDrag);
SetFont(Fonts::GetInstance()->GetCurrentFont(), false);
pil->SetDragCursorImage(0, CPoint(0, 0));
pil->BeginDrag(0, CPoint(0,0));
pil->DragMove(ptAction);
pil->DragEnter(this, ptAction);
pws_os::Trace(L"CPWTreeCtrl::OnBeginDrag() hide cursor\n");
while (ShowCursor(FALSE) >= 0)
;
SetCapture();
RECT rClient;
GetClientRect(&rClient);
if (m_DDType == FROMTREE_R && IsLeaf(m_hitemDrag)) {
DWORD_PTR itemData = GetItemData(m_hitemDrag);
CItemData *pci = (CItemData *)itemData;
if (pci->IsNormal() || pci->IsShortcutBase())
m_DDType = FROMTREE_RSC; // Shortcut creation allowed (if within this instance)
}
// Start dragging
m_bDropped = false;
DROPEFFECT de = m_DataSource->StartDragging(m_tcddCPFID, &rClient);
// If inter-process Move, we need to delete original
if (m_cfdropped == m_tcddCPFID &&
(de & DROPEFFECT_MOVE) == DROPEFFECT_MOVE &&
!m_bWithinThisInstance && !app.GetMainDlg()->IsDBReadOnly()) {
app.GetMainDlg()->Delete(); // XXX assume we've a selected item here!
}
// wrong place to clean up imagelist?
pil->DragLeave(GetDesktopWindow());
pil->EndDrag();
pil->DeleteImageList();
delete pil;
if (de == DROPEFFECT_NONE) {
pws_os::Trace(L"m_DataSource->StartDragging() failed\n");
// Do cleanup - otherwise this is the responsibility of the recipient!
if (m_hgDataALL != NULL) {
LPVOID lpData = GlobalLock(m_hgDataALL);
SIZE_T memsize = GlobalSize(m_hgDataALL);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_hgDataALL);
GlobalFree(m_hgDataALL);
m_hgDataALL = NULL;
}
if (m_hgDataTXT != NULL) {
LPVOID lpData = GlobalLock(m_hgDataTXT);
SIZE_T memsize = GlobalSize(m_hgDataTXT);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_hgDataTXT);
GlobalFree(m_hgDataTXT);
m_hgDataTXT = NULL;
}
if (m_hgDataUTXT != NULL) {
LPVOID lpData = GlobalLock(m_hgDataUTXT);
SIZE_T memsize = GlobalSize(m_hgDataUTXT);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_hgDataUTXT);
GlobalFree(m_hgDataUTXT);
m_hgDataUTXT = NULL;
}
}
pws_os::Trace(L"CPWTreeCtrl::OnBeginDrag() show cursor\n");
while (ShowCursor(TRUE) < 0)
;
// We did call SetCapture - do we release it here? If not, where else?
ReleaseCapture();
}
void CPWTreeCtrl::OnTimer(UINT_PTR nIDEvent)
{
switch (nIDEvent) {
case TIMER_ND_HOVER:
KillTimer(m_nHoverNDTimerID);
m_nHoverNDTimerID = 0;
if (app.GetMainDlg()->SetNotesWindow(m_HoverNDPoint)) {
if (m_nShowNDTimerID) {
KillTimer(m_nShowNDTimerID);
m_nShowNDTimerID = 0;
}
m_nShowNDTimerID = SetTimer(TIMER_ND_SHOWING, TIMEINT_ND_SHOWING, NULL);
}
break;
case TIMER_ND_SHOWING:
KillTimer(m_nShowNDTimerID);
m_nShowNDTimerID = 0;
m_HoverNDPoint = CPoint(0, 0);
app.GetMainDlg()->SetNotesWindow(m_HoverNDPoint, false);
break;
default:
CTreeCtrl::OnTimer(nIDEvent);
break;
}
}
void CPWTreeCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
app.GetMainDlg()->ResetIdleLockCounter();
if (!m_bShowNotes)
return;
if (m_nHoverNDTimerID) {
if (HitTest(m_HoverNDPoint) == HitTest(point))
return;
KillTimer(m_nHoverNDTimerID);
m_nHoverNDTimerID = 0;
}
if (m_nShowNDTimerID) {
if (HitTest(m_HoverNDPoint) == HitTest(point))
return;
KillTimer(m_nShowNDTimerID);
m_nShowNDTimerID = 0;
app.GetMainDlg()->SetNotesWindow(CPoint(0, 0), false);
}
if (!m_bMouseInWindow) {
m_bMouseInWindow = true;
TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, m_hWnd, 0};
VERIFY(TrackMouseEvent(&tme));
}
m_nHoverNDTimerID = SetTimer(TIMER_ND_HOVER, HOVER_TIME_ND, NULL);
m_HoverNDPoint = point;
CTreeCtrl::OnMouseMove(nFlags, point);
}
LRESULT CPWTreeCtrl::OnMouseLeave(WPARAM, LPARAM)
{
KillTimer(m_nHoverNDTimerID);
KillTimer(m_nShowNDTimerID);
m_nHoverNDTimerID = m_nShowNDTimerID = 0;
m_HoverNDPoint = CPoint(0, 0);
app.GetMainDlg()->SetNotesWindow(m_HoverNDPoint, false);
m_bMouseInWindow = false;
return 0L;
}
void CPWTreeCtrl::OnExpandCollapse(NMHDR *, LRESULT *)
{
// We need to update the parent's state vector of expanded nodes
// so that it will be persistent across miminize, lock, save, etc.
// (unless we're in the middle of restoring the state!)
if (!m_isRestoring) {
app.GetMainDlg()->SaveGUIStatusEx(DboxMain::iTreeOnly);
}
}
void CPWTreeCtrl::OnExpandAll()
{
// Updated to test for zero entries!
HTREEITEM hItem = this->GetRootItem();
if (hItem == NULL)
return;
SetRedraw(FALSE);
do {
Expand(hItem,TVE_EXPAND);
hItem = GetNextItem(hItem,TVGN_NEXTVISIBLE);
} while (hItem);
EnsureVisible(GetSelectedItem());
SetRedraw(TRUE);
app.GetMainDlg()->SaveGUIStatusEx(DboxMain::iTreeOnly);
}
void CPWTreeCtrl::OnCollapseAll()
{
// Courtesy of Zafir Anjum from www.codeguru.com
// Updated to test for zero entries!
HTREEITEM hItem = GetRootItem();
if (hItem == NULL)
return;
SetRedraw(FALSE);
do {
CollapseBranch(hItem);
} while((hItem = GetNextSiblingItem(hItem)) != NULL);
SetRedraw(TRUE);
app.GetMainDlg()->SaveGUIStatusEx(DboxMain::iTreeOnly);
}
void CPWTreeCtrl::CollapseBranch(HTREEITEM hItem)
{
// Courtesy of Zafir Anjumfrom www.codeguru.com
if (ItemHasChildren(hItem)) {
Expand(hItem, TVE_COLLAPSE);
hItem = GetChildItem(hItem);
do {
CollapseBranch(hItem);
} while((hItem = GetNextSiblingItem(hItem)) != NULL);
}
}
HTREEITEM CPWTreeCtrl::GetNextTreeItem(HTREEITEM hItem)
{
if (NULL == hItem)
return GetRootItem();
// First, try to go to this item's 1st child
HTREEITEM hReturn = GetChildItem(hItem);
// If no more child items...
while (hItem && !hReturn) {
// Get this item's next sibling
hReturn = GetNextSiblingItem(hItem);
// If hReturn is NULL, then there are no
// sibling items, and we're on a leaf node.
// Backtrack up the tree one level, and
// we'll look for a sibling on the next
// iteration (or we'll reach the root and
// quit).
hItem = GetParentItem(hItem);
}
return hReturn;
}
void CPWTreeCtrl::Iterate(HTREEITEM hItem, TreeItemFunctor &functor)
{
if (hItem) {
functor(hItem); // apply whatever needs to be done
hItem = GetNextItem(hItem, TVGN_CHILD);
while (hItem) {
Iterate(hItem, functor);
hItem = GetNextItem(hItem, TVGN_NEXT);
}
} else {
HTREEITEM hItem = GetNextItem(NULL, TVGN_ROOT);
while (hItem) {
Iterate(hItem, functor);
hItem = GetNextItem(hItem, TVGN_NEXT);
}
}
}
bool CPWTreeCtrl::CollectData(BYTE * &out_buffer, long &outLen)
{
DWORD_PTR itemData = GetItemData(m_hitemDrag);
CItemData *pci = (CItemData *)itemData;
CDDObList out_oblist;
if (IsLeaf(m_hitemDrag)) {
ASSERT(itemData != NULL);
m_nDragPathLen = 0;
out_oblist.m_bDragNode = false;
GetEntryData(out_oblist, pci);
} else {
m_nDragPathLen = GetGroup(GetParentItem(m_hitemDrag)).GetLength();
out_oblist.m_bDragNode = true;
GetGroupEntriesData(out_oblist, m_hitemDrag);
}
CSMemFile outDDmemfile;
out_oblist.DDSerialize(outDDmemfile);
outLen = (long)outDDmemfile.GetLength();
out_buffer = (BYTE *)outDDmemfile.Detach();
while (!out_oblist.IsEmpty()) {
delete (CDDObject *)out_oblist.RemoveHead();
}
return (outLen > 0);
}
bool CPWTreeCtrl::ProcessData(BYTE *in_buffer, const long &inLen,
const CSecString &DropGroup)
{
#ifdef DUMP_DATA
std:wstring stimestamp;
PWSUtil::GetTimeStamp(stimestamp);
pws_os::Trace(L"Drop data: length %d/0x%04x, value:\n", inLen, inLen);
pws_os::HexDump(in_buffer, inLen, stimestamp);
#endif /* DUMP_DATA */
if (inLen <= 0)
return false;
CDDObList in_oblist;
CSMemFile inDDmemfile;
inDDmemfile.Attach((BYTE *)in_buffer, inLen);
in_oblist.DDUnSerialize(inDDmemfile);
inDDmemfile.Detach();
if (!in_oblist.IsEmpty()) {
app.GetMainDlg()->AddDDEntries(in_oblist, DropGroup);
while (!in_oblist.IsEmpty()) {
delete (CDDObject *)in_oblist.RemoveHead();
}
}
return (inLen > 0);
}
void CPWTreeCtrl::GetGroupEntriesData(CDDObList &out_oblist, HTREEITEM hItem)
{
if (IsLeaf(hItem)) {
DWORD_PTR itemData = GetItemData(hItem);
ASSERT(itemData != NULL);
CItemData *pci = (CItemData *)itemData;
GetEntryData(out_oblist, pci);
} else {
HTREEITEM child;
for (child = GetChildItem(hItem);
child != NULL;
child = GetNextSiblingItem(child)) {
GetGroupEntriesData(out_oblist, child);
}
}
}
void CPWTreeCtrl::GetEntryData(CDDObList &out_oblist, CItemData *pci)
{
ASSERT(pci != NULL);
CDDObject *pDDObject = new CDDObject;
if (out_oblist.m_bDragNode && m_nDragPathLen > 0) {
CItemData ci2(*pci); // we need a copy since to modify the group
const CSecString cs_Group = pci->GetGroup();
ci2.SetGroup(cs_Group.Right(cs_Group.GetLength() - m_nDragPathLen - 1));
pDDObject->FromItem(ci2);
} else {
pDDObject->FromItem(*pci);
}
if (pci->IsDependent()) {
// I'm an alias or shortcut; pass on ptr to my base item
// to retrieve its group/title/user
const CItemData *pbci = app.GetMainDlg()->GetBaseEntry(pci);
ASSERT(pbci != NULL);
pDDObject->SetBaseItem(pbci);
}
out_oblist.AddTail(pDDObject);
}
CSecString CPWTreeCtrl::GetPrefix(HTREEITEM hItem) const
{
// return all path components beween hItem and root.
// e.g., if hItem is X in a.b.c.X.y.z, then return a.b.c
CSecString retval;
HTREEITEM p = GetParentItem(hItem);
while (p != NULL) {
retval = CSecString(GetItemText(p)) + retval;
p = GetParentItem(p);
if (p != NULL)
retval = CSecString(GROUP_SEP) + retval;
}
return retval;
}
CSecString CPWTreeCtrl::MakeTreeDisplayString(const CItemData &ci) const
{
PWSprefs *prefs = PWSprefs::GetInstance();
bool bShowUsernameInTree = prefs->GetPref(PWSprefs::ShowUsernameInTree);
bool bShowPasswordInTree = prefs->GetPref(PWSprefs::ShowPasswordInTree);
CSecString treeDispString = ci.GetTitle();
if (bShowUsernameInTree) {
treeDispString += L" [";
treeDispString += ci.GetUser();
treeDispString += L"]";
if (bShowPasswordInTree) {
treeDispString += L" {";
treeDispString += ci.GetPassword();
treeDispString += L"}";
}
}
if (ci.IsProtected())
treeDispString += L" #";
return treeDispString;
}
static int CALLBACK ExplorerCompareProc(LPARAM lParam1, LPARAM lParam2,
LPARAM /* closure */)
{
int iResult;
CItemData* pLHS = (CItemData *)lParam1;
CItemData* pRHS = (CItemData *)lParam2;
if (pLHS == pRHS) { // probably both null, in any case, equal
iResult = 0;
} else if (pLHS == NULL) {
iResult = -1;
} else if (pRHS == NULL) {
iResult = 1;
} else {
iResult = CompareNoCase(pLHS->GetGroup(), pRHS->GetGroup());
if (iResult == 0) {
iResult = CompareNoCase(pLHS->GetTitle(), pRHS->GetTitle());
if (iResult == 0) {
iResult = CompareNoCase(pLHS->GetUser(), pRHS->GetUser());
}
}
}
return iResult;
}
void CPWTreeCtrl::SortTree(const HTREEITEM htreeitem)
{
TVSORTCB tvs;
HTREEITEM hti(htreeitem);
if (hti == NULL)
hti = TVI_ROOT;
if (!PWSprefs::GetInstance()->GetPref(PWSprefs::ExplorerTypeTree)) {
SortChildren(hti);
return;
}
// here iff user prefers "explorer type view", that is,
// groups first.
// unbelievable, but we have to recurse ourselves!
// foreach child of hti
// if !IsLeaf
// SortTree(child)
if (hti == TVI_ROOT || !IsLeaf(hti)) {
HTREEITEM hChildItem = GetChildItem(hti);
while (hChildItem != NULL) {
if (ItemHasChildren(hChildItem))
SortTree(hChildItem);
hChildItem = GetNextItem(hChildItem, TVGN_NEXT);
}
}
tvs.hParent = hti;
tvs.lpfnCompare = ExplorerCompareProc;
tvs.lParam = (LPARAM)this;
SortChildrenCB(&tvs);
}
void CPWTreeCtrl::SetFilterState(bool bState)
{
m_bFilterActive = bState;
// Red if filter active, black if not
SetTextColor(m_bFilterActive ? RGB(168, 0, 0) : RGB(0, 0, 0));
}
BOOL CPWTreeCtrl::OnEraseBkgnd(CDC* pDC)
{
if (m_bFilterActive && app.GetMainDlg()->GetNumPassedFiltering() == 0) {
int nSavedDC = pDC->SaveDC(); //save the current DC state
// Set up variables
COLORREF clrText = RGB(168, 0, 0);
COLORREF clrBack = ::GetSysColor(COLOR_WINDOW); //system background color
CBrush cbBack(clrBack);
CRect rc;
GetClientRect(&rc); //get client area of the ListCtrl
// Here is the string we want to display (or you can use a StringTable entry)
const CString cs_emptytext(MAKEINTRESOURCE(IDS_NOITEMSPASSEDFILTERING));
// Now we actually display the text
// set the text color
pDC->SetTextColor(clrText);
// set the background color
pDC->SetBkColor(clrBack);
// fill the client area rect
pDC->FillRect(&rc, &cbBack);
// select a font
pDC->SelectStockObject(ANSI_VAR_FONT);
// and draw the text
pDC->DrawText(cs_emptytext, -1, rc,
DT_CENTER | DT_VCENTER | DT_WORDBREAK | DT_NOPREFIX | DT_NOCLIP);
// Restore dc
pDC->RestoreDC(nSavedDC);
ReleaseDC(pDC);
} else {
// If there are items in the TreeCtrl, we need to call the base class function
CTreeCtrl::OnEraseBkgnd(pDC);
}
return TRUE;
}
BOOL CPWTreeCtrl::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
{
if (m_hgDataALL != NULL) {
pws_os::Trace(L"CPWTreeCtrl::OnRenderGlobalData - Unlock/Free m_hgDataALL\n");
LPVOID lpData = GlobalLock(m_hgDataALL);
SIZE_T memsize = GlobalSize(m_hgDataALL);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_hgDataALL);
GlobalFree(m_hgDataALL);
m_hgDataALL = NULL;
}
if (m_hgDataTXT != NULL) {
pws_os::Trace(L"CPWTreeCtrl::OnRenderGlobalData - Unlock/Free m_hgDataTXT\n");
LPVOID lpData = GlobalLock(m_hgDataTXT);
SIZE_T memsize = GlobalSize(m_hgDataTXT);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_hgDataTXT);
GlobalFree(m_hgDataTXT);
m_hgDataTXT = NULL;
}
if (m_hgDataUTXT != NULL) {
pws_os::Trace(L"CPWTreeCtrl::OnRenderGlobalData - Unlock/Free m_hgDataUTXT\n");
LPVOID lpData = GlobalLock(m_hgDataUTXT);
SIZE_T memsize = GlobalSize(m_hgDataUTXT);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_hgDataUTXT);
GlobalFree(m_hgDataUTXT);
m_hgDataUTXT = NULL;
}
BOOL retval;
if (lpFormatEtc->cfFormat == CF_UNICODETEXT ||
lpFormatEtc->cfFormat == CF_TEXT) {
retval = RenderTextData(lpFormatEtc->cfFormat, phGlobal);
} else if (lpFormatEtc->cfFormat == m_tcddCPFID) {
m_cfdropped = m_tcddCPFID;
retval = RenderAllData(phGlobal);
} else
return FALSE;
return retval;
}
BOOL CPWTreeCtrl::RenderTextData(CLIPFORMAT &cfFormat, HGLOBAL* phGlobal)
{
if (!IsLeaf(m_hitemDrag)) {
pws_os::Trace(L"CPWTreeCtrl::RenderTextData - not a leaf!\n");
return FALSE;
}
// Save format for use by D&D to ensure we don't 'move' when dropping text!
m_cfdropped = cfFormat;
DWORD_PTR itemData = GetItemData(m_hitemDrag);
const CItemData *pci = (const CItemData *)itemData;
if (pci->IsDependent()) {
const CItemData *pbci = app.GetMainDlg()->GetBaseEntry(pci);
ASSERT(pbci != NULL);
pci = pbci;
}
CSecString cs_dragdata;
cs_dragdata = pci->GetPassword();
const int ilen = cs_dragdata.GetLength();
if (ilen == 0) {
// Password shouldn't ever be empty!
return FALSE;
}
DWORD dwBufLen;
LPSTR lpszA(NULL);
LPWSTR lpszW(NULL);
if (cfFormat == CF_UNICODETEXT) {
// So is requested data!
dwBufLen = (ilen + 1) * sizeof(wchar_t);
lpszW = new WCHAR[ilen + 1];
//pws_os::Trace(L"lpszW allocated %p, size %d\n", lpszW, dwBufLen);
#if (_MSC_VER >= 1400)
(void) wcsncpy_s(lpszW, ilen + 1, cs_dragdata, ilen);
#else
(void)wcsncpy(lpszW, cs_dragdata, ilen);
lpszW[ilen] = L'\0';
#endif
} else {
// They want it in ASCII - use lpszW temporarily
lpszW = cs_dragdata.GetBuffer(ilen + 1);
dwBufLen = WideCharToMultiByte(CP_ACP, 0, lpszW, -1, NULL, 0, NULL, NULL);
ASSERT(dwBufLen != 0);
lpszA = new char[dwBufLen];
pws_os::Trace(L"lpszA allocated %p, size %d\n", lpszA, dwBufLen);
WideCharToMultiByte(CP_ACP, 0, lpszW, -1, lpszA, dwBufLen, NULL, NULL);
cs_dragdata.ReleaseBuffer();
lpszW = NULL;
}
LPVOID lpData(NULL);
LPVOID lpDataBuffer;
HGLOBAL *phgData;
if (cfFormat == CF_UNICODETEXT) {
lpDataBuffer = (LPVOID)lpszW;
phgData = &m_hgDataUTXT;
} else {
lpDataBuffer = (LPVOID)lpszA;
phgData = &m_hgDataTXT;
}
BOOL retval(FALSE);
if (*phGlobal == NULL) {
pws_os::Trace(L"CPWTreeCtrl::OnRenderTextData - Alloc global memory\n");
*phgData = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBufLen);
ASSERT(*phgData != NULL);
if (*phgData == NULL)
goto bad_return;
lpData = GlobalLock(*phgData);
ASSERT(lpData != NULL);
if (lpData == NULL)
goto bad_return;
// Copy data
memcpy(lpData, lpDataBuffer, dwBufLen);
*phGlobal = *phgData;
GlobalUnlock(*phgData);
retval = TRUE;
} else {
pws_os::Trace(L"CPWTreeCtrl::OnRenderTextData - *phGlobal NOT NULL!\n");
SIZE_T inSize = GlobalSize(*phGlobal);
SIZE_T ourSize = GlobalSize(*phgData);
if (inSize < ourSize) {
// Pre-allocated space too small. Not allowed to increase it - FAIL
pws_os::Trace(L"CPWTreeCtrl::OnRenderTextData - NOT enough room - FAIL\n");
} else {
// Enough room - copy our data into supplied area
pws_os::Trace(L"CPWTreeCtrl::OnRenderTextData - enough room - copy our data\n");
LPVOID pInGlobalLock = GlobalLock(*phGlobal);
ASSERT(pInGlobalLock != NULL);
if (pInGlobalLock == NULL)
goto bad_return;
memcpy(pInGlobalLock, lpDataBuffer, ourSize);
GlobalUnlock(*phGlobal);
retval = TRUE;
}
}
bad_return:
// Finished with buffer - trash it
trashMemory(lpDataBuffer, dwBufLen);
// Free the strings (only one is actually in use)
//pws_os::Trace(L"lpszA freed %p\n", lpszA);
delete[] lpszA;
//pws_os::Trace(L"lpszW freed %p\n", lpszW);
delete[] lpszW;
// Since lpDataBuffer pointed to one of the above - just zero the pointer
lpDataBuffer = NULL;
// If retval == TRUE, recipient is responsible for freeing the global memory
// if D&D succeeds (see after StartDragging in OnMouseMove)
if (retval == FALSE) {
pws_os::Trace(L"CPWTreeCtrl::RenderTextData - returning FALSE!\n");
if (*phgData != NULL) {
LPVOID lpData = GlobalLock(*phgData);
SIZE_T memsize = GlobalSize(*phgData);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(*phgData);
GlobalFree(*phgData);
*phgData = NULL;
}
} else {
pws_os::Trace(L"CPWTreeCtrl::RenderTextData - D&D Data:");
if (cfFormat == CF_UNICODETEXT) {
pws_os::Trace(L"\"%s\"\n", (LPWSTR)lpData); // we are Unicode, data is Unicode
} else {
pws_os::Trace(L"\"%S\"\n", (LPSTR)lpData); // we are Unicode, data is NOT Unicode
}
}
if (lpData != NULL)
GlobalUnlock(*phgData);
return retval;
}
BOOL CPWTreeCtrl::RenderAllData(HGLOBAL* phGlobal)
{
long lBufLen;
BYTE *buffer = NULL;
ASSERT(m_hgDataALL == NULL);
// CollectData allocates buffer - need to free later
if (!CollectData(buffer, lBufLen))
return FALSE;
char header[OLE_HDR_LEN + 1];
// Note: GetDDType will return either FROMTREE or FROMTREE_R
#if (_MSC_VER >= 1400)
sprintf_s(header, sizeof(header),
OLE_HDR_FMT, GetCurrentProcessId(), GetDDType(), lBufLen);
#else
sprintf(header, OLE_HDR_FMT, GetCurrentProcessId(), GetDDType(), lBufLen);
#endif
CMemFile mf;
mf.Write(header, OLE_HDR_LEN);
mf.Write(buffer, lBufLen);
// Finished with buffer - trash it and free it
trashMemory((void *)buffer, lBufLen);
free(buffer);
LPVOID lpData(NULL);
LPVOID lpDataBuffer;
DWORD dwBufLen = (DWORD)mf.GetLength();
lpDataBuffer = (LPVOID)(mf.Detach());
#ifdef DUMP_DATA
std::wstring stimestamp;
PWSUtil::GetTimeStamp(stimestamp);
pws_os::Trace(L"Drag data: length %d/0x%04x, value:\n", dwBufLen, dwBufLen);
pws_os::HexDump(lpDataBuffer, dwBufLen, stimestamp);
#endif /* DUMP_DATA */
BOOL retval(FALSE);
if (*phGlobal == NULL) {
pws_os::Trace(L"CPWTreeCtrl::OnRenderAllData - Alloc global memory\n");
m_hgDataALL = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBufLen);
ASSERT(m_hgDataALL != NULL);
if (m_hgDataALL == NULL)
goto bad_return;
lpData = GlobalLock(m_hgDataALL);
ASSERT(lpData != NULL);
if (lpData == NULL)
goto bad_return;
// Copy data
memcpy(lpData, lpDataBuffer, dwBufLen);
*phGlobal = m_hgDataALL;
retval = TRUE;
} else {
pws_os::Trace(L"CPWTreeCtrl::OnRenderAllData - *phGlobal NOT NULL!\n");
SIZE_T inSize = GlobalSize(*phGlobal);
SIZE_T ourSize = GlobalSize(m_hgDataALL);
if (inSize < ourSize) {
// Pre-allocated space too small. Not allowed to increase it - FAIL
pws_os::Trace(L"CPWTreeCtrl::OnRenderAllData - NOT enough room - FAIL\n");
} else {
// Enough room - copy our data into supplied area
pws_os::Trace(L"CPWTreeCtrl::OnRenderAllData - enough room - copy our data\n");
LPVOID pInGlobalLock = GlobalLock(*phGlobal);
ASSERT(pInGlobalLock != NULL);
if (pInGlobalLock == NULL)
goto bad_return;
memcpy(pInGlobalLock, lpDataBuffer, ourSize);
GlobalUnlock(*phGlobal);
retval = TRUE;
}
}
bad_return:
if (dwBufLen != 0 && lpDataBuffer != NULL) {
trashMemory(lpDataBuffer, dwBufLen);
free(lpDataBuffer);
lpDataBuffer = NULL;
}
// If retval == TRUE, recipient is responsible for freeing the global memory
// if D&D succeeds
if (retval == FALSE) {
pws_os::Trace(L"CPWTreeCtrl::RenderAllData - returning FALSE!\n");
if (m_hgDataALL != NULL) {
LPVOID lpData = GlobalLock(m_hgDataALL);
SIZE_T memsize = GlobalSize(m_hgDataALL);
if (lpData != NULL && memsize > 0) {
trashMemory(lpData, memsize);
}
GlobalUnlock(m_hgDataALL);
GlobalFree(m_hgDataALL);
m_hgDataALL = NULL;
}
}
if (lpData != NULL)
GlobalUnlock(m_hgDataALL);
return retval;
}
CFont *CPWTreeCtrl::GetFontBasedOnStatus(HTREEITEM &hItem, CItemData *pci, COLORREF &cf)
{
Fonts *pFonts = Fonts::GetInstance();
if (pci == NULL) {
StringX path = GetGroup(hItem);
if (app.GetMainDlg()->IsNodeModified(path)) {
cf = pFonts->GetModified_Color();
return pFonts->GetModifiedFont();
} else
return NULL;
}
switch (pci->GetStatus()) {
case CItemData::ES_ADDED:
case CItemData::ES_MODIFIED:
cf = pFonts->GetModified_Color();
return pFonts->GetModifiedFont();
default:
break;
}
return NULL;
}
void CPWTreeCtrl::OnCustomDraw(NMHDR *pNotifyStruct, LRESULT *pLResult)
{
NMTVCUSTOMDRAW *pNMTVCUSTOMDRAW = (NMTVCUSTOMDRAW *)pNotifyStruct;
*pLResult = CDRF_DODEFAULT;
static bool bchanged_item_font(false);
static CFont *pcurrentfont;
static CDC *pDC = NULL;
HTREEITEM hItem = (HTREEITEM)pNMTVCUSTOMDRAW->nmcd.dwItemSpec;
CItemData *pci = (CItemData *)pNMTVCUSTOMDRAW->nmcd.lItemlParam;
switch (pNMTVCUSTOMDRAW->nmcd.dwDrawStage) {
case CDDS_PREPAINT:
// PrePaint
pDC = CDC::FromHandle(pNMTVCUSTOMDRAW->nmcd.hdc);
bchanged_item_font = false;
pcurrentfont = Fonts::GetInstance()->GetCurrentFont();
*pLResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
// Item PrePaint
if (m_bUseHighLighting) {
COLORREF cf;
CFont *uFont = GetFontBasedOnStatus(hItem, pci, cf);
if (uFont != NULL) {
bchanged_item_font = true;
pDC->SelectObject(uFont);
// Set text color only when current node isn't selected
if ( (GetItemState(hItem, TVIS_SELECTED) & TVIS_SELECTED) == 0)
pNMTVCUSTOMDRAW->clrText = cf;
*pLResult |= (CDRF_NOTIFYPOSTPAINT | CDRF_NEWFONT);
}
}
break;
case CDDS_ITEMPOSTPAINT:
// Item PostPaint - restore old font if any
if (bchanged_item_font) {
bchanged_item_font = false;
SelectObject(pNMTVCUSTOMDRAW->nmcd.hdc, (HFONT)pcurrentfont);
*pLResult |= CDRF_NEWFONT;
}
break;
/*
case CDDS_PREERASE:
case CDDS_POSTERASE:
case CDDS_ITEMPREERASE:
case CDDS_ITEMPOSTERASE:
case CDDS_POSTPAINT:
*/
default:
break;
}
}
HTREEITEM CPWTreeCtrl::FindItem(const CString &path, HTREEITEM hRoot)
{
// check whether the current item is the searched one
CString cs_thispath = GetGroup(hRoot);// + GROUP_SEP2 + GetItemText(hRoot);
if (cs_thispath.Compare(path) == 0)
return hRoot;
// get a handle to the first child item
HTREEITEM hSub = GetChildItem(hRoot);
// iterate as long a new item is found
while (hSub) {
// check the children of the current item
HTREEITEM hFound = FindItem(path, hSub);
if (hFound)
return hFound;
// get the next sibling of the current item
hSub = GetNextSiblingItem(hSub);
}
// return NULL if nothing was found
return NULL;
}