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.

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