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.

1162 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. Project::Item Project::Item::findItemForFile (const File& file) const
  427. {
  428. if (getFile() == file)
  429. return *this;
  430. if (isGroup())
  431. {
  432. for (int i = getNumChildren(); --i >= 0;)
  433. {
  434. Item found (getChild(i).findItemForFile (file));
  435. if (found.isValid())
  436. return found;
  437. }
  438. }
  439. return Item (getProject(), ValueTree::invalid);
  440. }
  441. File Project::Item::determineGroupFolder() const
  442. {
  443. jassert (isGroup());
  444. File f;
  445. for (int i = 0; i < getNumChildren(); ++i)
  446. {
  447. f = getChild(i).getFile();
  448. if (f.exists())
  449. return f.getParentDirectory();
  450. }
  451. Item parent (getParent());
  452. if (parent != *this)
  453. {
  454. f = parent.determineGroupFolder();
  455. if (f.getChildFile (getName().toString()).isDirectory())
  456. f = f.getChildFile (getName().toString());
  457. }
  458. else
  459. {
  460. f = getProject().getFile().getParentDirectory();
  461. if (f.getChildFile ("Source").isDirectory())
  462. f = f.getChildFile ("Source");
  463. }
  464. return f;
  465. }
  466. void Project::Item::initialiseNodeValues()
  467. {
  468. if (! node.hasProperty (Ids::id_))
  469. setID (createAlphaNumericUID());
  470. if (isFile())
  471. {
  472. node.setProperty (Ids::name, getFile().getFileName(), 0);
  473. }
  474. else if (isGroup())
  475. {
  476. for (int i = getNumChildren(); --i >= 0;)
  477. getChild(i).initialiseNodeValues();
  478. }
  479. }
  480. Value Project::Item::getName() const
  481. {
  482. return node.getPropertyAsValue (Ids::name, getUndoManager());
  483. }
  484. void Project::Item::addChild (const Item& newChild, int insertIndex)
  485. {
  486. node.addChild (newChild.getNode(), insertIndex, getUndoManager());
  487. }
  488. void Project::Item::removeItemFromProject()
  489. {
  490. node.getParent().removeChild (node, getUndoManager());
  491. }
  492. Project::Item Project::Item::getParent() const
  493. {
  494. if (isMainGroup() || ! isGroup())
  495. return *this;
  496. return Item (getProject(), node.getParent());
  497. }
  498. struct ItemSorter
  499. {
  500. static int compareElements (const ValueTree& first, const ValueTree& second)
  501. {
  502. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  503. }
  504. };
  505. struct ItemSorterWithGroupsAtStart
  506. {
  507. static int compareElements (const ValueTree& first, const ValueTree& second)
  508. {
  509. const bool firstIsGroup = first.hasType (Tags::group);
  510. const bool secondIsGroup = second.hasType (Tags::group);
  511. if (firstIsGroup == secondIsGroup)
  512. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  513. else
  514. return firstIsGroup ? -1 : 1;
  515. }
  516. };
  517. void Project::Item::sortAlphabetically (bool keepGroupsAtStart)
  518. {
  519. if (keepGroupsAtStart)
  520. {
  521. ItemSorterWithGroupsAtStart sorter;
  522. node.sort (sorter, getUndoManager(), true);
  523. }
  524. else
  525. {
  526. ItemSorter sorter;
  527. node.sort (sorter, getUndoManager(), true);
  528. }
  529. }
  530. Project::Item Project::Item::getOrCreateSubGroup (const String& name)
  531. {
  532. for (int i = node.getNumChildren(); --i >= 0;)
  533. {
  534. const ValueTree child (node.getChild (i));
  535. if (child.getProperty (Ids::name) == name && child.hasType (Tags::group))
  536. return Item (getProject(), child);
  537. }
  538. return addNewSubGroup (name, -1);
  539. }
  540. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  541. {
  542. Item group (createGroup (getProject(), name, createGUID (getID() + name + String (getNumChildren()))));
  543. jassert (canContain (group));
  544. addChild (group, insertIndex);
  545. return group;
  546. }
  547. bool Project::Item::addFile (const File& file, int insertIndex, const bool shouldCompile)
  548. {
  549. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  550. return false;
  551. if (file.isDirectory())
  552. {
  553. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  554. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  555. while (iter.next())
  556. {
  557. if (! getProject().getMainGroup().findItemForFile (iter.getFile()).isValid())
  558. group.addFile (iter.getFile(), -1, shouldCompile);
  559. }
  560. group.sortAlphabetically (false);
  561. }
  562. else if (file.existsAsFile())
  563. {
  564. if (! getProject().getMainGroup().findItemForFile (file).isValid())
  565. addFileUnchecked (file, insertIndex, shouldCompile);
  566. }
  567. else
  568. {
  569. jassertfalse;
  570. }
  571. return true;
  572. }
  573. void Project::Item::addFileUnchecked (const File& file, int insertIndex, const bool shouldCompile)
  574. {
  575. Item item (getProject(), ValueTree (Tags::file));
  576. item.initialiseNodeValues();
  577. item.getName() = file.getFileName();
  578. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  579. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  580. if (canContain (item))
  581. {
  582. item.setFile (file);
  583. addChild (item, insertIndex);
  584. }
  585. }
  586. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  587. {
  588. Item item (getProject(), ValueTree (Tags::file));
  589. item.initialiseNodeValues();
  590. item.getName() = file.getFileName();
  591. item.getShouldCompileValue() = shouldCompile;
  592. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  593. if (canContain (item))
  594. {
  595. item.setFile (file);
  596. addChild (item, insertIndex);
  597. return true;
  598. }
  599. return false;
  600. }
  601. const Drawable* Project::Item::getIcon() const
  602. {
  603. if (isFile())
  604. {
  605. if (isImageFile())
  606. return StoredSettings::getInstance()->getImageFileIcon();
  607. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  608. }
  609. else if (isMainGroup())
  610. {
  611. return &(getProject().mainProjectIcon);
  612. }
  613. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  614. }
  615. //==============================================================================
  616. ValueTree Project::getConfigNode()
  617. {
  618. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  619. }
  620. const char* const Project::configFlagDefault = "default";
  621. const char* const Project::configFlagEnabled = "enabled";
  622. const char* const Project::configFlagDisabled = "disabled";
  623. Value Project::getConfigFlag (const String& name)
  624. {
  625. const ValueTree configNode (getConfigNode());
  626. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  627. if (v.getValue().toString().isEmpty())
  628. v = configFlagDefault;
  629. return v;
  630. }
  631. bool Project::isConfigFlagEnabled (const String& name) const
  632. {
  633. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  634. }
  635. void Project::sanitiseConfigFlags()
  636. {
  637. ValueTree configNode (getConfigNode());
  638. for (int i = configNode.getNumProperties(); --i >= 0;)
  639. {
  640. const var value (configNode [configNode.getPropertyName(i)]);
  641. if (value != configFlagEnabled && value != configFlagDisabled)
  642. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  643. }
  644. }
  645. //==============================================================================
  646. ValueTree Project::getModulesNode()
  647. {
  648. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  649. }
  650. bool Project::isModuleEnabled (const String& moduleID) const
  651. {
  652. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  653. for (int i = 0; i < modules.getNumChildren(); ++i)
  654. if (modules.getChild(i) [Ids::id_] == moduleID)
  655. return true;
  656. return false;
  657. }
  658. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  659. {
  660. return getModulesNode().getChildWithProperty (Ids::id_, moduleID)
  661. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  662. }
  663. void Project::addModule (const String& moduleID)
  664. {
  665. if (! isModuleEnabled (moduleID))
  666. {
  667. ValueTree module (Tags::module);
  668. module.setProperty (Ids::id_, moduleID, nullptr);
  669. ValueTree modules (getModulesNode());
  670. modules.addChild (module, -1, getUndoManagerFor (modules));
  671. shouldShowAllModuleFilesInProject (moduleID) = true;
  672. }
  673. }
  674. void Project::removeModule (const String& moduleID)
  675. {
  676. ValueTree modules (getModulesNode());
  677. for (int i = 0; i < modules.getNumChildren(); ++i)
  678. if (modules.getChild(i) [Ids::id_] == moduleID)
  679. modules.removeChild (i, getUndoManagerFor (modules));
  680. }
  681. void Project::createRequiredModules (const ModuleList& availableModules, OwnedArray<LibraryModule>& modules) const
  682. {
  683. for (int i = 0; i < availableModules.modules.size(); ++i)
  684. if (isModuleEnabled (availableModules.modules.getUnchecked(i)->uid))
  685. modules.add (availableModules.modules.getUnchecked(i)->create());
  686. }
  687. //==============================================================================
  688. ValueTree Project::getConfigurations() const
  689. {
  690. return projectRoot.getChildWithName (Tags::configurations);
  691. }
  692. int Project::getNumConfigurations() const
  693. {
  694. return getConfigurations().getNumChildren();
  695. }
  696. Project::BuildConfiguration Project::getConfiguration (int index)
  697. {
  698. jassert (index < getConfigurations().getNumChildren());
  699. return BuildConfiguration (this, getConfigurations().getChild (index));
  700. }
  701. bool Project::hasConfigurationNamed (const String& name) const
  702. {
  703. const ValueTree configs (getConfigurations());
  704. for (int i = configs.getNumChildren(); --i >= 0;)
  705. if (configs.getChild(i) [Ids::name].toString() == name)
  706. return true;
  707. return false;
  708. }
  709. String Project::getUniqueConfigName (String name) const
  710. {
  711. String nameRoot (name);
  712. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  713. nameRoot = nameRoot.dropLastCharacters (1);
  714. nameRoot = nameRoot.trim();
  715. int suffix = 2;
  716. while (hasConfigurationNamed (name))
  717. name = nameRoot + " " + String (suffix++);
  718. return name;
  719. }
  720. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  721. {
  722. const String configName (getUniqueConfigName (configToCopy != nullptr ? configToCopy->config [Ids::name].toString()
  723. : "New Build Configuration"));
  724. ValueTree configs (getConfigurations());
  725. if (! configs.isValid())
  726. {
  727. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  728. configs = getConfigurations();
  729. }
  730. ValueTree newConfig (Tags::configuration);
  731. if (configToCopy != nullptr)
  732. newConfig = configToCopy->config.createCopy();
  733. newConfig.setProperty (Ids::name, configName, 0);
  734. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  735. }
  736. void Project::deleteConfiguration (int index)
  737. {
  738. ValueTree configs (getConfigurations());
  739. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  740. }
  741. void Project::createDefaultConfigs()
  742. {
  743. for (int i = 0; i < 2; ++i)
  744. {
  745. addNewConfiguration (nullptr);
  746. BuildConfiguration config = getConfiguration (i);
  747. const bool debugConfig = i == 0;
  748. config.getName() = debugConfig ? "Debug" : "Release";
  749. config.isDebug() = debugConfig;
  750. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  751. config.getTargetBinaryName() = getProjectFilenameRoot();
  752. }
  753. }
  754. //==============================================================================
  755. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  756. : project (project_),
  757. config (configNode)
  758. {
  759. }
  760. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  761. : project (other.project),
  762. config (other.config)
  763. {
  764. }
  765. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  766. {
  767. project = other.project;
  768. config = other.config;
  769. return *this;
  770. }
  771. Project::BuildConfiguration::~BuildConfiguration()
  772. {
  773. }
  774. String Project::BuildConfiguration::getGCCOptimisationFlag() const
  775. {
  776. const int level = (int) getOptimisationLevel().getValue();
  777. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  778. }
  779. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  780. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  781. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  782. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  783. const char* const Project::BuildConfiguration::osxArch_Default = "default";
  784. const char* const Project::BuildConfiguration::osxArch_Native = "Native";
  785. const char* const Project::BuildConfiguration::osxArch_32BitUniversal = "32BitUniversal";
  786. const char* const Project::BuildConfiguration::osxArch_64BitUniversal = "64BitUniversal";
  787. const char* const Project::BuildConfiguration::osxArch_64Bit = "64BitIntel";
  788. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  789. {
  790. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  791. props.getLast()->setTooltip ("The name of this configuration.");
  792. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  793. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  794. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  795. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  796. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  797. props.getLast()->setTooltip ("The optimisation level for this configuration");
  798. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  799. 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.");
  800. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  801. 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.");
  802. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  803. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  804. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  805. 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.");
  806. if (getMacSDKVersion().toString().isEmpty())
  807. getMacSDKVersion() = osxVersionDefault;
  808. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  809. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  810. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  811. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  812. if (getMacCompatibilityVersion().toString().isEmpty())
  813. getMacCompatibilityVersion() = osxVersionDefault;
  814. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  815. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  816. const char* osxArch[] = { "Use Default", "Native architecture of build machine", "Universal Binary (32-bit)", "Universal Binary (64-bit)", "64-bit Intel", 0 };
  817. const char* osxArchValues[] = { osxArch_Default, osxArch_Native, osxArch_32BitUniversal, osxArch_64BitUniversal, osxArch_64Bit, 0 };
  818. if (getMacArchitecture().toString().isEmpty())
  819. getMacArchitecture() = osxArch_Default;
  820. props.add (new ChoicePropertyComponent (getMacArchitecture(), "OSX Architecture", StringArray (osxArch), Array<var> (osxArchValues)));
  821. props.getLast()->setTooltip ("The type of OSX binary that will be produced.");
  822. for (int i = props.size(); --i >= 0;)
  823. props.getUnchecked(i)->setPreferredHeight (22);
  824. }
  825. StringPairArray Project::BuildConfiguration::getAllPreprocessorDefs() const
  826. {
  827. return mergePreprocessorDefs (project->getPreprocessorDefs(),
  828. parsePreprocessorDefs (getBuildConfigPreprocessorDefs().toString()));
  829. }
  830. StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  831. {
  832. StringArray s;
  833. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  834. return s;
  835. }
  836. //==============================================================================
  837. ValueTree Project::getExporters()
  838. {
  839. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  840. if (! exporters.isValid())
  841. {
  842. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  843. exporters = getExporters();
  844. }
  845. return exporters;
  846. }
  847. int Project::getNumExporters()
  848. {
  849. return getExporters().getNumChildren();
  850. }
  851. ProjectExporter* Project::createExporter (int index)
  852. {
  853. jassert (index >= 0 && index < getNumExporters());
  854. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  855. }
  856. void Project::addNewExporter (const String& exporterName)
  857. {
  858. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  859. ValueTree exporters (getExporters());
  860. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  861. }
  862. void Project::deleteExporter (int index)
  863. {
  864. ValueTree exporters (getExporters());
  865. exporters.removeChild (index, getUndoManagerFor (exporters));
  866. }
  867. void Project::createDefaultExporters()
  868. {
  869. ValueTree exporters (getExporters());
  870. exporters.removeAllChildren (getUndoManagerFor (exporters));
  871. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  872. for (int i = 0; i < exporterNames.size(); ++i)
  873. addNewExporter (exporterNames[i]);
  874. }
  875. //==============================================================================
  876. String Project::getFileTemplate (const String& templateName)
  877. {
  878. int dataSize;
  879. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  880. if (data == nullptr)
  881. {
  882. jassertfalse;
  883. return String::empty;
  884. }
  885. return String::fromUTF8 (data, dataSize);
  886. }
  887. //==============================================================================
  888. void Project::resaveJucerFile (const File& file)
  889. {
  890. if (! file.exists())
  891. {
  892. std::cout << "The file " << file.getFullPathName() << " doesn't exist!" << std::endl;
  893. return;
  894. }
  895. if (! file.hasFileExtension (Project::projectFileExtension))
  896. {
  897. std::cout << file.getFullPathName() << " isn't a valid jucer project file!" << std::endl;
  898. return;
  899. }
  900. Project newDoc (file);
  901. if (! newDoc.loadFrom (file, true))
  902. {
  903. std::cout << "Failed to load the project file: " << file.getFullPathName() << std::endl;
  904. return;
  905. }
  906. std::cout << "The Introjucer - Re-saving file: " << file.getFullPathName() << std::endl;
  907. String error (newDoc.saveDocument (file));
  908. if (error.isNotEmpty())
  909. {
  910. std::cout << "Error when writing project: " << error << std::endl;
  911. return;
  912. }
  913. }