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.

1138 lines
37KB

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