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.

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