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.

1149 lines
42KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. }
  36. const char* Project::projectFileExtension = ".jucer";
  37. //==============================================================================
  38. Project::Project (const File& file_)
  39. : FileBasedDocument (projectFileExtension,
  40. String ("*") + projectFileExtension,
  41. "Choose a Jucer project to load",
  42. "Save Jucer project"),
  43. projectRoot (Tags::projectRoot)
  44. {
  45. setFile (file_);
  46. setMissingDefaultValues();
  47. setChangedFlag (false);
  48. mainProjectIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  49. projectRoot.addListener (this);
  50. }
  51. Project::~Project()
  52. {
  53. projectRoot.removeListener (this);
  54. OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false);
  55. }
  56. //==============================================================================
  57. void Project::setTitle (const String& newTitle)
  58. {
  59. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  60. getMainGroup().getName() = newTitle;
  61. }
  62. const String Project::getDocumentTitle()
  63. {
  64. return getProjectName().toString();
  65. }
  66. void Project::updateProjectSettings()
  67. {
  68. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  69. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  70. }
  71. void Project::setMissingDefaultValues()
  72. {
  73. if (! projectRoot.hasProperty (Ids::id_))
  74. projectRoot.setProperty (Ids::id_, createAlphaNumericUID(), nullptr);
  75. // Create main file group if missing
  76. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  77. {
  78. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  79. projectRoot.addChild (mainGroup.getNode(), 0, 0);
  80. }
  81. getMainGroup().initialiseNodeValues();
  82. if (getDocumentTitle().isEmpty())
  83. setTitle ("Juce Project");
  84. if (! projectRoot.hasProperty (Ids::projectType))
  85. getProjectTypeValue() = ProjectType::getGUIAppTypeName();
  86. if (! projectRoot.hasProperty (Ids::version))
  87. getVersion() = "1.0.0";
  88. if (! projectRoot.hasProperty (Ids::juceLinkage))
  89. getJuceLinkageModeValue() = useAmalgamatedJuceViaMultipleTemplates;
  90. const String juceFolderPath (getRelativePathForFile (StoredSettings::getInstance()->getLastKnownJuceFolder()));
  91. // Create configs group
  92. if (! projectRoot.getChildWithName (Tags::configurations).isValid())
  93. {
  94. projectRoot.addChild (ValueTree (Tags::configurations), 0, 0);
  95. createDefaultConfigs();
  96. }
  97. if (! projectRoot.getChildWithName (Tags::exporters).isValid())
  98. createDefaultExporters();
  99. const String sanitisedProjectName (CodeHelpers::makeValidIdentifier (getProjectName().toString(), false, true, false));
  100. if (! projectRoot.hasProperty (Ids::buildVST))
  101. {
  102. shouldBuildVST() = true;
  103. shouldBuildRTAS() = false;
  104. shouldBuildAU() = true;
  105. getPluginName() = getProjectName().toString();
  106. getPluginDesc() = getProjectName().toString();
  107. getPluginManufacturer() = "yourcompany";
  108. getPluginManufacturerCode() = "Manu";
  109. getPluginCode() = "Plug";
  110. getPluginChannelConfigs() = "{1, 1}, {2, 2}";
  111. getPluginIsSynth() = false;
  112. getPluginWantsMidiInput() = false;
  113. getPluginProducesMidiOut() = false;
  114. getPluginSilenceInProducesSilenceOut() = false;
  115. getPluginTailLengthSeconds() = 0;
  116. getPluginEditorNeedsKeyFocus() = false;
  117. getPluginAUExportPrefix() = sanitisedProjectName + "AU";
  118. getPluginAUCocoaViewClassName() = sanitisedProjectName + "AU_V1";
  119. getPluginRTASCategory() = String::empty;
  120. }
  121. if (! projectRoot.hasProperty (Ids::bundleIdentifier))
  122. setBundleIdentifierToDefault();
  123. }
  124. //==============================================================================
  125. const String Project::loadDocument (const File& file)
  126. {
  127. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  128. if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString()))
  129. return "Not a valid Jucer project!";
  130. ValueTree newTree (ValueTree::fromXml (*xml));
  131. if (! newTree.hasType (Tags::projectRoot))
  132. return "The document contains errors and couldn't be parsed!";
  133. StoredSettings::getInstance()->recentFiles.addFile (file);
  134. StoredSettings::getInstance()->flush();
  135. projectRoot = newTree;
  136. setMissingDefaultValues();
  137. return String::empty;
  138. }
  139. const String Project::saveDocument (const File& file)
  140. {
  141. updateProjectSettings();
  142. {
  143. // (getting these forces the values to be sanitised)
  144. OwnedArray <Project::ConfigFlag> flags;
  145. getAllConfigFlags (flags);
  146. }
  147. if (FileHelpers::isJuceFolder (getLocalJuceFolder()))
  148. StoredSettings::getInstance()->setLastKnownJuceFolder (getLocalJuceFolder().getFullPathName());
  149. StoredSettings::getInstance()->recentFiles.addFile (file);
  150. ProjectSaver saver (*this, file);
  151. return saver.save();
  152. }
  153. //==============================================================================
  154. File Project::lastDocumentOpened;
  155. const File Project::getLastDocumentOpened()
  156. {
  157. return lastDocumentOpened;
  158. }
  159. void Project::setLastDocumentOpened (const File& file)
  160. {
  161. lastDocumentOpened = file;
  162. }
  163. //==============================================================================
  164. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  165. {
  166. if (getProjectType().isLibrary())
  167. getJuceLinkageModeValue() = notLinkedToJuce;
  168. changed();
  169. }
  170. void Project::valueTreeChildAdded (ValueTree& parentTree, ValueTree& childWhichHasBeenAdded)
  171. {
  172. changed();
  173. }
  174. void Project::valueTreeChildRemoved (ValueTree& parentTree, ValueTree& childWhichHasBeenRemoved)
  175. {
  176. changed();
  177. }
  178. void Project::valueTreeChildOrderChanged (ValueTree& parentTree)
  179. {
  180. changed();
  181. }
  182. void Project::valueTreeParentChanged (ValueTree& tree)
  183. {
  184. }
  185. //==============================================================================
  186. File Project::resolveFilename (String filename) const
  187. {
  188. if (filename.isEmpty())
  189. return File::nonexistent;
  190. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename)
  191. .replaceCharacter ('\\', '/');
  192. if (File::isAbsolutePath (filename))
  193. return File (filename);
  194. return getFile().getSiblingFile (filename);
  195. }
  196. String Project::getRelativePathForFile (const File& file) const
  197. {
  198. String filename (file.getFullPathName());
  199. File relativePathBase (getFile().getParentDirectory());
  200. String p1 (relativePathBase.getFullPathName());
  201. String p2 (file.getFullPathName());
  202. while (p1.startsWithChar (File::separator))
  203. p1 = p1.substring (1);
  204. while (p2.startsWithChar (File::separator))
  205. p2 = p2.substring (1);
  206. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  207. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  208. {
  209. filename = file.getRelativePathFrom (relativePathBase);
  210. }
  211. return filename;
  212. }
  213. //==============================================================================
  214. const ProjectType& Project::getProjectType() const
  215. {
  216. const ProjectType* type = ProjectType::findType (getProjectTypeValue().toString());
  217. jassert (type != nullptr);
  218. if (type == nullptr)
  219. {
  220. type = ProjectType::findType (ProjectType::getGUIAppTypeName());
  221. jassert (type != nullptr);
  222. }
  223. return *type;
  224. }
  225. const char* const Project::notLinkedToJuce = "none";
  226. const char* const Project::useLinkedJuce = "static";
  227. const char* const Project::useAmalgamatedJuce = "amalg_big";
  228. const char* const Project::useAmalgamatedJuceViaSingleTemplate = "amalg_template";
  229. const char* const Project::useAmalgamatedJuceViaMultipleTemplates = "amalg_multi";
  230. File Project::getLocalJuceFolder()
  231. {
  232. ScopedPointer <ProjectExporter> exp (ProjectExporter::createPlatformDefaultExporter (*this));
  233. if (exp != nullptr)
  234. {
  235. File f (resolveFilename (exp->getJuceFolder().toString()));
  236. if (FileHelpers::isJuceFolder (f))
  237. return f;
  238. }
  239. return StoredSettings::getInstance()->getLastKnownJuceFolder();
  240. }
  241. //==============================================================================
  242. void Project::createPropertyEditors (Array <PropertyComponent*>& props)
  243. {
  244. props.add (new TextPropertyComponent (getProjectName(), "Project Name", 256, false));
  245. props.getLast()->setTooltip ("The name of the project.");
  246. props.add (new TextPropertyComponent (getVersion(), "Project Version", 16, false));
  247. props.getLast()->setTooltip ("The project's version number, This should be in the format major.minor.point");
  248. {
  249. StringArray projectTypeNames;
  250. Array<var> projectTypeCodes;
  251. const Array<ProjectType*>& types = ProjectType::getAllTypes();
  252. for (int i = 0; i < types.size(); ++i)
  253. {
  254. projectTypeNames.add (types.getUnchecked(i)->getDescription());
  255. projectTypeCodes.add (types.getUnchecked(i)->getType());
  256. }
  257. props.add (new ChoicePropertyComponent (getProjectTypeValue(), "Project Type", projectTypeNames, projectTypeCodes));
  258. }
  259. const char* linkageTypes[] = { "Not linked to Juce", "Linked to Juce Static Library", "Include Juce Amalgamated Files", "Include Juce Source Code Directly (In a single file)", "Include Juce Source Code Directly (Split across several files)", 0 };
  260. const char* linkageTypeValues[] = { notLinkedToJuce, useLinkedJuce, useAmalgamatedJuce, useAmalgamatedJuceViaSingleTemplate, useAmalgamatedJuceViaMultipleTemplates, 0 };
  261. props.add (new ChoicePropertyComponent (getJuceLinkageModeValue(), "Juce Linkage Method", StringArray (linkageTypes), Array<var> (linkageTypeValues)));
  262. props.getLast()->setTooltip ("The method by which your project will be linked to Juce.");
  263. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false));
  264. props.getLast()->setTooltip ("A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  265. {
  266. OwnedArray<Project::Item> images;
  267. findAllImageItems (images);
  268. StringArray choices;
  269. Array<var> ids;
  270. choices.add ("<None>");
  271. ids.add (var::null);
  272. choices.add (String::empty);
  273. ids.add (var::null);
  274. for (int i = 0; i < images.size(); ++i)
  275. {
  276. choices.add (images.getUnchecked(i)->getName().toString());
  277. ids.add (images.getUnchecked(i)->getID());
  278. }
  279. props.add (new ChoicePropertyComponent (getSmallIconImageItemID(), "Icon (small)", choices, ids));
  280. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  281. props.add (new ChoicePropertyComponent (getBigIconImageItemID(), "Icon (large)", choices, ids));
  282. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  283. }
  284. if (getProjectType().isAudioPlugin())
  285. {
  286. props.add (new BooleanPropertyComponent (shouldBuildVST(), "Build VST", "Enabled"));
  287. props.getLast()->setTooltip ("Whether the project should produce a VST plugin.");
  288. props.add (new BooleanPropertyComponent (shouldBuildAU(), "Build AudioUnit", "Enabled"));
  289. props.getLast()->setTooltip ("Whether the project should produce an AudioUnit plugin.");
  290. props.add (new BooleanPropertyComponent (shouldBuildRTAS(), "Build RTAS", "Enabled"));
  291. props.getLast()->setTooltip ("Whether the project should produce an RTAS plugin.");
  292. }
  293. if (getProjectType().isAudioPlugin())
  294. {
  295. props.add (new TextPropertyComponent (getPluginName(), "Plugin Name", 128, false));
  296. props.getLast()->setTooltip ("The name of your plugin (keep it short!)");
  297. props.add (new TextPropertyComponent (getPluginDesc(), "Plugin Description", 256, false));
  298. props.getLast()->setTooltip ("A short description of your plugin.");
  299. props.add (new TextPropertyComponent (getPluginManufacturer(), "Plugin Manufacturer", 256, false));
  300. props.getLast()->setTooltip ("The name of your company (cannot be blank).");
  301. props.add (new TextPropertyComponent (getPluginManufacturerCode(), "Plugin Manufacturer Code", 4, false));
  302. props.getLast()->setTooltip ("A four-character unique ID for your company. Note that for AU compatibility, this must contain at least one upper-case letter!");
  303. props.add (new TextPropertyComponent (getPluginCode(), "Plugin Code", 4, false));
  304. props.getLast()->setTooltip ("A four-character unique ID for your plugin. Note that for AU compatibility, this must contain at least one upper-case letter!");
  305. props.add (new TextPropertyComponent (getPluginChannelConfigs(), "Plugin Channel Configurations", 256, false));
  306. props.getLast()->setTooltip ("This is the set of input/output channel configurations that your plugin can handle. The list is a comma-separated set of pairs of values in the form { numInputs, numOutputs }, and each "
  307. "pair indicates a valid configuration that the plugin can handle. So for example, {1, 1}, {2, 2} means that the plugin can be used in just two configurations: either with 1 input "
  308. "and 1 output, or with 2 inputs and 2 outputs.");
  309. props.add (new BooleanPropertyComponent (getPluginIsSynth(), "Plugin is a Synth", "Is a Synth"));
  310. props.getLast()->setTooltip ("Enable this if you want your plugin to be treated as a synth or generator. It doesn't make much difference to the plugin itself, but some hosts treat synths differently to other plugins.");
  311. props.add (new BooleanPropertyComponent (getPluginWantsMidiInput(), "Plugin Midi Input", "Plugin wants midi input"));
  312. props.getLast()->setTooltip ("Enable this if you want your plugin to accept midi messages.");
  313. props.add (new BooleanPropertyComponent (getPluginProducesMidiOut(), "Plugin Midi Output", "Plugin produces midi output"));
  314. props.getLast()->setTooltip ("Enable this if your plugin is going to produce midi messages.");
  315. props.add (new BooleanPropertyComponent (getPluginSilenceInProducesSilenceOut(), "Silence", "Silence in produces silence out"));
  316. props.getLast()->setTooltip ("Enable this if your plugin has no tail - i.e. if passing a silent buffer to it will always result in a silent buffer being produced.");
  317. props.add (new TextPropertyComponent (getPluginTailLengthSeconds(), "Tail Length (in seconds)", 12, false));
  318. props.getLast()->setTooltip ("This indicates the length, in seconds, of the plugin's tail. This information may or may not be used by the host.");
  319. props.add (new BooleanPropertyComponent (getPluginEditorNeedsKeyFocus(), "Key Focus", "Plugin editor requires keyboard focus"));
  320. props.getLast()->setTooltip ("Enable this if your plugin needs keyboard input - some hosts can be a bit funny about keyboard focus..");
  321. props.add (new TextPropertyComponent (getPluginAUExportPrefix(), "Plugin AU Export Prefix", 64, false));
  322. props.getLast()->setTooltip ("A prefix for the names of exported entry-point functions that the component exposes - typically this will be a version of your plugin's name that can be used as part of a C++ token.");
  323. props.add (new TextPropertyComponent (getPluginAUCocoaViewClassName(), "Plugin AU Cocoa View Name", 64, false));
  324. props.getLast()->setTooltip ("In an AU, this is the name of Cocoa class that creates the UI. Some hosts bizarrely display the class-name, so you might want to make it reflect your plugin. But the name must be "
  325. "UNIQUE to this exact version of your plugin, to avoid objective-C linkage mix-ups that happen when different plugins containing the same class-name are loaded simultaneously.");
  326. props.add (new TextPropertyComponent (getPluginRTASCategory(), "Plugin RTAS Category", 64, false));
  327. props.getLast()->setTooltip ("(Leave this blank if your plugin is a synth). This is one of the RTAS categories from FicPluginEnums.h, such as: ePlugInCategory_None, ePlugInCategory_EQ, ePlugInCategory_Dynamics, "
  328. "ePlugInCategory_PitchShift, ePlugInCategory_Reverb, ePlugInCategory_Delay, "
  329. "ePlugInCategory_Modulation, ePlugInCategory_Harmonic, ePlugInCategory_NoiseReduction, "
  330. "ePlugInCategory_Dither, ePlugInCategory_SoundField");
  331. }
  332. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  333. 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.");
  334. for (int i = props.size(); --i >= 0;)
  335. props.getUnchecked(i)->setPreferredHeight (22);
  336. }
  337. String Project::getVersionAsHex() const
  338. {
  339. StringArray configs;
  340. configs.addTokens (getVersion().toString(), ",.", String::empty);
  341. configs.trim();
  342. configs.removeEmptyStrings();
  343. int value = (configs[0].getIntValue() << 16) + (configs[1].getIntValue() << 8) + configs[2].getIntValue();
  344. if (configs.size() >= 4)
  345. value = (value << 8) + configs[3].getIntValue();
  346. return "0x" + String::toHexString (value);
  347. }
  348. Image Project::getBigIcon()
  349. {
  350. Item icon (getMainGroup().findItemWithID (getBigIconImageItemID().toString()));
  351. if (icon.isValid())
  352. return ImageCache::getFromFile (icon.getFile());
  353. return Image::null;
  354. }
  355. Image Project::getSmallIcon()
  356. {
  357. Item icon (getMainGroup().findItemWithID (getSmallIconImageItemID().toString()));
  358. if (icon.isValid())
  359. return ImageCache::getFromFile (icon.getFile());
  360. return Image::null;
  361. }
  362. StringPairArray Project::getPreprocessorDefs() const
  363. {
  364. return parsePreprocessorDefs (getProjectPreprocessorDefs().toString());
  365. }
  366. //==============================================================================
  367. Project::Item Project::getMainGroup()
  368. {
  369. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  370. }
  371. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  372. {
  373. if (item.isImageFile())
  374. {
  375. found.add (new Project::Item (item));
  376. }
  377. else if (item.isGroup())
  378. {
  379. for (int i = 0; i < item.getNumChildren(); ++i)
  380. findImages (item.getChild (i), found);
  381. }
  382. }
  383. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  384. {
  385. findImages (getMainGroup(), items);
  386. }
  387. //==============================================================================
  388. Project::Item::Item (Project& project_, const ValueTree& node_)
  389. : project (&project_), node (node_)
  390. {
  391. }
  392. Project::Item::Item (const Item& other)
  393. : project (other.project), node (other.node)
  394. {
  395. }
  396. Project::Item& Project::Item::operator= (const Project::Item& other)
  397. {
  398. project = other.project;
  399. node = other.node;
  400. return *this;
  401. }
  402. Project::Item::~Item()
  403. {
  404. }
  405. String Project::Item::getID() const { return node [Ids::id_]; }
  406. void Project::Item::setID (const String& newID) { node.setProperty (Ids::id_, newID, nullptr); }
  407. String Project::Item::getImageFileID() const { return "id:" + getID(); }
  408. Project::Item Project::Item::createGroup (Project& project, const String& name)
  409. {
  410. Item group (project, ValueTree (Tags::group));
  411. group.initialiseNodeValues();
  412. group.getName() = name;
  413. return group;
  414. }
  415. bool Project::Item::isFile() const { return node.hasType (Tags::file); }
  416. bool Project::Item::isGroup() const { return node.hasType (Tags::group) || isMainGroup(); }
  417. bool Project::Item::isMainGroup() const { return node.hasType (Tags::projectMainGroup); }
  418. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  419. Project::Item Project::Item::findItemWithID (const String& targetId) const
  420. {
  421. if (node [Ids::id_] == targetId)
  422. return *this;
  423. if (isGroup())
  424. {
  425. for (int i = getNumChildren(); --i >= 0;)
  426. {
  427. Item found (getChild(i).findItemWithID (targetId));
  428. if (found.isValid())
  429. return found;
  430. }
  431. }
  432. return Item (*project, ValueTree::invalid);
  433. }
  434. bool Project::Item::canContain (const Item& child) const
  435. {
  436. if (isFile())
  437. return false;
  438. if (isGroup())
  439. return child.isFile() || child.isGroup();
  440. jassertfalse
  441. return false;
  442. }
  443. bool Project::Item::shouldBeAddedToTargetProject() const
  444. {
  445. return isFile();
  446. }
  447. bool Project::Item::shouldBeCompiled() const { return getShouldCompileValue().getValue(); }
  448. Value Project::Item::getShouldCompileValue() const { return node.getPropertyAsValue (Ids::compile, getUndoManager()); }
  449. bool Project::Item::shouldBeAddedToBinaryResources() const { return getShouldAddToResourceValue().getValue(); }
  450. Value Project::Item::getShouldAddToResourceValue() const { return node.getPropertyAsValue (Ids::resource, getUndoManager()); }
  451. Value Project::Item::getShouldInhibitWarningsValue() const { return node.getPropertyAsValue (Ids::noWarnings, getUndoManager()); }
  452. String Project::Item::getFilePath() const
  453. {
  454. if (isFile())
  455. return node [Ids::file].toString();
  456. else
  457. return String::empty;
  458. }
  459. File Project::Item::getFile() const
  460. {
  461. if (isFile())
  462. return getProject().resolveFilename (node [Ids::file].toString());
  463. else
  464. return File::nonexistent;
  465. }
  466. void Project::Item::setFile (const File& file)
  467. {
  468. setFile (RelativePath (getProject().getRelativePathForFile (file), RelativePath::projectFolder));
  469. jassert (getFile() == file);
  470. }
  471. void Project::Item::setFile (const RelativePath& file)
  472. {
  473. jassert (file.getRoot() == RelativePath::projectFolder);
  474. jassert (isFile());
  475. node.setProperty (Ids::file, file.toUnixStyle(), getUndoManager());
  476. node.setProperty (Ids::name, file.getFileName(), getUndoManager());
  477. }
  478. bool Project::Item::renameFile (const File& newFile)
  479. {
  480. const File oldFile (getFile());
  481. if (oldFile.moveFileTo (newFile))
  482. {
  483. setFile (newFile);
  484. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  485. return true;
  486. }
  487. return false;
  488. }
  489. Project::Item Project::Item::findItemForFile (const File& file) const
  490. {
  491. if (getFile() == file)
  492. return *this;
  493. if (isGroup())
  494. {
  495. for (int i = getNumChildren(); --i >= 0;)
  496. {
  497. Item found (getChild(i).findItemForFile (file));
  498. if (found.isValid())
  499. return found;
  500. }
  501. }
  502. return Item (getProject(), ValueTree::invalid);
  503. }
  504. File Project::Item::determineGroupFolder() const
  505. {
  506. jassert (isGroup());
  507. File f;
  508. for (int i = 0; i < getNumChildren(); ++i)
  509. {
  510. f = getChild(i).getFile();
  511. if (f.exists())
  512. return f.getParentDirectory();
  513. }
  514. Item parent (getParent());
  515. if (parent != *this)
  516. {
  517. f = parent.determineGroupFolder();
  518. if (f.getChildFile (getName().toString()).isDirectory())
  519. f = f.getChildFile (getName().toString());
  520. }
  521. else
  522. {
  523. f = getProject().getFile().getParentDirectory();
  524. if (f.getChildFile ("Source").isDirectory())
  525. f = f.getChildFile ("Source");
  526. }
  527. return f;
  528. }
  529. void Project::Item::initialiseNodeValues()
  530. {
  531. if (! node.hasProperty (Ids::id_))
  532. setID (createAlphaNumericUID());
  533. if (isFile())
  534. {
  535. node.setProperty (Ids::name, getFile().getFileName(), 0);
  536. }
  537. else if (isGroup())
  538. {
  539. for (int i = getNumChildren(); --i >= 0;)
  540. getChild(i).initialiseNodeValues();
  541. }
  542. }
  543. Value Project::Item::getName() const
  544. {
  545. return node.getPropertyAsValue (Ids::name, getUndoManager());
  546. }
  547. void Project::Item::addChild (const Item& newChild, int insertIndex)
  548. {
  549. node.addChild (newChild.getNode(), insertIndex, getUndoManager());
  550. }
  551. void Project::Item::removeItemFromProject()
  552. {
  553. node.getParent().removeChild (node, getUndoManager());
  554. }
  555. Project::Item Project::Item::getParent() const
  556. {
  557. if (isMainGroup() || ! isGroup())
  558. return *this;
  559. return Item (getProject(), node.getParent());
  560. }
  561. struct ItemSorter
  562. {
  563. static int compareElements (const ValueTree& first, const ValueTree& second)
  564. {
  565. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  566. }
  567. };
  568. void Project::Item::sortAlphabetically()
  569. {
  570. ItemSorter sorter;
  571. node.sort (sorter, getUndoManager(), true);
  572. }
  573. Project::Item Project::Item::addNewSubGroup (const String& name, int insertIndex)
  574. {
  575. Item group (createGroup (getProject(), name));
  576. jassert (canContain (group));
  577. addChild (group, insertIndex);
  578. return group;
  579. }
  580. bool Project::Item::addFile (const File& file, int insertIndex)
  581. {
  582. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  583. return false;
  584. if (file.isDirectory())
  585. {
  586. Item group (addNewSubGroup (file.getFileNameWithoutExtension(), insertIndex));
  587. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  588. while (iter.next())
  589. {
  590. if (! getProject().getMainGroup().findItemForFile (iter.getFile()).isValid())
  591. group.addFile (iter.getFile(), -1);
  592. }
  593. group.sortAlphabetically();
  594. }
  595. else if (file.existsAsFile())
  596. {
  597. if (! getProject().getMainGroup().findItemForFile (file).isValid())
  598. {
  599. Item item (getProject(), ValueTree (Tags::file));
  600. item.initialiseNodeValues();
  601. item.getName() = file.getFileName();
  602. item.getShouldCompileValue() = file.hasFileExtension ("cpp;mm;c;m;cc;cxx");
  603. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  604. if (canContain (item))
  605. {
  606. item.setFile (file);
  607. addChild (item, insertIndex);
  608. }
  609. }
  610. }
  611. else
  612. {
  613. jassertfalse;
  614. }
  615. return true;
  616. }
  617. bool Project::Item::addRelativeFile (const RelativePath& file, int insertIndex, bool shouldCompile)
  618. {
  619. Item item (getProject(), ValueTree (Tags::file));
  620. item.initialiseNodeValues();
  621. item.getName() = file.getFileName();
  622. item.getShouldCompileValue() = shouldCompile;
  623. item.getShouldAddToResourceValue() = getProject().shouldBeAddedToBinaryResourcesByDefault (file);
  624. if (canContain (item))
  625. {
  626. item.setFile (file);
  627. addChild (item, insertIndex);
  628. return true;
  629. }
  630. return false;
  631. }
  632. const Drawable* Project::Item::getIcon() const
  633. {
  634. if (isFile())
  635. {
  636. if (isImageFile())
  637. return StoredSettings::getInstance()->getImageFileIcon();
  638. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  639. }
  640. else if (isMainGroup())
  641. {
  642. return &(getProject().mainProjectIcon);
  643. }
  644. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  645. }
  646. //==============================================================================
  647. ValueTree Project::getConfigNode()
  648. {
  649. return projectRoot.getOrCreateChildWithName (Tags::configGroup, nullptr);
  650. }
  651. void Project::getAllConfigFlags (OwnedArray <ConfigFlag>& flags)
  652. {
  653. OwnedArray<LibraryModule> modules;
  654. getProjectType().createRequiredModules (*this, modules);
  655. int i;
  656. for (i = 0; i < modules.size(); ++i)
  657. modules.getUnchecked(i)->getConfigFlags (*this, flags);
  658. for (i = 0; i < flags.size(); ++i)
  659. flags.getUnchecked(i)->value.referTo (getConfigFlag (flags.getUnchecked(i)->symbol));
  660. }
  661. const char* const Project::configFlagDefault = "default";
  662. const char* const Project::configFlagEnabled = "enabled";
  663. const char* const Project::configFlagDisabled = "disabled";
  664. Value Project::getConfigFlag (const String& name)
  665. {
  666. const ValueTree configNode (getConfigNode());
  667. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  668. if (v.getValue().toString().isEmpty())
  669. v = configFlagDefault;
  670. return v;
  671. }
  672. bool Project::isConfigFlagEnabled (const String& name) const
  673. {
  674. return projectRoot.getChildWithName (Tags::configGroup).getProperty (name) == configFlagEnabled;
  675. }
  676. //==============================================================================
  677. ValueTree Project::getConfigurations() const
  678. {
  679. return projectRoot.getChildWithName (Tags::configurations);
  680. }
  681. int Project::getNumConfigurations() const
  682. {
  683. return getConfigurations().getNumChildren();
  684. }
  685. Project::BuildConfiguration Project::getConfiguration (int index)
  686. {
  687. jassert (index < getConfigurations().getNumChildren());
  688. return BuildConfiguration (this, getConfigurations().getChild (index));
  689. }
  690. bool Project::hasConfigurationNamed (const String& name) const
  691. {
  692. const ValueTree configs (getConfigurations());
  693. for (int i = configs.getNumChildren(); --i >= 0;)
  694. if (configs.getChild(i) [Ids::name].toString() == name)
  695. return true;
  696. return false;
  697. }
  698. String Project::getUniqueConfigName (String name) const
  699. {
  700. String nameRoot (name);
  701. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  702. nameRoot = nameRoot.dropLastCharacters (1);
  703. nameRoot = nameRoot.trim();
  704. int suffix = 2;
  705. while (hasConfigurationNamed (name))
  706. name = nameRoot + " " + String (suffix++);
  707. return name;
  708. }
  709. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  710. {
  711. const String configName (getUniqueConfigName (configToCopy != nullptr ? configToCopy->config [Ids::name].toString()
  712. : "New Build Configuration"));
  713. ValueTree configs (getConfigurations());
  714. if (! configs.isValid())
  715. {
  716. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  717. configs = getConfigurations();
  718. }
  719. ValueTree newConfig (Tags::configuration);
  720. if (configToCopy != nullptr)
  721. newConfig = configToCopy->config.createCopy();
  722. newConfig.setProperty (Ids::name, configName, 0);
  723. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  724. }
  725. void Project::deleteConfiguration (int index)
  726. {
  727. ValueTree configs (getConfigurations());
  728. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  729. }
  730. void Project::createDefaultConfigs()
  731. {
  732. for (int i = 0; i < 2; ++i)
  733. {
  734. addNewConfiguration (nullptr);
  735. BuildConfiguration config = getConfiguration (i);
  736. const bool debugConfig = i == 0;
  737. config.getName() = debugConfig ? "Debug" : "Release";
  738. config.isDebug() = debugConfig;
  739. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  740. config.getTargetBinaryName() = getProjectFilenameRoot();
  741. }
  742. }
  743. //==============================================================================
  744. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  745. : project (project_),
  746. config (configNode)
  747. {
  748. }
  749. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  750. : project (other.project),
  751. config (other.config)
  752. {
  753. }
  754. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  755. {
  756. project = other.project;
  757. config = other.config;
  758. return *this;
  759. }
  760. Project::BuildConfiguration::~BuildConfiguration()
  761. {
  762. }
  763. String Project::BuildConfiguration::getGCCOptimisationFlag() const
  764. {
  765. const int level = (int) getOptimisationLevel().getValue();
  766. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  767. }
  768. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  769. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  770. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  771. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  772. const char* const Project::BuildConfiguration::osxArch_Default = "default";
  773. const char* const Project::BuildConfiguration::osxArch_Native = "Native";
  774. const char* const Project::BuildConfiguration::osxArch_32BitUniversal = "32BitUniversal";
  775. const char* const Project::BuildConfiguration::osxArch_64BitUniversal = "64BitUniversal";
  776. const char* const Project::BuildConfiguration::osxArch_64Bit = "64BitIntel";
  777. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  778. {
  779. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  780. props.getLast()->setTooltip ("The name of this configuration.");
  781. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  782. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  783. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  784. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  785. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  786. props.getLast()->setTooltip ("The optimisation level for this configuration");
  787. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  788. 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.");
  789. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  790. 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.");
  791. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  792. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  793. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  794. 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.");
  795. if (getMacSDKVersion().toString().isEmpty())
  796. getMacSDKVersion() = osxVersionDefault;
  797. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  798. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  799. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  800. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  801. if (getMacCompatibilityVersion().toString().isEmpty())
  802. getMacCompatibilityVersion() = osxVersionDefault;
  803. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  804. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  805. const char* osxArch[] = { "Use Default", "Native architecture of build machine", "Universal Binary (32-bit)", "Universal Binary (64-bit)", "64-bit Intel", 0 };
  806. const char* osxArchValues[] = { osxArch_Default, osxArch_Native, osxArch_32BitUniversal, osxArch_64BitUniversal, osxArch_64Bit, 0 };
  807. if (getMacArchitecture().toString().isEmpty())
  808. getMacArchitecture() = osxArch_Default;
  809. props.add (new ChoicePropertyComponent (getMacArchitecture(), "OSX Architecture", StringArray (osxArch), Array<var> (osxArchValues)));
  810. props.getLast()->setTooltip ("The type of OSX binary that will be produced.");
  811. for (int i = props.size(); --i >= 0;)
  812. props.getUnchecked(i)->setPreferredHeight (22);
  813. }
  814. StringPairArray Project::BuildConfiguration::getAllPreprocessorDefs() const
  815. {
  816. return mergePreprocessorDefs (project->getPreprocessorDefs(),
  817. parsePreprocessorDefs (getBuildConfigPreprocessorDefs().toString()));
  818. }
  819. StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  820. {
  821. StringArray s;
  822. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  823. return s;
  824. }
  825. //==============================================================================
  826. ValueTree Project::getExporters()
  827. {
  828. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  829. if (! exporters.isValid())
  830. {
  831. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  832. exporters = getExporters();
  833. }
  834. return exporters;
  835. }
  836. int Project::getNumExporters()
  837. {
  838. return getExporters().getNumChildren();
  839. }
  840. ProjectExporter* Project::createExporter (int index)
  841. {
  842. jassert (index >= 0 && index < getNumExporters());
  843. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  844. }
  845. void Project::addNewExporter (int exporterIndex)
  846. {
  847. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterIndex));
  848. ValueTree exporters (getExporters());
  849. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  850. }
  851. void Project::deleteExporter (int index)
  852. {
  853. ValueTree exporters (getExporters());
  854. exporters.removeChild (index, getUndoManagerFor (exporters));
  855. }
  856. void Project::createDefaultExporters()
  857. {
  858. ValueTree exporters (getExporters());
  859. exporters.removeAllChildren (getUndoManagerFor (exporters));
  860. for (int i = 0; i < ProjectExporter::getNumExporters(); ++i)
  861. addNewExporter (i);
  862. }
  863. //==============================================================================
  864. String Project::getFileTemplate (const String& templateName)
  865. {
  866. int dataSize;
  867. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  868. if (data == nullptr)
  869. {
  870. jassertfalse;
  871. return String::empty;
  872. }
  873. return String::fromUTF8 (data, dataSize);
  874. }
  875. //==============================================================================
  876. void Project::resaveJucerFile (const File& file)
  877. {
  878. if (! file.exists())
  879. {
  880. std::cout << "The file " << file.getFullPathName() << " doesn't exist!" << std::endl;
  881. return;
  882. }
  883. if (! file.hasFileExtension (Project::projectFileExtension))
  884. {
  885. std::cout << file.getFullPathName() << " isn't a valid jucer project file!" << std::endl;
  886. return;
  887. }
  888. Project newDoc (file);
  889. if (! newDoc.loadFrom (file, true))
  890. {
  891. std::cout << "Failed to load the project file: " << file.getFullPathName() << std::endl;
  892. return;
  893. }
  894. std::cout << "The Jucer - Re-saving file: " << file.getFullPathName() << std::endl;
  895. String error (newDoc.saveDocument (file));
  896. if (error.isNotEmpty())
  897. {
  898. std::cout << "Error when writing project: " << error << std::endl;
  899. return;
  900. }
  901. }