The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1123 lines
38KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_Project.h"
  19. #include "jucer_ProjectType.h"
  20. #include "../Project Saving/jucer_ProjectExporter.h"
  21. #include "../Project Saving/jucer_ProjectSaver.h"
  22. #include "../Application/jucer_OpenDocumentManager.h"
  23. //==============================================================================
  24. namespace Tags
  25. {
  26. const Identifier projectRoot ("JUCERPROJECT");
  27. const Identifier projectMainGroup ("MAINGROUP");
  28. const Identifier group ("GROUP");
  29. const Identifier file ("FILE");
  30. const Identifier configurations ("CONFIGURATIONS");
  31. const Identifier configuration ("CONFIGURATION");
  32. const Identifier exporters ("EXPORTFORMATS");
  33. const Identifier configGroup ("JUCEOPTIONS");
  34. const Identifier modulesGroup ("MODULES");
  35. const Identifier module ("MODULE");
  36. }
  37. const char* Project::projectFileExtension = ".jucer";
  38. //==============================================================================
  39. Project::Project (const File& file_)
  40. : FileBasedDocument (projectFileExtension,
  41. String ("*") + projectFileExtension,
  42. "Choose a Jucer project to load",
  43. "Save Jucer project"),
  44. projectRoot (Tags::projectRoot)
  45. {
  46. setFile (file_);
  47. setMissingDefaultValues();
  48. setChangedFlag (false);
  49. mainProjectIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  50. projectRoot.addListener (this);
  51. }
  52. Project::~Project()
  53. {
  54. projectRoot.removeListener (this);
  55. OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false);
  56. }
  57. //==============================================================================
  58. void Project::setTitle (const String& newTitle)
  59. {
  60. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  61. getMainGroup().getName() = newTitle;
  62. }
  63. const String Project::getDocumentTitle()
  64. {
  65. return getProjectName().toString();
  66. }
  67. void Project::updateProjectSettings()
  68. {
  69. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  70. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  71. }
  72. void Project::setMissingDefaultValues()
  73. {
  74. if (! projectRoot.hasProperty (ComponentBuilder::idProperty))
  75. projectRoot.setProperty (ComponentBuilder::idProperty, createAlphaNumericUID(), nullptr);
  76. // Create main file group if missing
  77. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  78. {
  79. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  80. projectRoot.addChild (mainGroup.state, 0, 0);
  81. }
  82. getMainGroup().initialiseMissingProperties();
  83. if (getDocumentTitle().isEmpty())
  84. setTitle ("Juce Project");
  85. if (! projectRoot.hasProperty (Ids::projectType))
  86. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  87. if (! projectRoot.hasProperty (Ids::version))
  88. getVersion() = "1.0.0";
  89. // Create configs group
  90. if (! projectRoot.getChildWithName (Tags::configurations).isValid())
  91. {
  92. projectRoot.addChild (ValueTree (Tags::configurations), 0, 0);
  93. createDefaultConfigs();
  94. }
  95. if (! projectRoot.getChildWithName (Tags::exporters).isValid())
  96. createDefaultExporters();
  97. getProjectType().setMissingProjectProperties (*this);
  98. if (! projectRoot.hasProperty (Ids::bundleIdentifier))
  99. setBundleIdentifierToDefault();
  100. if (! projectRoot.getChildWithName (Tags::modulesGroup).isValid())
  101. addDefaultModules (false);
  102. }
  103. void Project::addDefaultModules (bool shouldCopyFilesLocally)
  104. {
  105. addModule ("juce_core", shouldCopyFilesLocally);
  106. if (! isConfigFlagEnabled ("JUCE_ONLY_BUILD_CORE_LIBRARY"))
  107. {
  108. addModule ("juce_events", shouldCopyFilesLocally);
  109. addModule ("juce_graphics", shouldCopyFilesLocally);
  110. addModule ("juce_data_structures", shouldCopyFilesLocally);
  111. addModule ("juce_gui_basics", shouldCopyFilesLocally);
  112. addModule ("juce_gui_extra", shouldCopyFilesLocally);
  113. addModule ("juce_gui_audio", shouldCopyFilesLocally);
  114. addModule ("juce_cryptography", shouldCopyFilesLocally);
  115. addModule ("juce_video", shouldCopyFilesLocally);
  116. addModule ("juce_opengl", shouldCopyFilesLocally);
  117. addModule ("juce_audio_basics", shouldCopyFilesLocally);
  118. addModule ("juce_audio_devices", shouldCopyFilesLocally);
  119. addModule ("juce_audio_formats", shouldCopyFilesLocally);
  120. addModule ("juce_audio_processors", shouldCopyFilesLocally);
  121. }
  122. }
  123. //==============================================================================
  124. const String Project::loadDocument (const File& file)
  125. {
  126. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  127. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  128. return "Not a valid Jucer project!";
  129. ValueTree newTree (ValueTree::fromXml (*xml));
  130. if (! newTree.hasType (Tags::projectRoot))
  131. return "The document contains errors and couldn't be parsed!";
  132. StoredSettings::getInstance()->recentFiles.addFile (file);
  133. StoredSettings::getInstance()->flush();
  134. projectRoot = newTree;
  135. setMissingDefaultValues();
  136. return String::empty;
  137. }
  138. const String Project::saveDocument (const File& file)
  139. {
  140. updateProjectSettings();
  141. sanitiseConfigFlags();
  142. StoredSettings::getInstance()->recentFiles.addFile (file);
  143. ProjectSaver saver (*this, file);
  144. return saver.save();
  145. }
  146. //==============================================================================
  147. File Project::lastDocumentOpened;
  148. const File Project::getLastDocumentOpened()
  149. {
  150. return lastDocumentOpened;
  151. }
  152. void Project::setLastDocumentOpened (const File& file)
  153. {
  154. lastDocumentOpened = file;
  155. }
  156. //==============================================================================
  157. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  158. {
  159. if (property == Ids::projectType)
  160. setMissingDefaultValues();
  161. changed();
  162. }
  163. void Project::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
  164. {
  165. changed();
  166. }
  167. void Project::valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved)
  168. {
  169. changed();
  170. }
  171. void Project::valueTreeChildOrderChanged (ValueTree& parentTree)
  172. {
  173. changed();
  174. }
  175. void Project::valueTreeParentChanged (ValueTree& tree)
  176. {
  177. }
  178. //==============================================================================
  179. File Project::resolveFilename (String filename) const
  180. {
  181. if (filename.isEmpty())
  182. return File::nonexistent;
  183. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  184. .replaceCharacter ('\\', '/');
  185. if (File::isAbsolutePath (filename))
  186. return File (filename);
  187. return getFile().getSiblingFile (filename);
  188. }
  189. String Project::getRelativePathForFile (const File& file) const
  190. {
  191. String filename (file.getFullPathName());
  192. File relativePathBase (getFile().getParentDirectory());
  193. String p1 (relativePathBase.getFullPathName());
  194. String p2 (file.getFullPathName());
  195. while (p1.startsWithChar (File::separator))
  196. p1 = p1.substring (1);
  197. while (p2.startsWithChar (File::separator))
  198. p2 = p2.substring (1);
  199. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  200. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  201. {
  202. filename = file.getRelativePathFrom (relativePathBase);
  203. }
  204. return filename;
  205. }
  206. //==============================================================================
  207. const ProjectType& Project::getProjectType() const
  208. {
  209. const ProjectType* type = ProjectType::findType (getProjectTypeValue().toString());
  210. jassert (type != nullptr);
  211. if (type == nullptr)
  212. {
  213. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  214. jassert (type != nullptr);
  215. }
  216. return *type;
  217. }
  218. //==============================================================================
  219. void Project::createPropertyEditors (Array <PropertyComponent*>& props)
  220. {
  221. props.add (new TextPropertyComponent (getProjectName(), "Project Name", 256, false));
  222. props.getLast()->setTooltip ("The name of the project.");
  223. props.add (new TextPropertyComponent (getVersion(), "Project Version", 16, false));
  224. props.getLast()->setTooltip ("The project's version number, This should be in the format major.minor.point");
  225. {
  226. StringArray projectTypeNames;
  227. Array<var> projectTypeCodes;
  228. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  229. for (int i = 0; i < types.size(); ++i)
  230. {
  231. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  232. projectTypeCodes.add (types.getUnchecked(i)->getType());
  233. }
  234. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  235. }
  236. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false));
  237. props.getLast()->setTooltip ("A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  238. {
  239. OwnedArray<Project::Item> images;
  240. findAllImageItems (images);
  241. StringArray choices;
  242. Array<var> ids;
  243. choices.add ("<None>");
  244. ids.add (var::null);
  245. choices.add (String::empty);
  246. ids.add (var::null);
  247. for (int i = 0; i < images.size(); ++i)
  248. {
  249. choices.add (images.getUnchecked(i)->getName().toString());
  250. ids.add (images.getUnchecked(i)->getID());
  251. }
  252. props.add (new ChoicePropertyComponent (getSmallIconImageItemID(), "Icon (small)", choices, ids));
  253. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  254. props.add (new ChoicePropertyComponent (getBigIconImageItemID(), "Icon (large)", choices, ids));
  255. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  256. }
  257. getProjectType().createPropertyEditors(*this, props);
  258. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  259. props.getLast()->setTooltip ("Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  260. for (int i = props.size(); --i >= 0;)
  261. props.getUnchecked(i)->setPreferredHeight (22);
  262. }
  263. String Project::getVersionAsHex() const
  264. {
  265. StringArray configs;
  266. configs.addTokens (getVersion().toString(), ",.", String::empty);
  267. configs.trim();
  268. configs.removeEmptyStrings();
  269. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  270. if (configs.size() >= 4)
  271. value = (value << 8) + configs[3].getIntValue();
  272. return "0x" + String::toHexString (value);
  273. }
  274. Image Project::getBigIcon()
  275. {
  276. return getMainGroup().findItemWithID (getBigIconImageItemID().toString()).loadAsImageFile();
  277. }
  278. Image Project::getSmallIcon()
  279. {
  280. return getMainGroup().findItemWithID (getSmallIconImageItemID().toString()).loadAsImageFile();
  281. }
  282. StringPairArray Project::getPreprocessorDefs() const
  283. {
  284. return parsePreprocessorDefs (getProjectPreprocessorDefs().toString());
  285. }
  286. //==============================================================================
  287. Project::Item Project::getMainGroup()
  288. {
  289. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  290. }
  291. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  292. {
  293. if (item.isImageFile())
  294. {
  295. found.add (new Project::Item (item));
  296. }
  297. else if (item.isGroup())
  298. {
  299. for (int i = 0; i < item.getNumChildren(); ++i)
  300. findImages (item.getChild (i), found);
  301. }
  302. }
  303. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  304. {
  305. findImages (getMainGroup(), items);
  306. }
  307. //==============================================================================
  308. Project::Item::Item (Project& project_, const ValueTree& state_)
  309. : project (project_), state (state_)
  310. {
  311. }
  312. Project::Item::Item (const Item& other)
  313. : project (other.project), state (other.state)
  314. {
  315. }
  316. Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.state.createCopy(); return i; }
  317. String Project::Item::getID() const { return state [ComponentBuilder::idProperty]; }
  318. void Project::Item::setID (const String& newID) { state.setProperty (ComponentBuilder::idProperty, newID, nullptr); }
  319. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  320. Image Project::Item::loadAsImageFile() const
  321. {
  322. return isValid() ? ImageCache::getFromFile (getFile())
  323. : Image::null;
  324. }
  325. Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid)
  326. {
  327. Item group (project, ValueTree (Tags::group));
  328. group.setID (uid);
  329. group.initialiseMissingProperties();
  330. group.getName() = name;
  331. return group;
  332. }
  333. bool Project::Item::isFile() const { return state.hasType (Tags::file); }
  334. bool Project::Item::isGroup() const { return state.hasType (Tags::group) || isMainGroup(); }
  335. bool Project::Item::isMainGroup() const { return state.hasType (Tags::projectMainGroup); }
  336. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  337. Project::Item Project::Item::findItemWithID (const String& targetId) const
  338. {
  339. if (state [ComponentBuilder::idProperty] == targetId)
  340. return *this;
  341. if (isGroup())
  342. {
  343. for (int i = getNumChildren(); --i >= 0;)
  344. {
  345. Item found (getChild(i).findItemWithID (targetId));
  346. if (found.isValid())
  347. return found;
  348. }
  349. }
  350. return Item (project, ValueTree::invalid);
  351. }
  352. bool Project::Item::canContain (const Item& child) const
  353. {
  354. if (isFile())
  355. return false;
  356. if (isGroup())
  357. return child.isFile() || child.isGroup();
  358. jassertfalse
  359. return false;
  360. }
  361. bool Project::Item::shouldBeAddedToTargetProject() const { return isFile(); }
  362. bool Project::Item::shouldBeCompiled() const { return getShouldCompileValue().getValue(); }
  363. Value Project::Item::getShouldCompileValue() const { return state.getPropertyAsValue (Ids::compile, getUndoManager()); }
  364. bool Project::Item::shouldBeAddedToBinaryResources() const { return getShouldAddToResourceValue().getValue(); }
  365. Value Project::Item::getShouldAddToResourceValue() const { return state.getPropertyAsValue (Ids::resource, getUndoManager()); }
  366. Value Project::Item::getShouldInhibitWarningsValue() const { return state.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  367. Value Project::Item::getShouldUseStdCallValue() const { return state.getPropertyAsValue (Ids::useStdCall, nullptr); }
  368. String Project::Item::getFilePath() const
  369. {
  370. if (isFile())
  371. return state [Ids::file].toString();
  372. else
  373. return String::empty;
  374. }
  375. File Project::Item::getFile() const
  376. {
  377. if (isFile())
  378. return project.resolveFilename (state [Ids::file].toString());
  379. else
  380. return File::nonexistent;
  381. }
  382. void Project::Item::setFile (const File& file)
  383. {
  384. setFile (RelativePath (project.getRelativePathForFile (file), RelativePath::projectFolder));
  385. jassert (getFile() == file);
  386. }
  387. void Project::Item::setFile (const RelativePath& file)
  388. {
  389. jassert (file.getRoot() == RelativePath::projectFolder);
  390. jassert (isFile());
  391. state.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  392. state.setProperty (Ids::name, file.getFileName(), getUndoManager());
  393. }
  394. bool Project::Item::renameFile (const File& newFile)
  395. {
  396. const File oldFile (getFile());
  397. if (oldFile.moveFileTo (newFile))
  398. {
  399. setFile (newFile);
  400. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  401. return true;
  402. }
  403. return false;
  404. }
  405. bool Project::Item::containsChildForFile (const RelativePath& file) const
  406. {
  407. return state.getChildWithProperty (Ids::file, file.toUnixStyle()).isValid();
  408. }
  409. Project::Item Project::Item::findItemForFile (const File& file) const
  410. {
  411. if (getFile() == file)
  412. return *this;
  413. if (isGroup())
  414. {
  415. for (int i = getNumChildren(); --i >= 0;)
  416. {
  417. Item found (getChild(i).findItemForFile (file));
  418. if (found.isValid())
  419. return found;
  420. }
  421. }
  422. return Item (project, ValueTree::invalid);
  423. }
  424. File Project::Item::determineGroupFolder() const
  425. {
  426. jassert (isGroup());
  427. File f;
  428. for (int i = 0; i < getNumChildren(); ++i)
  429. {
  430. f = getChild(i).getFile();
  431. if (f.exists())
  432. return f.getParentDirectory();
  433. }
  434. Item parent (getParent());
  435. if (parent != *this)
  436. {
  437. f = parent.determineGroupFolder();
  438. if (f.getChildFile (getName().toString()).isDirectory())
  439. f = f.getChildFile (getName().toString());
  440. }
  441. else
  442. {
  443. f = project.getFile().getParentDirectory();
  444. if (f.getChildFile ("Source").isDirectory())
  445. f = f.getChildFile ("Source");
  446. }
  447. return f;
  448. }
  449. void Project::Item::initialiseMissingProperties()
  450. {
  451. if (! state.hasProperty (ComponentBuilder::idProperty))
  452. setID (createAlphaNumericUID());
  453. if (isFile())
  454. {
  455. state.setProperty (Ids::name, getFile().getFileName(), 0);
  456. }
  457. else if (isGroup())
  458. {
  459. for (int i = getNumChildren(); --i >= 0;)
  460. getChild(i).initialiseMissingProperties();
  461. }
  462. }
  463. Value Project::Item::getName() const
  464. {
  465. return state.getPropertyAsValue (Ids::name, getUndoManager());
  466. }
  467. void Project::Item::addChild (const Item& newChild, int insertIndex)
  468. {
  469. state.addChild (newChild.state, insertIndex, getUndoManager());
  470. }
  471. void Project::Item::removeItemFromProject()
  472. {
  473. state.getParent().removeChild (state, getUndoManager());
  474. }
  475. Project::Item Project::Item::getParent() const
  476. {
  477. if (isMainGroup() || ! isGroup())
  478. return *this;
  479. return Item (project, state.getParent());
  480. }
  481. struct ItemSorter
  482. {
  483. static int compareElements (const ValueTree& first, const ValueTree& second)
  484. {
  485. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  486. }
  487. };
  488. struct ItemSorterWithGroupsAtStart
  489. {
  490. static int compareElements (const ValueTree& first, const ValueTree& second)
  491. {
  492. const bool firstIsGroup = first.hasType (Tags::group);
  493. const bool secondIsGroup = second.hasType (Tags::group);
  494. if (firstIsGroup == secondIsGroup)
  495. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  496. else
  497. return firstIsGroup ? -1 : 1;
  498. }
  499. };
  500. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  501. {
  502. if (keepGroupsAtStart)
  503. {
  504. ItemSorterWithGroupsAtStart sorter;
  505. state.sort (sorter, getUndoManager(), true);
  506. }
  507. else
  508. {
  509. ItemSorter sorter;
  510. state.sort (sorter, getUndoManager(), true);
  511. }
  512. }
  513. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  514. {
  515. for (int i = state.getNumChildren(); --i >= 0;)
  516. {
  517. const ValueTree child (state.getChild (i));
  518. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  519. return Item (project, child);
  520. }
  521. return addNewSubGroup (name, -1);
  522. }
  523. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  524. {
  525. Item group (createGroup (project, name, createGUID (getID() + name + String (getNumChildren()))));
  526. jassert (canContain (group));
  527. addChild (group, insertIndex);
  528. return group;
  529. }
  530. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  531. {
  532. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  533. return false;
  534. if (file.isDirectory())
  535. {
  536. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  537. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  538. while (iter.next())
  539. {
  540. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  541. group.addFile (iter.getFile(), -1, shouldCompile);
  542. }
  543. group.sortAlphabetically (false);
  544. }
  545. else if (file.existsAsFile())
  546. {
  547. if (! project.getMainGroup().findItemForFile (file).isValid())
  548. addFileUnchecked (file, insertIndex, shouldCompile);
  549. }
  550. else
  551. {
  552. jassertfalse;
  553. }
  554. return true;
  555. }
  556. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  557. {
  558. Item item (project, ValueTree (Tags::file));
  559. item.initialiseMissingProperties();
  560. item.getName() = file.getFileName();
  561. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  562. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  563. if (canContain (item))
  564. {
  565. item.setFile (file);
  566. addChild (item, insertIndex);
  567. }
  568. }
  569. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  570. {
  571. Item item (project, ValueTree (Tags::file));
  572. item.initialiseMissingProperties();
  573. item.getName() = file.getFileName();
  574. item.getShouldCompileValue() = shouldCompile;
  575. item.getShouldAddToResourceValue() = project.shouldBeAddedToBinaryResourcesByDefault (file);
  576. if (canContain (item))
  577. {
  578. item.setFile (file);
  579. addChild (item, insertIndex);
  580. return true;
  581. }
  582. return false;
  583. }
  584. const Drawable* Project::Item::getIcon() const
  585. {
  586. if (isFile())
  587. {
  588. if (isImageFile())
  589. return StoredSettings::getInstance()->getImageFileIcon();
  590. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  591. }
  592. else if (isMainGroup())
  593. {
  594. return &(project.mainProjectIcon);
  595. }
  596. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  597. }
  598. //==============================================================================
  599. ValueTree Project::getConfigNode()
  600. {
  601. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  602. }
  603. const char* const Project::configFlagDefault = "default";
  604. const char* const Project::configFlagEnabled = "enabled";
  605. const char* const Project::configFlagDisabled = "disabled";
  606. Value Project::getConfigFlag (const String& name)
  607. {
  608. const ValueTree configNode (getConfigNode());
  609. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  610. if (v.getValue().toString().isEmpty())
  611. v = configFlagDefault;
  612. return v;
  613. }
  614. bool Project::isConfigFlagEnabled (const String& name) const
  615. {
  616. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  617. }
  618. void Project::sanitiseConfigFlags()
  619. {
  620. ValueTree configNode (getConfigNode());
  621. for (int i = configNode.getNumProperties(); --i >= 0;)
  622. {
  623. const var value (configNode [configNode.getPropertyName(i)]);
  624. if (value != configFlagEnabled && value != configFlagDisabled)
  625. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  626. }
  627. }
  628. //==============================================================================
  629. ValueTree Project::getModulesNode()
  630. {
  631. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  632. }
  633. bool Project::isModuleEnabled (const String& moduleID) const
  634. {
  635. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  636. for (int i = 0; i < modules.getNumChildren(); ++i)
  637. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  638. return true;
  639. return false;
  640. }
  641. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  642. {
  643. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  644. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  645. }
  646. Value Project::shouldCopyModuleFilesLocally (const String& moduleID)
  647. {
  648. return getModulesNode().getChildWithProperty (ComponentBuilder::idProperty, moduleID)
  649. .getPropertyAsValue (Ids::useLocalCopy, getUndoManagerFor (getModulesNode()));
  650. }
  651. void Project::addModule (const String& moduleID, bool shouldCopyFilesLocally)
  652. {
  653. if (! isModuleEnabled (moduleID))
  654. {
  655. ValueTree module (Tags::module);
  656. module.setProperty (ComponentBuilder::idProperty, moduleID, nullptr);
  657. ValueTree modules (getModulesNode());
  658. modules.addChild (module, -1, getUndoManagerFor (modules));
  659. shouldShowAllModuleFilesInProject (moduleID) = true;
  660. }
  661. if (shouldCopyFilesLocally)
  662. shouldCopyModuleFilesLocally (moduleID) = true;
  663. }
  664. void Project::removeModule (const String& moduleID)
  665. {
  666. ValueTree modules (getModulesNode());
  667. for (int i = 0; i < modules.getNumChildren(); ++i)
  668. if (modules.getChild(i) [ComponentBuilder::idProperty] == moduleID)
  669. modules.removeChild (i, getUndoManagerFor (modules));
  670. }
  671. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  672. {
  673. for (int i = 0; i < availableModules.modules.size(); ++i)
  674. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  675. modules.add (availableModules.modules.getUnchecked(i)->create());
  676. }
  677. int Project::getNumModules() const
  678. {
  679. return projectRoot.getChildWithName (Tags::modulesGroup).getNumChildren();
  680. }
  681. String Project::getModuleID (int index) const
  682. {
  683. return projectRoot.getChildWithName (Tags::modulesGroup).getChild (index) [ComponentBuilder::idProperty].toString();
  684. }
  685. //==============================================================================
  686. ValueTree Project::getConfigurations() const
  687. {
  688. return projectRoot.getChildWithName (Tags::configurations);
  689. }
  690. int Project::getNumConfigurations() const
  691. {
  692. return getConfigurations().getNumChildren();
  693. }
  694. Project::BuildConfiguration Project::getConfiguration (int index)
  695. {
  696. jassert (index < getConfigurations().getNumChildren());
  697. return BuildConfiguration (this, getConfigurations().getChild (index));
  698. }
  699. bool Project::hasConfigurationNamed (const String& name) const
  700. {
  701. const ValueTree configs (getConfigurations());
  702. for (int i = configs.getNumChildren(); --i >= 0;)
  703. if (configs.getChild(i) [Ids::name].toString() == name)
  704. return true;
  705. return false;
  706. }
  707. String Project::getUniqueConfigName (String name) const
  708. {
  709. String nameRoot (name);
  710. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  711. nameRoot = nameRoot.dropLastCharacters (1);
  712. nameRoot = nameRoot.trim();
  713. int suffix = 2;
  714. while (hasConfigurationNamed (name))
  715. name = nameRoot + " " + String (suffix++);
  716. return name;
  717. }
  718. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  719. {
  720. const String configName (getUniqueConfigName (configToCopy != nullptr ? configToCopy->config [Ids::name].toString()
  721. : "New Build Configuration"));
  722. ValueTree configs (getConfigurations());
  723. if (! configs.isValid())
  724. {
  725. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  726. configs = getConfigurations();
  727. }
  728. ValueTree newConfig (Tags::configuration);
  729. if (configToCopy != nullptr)
  730. newConfig = configToCopy->config.createCopy();
  731. newConfig.setProperty (Ids::name, configName, 0);
  732. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  733. }
  734. void Project::deleteConfiguration (int index)
  735. {
  736. ValueTree configs (getConfigurations());
  737. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  738. }
  739. void Project::createDefaultConfigs()
  740. {
  741. for (int i = 0; i < 2; ++i)
  742. {
  743. addNewConfiguration (nullptr);
  744. BuildConfiguration config = getConfiguration (i);
  745. const bool debugConfig = i == 0;
  746. config.getName() = debugConfig ? "Debug" : "Release";
  747. config.isDebug() = debugConfig;
  748. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  749. config.getTargetBinaryName() = getProjectFilenameRoot();
  750. }
  751. }
  752. //==============================================================================
  753. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  754. : project (project_),
  755. config (configNode)
  756. {
  757. }
  758. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  759. : project (other.project),
  760. config (other.config)
  761. {
  762. }
  763. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  764. {
  765. project = other.project;
  766. config = other.config;
  767. return *this;
  768. }
  769. Project::BuildConfiguration::~BuildConfiguration()
  770. {
  771. }
  772. String Project::BuildConfiguration::getGCCOptimisationFlag() const
  773. {
  774. const int level = (int) getOptimisationLevel().getValue();
  775. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  776. }
  777. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  778. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  779. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  780. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  781. const char* const Project::BuildConfiguration::osxArch_Default = "default";
  782. const char* const Project::BuildConfiguration::osxArch_Native = "Native";
  783. const char* const Project::BuildConfiguration::osxArch_32BitUniversal = "32BitUniversal";
  784. const char* const Project::BuildConfiguration::osxArch_64BitUniversal = "64BitUniversal";
  785. const char* const Project::BuildConfiguration::osxArch_64Bit = "64BitIntel";
  786. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  787. {
  788. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  789. props.getLast()->setTooltip ("The name of this configuration.");
  790. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  791. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  792. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  793. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  794. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  795. props.getLast()->setTooltip ("The optimisation level for this configuration");
  796. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  797. props.getLast()->setTooltip ("The filename to use for the destination binary executable file. Don't add a suffix to this, because platform-specific suffixes will be added for each target platform.");
  798. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  799. props.getLast()->setTooltip ("The folder in which the finished binary should be placed. Leave this blank to cause the binary to be placed in its default location in the build folder.");
  800. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  801. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  802. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  803. props.getLast()->setTooltip ("Extra preprocessor definitions. Use the form \"NAME1=value NAME2=value\", using whitespace or commas to separate the items - to include a space or comma in a definition, precede it with a backslash.");
  804. if (getMacSDKVersion().toString().isEmpty())
  805. getMacSDKVersion() = osxVersionDefault;
  806. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  807. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  808. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  809. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  810. if (getMacCompatibilityVersion().toString().isEmpty())
  811. getMacCompatibilityVersion() = osxVersionDefault;
  812. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  813. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  814. const char* osxArch[] = { "Use Default", "Native architecture of build machine", "Universal Binary (32-bit)", "Universal Binary (64-bit)", "64-bit Intel", 0 };
  815. const char* osxArchValues[] = { osxArch_Default, osxArch_Native, osxArch_32BitUniversal, osxArch_64BitUniversal, osxArch_64Bit, 0 };
  816. if (getMacArchitecture().toString().isEmpty())
  817. getMacArchitecture() = osxArch_Default;
  818. props.add (new ChoicePropertyComponent (getMacArchitecture(), "OSX Architecture", StringArray (osxArch), Array<var> (osxArchValues)));
  819. props.getLast()->setTooltip ("The type of OSX binary that will be produced.");
  820. for (int i = props.size(); --i >= 0;)
  821. props.getUnchecked(i)->setPreferredHeight (22);
  822. }
  823. StringPairArray Project::BuildConfiguration::getAllPreprocessorDefs() const
  824. {
  825. return mergePreprocessorDefs (project->getPreprocessorDefs(),
  826. parsePreprocessorDefs (getBuildConfigPreprocessorDefs().toString()));
  827. }
  828. StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  829. {
  830. StringArray s;
  831. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  832. return s;
  833. }
  834. //==============================================================================
  835. ValueTree Project::getExporters()
  836. {
  837. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  838. if (! exporters.isValid())
  839. {
  840. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  841. exporters = getExporters();
  842. }
  843. return exporters;
  844. }
  845. int Project::getNumExporters()
  846. {
  847. return getExporters().getNumChildren();
  848. }
  849. ProjectExporter* Project::createExporter (int index)
  850. {
  851. jassert (index >= 0 && index < getNumExporters());
  852. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  853. }
  854. void Project::addNewExporter (const String& exporterName)
  855. {
  856. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  857. ValueTree exporters (getExporters());
  858. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  859. }
  860. void Project::deleteExporter (int index)
  861. {
  862. ValueTree exporters (getExporters());
  863. exporters.removeChild (index, getUndoManagerFor (exporters));
  864. }
  865. void Project::createDefaultExporters()
  866. {
  867. ValueTree exporters (getExporters());
  868. exporters.removeAllChildren (getUndoManagerFor (exporters));
  869. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  870. for (int i = 0; i < exporterNames.size(); ++i)
  871. addNewExporter (exporterNames[i]);
  872. }
  873. //==============================================================================
  874. String Project::getFileTemplate (const String& templateName)
  875. {
  876. int dataSize;
  877. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  878. if (data == nullptr)
  879. {
  880. jassertfalse;
  881. return String::empty;
  882. }
  883. return String::fromUTF8 (data, dataSize);
  884. }