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.

1151 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 "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. {
  566. Item item (getProject(), ValueTree (Tags::file));
  567. item.initialiseNodeValues();
  568. item.getName() = file.getFileName();
  569. item.getShouldCompileValue() = shouldCompile && file.hasFileExtension ("cpp;mm;c;m;cc;cxx;r");
  570. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  571. if (canContain (item))
  572. {
  573. item.setFile (file);
  574. addChild (item, insertIndex);
  575. }
  576. }
  577. }
  578. else
  579. {
  580. jassertfalse;
  581. }
  582. return true;
  583. }
  584. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  585. {
  586. Item item (getProject(), ValueTree (Tags::file));
  587. item.initialiseNodeValues();
  588. item.getName() = file.getFileName();
  589. item.getShouldCompileValue() = shouldCompile;
  590. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  591. if (canContain (item))
  592. {
  593. item.setFile (file);
  594. addChild (item, insertIndex);
  595. return true;
  596. }
  597. return false;
  598. }
  599. const Drawable* Project::Item::getIcon() const
  600. {
  601. if (isFile())
  602. {
  603. if (isImageFile())
  604. return StoredSettings::getInstance()->getImageFileIcon();
  605. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  606. }
  607. else if (isMainGroup())
  608. {
  609. return &(getProject().mainProjectIcon);
  610. }
  611. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  612. }
  613. //==============================================================================
  614. ValueTree Project::getConfigNode()
  615. {
  616. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  617. }
  618. const char* const Project::configFlagDefault = "default";
  619. const char* const Project::configFlagEnabled = "enabled";
  620. const char* const Project::configFlagDisabled = "disabled";
  621. Value Project::getConfigFlag (const String& name)
  622. {
  623. const ValueTree configNode (getConfigNode());
  624. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  625. if (v.getValue().toString().isEmpty())
  626. v = configFlagDefault;
  627. return v;
  628. }
  629. bool Project::isConfigFlagEnabled (const String& name) const
  630. {
  631. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  632. }
  633. void Project::sanitiseConfigFlags()
  634. {
  635. ValueTree configNode (getConfigNode());
  636. for (int i = configNode.getNumProperties(); --i >= 0;)
  637. {
  638. const var value (configNode [configNode.getPropertyName(i)]);
  639. if (value != configFlagEnabled && value != configFlagDisabled)
  640. configNode.removeProperty (configNode.getPropertyName(i), getUndoManagerFor (configNode));
  641. }
  642. }
  643. //==============================================================================
  644. ValueTree Project::getModulesNode()
  645. {
  646. return projectRoot.getOrCreateChildWithName (Tags::modulesGroup, nullptr);
  647. }
  648. bool Project::isModuleEnabled (const String& moduleID) const
  649. {
  650. ValueTree modules (projectRoot.getChildWithName (Tags::modulesGroup));
  651. for (int i = 0; i < modules.getNumChildren(); ++i)
  652. if (modules.getChild(i) [Ids::id_] == moduleID)
  653. return true;
  654. return false;
  655. }
  656. Value Project::shouldShowAllModuleFilesInProject (const String& moduleID)
  657. {
  658. return getModulesNode().getChildWithProperty (Ids::id_, moduleID)
  659. .getPropertyAsValue (Ids::showAllCode, getUndoManagerFor (getModulesNode()));
  660. }
  661. void Project::addModule (const String& moduleID)
  662. {
  663. if (! isModuleEnabled (moduleID))
  664. {
  665. ValueTree module (Tags::module);
  666. module.setProperty (Ids::id_, moduleID, nullptr);
  667. ValueTree modules (getModulesNode());
  668. modules.addChild (module, -1, getUndoManagerFor (modules));
  669. shouldShowAllModuleFilesInProject (moduleID) = true;
  670. }
  671. }
  672. void Project::removeModule (const String& moduleID)
  673. {
  674. ValueTree modules (getModulesNode());
  675. for (int i = 0; i < modules.getNumChildren(); ++i)
  676. if (modules.getChild(i) [Ids::id_] == moduleID)
  677. modules.removeChild (i, getUndoManagerFor (modules));
  678. }
  679. //==============================================================================
  680. ValueTree Project::getConfigurations() const
  681. {
  682. return projectRoot.getChildWithName (Tags::configurations);
  683. }
  684. int Project::getNumConfigurations() const
  685. {
  686. return getConfigurations().getNumChildren();
  687. }
  688. Project::BuildConfiguration Project::getConfiguration (int index)
  689. {
  690. jassert (index < getConfigurations().getNumChildren());
  691. return BuildConfiguration (this, getConfigurations().getChild (index));
  692. }
  693. bool Project::hasConfigurationNamed (const String& name) const
  694. {
  695. const ValueTree configs (getConfigurations());
  696. for (int i = configs.getNumChildren(); --i >= 0;)
  697. if (configs.getChild(i) [Ids::name].toString() == name)
  698. return true;
  699. return false;
  700. }
  701. String Project::getUniqueConfigName (String name) const
  702. {
  703. String nameRoot (name);
  704. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  705. nameRoot = nameRoot.dropLastCharacters (1);
  706. nameRoot = nameRoot.trim();
  707. int suffix = 2;
  708. while (hasConfigurationNamed (name))
  709. name = nameRoot + " " + String (suffix++);
  710. return name;
  711. }
  712. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  713. {
  714. const String configName (getUniqueConfigName (configToCopy != nullptr ? configToCopy->config [Ids::name].toString()
  715. : "New Build Configuration"));
  716. ValueTree configs (getConfigurations());
  717. if (! configs.isValid())
  718. {
  719. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  720. configs = getConfigurations();
  721. }
  722. ValueTree newConfig (Tags::configuration);
  723. if (configToCopy != nullptr)
  724. newConfig = configToCopy->config.createCopy();
  725. newConfig.setProperty (Ids::name, configName, 0);
  726. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  727. }
  728. void Project::deleteConfiguration (int index)
  729. {
  730. ValueTree configs (getConfigurations());
  731. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  732. }
  733. void Project::createDefaultConfigs()
  734. {
  735. for (int i = 0; i < 2; ++i)
  736. {
  737. addNewConfiguration (nullptr);
  738. BuildConfiguration config = getConfiguration (i);
  739. const bool debugConfig = i == 0;
  740. config.getName() = debugConfig ? "Debug" : "Release";
  741. config.isDebug() = debugConfig;
  742. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  743. config.getTargetBinaryName() = getProjectFilenameRoot();
  744. }
  745. }
  746. //==============================================================================
  747. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  748. : project (project_),
  749. config (configNode)
  750. {
  751. }
  752. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  753. : project (other.project),
  754. config (other.config)
  755. {
  756. }
  757. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  758. {
  759. project = other.project;
  760. config = other.config;
  761. return *this;
  762. }
  763. Project::BuildConfiguration::~BuildConfiguration()
  764. {
  765. }
  766. String Project::BuildConfiguration::getGCCOptimisationFlag() const
  767. {
  768. const int level = (int) getOptimisationLevel().getValue();
  769. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  770. }
  771. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  772. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  773. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  774. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  775. const char* const Project::BuildConfiguration::osxArch_Default = "default";
  776. const char* const Project::BuildConfiguration::osxArch_Native = "Native";
  777. const char* const Project::BuildConfiguration::osxArch_32BitUniversal = "32BitUniversal";
  778. const char* const Project::BuildConfiguration::osxArch_64BitUniversal = "64BitUniversal";
  779. const char* const Project::BuildConfiguration::osxArch_64Bit = "64BitIntel";
  780. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  781. {
  782. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  783. props.getLast()->setTooltip ("The name of this configuration.");
  784. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  785. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  786. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  787. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  788. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  789. props.getLast()->setTooltip ("The optimisation level for this configuration");
  790. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  791. 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.");
  792. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  793. 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.");
  794. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  795. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  796. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  797. 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.");
  798. if (getMacSDKVersion().toString().isEmpty())
  799. getMacSDKVersion() = osxVersionDefault;
  800. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  801. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  802. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  803. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  804. if (getMacCompatibilityVersion().toString().isEmpty())
  805. getMacCompatibilityVersion() = osxVersionDefault;
  806. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  807. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  808. const char* osxArch[] = { "Use Default", "Native architecture of build machine", "Universal Binary (32-bit)", "Universal Binary (64-bit)", "64-bit Intel", 0 };
  809. const char* osxArchValues[] = { osxArch_Default, osxArch_Native, osxArch_32BitUniversal, osxArch_64BitUniversal, osxArch_64Bit, 0 };
  810. if (getMacArchitecture().toString().isEmpty())
  811. getMacArchitecture() = osxArch_Default;
  812. props.add (new ChoicePropertyComponent (getMacArchitecture(), "OSX Architecture", StringArray (osxArch), Array<var> (osxArchValues)));
  813. props.getLast()->setTooltip ("The type of OSX binary that will be produced.");
  814. for (int i = props.size(); --i >= 0;)
  815. props.getUnchecked(i)->setPreferredHeight (22);
  816. }
  817. StringPairArray Project::BuildConfiguration::getAllPreprocessorDefs() const
  818. {
  819. return mergePreprocessorDefs (project->getPreprocessorDefs(),
  820. parsePreprocessorDefs (getBuildConfigPreprocessorDefs().toString()));
  821. }
  822. StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  823. {
  824. StringArray s;
  825. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  826. return s;
  827. }
  828. //==============================================================================
  829. ValueTree Project::getExporters()
  830. {
  831. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  832. if (! exporters.isValid())
  833. {
  834. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  835. exporters = getExporters();
  836. }
  837. return exporters;
  838. }
  839. int Project::getNumExporters()
  840. {
  841. return getExporters().getNumChildren();
  842. }
  843. ProjectExporter* Project::createExporter (int index)
  844. {
  845. jassert (index >= 0 && index < getNumExporters());
  846. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  847. }
  848. void Project::addNewExporter (const String& exporterName)
  849. {
  850. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterName));
  851. ValueTree exporters (getExporters());
  852. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  853. }
  854. void Project::deleteExporter (int index)
  855. {
  856. ValueTree exporters (getExporters());
  857. exporters.removeChild (index, getUndoManagerFor (exporters));
  858. }
  859. void Project::createDefaultExporters()
  860. {
  861. ValueTree exporters (getExporters());
  862. exporters.removeAllChildren (getUndoManagerFor (exporters));
  863. const StringArray exporterNames (ProjectExporter::getDefaultExporters());
  864. for (int i = 0; i < exporterNames.size(); ++i)
  865. addNewExporter (exporterNames[i]);
  866. }
  867. //==============================================================================
  868. String Project::getFileTemplate (const String& templateName)
  869. {
  870. int dataSize;
  871. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  872. if (data == nullptr)
  873. {
  874. jassertfalse;
  875. return String::empty;
  876. }
  877. return String::fromUTF8 (data, dataSize);
  878. }
  879. //==============================================================================
  880. void Project::resaveJucerFile (const File& file)
  881. {
  882. if (! file.exists())
  883. {
  884. std::cout << "The file " << file.getFullPathName() << " doesn't exist!" << std::endl;
  885. return;
  886. }
  887. if (! file.hasFileExtension (Project::projectFileExtension))
  888. {
  889. std::cout << file.getFullPathName() << " isn't a valid jucer project file!" << std::endl;
  890. return;
  891. }
  892. Project newDoc (file);
  893. if (! newDoc.loadFrom (file, true))
  894. {
  895. std::cout << "Failed to load the project file: " << file.getFullPathName() << std::endl;
  896. return;
  897. }
  898. std::cout << "The Introjucer - Re-saving file: " << file.getFullPathName() << std::endl;
  899. String error (newDoc.saveDocument (file));
  900. if (error.isNotEmpty())
  901. {
  902. std::cout << "Error when writing project: " << error << std::endl;
  903. return;
  904. }
  905. }