Menu

[r6913]: / branches / wxsmith_addons / src / sdk / cbproject.cpp  Maximize  Restore  History

Download this file

2337 lines (2047 with data), 76.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
/*
* This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
* http://www.gnu.org/licenses/lgpl-3.0.html
*
* $Revision$
* $Id$
* $HeadURL$
*/
#include "sdk_precomp.h"
#ifndef wxUSE_CHOICEDLG
#define wxUSE_CHOICEDLG 1
#endif
#include <wx/choicdlg.h>
#include <wx/settings.h>
#include <wx/filedlg.h>
#include <wx/filename.h>
#include <wx/textdlg.h>
#ifndef CB_PRECOMP
#include "cbproject.h" // class's header file
#include "compiler.h" // GetSwitches
#include "sdk_events.h"
#include "manager.h"
#include "cbeditor.h"
#include "globals.h"
#include "pluginmanager.h"
#include "projectmanager.h"
#include "macrosmanager.h"
#include "logmanager.h"
#include "editormanager.h"
#include "filemanager.h"
#include "configmanager.h"
#include "compilerfactory.h"
#include "projectbuildtarget.h"
#include "projectfile.h"
#include "infowindow.h"
#endif
#include <map>
#include "projectoptionsdlg.h"
#include "projectloader.h"
#include "projectlayoutloader.h"
#include "selecttargetdlg.h"
#include "filegroupsandmasks.h"
#include "filefilters.h"
#include "annoyingdialog.h"
#include "genericmultilinenotesdlg.h"
namespace compatibility { typedef TernaryCondTypedef<wxMinimumVersion<2,5>::eval, wxTreeItemIdValue, long int>::eval tree_cookie_t; };
// class constructor
cbProject::cbProject(const wxString& filename)
: m_CustomMakefile(false),
m_Loaded(false),
m_CurrentlyLoading(false),
m_PCHMode(pchSourceFile),
m_CurrentlyCompilingTarget(0),
m_ExtendedObjectNamesGeneration(false),
m_AutoShowNotesOnLoad(false),
m_pExtensionsElement(0)
{
SetCompilerID(CompilerFactory::GetDefaultCompilerID());
SetModified(false);
wxString realFile = realpath(filename);
m_Files.Clear();
if (!realFile.IsEmpty() && (wxFileExists(realFile) || wxDirExists(realFile)))
{
// existing project
m_Filename = realFile;
m_BasePath = GetBasePath();
Open();
}
else
{
// new project
SetModified(true);
if (realFile.IsEmpty())
{
m_Filename = CreateUniqueFilename();
m_Loaded = SaveAs();
}
else
{
m_Filename = realFile;
m_Loaded = Save();
}
if (m_Loaded)
{
wxFileName fname(m_Filename);
m_Title = fname.GetName();
m_BasePath = GetBasePath();
m_CommonTopLevelPath = GetBasePath() + wxFileName::GetPathSeparator();
// moved to ProjectManager::LoadProject()
// see explanation there...
// NotifyPlugins(cbEVT_PROJECT_OPEN);
}
}
}
// class destructor
cbProject::~cbProject()
{
// moved to ProjectManager::CloseProject()
// see explanation there...
// NotifyPlugins(cbEVT_PROJECT_CLOSE);
ClearAllProperties();
}
void cbProject::NotifyPlugins(wxEventType type, const wxString& targetName, const wxString& oldTargetName)
{
CodeBlocksEvent event(type);
event.SetProject(this);
event.SetBuildTargetName(targetName);
event.SetOldBuildTargetName(oldTargetName);
Manager::Get()->ProcessEvent(event);
}
void cbProject::SetCompilerID(const wxString& id)
{
// TODO (mandrav##): Is this needed? The project's compiler has nothing to do with the targets' compilers...
CompileTargetBase::SetCompilerID(id);
if (id != GetCompilerID())
{
// update object filenames
for (unsigned int i = 0; i < m_Targets.GetCount(); ++i)
{
ProjectBuildTarget* target = m_Targets[i];
if (target)
{
Compiler* compiler = CompilerFactory::GetCompiler(target->GetCompilerID());
if (!compiler)
continue;
int count = GetFilesCount();
for (int i = 0; i < count; ++i)
{
ProjectFile* pf = GetFile(i);
wxFileName obj(pf->GetObjName());
if (FileTypeOf(pf->relativeFilename) != ftResource &&
obj.GetExt() == compiler->GetSwitches().objectExtension)
{
obj.SetExt(compiler->GetSwitches().objectExtension);
pf->SetObjName(obj.GetFullName());
}
}
}
}
}
}
bool cbProject::GetModified() const
{
// check base options
if (CompileOptionsBase::GetModified())
return true;
// check targets
for (unsigned int i = 0; i < m_Targets.GetCount(); ++i)
{
ProjectBuildTarget* target = m_Targets[i];
if (target->GetModified())
return true;
}
return false;
}
void cbProject::SetModified(bool modified)
{
CompileOptionsBase::SetModified(modified);
// modify targets
for (unsigned int i = 0; i < m_Targets.GetCount(); ++i)
{
ProjectBuildTarget* target = m_Targets[i];
target->SetModified(modified);
}
if (!modified)
m_LastSavedActiveTarget = m_ActiveTarget;
}
void cbProject::SetMakefileCustom(bool custom)
{
if (m_CustomMakefile != custom)
{
m_CustomMakefile = custom;
SetModified(true);
}
}
wxString cbProject::CreateUniqueFilename()
{
const wxString prefix = _("Untitled");
wxString tmp;
ProjectsArray* arr = Manager::Get()->GetProjectManager()->GetProjects();
int projCount = arr->GetCount();
int iter = 1;
bool ok = false;
tmp << prefix << wxString::Format(_T("%d"), iter);
while (!ok)
{
tmp.Clear();
tmp << prefix << wxString::Format(_T("%d"), iter);
ok = true;
for (int i = 0; i < projCount; ++i)
{
cbProject* prj = arr->Item(i);
wxFileName fname(prj->GetFilename());
if (fname.GetName().Matches(tmp))
{
ok = false;
break;
}
}
if (ok)
break;
++iter;
}
return tmp << _T(".") << FileFilters::CODEBLOCKS_EXT;
}
void cbProject::ClearAllProperties()
{
Delete(m_pExtensionsElement);
m_Files.DeleteContents(true);
m_Files.Clear();
m_Files.DeleteContents(false);
m_CompilerOptions.Clear();
m_LinkerOptions.Clear();
m_IncludeDirs.Clear();
m_LibDirs.Clear();
while (m_Targets.GetCount())
{
ProjectBuildTarget* target = m_Targets[0];
delete target;
m_Targets.RemoveAt(0);
}
SetModified(true);
NotifyPlugins(cbEVT_BUILDTARGET_SELECTED);
}
void cbProject::Open()
{
m_Loaded = false;
m_ProjectFilesMap.clear();
Delete(m_pExtensionsElement);
if (!wxFileName::FileExists(m_Filename) && !wxFileName::DirExists(m_Filename))
{
wxString msg;
msg.Printf(_("Project '%s' does not exist..."), m_Filename.c_str());
cbMessageBox(msg, _("Error"), wxOK | wxCENTRE | wxICON_ERROR);
return;
}
bool fileUpgraded = false;
bool fileModified = false;
wxFileName fname(m_Filename);
FileType ft = FileTypeOf(m_Filename);
if (ft == ftCodeBlocksProject)
{
Manager::Get()->GetLogManager()->Log(_("Opening ") + m_Filename);
m_CurrentlyLoading = true;
ProjectLoader loader(this);
m_Loaded = loader.Open(m_Filename, &m_pExtensionsElement);
fileUpgraded = loader.FileUpgraded();
fileModified = loader.FileModified();
m_CurrentlyLoading = false;
if (m_Loaded)
{
CalculateCommonTopLevelPath();
Manager::Get()->GetLogManager()->Log(_("done"));
if (!m_Targets.GetCount())
AddDefaultBuildTarget();
// in case of batch build discard upgrade messages
fileUpgraded = fileUpgraded && !Manager::IsBatchBuild();
SetModified(ft != ftCodeBlocksProject || fileUpgraded || fileModified);
// moved to ProjectManager::LoadProject()
// see explanation there...
// NotifyPlugins(cbEVT_PROJECT_OPEN);
if (fileUpgraded)
{
InfoWindow::Display(m_Title,
_("The loaded project file was generated\n"
"with an older version of Code::Blocks.\n\n"
"Code::Blocks can import older project files,\n"
"but will always save in the current format."), 12000, 2000);
}
m_LastModified = fname.GetModificationTime();
}
}
} // end of Open
void cbProject::CalculateCommonTopLevelPath()
{
// find the common toplevel path
// for simple projects, this might be the path to the project file
// for projects where the project file is in a subdir, files will have ..
// in their paths
wxString sep = wxFileName::GetPathSeparator();
wxFileName base = GetBasePath() + sep;
Manager::Get()->GetLogManager()->DebugLog(_T("Project's base path: ") + base.GetFullPath());
// this loop takes ~30ms for 1000 project files
// it's as fast as it can get, considered that it used to take ~1200ms ;)
// don't even bother making it faster - you can't :)
for (FilesList::Node* node = m_Files.GetFirst(); node; node = node->GetNext())
{
ProjectFile* f = node->GetData();
wxString tmp = f->relativeFilename;
wxString tmpbase = m_BasePath;
size_t pos = 0;
while (pos < tmp.Length() &&
(tmp.GetChar(pos) == _T('.') || tmp.GetChar(pos) == _T('/') || tmp.GetChar(pos) == _T('\\')))
{
++pos;
}
if (pos > 0 && pos < tmp.Length())
{
tmpbase << sep << tmp.Left(pos) << sep;
f->relativeToCommonTopLevelPath = tmp.Right(tmp.Length() - pos);
}
else
f->relativeToCommonTopLevelPath = tmp;
f->SetObjName(f->relativeToCommonTopLevelPath);
wxFileName tmpbaseF(tmpbase);
tmpbaseF.Normalize(wxPATH_NORM_DOTS);
if (tmpbaseF.GetDirCount() < base.GetDirCount())
base = tmpbaseF;
}
m_CommonTopLevelPath = base.GetFullPath();
Manager::Get()->GetLogManager()->DebugLog(_T("Project's common toplevel path: ") + m_CommonTopLevelPath);
}
wxString cbProject::GetCommonTopLevelPath() const
{
return m_CommonTopLevelPath;
}
void cbProject::Touch()
{
m_LastModified = wxDateTime::Now();
}
bool cbProject::SaveAs()
{
wxFileName fname;
fname.Assign(m_Filename);
wxFileDialog dlg(Manager::Get()->GetAppWindow(),
_("Save file"),
fname.GetPath(),
fname.GetFullName(),
FileFilters::GetFilterString(_T('.') + FileFilters::CODEBLOCKS_EXT),
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
PlaceWindow(&dlg);
if (dlg.ShowModal() != wxID_OK)
return false;
m_Filename = dlg.GetPath();
fname.Assign(m_Filename);
// make sure the project file uses the correct extension
// we don't use wxFileName::SetExt() because if the user has added a dot
// in the filename, the part after it would be interpeted as extension
// (and it might not be)
// so we just append the correct extension
if (!fname.GetExt().Matches(FileFilters::CODEBLOCKS_EXT))
fname.Assign(m_Filename + _T('.') + FileFilters::CODEBLOCKS_EXT);
// Manager::Get()->GetProjectManager()->GetTree()->SetItemText(m_ProjectNode, fname.GetFullName());
if (!m_Loaded)
AddDefaultBuildTarget();
ProjectLoader loader(this);
if (loader.Save(m_Filename, m_pExtensionsElement))
{
wxFileName fname(m_Filename);
m_LastModified = fname.GetModificationTime();
NotifyPlugins(cbEVT_PROJECT_SAVE);
return true;
}
cbMessageBox(_("Couldn't save project ") + m_Filename + _("\n(Maybe the file is write-protected?)"), _("Warning"), wxICON_WARNING);
return false;
}
bool cbProject::Save()
{
if (m_Filename.IsEmpty())
return SaveAs();
ProjectLoader loader(this);
if (loader.Save(m_Filename, m_pExtensionsElement))
{
wxFileName fname(m_Filename);
m_LastModified = fname.GetModificationTime();
NotifyPlugins(cbEVT_PROJECT_SAVE);
return true;
}
cbMessageBox(_("Couldn't save project ") + m_Filename + _("\n(Maybe the file is write-protected?)"), _("Warning"), wxICON_WARNING);
return false;
}
bool cbProject::SaveLayout()
{
if (m_Filename.IsEmpty())
return false;
wxFileName fname(m_Filename);
fname.SetExt(_T("layout"));
ProjectLayoutLoader loader(this);
return loader.Save(fname.GetFullPath());
}
bool cbProject::LoadLayout()
{
if (m_Filename.IsEmpty())
return false;
int openmode = Manager::Get()->GetConfigManager(_T("project_manager"))->ReadInt(_T("/open_files"), (long int)1);
bool result = false;
if(openmode==2)
{
// Do not open any files
result = true;
}
else
{
Manager::Get()->GetEditorManager()->HideNotebook();
if(openmode == 0) // Open all files
{
FilesList::Node* node = m_Files.GetFirst();
while(node)
{
ProjectFile* f = node->GetData();
Manager::Get()->GetEditorManager()->Open(f->file.GetFullPath(),0,f);
node = node->GetNext();
}
result = true;
}
else if(openmode == 1)// Open last open files
{
wxFileName fname(m_Filename);
fname.SetExt(_T("layout"));
ProjectLayoutLoader loader(this);
if (loader.Open(fname.GetFullPath()))
{
typedef std::map<int, ProjectFile*> open_files_map;
open_files_map open_files;
// Get all files to open and sort them according to their tab-position:
FilesList::Node* node = m_Files.GetFirst();
while(node)
{
ProjectFile* f = node->GetData();
if (f->editorOpen)
open_files[f->editorTabPos] = f;
node = node->GetNext();
}
// Load all requested files
std::vector<LoaderBase*> filesInMemory;
for (open_files_map::iterator it = open_files.begin(); it != open_files.end(); ++it)
{
filesInMemory.push_back(Manager::Get()->GetFileManager()->Load((*it).second->file.GetFullPath()));
}
// Open all requested files:
size_t i = 0;
for (open_files_map::iterator it = open_files.begin(); it != open_files.end(); ++it)
{
cbEditor* ed = Manager::Get()->GetEditorManager()->Open(filesInMemory[i], (*it).second->file.GetFullPath(),0,(*it).second);
if (ed)
ed->SetProjectFile((*it).second);
++i;
}
ProjectFile* f = loader.GetTopProjectFile();
if (f)
{
Manager::Get()->GetLogManager()->DebugLog(_T("Top Editor: ") + f->file.GetFullPath());
EditorBase* eb = Manager::Get()->GetEditorManager()->Open(f->file.GetFullPath());
if(eb)
eb->Activate();
}
// Manager::Get()->GetAppWindow()->Thaw();
}
result = true;
}
else
result = false;
Manager::Get()->GetEditorManager()->ShowNotebook();
}
return result;
}
void cbProject::BeginAddFiles()
{
CodeBlocksEvent event(cbEVT_PROJECT_BEGIN_ADD_FILES);
event.SetProject(this);
Manager::Get()->ProcessEvent(event);
}
void cbProject::EndAddFiles()
{
CodeBlocksEvent event(cbEVT_PROJECT_END_ADD_FILES);
event.SetProject(this);
Manager::Get()->ProcessEvent(event);
}
void cbProject::BeginRemoveFiles()
{
CodeBlocksEvent event(cbEVT_PROJECT_BEGIN_REMOVE_FILES);
event.SetProject(this);
Manager::Get()->ProcessEvent(event);
}
void cbProject::EndRemoveFiles()
{
CodeBlocksEvent event(cbEVT_PROJECT_END_REMOVE_FILES);
event.SetProject(this);
Manager::Get()->ProcessEvent(event);
}
ProjectFile* cbProject::AddFile(const wxString& targetName, const wxString& filename, bool compile, bool link, unsigned short int weight)
{
int idx = IndexOfBuildTargetName(targetName);
return AddFile(idx, filename, compile, link, weight);
}
ProjectFile* cbProject::AddFile(int targetIndex, const wxString& filename, bool compile, bool link, unsigned short int /*weight*/)
{
// NOTE (Rick#1#): When loading the project, do not search for existing files
// (Assumming that there are no duplicate entries in the .cbp file)
// This saves us a lot of processing when loading large projects.
// Remove the if to do the search anyway
// NOTE (mandrav#1#): We can't ignore that because even if we can rely on .cbp
// containing discrete files, we can't do that for imported projects...
// This means we have to search anyway.
// NP though, I added a hashmap for fast searches in GetFileByFilename()
/* NOTE (mandrav#1#): Calling GetFileByFilename() twice, is costly.
Instead of searching for duplicate files when entering here,
we 'll search before exiting.
The rationale is that by then, we 'll have the relative filename
in our own representation and this will make everything quicker
(check GetFileByFilename implementation to understand why)...
*/
// f = GetFileByFilename(filename, true, true);
// if (!f)
// f = GetFileByFilename(filename, false, true);
// if (f)
// {
// if (targetIndex >= 0 && targetIndex < (int)m_Targets.GetCount())
// f->AddBuildTarget(m_Targets[targetIndex]->GetTitle());
// return f;
// }
// quick test
ProjectFile* pf = m_ProjectFilesMap[UnixFilename(filename)];
if (pf)
return pf;
// create file
pf = new ProjectFile(this);
bool localCompile, localLink;
wxFileName fname(filename);
wxString ext;
FileType ft = FileTypeOf(filename);
ext = filename.AfterLast(_T('.')).Lower();
if (ext.IsSameAs(FileFilters::C_EXT))
pf->compilerVar = _T("CC");
else if (platform::windows && ext.IsSameAs(FileFilters::RESOURCE_EXT))
pf->compilerVar = _T("WINDRES");
else
pf->compilerVar = _T("CPP"); // default
if (!m_Targets.GetCount())
{
// no targets in project; add default
AddDefaultBuildTarget();
if (!m_Targets.GetCount())
{
delete pf;
return 0L; // if that failed, fail addition of file...
}
}
bool isResource = FileTypeOf(filename) == ftResource;
// NOTE (mandrav#1#): targetIndex == -1 means "don't add file to any targets"
// This case gives us problems though because then we don't know the compiler
// this file will be using (per-target) which means we can't decide if it
// generates any files...
// We solve this issue with a hack (only for the targetIndex == -1 case!):
// We iterate all available target compilers tool and use generatedFiles from
// all of them. It works and is also safe.
std::map<Compiler*, const CompilerTool*> GenFilesHackMap;
if (targetIndex < 0 || targetIndex >= (int)m_Targets.GetCount())
{
Compiler* c = CompilerFactory::GetCompiler(GetCompilerID());
if (c)
{
const CompilerTool* t = &c->GetCompilerTool(isResource ? ctCompileResourceCmd : ctCompileObjectCmd, fname.GetExt());
if (t && t->generatedFiles.GetCount())
{
GenFilesHackMap[c] = t;
}
}
for (unsigned int i = 0; i < m_Targets.GetCount(); ++i)
{
Compiler* c = CompilerFactory::GetCompiler(m_Targets[i]->GetCompilerID());
if (GenFilesHackMap.find(c) != GenFilesHackMap.end())
continue; // compiler already in map
if (c)
{
const CompilerTool* t = &c->GetCompilerTool(isResource ? ctCompileResourceCmd : ctCompileObjectCmd, fname.GetExt());
if (t && t->generatedFiles.GetCount())
{
GenFilesHackMap[c] = t;
}
}
}
}
else
{
// targetIndex is valid: just add a single entry to the map
Compiler* c = CompilerFactory::GetCompiler(m_Targets[targetIndex]->GetCompilerID());
if (c)
{
const CompilerTool* t = &c->GetCompilerTool(isResource ? ctCompileResourceCmd : ctCompileObjectCmd, fname.GetExt());
if (t && t->generatedFiles.GetCount())
{
GenFilesHackMap[c] = t;
}
}
}
// so... now, if GenFilesHackMap is not empty, we know
// 1) this file generates other files and
// 2) iterating the map will give us the generated file names :)
// add the build target
if (targetIndex >= 0 && targetIndex < (int)m_Targets.GetCount())
pf->AddBuildTarget(m_Targets[targetIndex]->GetTitle());
localCompile = compile
&& ( ft == ftSource
|| ft == ftResource
|| !GenFilesHackMap.empty() );
localLink = link
&& ( ft == ftSource
|| ft == ftResource
|| ft == ftObject
|| ft == ftResourceBin
|| ft == ftStaticLib );
pf->compile = localCompile;
pf->link = localLink;
wxString local_filename = filename;
#ifdef __WXMSW__
// for windows, make sure the filename is not on another drive...
if (local_filename.Length() > 1 &&
local_filename.GetChar(1) == _T(':') && // quick test to avoid the costly wxFileName ctor below
fname.GetVolume() != wxFileName(m_Filename).GetVolume())
{
fname.Assign(filename);
}
else
#endif
{
// make sure the filename is relative to the project's base path
if (fname.IsAbsolute())
{
fname.MakeRelativeTo(GetBasePath());
local_filename = fname.GetFullPath();
}
fname.Assign(GetBasePath() + wxFILE_SEP_PATH + local_filename);
}
fname.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE, GetBasePath());
wxString fullFilename = realpath(fname.GetFullPath());
pf->file = fullFilename;
pf->relativeFilename = UnixFilename(local_filename);
// now check if we have already added this file
// if we have, return the existing file, but add the specified target
ProjectFile* existing = GetFileByFilename(pf->relativeFilename, true, true);
if (existing == pf)
{
delete pf;
if (targetIndex >= 0 && targetIndex < (int)m_Targets.GetCount())
existing->AddBuildTarget(m_Targets[targetIndex]->GetTitle());
return existing;
}
m_Files.Append(pf);
if (!m_CurrentlyLoading)
{
// check if we really need to recalculate the common top-level path for the project
if (!fullFilename.StartsWith(m_CommonTopLevelPath))
CalculateCommonTopLevelPath();
else
{
// set f->relativeToCommonTopLevelPath
pf->relativeToCommonTopLevelPath = fullFilename.Right(fullFilename.Length() - m_CommonTopLevelPath.Length());
}
}
SetModified(true);
m_ProjectFilesMap[UnixFilename(pf->relativeFilename)] = pf; // add to hashmap
if (!wxFileExists(fullFilename))
pf->SetFileState(fvsMissing);
else if (!wxFile::Access(fullFilename.c_str(), wxFile::write)) // readonly
pf->SetFileState(fvsReadOnly);
if (!GenFilesHackMap.empty())
{
// auto-generated files!
wxFileName tmp = pf->file;
for (std::map<Compiler*, const CompilerTool*>::const_iterator it = GenFilesHackMap.begin(); it != GenFilesHackMap.end(); ++it)
{
const CompilerTool* tool = it->second;
for (size_t i = 0; i < tool->generatedFiles.GetCount(); ++i)
{
tmp.SetFullName(tool->generatedFiles[i]);
wxString tmps = tmp.GetFullPath();
// any macro replacements here, should also be done in
// CompilerCommandGenerator::GenerateCommandLine !!!
tmps.Replace(_T("$file_basename"), pf->file.GetName()); // old way - remove later
tmps.Replace(_T("$file_name"), pf->file.GetName());
tmps.Replace(_T("$file_dir"), pf->file.GetPath());
tmps.Replace(_T("$file_ext"), pf->file.GetExt());
tmps.Replace(_T("$file"), pf->file.GetFullName());
Manager::Get()->GetMacrosManager()->ReplaceMacros(tmps);
ProjectFile* pfile = AddFile(targetIndex, UnixFilename(tmps));
if (!pfile)
Manager::Get()->GetLogManager()->DebugLog(_T("Can't add auto-generated file ") + tmps);
else
{
pf->generatedFiles.push_back(pfile);
pfile->SetAutoGeneratedBy(pf);
}
}
}
}
return pf;
}
bool cbProject::RemoveFile(ProjectFile* pf)
{
if (!pf)
return false;
m_ProjectFilesMap.erase(UnixFilename(pf->relativeFilename)); // remove from hashmap
Manager::Get()->GetEditorManager()->Close(pf->file.GetFullPath());
FilesList::Node* node = m_Files.Find(pf);
if (!node)
{
Manager::Get()->GetLogManager()->DebugLog(_T("Can't locate node for ProjectFile* !"));
}
else
{
m_Files.DeleteNode(node);
}
// remove this file from all targets too
for (unsigned int i = 0; i < m_Targets.GetCount(); ++i)
{
if (ProjectBuildTarget* target = m_Targets[i])
{
target->GetFilesList().DeleteObject(pf);
}
}
// if this is auto-generated, inform "parent"
if (pf->AutoGeneratedBy())
{
ProjectFilesVector::iterator it = std::find(pf->AutoGeneratedBy()->generatedFiles.begin(),
pf->AutoGeneratedBy()->generatedFiles.end(), pf);
pf->AutoGeneratedBy()->generatedFiles.erase(it);
}
// also remove generated files (see above code: files will empty the vector)
while (pf->generatedFiles.size())
{
RemoveFile(pf->generatedFiles[0]);
}
pf->generatedFiles.clear();
delete pf;
SetModified(true);
return true;
}
bool cbProject::RemoveFile(int index)
{
if (index < 0 || index >= (int)m_Files.GetCount())
return false; // invalid index
ProjectFile* f = m_Files[index];
return RemoveFile(f);
}
int filesSort(const ProjectFile** arg1, const ProjectFile** arg2)
{
return (*arg1)->file.GetFullPath().CompareTo((*arg2)->file.GetFullPath());
}
void cbProject::BuildTree(wxTreeCtrl* tree, const wxTreeItemId& root, bool categorize, bool useFolders, FilesGroupsAndMasks* fgam)
{
if (!tree)
return;
int fldIdx = Manager::Get()->GetProjectManager()->FolderIconIndex();
int vfldIdx = Manager::Get()->GetProjectManager()->VirtualFolderIconIndex();
bool read_only = (!wxFile::Access(GetFilename().c_str(), wxFile::write));
int prjIdx = Manager::Get()->GetProjectManager()->ProjectIconIndex(read_only);
//sort list of files
m_Files.Sort(filesSort);
// add our project's root item
FileTreeData* ftd = new FileTreeData(this, FileTreeData::ftdkProject);
m_ProjectNode = tree->AppendItem(root, GetTitle(), prjIdx, prjIdx, ftd);
wxTreeItemId others = m_ProjectNode;
wxTreeItemId generated = m_ProjectNode;
// create file-type categories nodes (if enabled)
wxTreeItemId* pGroupNodes = 0L;
if (categorize && fgam)
{
pGroupNodes = new wxTreeItemId[fgam->GetGroupsCount()];
for (unsigned int i = 0; i < fgam->GetGroupsCount(); ++i)
{
ftd = new FileTreeData(this, FileTreeData::ftdkVirtualGroup);
ftd->SetFolder(fgam->GetGroupName(i));
pGroupNodes[i] = tree->AppendItem(m_ProjectNode, fgam->GetGroupName(i), fldIdx, fldIdx, ftd);
}
// add a default category "Generated" for all auto-generated file types
ftd = new FileTreeData(this, FileTreeData::ftdkVirtualGroup);
generated = tree->AppendItem(m_ProjectNode, _("Auto-generated"), fldIdx, fldIdx, ftd);
// add a default category "Others" for all non-matching file-types
ftd = new FileTreeData(this, FileTreeData::ftdkVirtualGroup);
others = tree->AppendItem(m_ProjectNode, _("Others"), fldIdx, fldIdx, ftd);
}
// Now add any virtual folders
for (size_t i = 0; i < m_VirtualFolders.GetCount(); ++i)
{
ftd = new FileTreeData(this, FileTreeData::ftdkVirtualFolder);
ftd->SetFolder(m_VirtualFolders[i]);
AddTreeNode(tree, m_VirtualFolders[i], m_ProjectNode, true, FileTreeData::ftdkVirtualFolder, true, vfldIdx, ftd);
}
// iterate all project files and add them to the tree
int count = 0;
for (FilesList::Node* node = m_Files.GetFirst(); node; node = node->GetNext())
{
ProjectFile* f = node->GetData();
ftd = new FileTreeData(this, FileTreeData::ftdkFile);
ftd->SetFileIndex(count++);
ftd->SetProjectFile(f);
ftd->SetFolder(f->file.GetFullPath());
wxFileName nodefile = f->file;
nodefile.MakeRelativeTo(m_CommonTopLevelPath);
wxString nodetext = nodefile.GetFullPath();
FileTreeData::FileTreeDataKind folders_kind = FileTreeData::ftdkFolder;
wxTreeItemId parentNode = m_ProjectNode;
// check if files grouping is enabled and find the group parent
// Also make a check that the file is not under virtual folder
if (categorize && pGroupNodes && fgam && f->virtual_path.IsEmpty())
{
bool found = false;
// auto-generated files end up all together
if (f->AutoGeneratedBy())
{
parentNode = generated;
found = true;
}
// else try to match a group
if (!found)
{
for (unsigned int i = 0; i < fgam->GetGroupsCount(); ++i)
{
wxFileName fname(f->relativeToCommonTopLevelPath);
if (fgam->MatchesMask(fname.GetFullName(), i))
{
parentNode = pGroupNodes[i];
found = true;
break;
}
}
}
// if not matched a group, put it in "Others" group
if (!found)
parentNode = others;
}
else if ((!categorize || !pGroupNodes || !fgam) && f->virtual_path.IsEmpty())
{
parentNode = m_ProjectNode;
}
/* Else put the file under virtual folder */
else if (!f->virtual_path.IsEmpty())
{
nodetext = f->virtual_path + wxFILE_SEP_PATH + f->file.GetFullName();
folders_kind = FileTreeData::ftdkVirtualFolder;
wxString slash = f->virtual_path.Last() == wxFILE_SEP_PATH ? _T("") : wxString(wxFILE_SEP_PATH);
ftd->SetFolder(f->virtual_path);
if (m_VirtualFolders.Index(f->virtual_path + slash) == wxNOT_FOUND)
m_VirtualFolders.Add(f->virtual_path + slash);
}
// add file in the tree
f->m_TreeItemId = AddTreeNode(tree, nodetext, parentNode, useFolders || folders_kind == FileTreeData::ftdkVirtualFolder, folders_kind, f->compile, (int)f->m_VisualState, ftd);
}
// remove empty tree nodes (like empty groups)
if (categorize && fgam)
{
for (unsigned int i = 0; i < fgam->GetGroupsCount(); ++i)
{
if (tree->GetChildrenCount(pGroupNodes[i], false) == 0)
tree->Delete(pGroupNodes[i]);
}
if (tree->GetChildrenCount(others, false) == 0)
tree->Delete(others);
if (tree->GetChildrenCount(generated, false) == 0)
tree->Delete(generated);
}
delete[] pGroupNodes;
tree->Expand(m_ProjectNode);
}
// helper function used by AddTreeNode
static wxString GetRelativeFolderPath(wxTreeCtrl* tree, wxTreeItemId parent)
{
wxString fld;
while (parent.IsOk())
{
FileTreeData* ftd = (FileTreeData*)tree->GetItemData(parent);
if (!ftd || (ftd->GetKind() != FileTreeData::ftdkFolder && ftd->GetKind() != FileTreeData::ftdkVirtualFolder))
break;
fld.Prepend(tree->GetItemText(parent) + wxFILE_SEP_PATH);
parent = tree->GetItemParent(parent);
}
return fld;
}
wxTreeItemId cbProject::AddTreeNode(wxTreeCtrl* tree,
const wxString& text,
const wxTreeItemId& parent,
bool useFolders,
FileTreeData::FileTreeDataKind folders_kind,
bool compiles,
int image,
FileTreeData* data)
{
// see if the text contains any path info, e.g. plugins/compilergcc/compilergcc.cpp
// in that case, take the first element (plugins in this example), create a sub-folder
// with the same name and recurse with the result...
wxTreeItemId ret;
if (text.IsEmpty())
return ret;
wxString path = text;
// special case for windows and files on a different drive
if (platform::windows && path.Length() > 1 && path.GetChar(1) == _T(':'))
path.Remove(1, 1);
int pos = path.Find(_T('/'));
if (pos == -1)
pos = path.Find(_T('\\'));
if (useFolders && pos >= 0)
{
// ok, we got it. now split it up and recurse
wxString folder = path.Left(pos);
// avoid consecutive path separators
while (path.GetChar(pos + 1) == _T('/') || path.GetChar(pos + 1) == _T('\\'))
++pos;
path = path.Right(path.Length() - pos - 1);
compatibility::tree_cookie_t cookie = 0;
wxTreeItemId newparent = tree->GetFirstChild(parent, cookie);
while (newparent)
{
wxString itemText = tree->GetItemText(newparent);
if (itemText.Matches(folder))
break;
newparent = tree->GetNextChild(parent, cookie);
}
if (!newparent)
{
// in order not to override wxTreeCtrl to sort alphabetically but the
// folders be always on top, we just search here where to put the new folder...
int fldIdx = Manager::Get()->GetProjectManager()->FolderIconIndex();
int vfldIdx = Manager::Get()->GetProjectManager()->VirtualFolderIconIndex();
newparent = FindNodeToInsertAfter(tree, folder, parent, true);
FileTreeData* ftd = new FileTreeData(*data);
ftd->SetKind(folders_kind);
if (folders_kind != FileTreeData::ftdkVirtualFolder)
ftd->SetFolder(m_CommonTopLevelPath + GetRelativeFolderPath(tree, parent) + folder + wxFILE_SEP_PATH);
else
ftd->SetFolder(GetRelativeFolderPath(tree, parent) + folder + wxFILE_SEP_PATH);
ftd->SetProjectFile(0);
int idx = folders_kind != FileTreeData::ftdkVirtualFolder ? fldIdx : vfldIdx;
newparent = tree->InsertItem(parent, newparent, folder, idx, idx, ftd);
}
ret = AddTreeNode(tree, path, newparent, true, folders_kind, compiles, image, data);
}
else
{
ret = tree->AppendItem(parent, text, image, image, data);
if (!compiles)
tree->SetItemTextColour(ret, wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
}
return ret;
}
wxTreeItemId cbProject::FindNodeToInsertAfter(wxTreeCtrl* tree, const wxString& text, const wxTreeItemId& parent, bool in_folders)
{
wxTreeItemId result;
if (tree && parent.IsOk())
{
compatibility::tree_cookie_t cookie = 0;
int fldIdx = Manager::Get()->GetProjectManager()->FolderIconIndex();
int vfldIdx = Manager::Get()->GetProjectManager()->VirtualFolderIconIndex();
wxTreeItemId last;
bool last_is_folder = false;
wxTreeItemId child = tree->GetFirstChild(parent, cookie);
while (child)
{
bool is_folder = tree->GetItemImage(child) == fldIdx || tree->GetItemImage(child) == vfldIdx;
if (in_folders)
{
if (!is_folder || text.CmpNoCase(tree->GetItemText(child)) < 0)
{
result = last;
break;
}
}
else
{
if (!is_folder && text.CmpNoCase(tree->GetItemText(child)) < 0)
{
result = last;
break;
}
}
last = child;
last_is_folder = is_folder;
child = tree->GetNextChild(parent, cookie);
}
if (!result.IsOk())
result = last;
}
return result;
}
void cbProject::CopyTreeNodeRecursively(wxTreeCtrl* tree, const wxTreeItemId& item, const wxTreeItemId& new_parent)
{
// first, some sanity checks
if (!tree || !item.IsOk() || !new_parent.IsOk())
return;
FileTreeData* ftd = (FileTreeData*)tree->GetItemData(item);
FileTreeData* ftd_moved = ftd ? new FileTreeData(*ftd) : 0;
int idx = tree->GetItemImage(item); // old image
wxColour col = tree->GetItemTextColour(item); // old colour
wxTreeItemId insert = FindNodeToInsertAfter(tree, tree->GetItemText(item), new_parent, ftd && ftd->GetKind() == FileTreeData::ftdkVirtualFolder);
wxTreeItemId target = tree->InsertItem(new_parent, insert, tree->GetItemText(item), idx, idx, ftd_moved);
tree->SetItemTextColour(target, col);
// recurse for folders
if (tree->ItemHasChildren(item))
{
// vfolder: recurse for files all contained files virtual path
wxTreeItemIdValue cookie;
wxTreeItemId child = tree->GetFirstChild(item, cookie);
while (child.IsOk())
{
CopyTreeNodeRecursively(tree, child, target);
child = tree->GetNextChild(item, cookie);
}
}
if (!tree->IsExpanded(new_parent))
tree->Expand(new_parent);
if (ftd_moved->GetProjectFile())
ftd_moved->GetProjectFile()->virtual_path = GetRelativeFolderPath(tree, new_parent);
}
const wxArrayString& cbProject::GetVirtualFolders() const
{
return m_VirtualFolders;
}
void cbProject::SetVirtualFolders(const wxArrayString& folders)
{
m_VirtualFolders = folders;
for (size_t i = 0; i < m_VirtualFolders.GetCount(); ++i)
{
m_VirtualFolders[i].Replace(_T("/"), wxString(wxFILE_SEP_PATH));
m_VirtualFolders[i].Replace(_T("\\"), wxString(wxFILE_SEP_PATH));
}
}
bool cbProject::CanDragNode(wxTreeCtrl* tree, wxTreeItemId node)
{
// what item do we start dragging?
if (!node.IsOk())
return false;
// if no data associated with it, disallow
FileTreeData* ftd = (FileTreeData*)tree->GetItemData(node);
if (!ftd)
return false;
// if not ours, disallow
if (ftd->GetProject() != this)
return false;
// allow only if it is a file or a virtual folder
return ftd->GetKind() == FileTreeData::ftdkFile || ftd->GetKind() == FileTreeData::ftdkVirtualFolder;
}
bool cbProject::NodeDragged(wxTreeCtrl* tree, wxTreeItemId from, wxTreeItemId to)
{
// what items did we drag?
if (!from.IsOk() || !to.IsOk())
return false;
// if no data associated with it, disallow
FileTreeData* ftd1 = (FileTreeData*)tree->GetItemData(from);
FileTreeData* ftd2 = (FileTreeData*)tree->GetItemData(to);
if (!ftd1 || !ftd2)
return false;
// if not ours, disallow
if (ftd1->GetProject() != this || ftd2->GetProject() != this)
return false;
// allow only if a file or vfolder was dragged on a file, another vfolder or the project itself
if ((ftd1->GetKind() != FileTreeData::ftdkFile &&
ftd1->GetKind() != FileTreeData::ftdkVirtualFolder) ||
(ftd2->GetKind() != FileTreeData::ftdkFile &&
ftd2->GetKind() != FileTreeData::ftdkVirtualFolder &&
ftd2->GetKind() != FileTreeData::ftdkProject))
{
return false;
}
// don't drag under the same parent
wxTreeItemId parent1 = ftd1->GetKind() == FileTreeData::ftdkFile ? tree->GetItemParent(from) : from;
wxTreeItemId parent2 = ftd2->GetKind() == FileTreeData::ftdkFile ? tree->GetItemParent(to) : to;
if (parent1 == parent2)
return false;
// A special check for virtual folders.
if (ftd1->GetKind() == FileTreeData::ftdkVirtualFolder || ftd2->GetKind() == FileTreeData::ftdkVirtualFolder)
{
wxTreeItemId root = tree->GetRootItem();
wxTreeItemId toParent = tree->GetItemParent(to);
while (toParent != root)
{
if (toParent == from)
return false;
toParent = tree->GetItemParent(toParent);
}
if (!VirtualFolderDragged(tree, from, to))
{
return false;
}
}
// finally; make the move
CopyTreeNodeRecursively(tree, from, parent2);
// remove old node
tree->Delete(from);
SetModified(true);
Manager::Get()->GetProjectManager()->RebuildTree();
return true;
}
bool cbProject::VirtualFolderAdded(wxTreeCtrl* tree, wxTreeItemId parent_node, const wxString& virtual_folder)
{
wxString foldername = GetRelativeFolderPath(tree, parent_node);
foldername << virtual_folder;
foldername.Replace(_T("/"), wxString(wxFILE_SEP_PATH), true);
foldername.Replace(_T("\\"), wxString(wxFILE_SEP_PATH), true);
if (foldername.Last() != wxFILE_SEP_PATH)
foldername << wxFILE_SEP_PATH;
for (size_t i = 0; i < m_VirtualFolders.GetCount(); ++i)
{
if (m_VirtualFolders[i].StartsWith(foldername))
{
cbMessageBox(_("A virtual folder with the same name already exists."),
_("Error"), wxICON_WARNING);
return false;
}
}
m_VirtualFolders.Add(foldername);
FileTreeData* ftd = new FileTreeData(this, FileTreeData::ftdkVirtualFolder);
ftd->SetProjectFile(0);
ftd->SetFolder(foldername);
int vfldIdx = Manager::Get()->GetProjectManager()->VirtualFolderIconIndex();
AddTreeNode(tree, foldername, m_ProjectNode, true, FileTreeData::ftdkVirtualFolder, true, vfldIdx, ftd);
if (!tree->IsExpanded(parent_node))
tree->Expand(parent_node);
SetModified(true);
// Manager::Get()->GetLogManager()->DebugLog(F(_T("VirtualFolderAdded: %s: %s"), foldername.c_str(), GetStringFromArray(m_VirtualFolders, _T(";")).c_str()));
return true;
}
void cbProject::VirtualFolderDeleted(wxTreeCtrl* tree, wxTreeItemId node)
{
// what item do we start dragging?
if (!node.IsOk())
return;
// if no data associated with it, disallow
FileTreeData* ftd = (FileTreeData*)tree->GetItemData(node);
if (!ftd)
return;
// if not ours, disallow
if (ftd->GetProject() != this)
return;
wxString foldername = GetRelativeFolderPath(tree, node);
wxString parent_foldername = GetRelativeFolderPath(tree, tree->GetItemParent(node));
// now loop all project files and remove them from this virtual folder
for (FilesList::Node* node = m_Files.GetFirst(); node; node = node->GetNext())
{
ProjectFile* f = node->GetData();
if (f && !f->virtual_path.IsEmpty())
{
if (f->virtual_path.StartsWith(foldername)) // need 2 checks because of last separator
f->virtual_path.Clear();
}
}
for (int i = (int)m_VirtualFolders.GetCount() - 1; i >= 0; --i)
{
if (m_VirtualFolders[i].StartsWith(foldername))
m_VirtualFolders.RemoveAt(i);
}
if (!parent_foldername.IsEmpty() && m_VirtualFolders.Index(parent_foldername) == wxNOT_FOUND)
m_VirtualFolders.Add(parent_foldername);
SetModified(true);
// Manager::Get()->GetLogManager()->DebugLog(F(_T("VirtualFolderDeleted: %s: %s"), foldername.c_str(), GetStringFromArray(m_VirtualFolders, _T(";")).c_str()));
}
bool cbProject::VirtualFolderRenamed(wxTreeCtrl* tree, wxTreeItemId node, const wxString& new_name)
{
if (new_name.IsEmpty())
return false;
if (new_name.First(_T(';')) != wxNOT_FOUND ||
new_name.First(_T('/')) != wxNOT_FOUND ||
new_name.First(_T('\\')) != wxNOT_FOUND)
{
cbMessageBox(_("A virtual folder name cannot contain these special characters: \";\", \"\\\" or \"/\"."),
_("Error"), wxICON_WARNING);
return false;
}
// what item are we renaming?
if (!node.IsOk())
return false;
// is it a different name?
if (tree->GetItemText(node) == new_name)
return false;
// if no data associated with it, disallow
FileTreeData* ftd = (FileTreeData*)tree->GetItemData(node);
if (!ftd)
return false;
// if not ours, disallow
if (ftd->GetProject() != this)
return false;
wxString old_foldername = GetRelativeFolderPath(tree, node);
wxString new_foldername = GetRelativeFolderPath(tree, tree->GetItemParent(node)) + new_name + wxFILE_SEP_PATH;
for (size_t i = 0; i < m_VirtualFolders.GetCount(); ++i)
{
if (m_VirtualFolders[i].StartsWith(new_foldername))
{
cbMessageBox(_("A virtual folder with the same name already exists."),
_("Error"), wxICON_WARNING);
return false;
}
}
int idx = m_VirtualFolders.Index(old_foldername);
if (idx != wxNOT_FOUND)
m_VirtualFolders[idx] = new_foldername;
else
m_VirtualFolders.Add(new_foldername);
// now loop all project files and rename this virtual folder
for (FilesList::Node* node = m_Files.GetFirst(); node; node = node->GetNext())
{
ProjectFile* f = node->GetData();
if (f && !f->virtual_path.IsEmpty())
{
if (f->virtual_path.StartsWith(old_foldername))
f->virtual_path.Replace(old_foldername, new_foldername);
}
}
SetModified(true);
// Manager::Get()->GetLogManager()->DebugLog(F(_T("VirtualFolderRenamed: %s to %s: %s"), old_foldername.c_str(), new_foldername.c_str(), GetStringFromArray(m_VirtualFolders, _T(";")).c_str()));
return true;
}
bool cbProject::VirtualFolderDragged(wxTreeCtrl* tree, wxTreeItemId from, wxTreeItemId to)
{
FileTreeData* ftdFrom = static_cast<FileTreeData*>(tree->GetItemData(from));
FileTreeData* ftdTo = static_cast<FileTreeData*>(tree->GetItemData(to));
wxString sep = wxString(wxFileName::GetPathSeparator());
wxChar sepChar = wxFileName::GetPathSeparator();
wxString fromFolderPath = ftdFrom->GetFolder();
wxString toFolderPath = ftdTo->GetFolder();
wxString fromFolder = fromFolderPath;
fromFolder = fromFolder.RemoveLast();
fromFolder = fromFolder.AfterLast(sepChar) + sep;
wxString toFolder = toFolderPath;
toFolder = toFolder.RemoveLast();
toFolder = toFolder.AfterLast(sepChar) + sep;
if (ftdFrom->GetKind() == FileTreeData::ftdkVirtualFolder && ftdTo->GetKind() == FileTreeData::ftdkVirtualFolder)
{
wxArrayString oldArray = m_VirtualFolders;
m_VirtualFolders.Clear();
for (size_t i = 0; i < oldArray.GetCount(); ++i)
{
wxString item = oldArray[i];
wxString toFolderStr;
if (toFolderPath.StartsWith(fromFolderPath.BeforeFirst(sepChar)))
{
// The virtual folder is drageed under same root
int posFrom = item.Find(fromFolderPath);
if (posFrom != wxNOT_FOUND)
{
wxString fromFolderStr = item.Mid(posFrom);
item = item.Left(posFrom);
if (!item.IsEmpty())
{
m_VirtualFolders.Add(item);
}
}
else if (item.IsSameAs(toFolderPath))
{
// First add it to folder structure
m_VirtualFolders.Add(item);
wxString fromFolderStr = fromFolderPath;
fromFolderStr = fromFolderStr.RemoveLast();
fromFolderStr = fromFolderStr.AfterLast(wxFileName::GetPathSeparator());
m_VirtualFolders.Add(item + fromFolderStr + sep);
}
else
{
m_VirtualFolders.Add(item);
}
}
else
{
// A virtual folder has been dropped from a different place
if (item.Find(toFolderPath + fromFolder) != wxNOT_FOUND)
{
cbMessageBox(_T("Another Virtual folder with same name exists in the destination folder!"),
_T("Error"), wxOK | wxICON_ERROR, Manager::Get()->GetAppWindow());
m_VirtualFolders = oldArray;
return false;
}
if (item.StartsWith(toFolderPath.BeforeFirst(sepChar)))
{
m_VirtualFolders.Add(item);
}
else if (item.StartsWith(fromFolderPath.BeforeFirst(sepChar)))
{
int pos = item.Find(fromFolder);
if (pos == 0)
{
if (!toFolderPath.IsEmpty())
{
m_VirtualFolders.Add(toFolderPath + item);
}
}
else
{
wxString temp = item.Left(pos);
m_VirtualFolders.Add(item.Left(pos));
if (!toFolderPath.IsEmpty())
{
m_VirtualFolders.Add(toFolderPath + item.Mid(pos));
}
}
}
}
}
}
else if (ftdFrom->GetKind() == FileTreeData::ftdkVirtualFolder && ftdTo->GetKind() == FileTreeData::ftdkProject)
{
wxArrayString oldArray = m_VirtualFolders;
m_VirtualFolders.Clear();
m_VirtualFolders.Add(fromFolder);
for (size_t i = 0; i < oldArray.GetCount(); ++i)
{
wxString item = oldArray[i];
if (item.StartsWith(fromFolder))
{
// We can't overwrite an existing folder
cbMessageBox(_T("Another Virtual folder with same name exists in the Project tree!"),
_T("Error"), wxOK | wxICON_ERROR, Manager::Get()->GetAppWindow());
m_VirtualFolders.Clear();
m_VirtualFolders = oldArray;
return false;
}
else if (item.StartsWith(fromFolderPath))
{
int pos = item.Find(fromFolderPath);
if (pos != wxNOT_FOUND)
{
item = item.Mid(pos + fromFolderPath.Length());
if (item.Length() > 1)
{
item = item.Prepend(fromFolder);
if (m_VirtualFolders.Index(item) == wxNOT_FOUND)
{
m_VirtualFolders.Add(item);
}
}
else
{
continue;
}
}
else
{
m_VirtualFolders.Add(item);
}
}
else
{
m_VirtualFolders.Add(item);
}
}
}
return true;
}
void cbProject::RenameInTree(const wxString &newname)
{
wxTreeCtrl* tree = Manager::Get()->GetProjectManager()->GetTree();
if(!tree || !m_ProjectNode)
return;
tree->SetItemText(m_ProjectNode, newname);
}
void cbProject::SaveTreeState(wxTreeCtrl* tree)
{
::SaveTreeState(tree, m_ProjectNode, m_ExpandedNodes, m_SelectedNode);
}
void cbProject::RestoreTreeState(wxTreeCtrl* tree)
{
::RestoreTreeState(tree, m_ProjectNode, m_ExpandedNodes, m_SelectedNode);
}
const wxString& cbProject::GetMakefile()
{
if (!m_Makefile.IsEmpty())
return m_Makefile;
wxFileName makefile(m_Makefile);
makefile.Assign(m_Filename);
makefile.SetName(_T("Makefile"));
makefile.SetExt(_T(""));
makefile.MakeRelativeTo(GetBasePath());
m_Makefile = makefile.GetFullPath();
return m_Makefile;
}
void cbProject::SetMakefileExecutionDir(const wxString& dir)
{
if (m_MakefileExecutionDir != dir)
{
m_MakefileExecutionDir = dir;
SetModified(true);
}
}
wxString cbProject::GetMakefileExecutionDir()
{
if (m_MakefileExecutionDir.IsEmpty())
{
wxFileName execution_dir(GetBasePath());
m_MakefileExecutionDir = execution_dir.GetFullPath();
}
return m_MakefileExecutionDir;
}
wxString cbProject::GetExecutionDir()
{
if(!m_CustomMakefile)
{
return GetBasePath();
}
return GetMakefileExecutionDir();
}
ProjectFile* cbProject::GetFile(int index)
{
FilesList::Node* node = m_Files.Item(index);
if (node)
return node->GetData();
return NULL;
}
ProjectFile* cbProject::GetFileByFilename(const wxString& filename, bool isRelative, bool isUnixFilename)
{
// m_ProjectFilesMap keeps UnixFilename(ProjectFile::relativeFilename)
wxString tmp = filename;
if (!isRelative)
{
// if the search is not relative, make it
wxFileName fname(realpath(filename));
fname.MakeRelativeTo(GetBasePath());
tmp = fname.GetFullPath();
}
else
{
// make sure filename doesn't start with ".\"
// our own relative files don't have it, so the search would fail
// this happens when importing MS projects...
if (tmp.StartsWith(_T(".\\")) ||
tmp.StartsWith(_T("./")))
{
tmp.Remove(0, 2);
}
}
if (isUnixFilename)
return m_ProjectFilesMap[tmp];
return m_ProjectFilesMap[UnixFilename(tmp)];
}
bool cbProject::QueryCloseAllFiles()
{
FilesList::Node* node;
node = m_Files.GetFirst();
while(node)
{
ProjectFile* f = node->GetData();
cbEditor* ed = Manager::Get()->GetEditorManager()->IsBuiltinOpen(f->file.GetFullPath());
if (ed && ed->GetModified())
{
if (!Manager::Get()->GetEditorManager()->QueryClose(ed))
return false;
}
node = node->GetNext();
}
return true;
}
bool cbProject::CloseAllFiles(bool dontsave)
{
// first try to close modified editors
if(!dontsave)
if(!QueryCloseAllFiles())
return false;
// now free the rest of the project files
Manager::Get()->GetEditorManager()->HideNotebook();
FilesList::Node* node = m_Files.GetFirst();
while(node)
{
ProjectFile* f = node->GetData();
Manager::Get()->GetEditorManager()->Close(f->file.GetFullPath(),true);
delete f;
delete node;
node = m_Files.GetFirst();
}
Manager::Get()->GetEditorManager()->ShowNotebook();
return true;
}
bool cbProject::SaveAllFiles()
{
int count = m_Files.GetCount();
FilesList::Node* node = m_Files.GetFirst();
while(node)
{
ProjectFile* f = node->GetData();
if (Manager::Get()->GetEditorManager()->Save(f->file.GetFullPath()))
--count;
node = node->GetNext();
}
return count == 0;
}
bool cbProject::ShowOptions()
{
ProjectOptionsDlg dlg(Manager::Get()->GetAppWindow(), this);
PlaceWindow(&dlg);
if (dlg.ShowModal() == wxID_OK)
{
// update file details
FilesList::Node* node = m_Files.GetFirst();
while(node)
{
ProjectFile* f = node->GetData();
f->UpdateFileDetails();
node = node->GetNext();
}
return true;
}
return false;
}
int cbProject::SelectTarget(int initial, bool evenIfOne)
{
if (!evenIfOne && GetBuildTargetsCount() == 1)
return 0;
SelectTargetDlg dlg(0L, this, initial);
PlaceWindow(&dlg);
if (dlg.ShowModal() == wxID_OK)
return dlg.GetSelection();
return -1;
}
// Build targets
ProjectBuildTarget* cbProject::AddDefaultBuildTarget()
{
return AddBuildTarget(_T("default"));
}
ProjectBuildTarget* cbProject::AddBuildTarget(const wxString& targetName)
{
if (GetBuildTarget(targetName)) // Don't add the target if it exists
return 0L;
ProjectBuildTarget* target = new ProjectBuildTarget(this);
target->m_Filename = m_Filename; // really important
target->SetTitle(targetName);
target->SetCompilerID(GetCompilerID()); // same compiler as project's
target->SetOutputFilename(wxFileName(GetOutputFilename()).GetFullName());
target->SetWorkingDir(_T("."));
target->SetObjectOutput(_T(".objs"));
target->SetDepsOutput(_T(".deps"));
m_Targets.Add(target);
// remove any virtual targets with the same name
if (HasVirtualBuildTarget(targetName))
{
RemoveVirtualBuildTarget(targetName);
#if wxCHECK_VERSION(2, 9, 0)
Manager::Get()->GetLogManager()->LogWarning(F(_T("Deleted existing virtual target '%s' because real target was added with the same name"), targetName.wx_str()));
#else
Manager::Get()->GetLogManager()->LogWarning(F(_T("Deleted existing virtual target '%s' because real target was added with the same name"), targetName.c_str()));
#endif
}
SetModified(true);
NotifyPlugins(cbEVT_BUILDTARGET_ADDED, targetName);
NotifyPlugins(cbEVT_PROJECT_TARGETS_MODIFIED);
return target;
}
bool cbProject::RenameBuildTarget(int index, const wxString& targetName)
{
ProjectBuildTarget* target = GetBuildTarget(index);
if (target)
{
wxString oldTargetName = target->GetTitle();
// rename target if referenced in any virtual target too
for (VirtualBuildTargetsMap::iterator it = m_VirtualTargets.begin(); it != m_VirtualTargets.end(); ++it)
{
wxArrayString& tgts = it->second;
int index = tgts.Index(target->GetTitle());
if (index != -1)
{
tgts[index] = targetName;
}
}
// rename target for all files that reference it
int count = GetFilesCount();
for (int i = 0; i < count; ++i)
{
ProjectFile* pf = GetFile(i);
pf->RenameBuildTarget(target->GetTitle(), targetName);
}
// finally rename the target
target->SetTitle(targetName);
SetModified(true);
NotifyPlugins(cbEVT_BUILDTARGET_RENAMED, targetName, oldTargetName);
NotifyPlugins(cbEVT_PROJECT_TARGETS_MODIFIED);
return true;
}
return false;
}
bool cbProject::RenameBuildTarget(const wxString& oldTargetName, const wxString& newTargetName)
{
return RenameBuildTarget(IndexOfBuildTargetName(oldTargetName), newTargetName);
}
ProjectBuildTarget* cbProject::DuplicateBuildTarget(int index, const wxString& newName)
{
ProjectBuildTarget* newTarget = 0;
ProjectBuildTarget* target = GetBuildTarget(index);
if (target)
{
newTarget = new ProjectBuildTarget(*target);
wxString newTargetName = !newName.IsEmpty() ? newName : (_("Copy of ") + target->GetTitle());
newTarget->SetTitle(newTargetName);
// just notify the files of this target that they belong to the new target too
for (FilesList::Node* it = newTarget->GetFilesList().GetFirst(); it; it = it->GetNext())
{
ProjectFile* pf = it->GetData();
pf->AddBuildTarget(newTargetName);
}
SetModified(true);
m_Targets.Add(newTarget);
// send also the old target name, so plugins see that the target is duplicated and not a new one added
// so that plugin specific parameters can be copied, too.
NotifyPlugins(cbEVT_BUILDTARGET_ADDED, newName, target->GetTitle());
NotifyPlugins(cbEVT_PROJECT_TARGETS_MODIFIED);
}
return newTarget;
}
ProjectBuildTarget* cbProject::DuplicateBuildTarget(const wxString& targetName, const wxString& newName)
{
return DuplicateBuildTarget(IndexOfBuildTargetName(targetName), newName);
}
bool cbProject::ExportTargetAsProject(int index)
{
ProjectBuildTarget* target = GetBuildTarget(index);
if (!target)
return false;
return ExportTargetAsProject(target->GetTitle());
}
bool cbProject::ExportTargetAsProject(const wxString& targetName)
{
ProjectBuildTarget* target = GetBuildTarget(targetName);
if (!target)
return false;
// ask for the new project's name
wxString newName = wxGetTextFromUser(_("Please enter the new project's name (no path, no extension)."),
_("Export target as new project"),
target->GetTitle());
if (newName.IsEmpty())
return false;
wxFileName fname(GetFilename());
fname.SetName(newName);
Save();
bool alreadyModified = GetModified();
wxString oldTitle = GetTitle();
SetTitle(targetName);
ProjectLoader loader(this);
bool ret = loader.ExportTargetAsProject(fname.GetFullPath(), target->GetTitle(), m_pExtensionsElement);
SetTitle(oldTitle);
if (!alreadyModified)
SetModified(false);
return ret;
}
bool cbProject::RemoveBuildTarget(int index)
{
ProjectBuildTarget* target = GetBuildTarget(index);
if (target)
{
wxString oldTargetName = target->GetTitle();
// remove target from any virtual targets it belongs to
for (VirtualBuildTargetsMap::iterator it = m_VirtualTargets.begin(); it != m_VirtualTargets.end(); ++it)
{
wxArrayString& tgts = it->second;
int index = tgts.Index(target->GetTitle());
if (index != -1)
{
tgts.RemoveAt(index);
}
}
// remove target from any project files that reference it
int count = GetFilesCount();
for (int i = 0; i < count; ++i)
{
ProjectFile* pf = GetFile(i);
pf->RemoveBuildTarget(target->GetTitle());
}
// notify plugins, before the target is deleted, to make a cleanup possible before the target is really deleted
NotifyPlugins(cbEVT_BUILDTARGET_REMOVED, oldTargetName);
// finally remove the target
delete target;
m_Targets.RemoveAt(index);
SetModified(true);
NotifyPlugins(cbEVT_PROJECT_TARGETS_MODIFIED);
return true;
}
return false;
}
bool cbProject::RemoveBuildTarget(const wxString& targetName)
{
return RemoveBuildTarget(IndexOfBuildTargetName(targetName));
}
int cbProject::IndexOfBuildTargetName(const wxString& targetName) const
{
for (unsigned int i = 0; i < m_Targets.GetCount(); ++i)
{
ProjectBuildTarget* target = m_Targets[i];
if (target->GetTitle().Matches(targetName))
return i;
}
return -1;
}
bool cbProject::BuildTargetValid(const wxString& name, bool virtuals_too) const
{
if (virtuals_too && HasVirtualBuildTarget(name))
return true;
else if (IndexOfBuildTargetName(name) != -1)
return true;
return false;
}
wxString cbProject::GetFirstValidBuildTargetName(bool virtuals_too) const
{
if (virtuals_too && !m_VirtualTargets.empty())
return m_VirtualTargets.begin()->first;
else if (m_Targets.GetCount() && m_Targets[0])
return m_Targets[0]->GetTitle();
return wxEmptyString;
}
bool cbProject::SetActiveBuildTarget(const wxString& name)
{
if (name == m_ActiveTarget)
return true;
wxString oldActiveTarget = m_ActiveTarget;
m_ActiveTarget = name;
bool valid = BuildTargetValid(name);
if (!valid)
{
// no target (virtual or real) by that name
m_ActiveTarget = GetFirstValidBuildTargetName();
}
NotifyPlugins(cbEVT_BUILDTARGET_SELECTED, m_ActiveTarget, oldActiveTarget);
return valid;
}
const wxString& cbProject::GetActiveBuildTarget() const
{
return m_ActiveTarget;
}
void cbProject::SetDefaultExecuteTarget(const wxString& name)
{
if (name == m_DefaultExecuteTarget)
return;
m_DefaultExecuteTarget = name;
SetModified(true);
}
const wxString& cbProject::GetDefaultExecuteTarget() const
{
return m_DefaultExecuteTarget;
}
ProjectBuildTarget* cbProject::GetBuildTarget(int index)
{
if (index >= 0 && index < (int)m_Targets.GetCount())
return m_Targets[index];
return 0L;
}
ProjectBuildTarget* cbProject::GetBuildTarget(const wxString& targetName)
{
int idx = IndexOfBuildTargetName(targetName);
return GetBuildTarget(idx);
}
void cbProject::ReOrderTargets(const wxArrayString& nameOrder)
{
LogManager* msgMan = Manager::Get()->GetLogManager();
if (nameOrder.GetCount() != m_Targets.GetCount())
{
msgMan->DebugLog(F(_T("cbProject::ReOrderTargets() : Count does not match (%d sent, %d had)..."), nameOrder.GetCount(), m_Targets.GetCount()));
return;
}
for (unsigned int i = 0; i < nameOrder.GetCount(); ++i)
{
ProjectBuildTarget* target = GetBuildTarget(nameOrder[i]);
if (!target)
{
#if wxCHECK_VERSION(2, 9, 0)
msgMan->DebugLog(F(_T("cbProject::ReOrderTargets() : Target \"%s\" not found..."), nameOrder[i].wx_str()));
#else
msgMan->DebugLog(F(_T("cbProject::ReOrderTargets() : Target \"%s\" not found..."), nameOrder[i].c_str()));
#endif
break;
}
m_Targets.Remove(target);
m_Targets.Insert(target, i);
// we have to re-order the targets which are kept inside
// the virtual targets array too!
VirtualBuildTargetsMap::iterator it;
for (it = m_VirtualTargets.begin(); it != m_VirtualTargets.end(); ++it)
{
wxArrayString& vt = it->second;
if (vt.Index(nameOrder[i]) != wxNOT_FOUND)
{
vt.Remove(nameOrder[i]);
vt.Insert(nameOrder[i], (vt.Count()<=i) ? vt.Count()-1 : i);
}
}
}
SetModified(true);
}
void cbProject::SetCurrentlyCompilingTarget(ProjectBuildTarget* bt)
{
m_CurrentlyCompilingTarget = bt;
}
bool cbProject::DefineVirtualBuildTarget(const wxString& alias, const wxArrayString& targets)
{
if (targets.GetCount() == 0)
{
#if wxCHECK_VERSION(2, 9, 0)
Manager::Get()->GetLogManager()->LogWarning(F(_T("Can't define virtual build target '%s': Group of build targets is empty!"), alias.wx_str()));
#else
Manager::Get()->GetLogManager()->LogWarning(F(_T("Can't define virtual build target '%s': Group of build targets is empty!"), alias.c_str()));
#endif
return false;
}
ProjectBuildTarget* existing = GetBuildTarget(alias);
if (existing)
{
#if wxCHECK_VERSION(2, 9, 0)
Manager::Get()->GetLogManager()->LogWarning(F(_T("Can't define virtual build target '%s': Real build target exists with that name!"), alias.wx_str()));
#else
Manager::Get()->GetLogManager()->LogWarning(F(_T("Can't define virtual build target '%s': Real build target exists with that name!"), alias.c_str()));
#endif
return false;
}
m_VirtualTargets[alias] = targets;
SetModified(true);
NotifyPlugins(cbEVT_PROJECT_TARGETS_MODIFIED);
return true;
}
bool cbProject::HasVirtualBuildTarget(const wxString& alias) const
{
return m_VirtualTargets.find(alias) != m_VirtualTargets.end();
}
bool cbProject::RemoveVirtualBuildTarget(const wxString& alias)
{
VirtualBuildTargetsMap::iterator it = m_VirtualTargets.find(alias);
if (it == m_VirtualTargets.end())
return false;
m_VirtualTargets.erase(it);
SetModified(true);
NotifyPlugins(cbEVT_PROJECT_TARGETS_MODIFIED);
return true;
}
wxArrayString cbProject::GetVirtualBuildTargets() const
{
wxArrayString result;
for (VirtualBuildTargetsMap::const_iterator it = m_VirtualTargets.begin(); it != m_VirtualTargets.end(); ++it)
result.Add(it->first);
return result;
}
const wxArrayString& cbProject::GetVirtualBuildTargetGroup(const wxString& alias) const
{
static wxArrayString resultIfError;
VirtualBuildTargetsMap::const_iterator it = m_VirtualTargets.find(alias);
if (it == m_VirtualTargets.end())
return resultIfError;
return it->second;
}
wxArrayString cbProject::GetExpandedVirtualBuildTargetGroup(const wxString& alias) const
{
wxArrayString result;
VirtualBuildTargetsMap::const_iterator it = m_VirtualTargets.find(alias);
if (it == m_VirtualTargets.end())
return result;
ExpandVirtualBuildTargetGroup(alias, result);
return result;
}
bool cbProject::CanAddToVirtualBuildTarget(const wxString& alias, const wxString& target)
{
// virtual not there?
if (!HasVirtualBuildTarget(alias))
return false;
// real targets can be added safely (as long as they 're unique)
if (!HasVirtualBuildTarget(target))
return true;
// virtual targets are checked in two ways:
// 1) it is checked if it contains alias
// 2) all its virtual targets are recursively checked if they contain alias
const wxArrayString& group = GetVirtualBuildTargetGroup(target);
if (group.Index(alias) != wxNOT_FOUND)
return false;
for (size_t i = 0; i < group.GetCount(); ++i)
{
// only virtuals
if (HasVirtualBuildTarget(group[i]))
{
if (!CanAddToVirtualBuildTarget(group[i], alias))
return false;
}
}
return true;
}
void cbProject::ExpandVirtualBuildTargetGroup(const wxString& alias, wxArrayString& result) const
{
const wxArrayString& group = GetVirtualBuildTargetGroup(alias);
for (size_t i = 0; i < group.GetCount(); ++i)
{
// real targets get added
if (IndexOfBuildTargetName(group[i]) != -1)
{
if (result.Index(group[i]) == wxNOT_FOUND)
result.Add(group[i]);
}
// virtual targets recurse
else
ExpandVirtualBuildTargetGroup(group[i], result);
}
}
#ifdef USE_OPENFILES_TREE
bool MiscTreeItemData::OwnerCheck(wxTreeEvent& event,wxTreeCtrl *tree,wxEvtHandler *handler,bool strict)
{
if(!tree) // No tree to get data from - ignore event
return false;
MiscTreeItemData* data =
(MiscTreeItemData*)tree->GetItemData(event.GetItem());
if(!data)
{
if(!strict)
return true; // On doubt, allow event
else
{
event.Skip();
return false;
}
}
wxEvtHandler *h = data->GetOwner();
if((h && h!=handler) || (strict && !h))
{ // Tree Item belongs to another handler - skip
event.Skip();
return false;
}
return true;
}
#endif
void cbProject::SetExtendedObjectNamesGeneration(bool ext)
{
bool changed = m_ExtendedObjectNamesGeneration != ext;
// update it now because SetObjName() below will call GetExtendedObjectNamesGeneration()
// so it must be up-to-date
m_ExtendedObjectNamesGeneration = ext;
if (changed)
{
for (FilesList::Node* node = m_Files.GetFirst(); node; node = node->GetNext())
{
ProjectFile* f = node->GetData();
f->SetObjName(f->relativeToCommonTopLevelPath);
f->UpdateFileDetails();
}
SetModified(true);
}
}
bool cbProject::GetExtendedObjectNamesGeneration() const
{
return m_ExtendedObjectNamesGeneration;
}
void cbProject::SetNotes(const wxString& notes)
{
if (m_Notes != notes)
{
m_Notes = notes;
SetModified(true);
}
}
const wxString& cbProject::GetNotes() const
{
return m_Notes;
}
void cbProject::SetShowNotesOnLoad(bool show)
{
if (m_AutoShowNotesOnLoad != show)
{
m_AutoShowNotesOnLoad = show;
SetModified(true);
}
}
bool cbProject::GetShowNotesOnLoad() const
{
return m_AutoShowNotesOnLoad;
}
void cbProject::ShowNotes(bool nonEmptyOnly, bool editable)
{
if (!editable && nonEmptyOnly && m_Notes.IsEmpty())
return;
GenericMultiLineNotesDlg dlg(Manager::Get()->GetAppWindow(),
_("Notes about ") + m_Title,
m_Notes,
!editable);
PlaceWindow(&dlg);
if (dlg.ShowModal() == wxID_OK)
{
if (editable)
SetNotes(dlg.GetNotes());
}
}
void cbProject::SetTitle(const wxString& title)
{
if ( title != GetTitle() )
{
CompileTargetBase::SetTitle(title);
NotifyPlugins(cbEVT_PROJECT_RENAMED);
}
}
TiXmlNode* cbProject::GetExtensionsNode()
{
if (!m_pExtensionsElement)
m_pExtensionsElement = new TiXmlElement(cbU2C(_T("Extensions")));
return m_pExtensionsElement;
}
void cbProject::AddToExtensions(const wxString& stringDesc)
{
// sample stringDesc:
// node/+subnode/subsubnode:attr=val
TiXmlElement* elem = GetExtensionsNode()->ToElement();
size_t pos = 0;
while (true)
{
// ignore consecutive slashes
while (pos < stringDesc.Length() && stringDesc.GetChar(pos) == _T('/'))
{
++pos;
}
// find next slash or colon
size_t nextPos = pos;
while (nextPos < stringDesc.Length() && stringDesc.GetChar(++nextPos) != _T('/') && stringDesc.GetChar(nextPos) != _T(':'))
;
wxString current = stringDesc.Mid(pos, nextPos - pos);
if (current.IsEmpty() || current[0] == _T(':')) // abort on invalid case: "node/:attr=val" (consecutive "/:")
break;
// find or create the subnode
bool forceAdd = current[0] == _T('+');
if (forceAdd)
current.Remove(0, 1); // remove '+'
TiXmlElement* sub = !forceAdd ? elem->FirstChildElement(cbU2C(current)) : 0;
if (!sub)
{
sub = elem->InsertEndChild(TiXmlElement(cbU2C(current)))->ToElement();
SetModified(true);
}
elem = sub;
// last node?
if (stringDesc.GetChar(nextPos) == _T(':'))
{
// yes, just parse the attribute now
pos = nextPos + 1; // skip the colon
nextPos = pos;
while (nextPos < stringDesc.Length() && stringDesc.GetChar(++nextPos) != _T('='))
;
if (pos == nextPos || nextPos == stringDesc.Length())
{
// invalid attribute
}
else
{
wxString key = stringDesc.Mid(pos, nextPos - pos);
wxString val = stringDesc.Mid(nextPos + 1, stringDesc.Length() - nextPos - 1);
sub->SetAttribute(cbU2C(key), cbU2C(val));
SetModified(true);
}
// all done
break;
}
pos = nextPos; // prepare for next loop
}
}
void cbProject::ProjectFileRenamed(ProjectFile* pf)
{
for (ProjectFiles::iterator it = m_ProjectFilesMap.begin(); it != m_ProjectFilesMap.end(); ++it)
{
ProjectFile* itpf = it->second;
if (itpf == pf)
{
// got it
m_ProjectFilesMap.erase(it);
m_ProjectFilesMap[UnixFilename(pf->file.GetFullPath())] = pf;
break;
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.