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.

1035 lines
37KB

  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_ProjectExporter.h"
  20. #include "jucer_ResourceFile.h"
  21. #include "jucer_ProjectSaver.h"
  22. #include "../Application/jucer_OpenDocumentManager.h"
  23. //==============================================================================
  24. namespace Tags
  25. {
  26. const Identifier projectRoot ("JUCERPROJECT");
  27. const Identifier projectMainGroup ("MAINGROUP");
  28. const Identifier group ("GROUP");
  29. const Identifier file ("FILE");
  30. const Identifier configurations ("CONFIGURATIONS");
  31. const Identifier configuration ("CONFIGURATION");
  32. const Identifier exporters ("EXPORTFORMATS");
  33. const Identifier configGroup ("JUCEOPTIONS");
  34. }
  35. const char* Project::projectFileExtension = ".jucer";
  36. //==============================================================================
  37. Project::Project (const File& file_)
  38. : FileBasedDocument (projectFileExtension,
  39. String ("*") + projectFileExtension,
  40. "Choose a Jucer project to load",
  41. "Save Jucer project"),
  42. projectRoot (Tags::projectRoot)
  43. {
  44. setFile (file_);
  45. setMissingDefaultValues();
  46. setChangedFlag (false);
  47. projectRoot.addListener (this);
  48. }
  49. Project::~Project()
  50. {
  51. projectRoot.removeListener (this);
  52. OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false);
  53. }
  54. //==============================================================================
  55. void Project::setTitle (const String& newTitle)
  56. {
  57. projectRoot.setProperty (Ids::name, newTitle, getUndoManagerFor (projectRoot));
  58. getMainGroup().getName() = newTitle;
  59. }
  60. const String Project::getDocumentTitle()
  61. {
  62. return getProjectName().toString();
  63. }
  64. void Project::updateProjectSettings()
  65. {
  66. projectRoot.setProperty (Ids::jucerVersion, ProjectInfo::versionString, 0);
  67. projectRoot.setProperty (Ids::name, getDocumentTitle(), 0);
  68. }
  69. void Project::setMissingDefaultValues()
  70. {
  71. if (! projectRoot.hasProperty (Ids::id_))
  72. projectRoot.setProperty (Ids::id_, createAlphaNumericUID(), 0);
  73. // Create main file group if missing
  74. if (! projectRoot.getChildWithName (Tags::projectMainGroup).isValid())
  75. {
  76. Item mainGroup (*this, ValueTree (Tags::projectMainGroup));
  77. projectRoot.addChild (mainGroup.getNode(), 0, 0);
  78. }
  79. getMainGroup().initialiseNodeValues();
  80. if (getDocumentTitle().isEmpty())
  81. setTitle ("Juce Project");
  82. if (! projectRoot.hasProperty (Ids::projectType))
  83. getProjectType() = (int) application;
  84. if (! projectRoot.hasProperty (Ids::version))
  85. getVersion() = "1.0.0";
  86. if (! projectRoot.hasProperty (Ids::juceLinkage))
  87. getJuceLinkageModeValue() = useAmalgamatedJuceViaMultipleTemplates;
  88. const String juceFolderPath (getRelativePathForFile (StoredSettings::getInstance()->getLastKnownJuceFolder()));
  89. // Create configs group
  90. if (! projectRoot.getChildWithName (Tags::configurations).isValid())
  91. {
  92. projectRoot.addChild (ValueTree (Tags::configurations), 0, 0);
  93. createDefaultConfigs();
  94. }
  95. if (! projectRoot.getChildWithName (Tags::exporters).isValid())
  96. createDefaultExporters();
  97. const String sanitisedProjectName (CodeHelpers::makeValidIdentifier (getProjectName().toString(), false, true, false));
  98. if (! projectRoot.hasProperty (Ids::buildVST))
  99. {
  100. shouldBuildVST() = true;
  101. shouldBuildRTAS() = false;
  102. shouldBuildAU() = true;
  103. getPluginName() = getProjectName().toString();
  104. getPluginDesc() = getProjectName().toString();
  105. getPluginManufacturer() = "yourcompany";
  106. getPluginManufacturerCode() = "abcd";
  107. getPluginCode() = "Abcd";
  108. getPluginChannelConfigs() = "{1, 1}, {2, 2}";
  109. getPluginIsSynth() = false;
  110. getPluginWantsMidiInput() = false;
  111. getPluginProducesMidiOut() = false;
  112. getPluginSilenceInProducesSilenceOut() = false;
  113. getPluginTailLengthSeconds() = 0;
  114. getPluginEditorNeedsKeyFocus() = false;
  115. getPluginAUExportPrefix() = sanitisedProjectName + "AU";
  116. getPluginAUCocoaViewClassName() = sanitisedProjectName + "AU_V1";
  117. getPluginRTASCategory() = String::empty;
  118. }
  119. if (! projectRoot.hasProperty (Ids::bundleIdentifier))
  120. setBundleIdentifierToDefault();
  121. }
  122. //==============================================================================
  123. const String Project::loadDocument (const File& file)
  124. {
  125. XmlDocument doc (file);
  126. ScopedPointer <XmlElement> xml (doc.getDocumentElement());
  127. if (xml == 0 || ! xml->hasTagName (Tags::projectRoot.toString()))
  128. return "Not a valid Jucer project!";
  129. ValueTree newTree (ValueTree::fromXml (*xml));
  130. if (! newTree.hasType (Tags::projectRoot))
  131. return "The document contains errors and couldn't be parsed!";
  132. StoredSettings::getInstance()->recentFiles.addFile (file);
  133. StoredSettings::getInstance()->flush();
  134. projectRoot = newTree;
  135. setMissingDefaultValues();
  136. return String::empty;
  137. }
  138. const String Project::saveDocument (const File& file)
  139. {
  140. updateProjectSettings();
  141. {
  142. // (getting these forces the values to be sanitised)
  143. OwnedArray <Project::JuceConfigFlag> flags;
  144. getJuceConfigFlags (flags);
  145. }
  146. if (FileHelpers::isJuceFolder (getLocalJuceFolder()))
  147. StoredSettings::getInstance()->setLastKnownJuceFolder (getLocalJuceFolder().getFullPathName());
  148. StoredSettings::getInstance()->recentFiles.addFile (file);
  149. ProjectSaver saver (*this, file);
  150. return saver.save();
  151. }
  152. //==============================================================================
  153. File Project::lastDocumentOpened;
  154. const File Project::getLastDocumentOpened()
  155. {
  156. return lastDocumentOpened;
  157. }
  158. void Project::setLastDocumentOpened (const File& file)
  159. {
  160. lastDocumentOpened = file;
  161. }
  162. //==============================================================================
  163. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  164. {
  165. if (isLibrary())
  166. getJuceLinkageModeValue() = notLinkedToJuce;
  167. changed();
  168. }
  169. void Project::valueTreeChildrenChanged (ValueTree& tree)
  170. {
  171. changed();
  172. }
  173. void Project::valueTreeParentChanged (ValueTree& tree)
  174. {
  175. }
  176. //==============================================================================
  177. const File Project::resolveFilename (const String& filename) const
  178. {
  179. if (filename.isEmpty())
  180. return File::nonexistent;
  181. if (File::isAbsolutePath (filename))
  182. return File (filename);
  183. return getFile().getSiblingFile (filename);
  184. }
  185. const String Project::getRelativePathForFile (const File& file) const
  186. {
  187. String filename (file.getFullPathName());
  188. File relativePathBase (getFile().getParentDirectory());
  189. String p1 (relativePathBase.getFullPathName());
  190. String p2 (file.getFullPathName());
  191. while (p1.startsWithChar (File::separator))
  192. p1 = p1.substring (1);
  193. while (p2.startsWithChar (File::separator))
  194. p2 = p2.substring (1);
  195. if (p1.upToFirstOccurrenceOf (File::separatorString, true, false)
  196. .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false)))
  197. {
  198. filename = file.getRelativePathFrom (relativePathBase);
  199. }
  200. return filename;
  201. }
  202. //==============================================================================
  203. bool Project::shouldBeAddedToBinaryResourcesByDefault (const File& file)
  204. {
  205. return ! file.hasFileExtension (sourceFileExtensions);
  206. }
  207. //==============================================================================
  208. const char* const Project::application = "guiapp";
  209. const char* const Project::commandLineApp = "consoleapp";
  210. const char* const Project::audioPlugin = "audioplug";
  211. const char* const Project::library = "library";
  212. const char* const Project::browserPlugin = "browserplug";
  213. bool Project::isLibrary() const { return getProjectType().toString() == library; }
  214. bool Project::isGUIApplication() const { return getProjectType().toString() == application; }
  215. bool Project::isCommandLineApp() const { return getProjectType().toString() == commandLineApp; }
  216. bool Project::isAudioPlugin() const { return getProjectType().toString() == audioPlugin; }
  217. bool Project::isBrowserPlugin() const { return getProjectType().toString() == browserPlugin; }
  218. const char* const Project::notLinkedToJuce = "none";
  219. const char* const Project::useLinkedJuce = "static";
  220. const char* const Project::useAmalgamatedJuce = "amalg_big";
  221. const char* const Project::useAmalgamatedJuceViaSingleTemplate = "amalg_template";
  222. const char* const Project::useAmalgamatedJuceViaMultipleTemplates = "amalg_multi";
  223. const File Project::getLocalJuceFolder()
  224. {
  225. ScopedPointer <ProjectExporter> exp (ProjectExporter::createPlatformDefaultExporter (*this));
  226. if (exp != 0)
  227. {
  228. File f (resolveFilename (exp->getJuceFolder().toString()));
  229. if (FileHelpers::isJuceFolder (f))
  230. return f;
  231. }
  232. return StoredSettings::getInstance()->getLastKnownJuceFolder();
  233. }
  234. //==============================================================================
  235. void Project::createPropertyEditors (Array <PropertyComponent*>& props)
  236. {
  237. props.add (new TextPropertyComponent (getProjectName(), "Project Name", 256, false));
  238. props.getLast()->setTooltip ("The name of the project.");
  239. props.add (new TextPropertyComponent (getVersion(), "Project Version", 16, false));
  240. props.getLast()->setTooltip ("The project's version number, This should be in the format major.minor.point");
  241. const char* projectTypes[] = { "Application (GUI)", "Application (Non-GUI)", "Audio Plug-in", "Static Library", 0 };
  242. const char* projectTypeValues[] = { application, commandLineApp, audioPlugin, library, 0 };
  243. props.add (new ChoicePropertyComponent (getProjectType(), "Project Type", StringArray (projectTypes), Array<var> (projectTypeValues)));
  244. 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 };
  245. const char* linkageTypeValues[] = { notLinkedToJuce, useLinkedJuce, useAmalgamatedJuce, useAmalgamatedJuceViaSingleTemplate, useAmalgamatedJuceViaMultipleTemplates, 0 };
  246. props.add (new ChoicePropertyComponent (getJuceLinkageModeValue(), "Juce Linkage Method", StringArray (linkageTypes), Array<var> (linkageTypeValues)));
  247. props.getLast()->setTooltip ("The method by which your project will be linked to Juce.");
  248. props.add (new TextPropertyComponent (getBundleIdentifier(), "Bundle Identifier", 256, false));
  249. props.getLast()->setTooltip ("A unique identifier for this product, mainly for use in Mac builds. It should be something like 'com.yourcompanyname.yourproductname'");
  250. if (isAudioPlugin())
  251. {
  252. props.add (new BooleanPropertyComponent (shouldBuildVST(), "Build VST", "Enabled"));
  253. props.getLast()->setTooltip ("Whether the project should produce a VST plugin.");
  254. props.add (new BooleanPropertyComponent (shouldBuildAU(), "Build AudioUnit", "Enabled"));
  255. props.getLast()->setTooltip ("Whether the project should produce an AudioUnit plugin.");
  256. props.add (new BooleanPropertyComponent (shouldBuildRTAS(), "Build RTAS", "Enabled"));
  257. props.getLast()->setTooltip ("Whether the project should produce an RTAS plugin.");
  258. }
  259. if (isAudioPlugin())
  260. {
  261. props.add (new TextPropertyComponent (getPluginName(), "Plugin Name", 128, false));
  262. props.getLast()->setTooltip ("The name of your plugin (keep it short!)");
  263. props.add (new TextPropertyComponent (getPluginDesc(), "Plugin Description", 256, false));
  264. props.getLast()->setTooltip ("A short description of your plugin.");
  265. props.add (new TextPropertyComponent (getPluginManufacturer(), "Plugin Manufacturer", 256, false));
  266. props.getLast()->setTooltip ("The name of your company (cannot be blank).");
  267. props.add (new TextPropertyComponent (getPluginManufacturerCode(), "Plugin Manufacturer Code", 4, false));
  268. props.getLast()->setTooltip ("A four-character unique ID for your company.");
  269. props.add (new TextPropertyComponent (getPluginCode(), "Plugin Code", 4, false));
  270. 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!");
  271. props.add (new TextPropertyComponent (getPluginChannelConfigs(), "Plugin Channel Configurations", 256, false));
  272. 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 "
  273. "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 "
  274. "and 1 output, or with 2 inputs and 2 outputs.");
  275. props.add (new BooleanPropertyComponent (getPluginIsSynth(), "Plugin is a Synth", "Is a Synth"));
  276. 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.");
  277. props.add (new BooleanPropertyComponent (getPluginWantsMidiInput(), "Plugin Midi Input", "Plugin wants midi input"));
  278. props.getLast()->setTooltip ("Enable this if you want your plugin to accept midi messages.");
  279. props.add (new BooleanPropertyComponent (getPluginProducesMidiOut(), "Plugin Midi Output", "Plugin produces midi output"));
  280. props.getLast()->setTooltip ("Enable this if your plugin is going to produce midi messages.");
  281. props.add (new BooleanPropertyComponent (getPluginSilenceInProducesSilenceOut(), "Silence", "Silence in produces silence out"));
  282. 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.");
  283. props.add (new TextPropertyComponent (getPluginTailLengthSeconds(), "Tail Length (in seconds)", 12, false));
  284. 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.");
  285. props.add (new BooleanPropertyComponent (getPluginEditorNeedsKeyFocus(), "Key Focus", "Plugin editor requires keyboard focus"));
  286. props.getLast()->setTooltip ("Enable this if your plugin needs keyboard input - some hosts can be a bit funny about keyboard focus..");
  287. props.add (new TextPropertyComponent (getPluginAUExportPrefix(), "Plugin AU Export Prefix", 64, false));
  288. 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.");
  289. props.add (new TextPropertyComponent (getPluginAUCocoaViewClassName(), "Plugin AU Cocoa View Name", 64, false));
  290. 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 "
  291. "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.");
  292. props.add (new TextPropertyComponent (getPluginRTASCategory(), "Plugin RTAS Category", 64, false));
  293. 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, "
  294. "ePlugInCategory_PitchShift, ePlugInCategory_Reverb, ePlugInCategory_Delay, "
  295. "ePlugInCategory_Modulation, ePlugInCategory_Harmonic, ePlugInCategory_NoiseReduction, "
  296. "ePlugInCategory_Dither, ePlugInCategory_SoundField");
  297. }
  298. for (int i = props.size(); --i >= 0;)
  299. props.getUnchecked(i)->setPreferredHeight (22);
  300. }
  301. //==============================================================================
  302. Project::Item Project::getMainGroup()
  303. {
  304. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  305. }
  306. Project::Item Project::createNewGroup()
  307. {
  308. Item item (*this, ValueTree (Tags::group));
  309. item.initialiseNodeValues();
  310. item.getName() = "New Group";
  311. return item;
  312. }
  313. Project::Item Project::createNewItem (const File& file)
  314. {
  315. Item item (*this, ValueTree (Tags::file));
  316. item.initialiseNodeValues();
  317. item.getName() = file.getFileName();
  318. item.getShouldCompileValue() = file.hasFileExtension ("cpp;mm;c;m");
  319. item.getShouldAddToResourceValue() = shouldBeAddedToBinaryResourcesByDefault (file);
  320. return item;
  321. }
  322. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  323. {
  324. if (item.isFile())
  325. {
  326. if (item.getFile().hasFileExtension ("png;jpg;jpeg;gif"))
  327. found.add (new Project::Item (item));
  328. }
  329. else if (item.isGroup())
  330. {
  331. for (int i = 0; i < item.getNumChildren(); ++i)
  332. findImages (item.getChild (i), found);
  333. }
  334. }
  335. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  336. {
  337. findImages (getMainGroup(), items);
  338. }
  339. //==============================================================================
  340. Project::Item::Item (Project& project_, const ValueTree& node_)
  341. : project (project_), node (node_)
  342. {
  343. }
  344. Project::Item::Item (const Item& other)
  345. : project (other.project), node (other.node)
  346. {
  347. }
  348. Project::Item::~Item()
  349. {
  350. }
  351. const String Project::Item::getID() const { return node [Ids::id_]; }
  352. const String Project::Item::getImageFileID() const { return "id:" + getID(); }
  353. bool Project::Item::isFile() const { return node.hasType (Tags::file); }
  354. bool Project::Item::isGroup() const { return node.hasType (Tags::group) || isMainGroup(); }
  355. bool Project::Item::isMainGroup() const { return node.hasType (Tags::projectMainGroup); }
  356. Project::Item Project::Item::findItemWithID (const String& targetId) const
  357. {
  358. if (node [Ids::id_] == targetId)
  359. return *this;
  360. if (isGroup())
  361. {
  362. for (int i = getNumChildren(); --i >= 0;)
  363. {
  364. Item found (getChild(i).findItemWithID (targetId));
  365. if (found.isValid())
  366. return found;
  367. }
  368. }
  369. return Item (project, ValueTree::invalid);
  370. }
  371. bool Project::Item::canContain (const Item& child) const
  372. {
  373. if (isFile())
  374. return false;
  375. if (isGroup())
  376. return child.isFile() || child.isGroup();
  377. jassertfalse
  378. return false;
  379. }
  380. bool Project::Item::shouldBeAddedToTargetProject() const
  381. {
  382. return isFile();
  383. }
  384. bool Project::Item::shouldBeCompiled() const
  385. {
  386. return getShouldCompileValue().getValue();
  387. }
  388. Value Project::Item::getShouldCompileValue() const
  389. {
  390. return node.getPropertyAsValue (Ids::compile, getUndoManager());
  391. }
  392. bool Project::Item::shouldBeAddedToBinaryResources() const
  393. {
  394. return getShouldAddToResourceValue().getValue();
  395. }
  396. Value Project::Item::getShouldAddToResourceValue() const
  397. {
  398. return node.getPropertyAsValue (Ids::resource, getUndoManager());
  399. }
  400. const File Project::Item::getFile() const
  401. {
  402. if (isFile())
  403. return project.resolveFilename (node [Ids::file].toString());
  404. else
  405. return File::nonexistent;
  406. }
  407. void Project::Item::setFile (const File& file)
  408. {
  409. jassert (isFile());
  410. node.setProperty (Ids::file, project.getRelativePathForFile (file), getUndoManager());
  411. node.setProperty (Ids::name, file.getFileName(), getUndoManager());
  412. jassert (getFile() == file);
  413. }
  414. bool Project::Item::renameFile (const File& newFile)
  415. {
  416. const File oldFile (getFile());
  417. if (oldFile.moveFileTo (newFile))
  418. {
  419. setFile (newFile);
  420. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  421. return true;
  422. }
  423. return false;
  424. }
  425. Project::Item Project::Item::findItemForFile (const File& file) const
  426. {
  427. if (getFile() == file)
  428. return *this;
  429. if (isGroup())
  430. {
  431. for (int i = getNumChildren(); --i >= 0;)
  432. {
  433. Item found (getChild(i).findItemForFile (file));
  434. if (found.isValid())
  435. return found;
  436. }
  437. }
  438. return Item (project, ValueTree::invalid);
  439. }
  440. const File Project::Item::determineGroupFolder() const
  441. {
  442. jassert (isGroup());
  443. File f;
  444. for (int i = 0; i < getNumChildren(); ++i)
  445. {
  446. f = getChild(i).getFile();
  447. if (f.exists())
  448. return f.getParentDirectory();
  449. }
  450. Item parent (getParent());
  451. if (parent != *this)
  452. {
  453. f = parent.determineGroupFolder();
  454. if (f.getChildFile (getName().toString()).isDirectory())
  455. f = f.getChildFile (getName().toString());
  456. }
  457. else
  458. {
  459. f = project.getFile().getParentDirectory();
  460. if (f.getChildFile ("Source").isDirectory())
  461. f = f.getChildFile ("Source");
  462. }
  463. return f;
  464. }
  465. void Project::Item::initialiseNodeValues()
  466. {
  467. if (! node.hasProperty (Ids::id_))
  468. node.setProperty (Ids::id_, createAlphaNumericUID(), 0);
  469. if (isFile())
  470. {
  471. node.setProperty (Ids::name, getFile().getFileName(), 0);
  472. }
  473. else if (isGroup())
  474. {
  475. for (int i = getNumChildren(); --i >= 0;)
  476. getChild(i).initialiseNodeValues();
  477. }
  478. }
  479. Value Project::Item::getName() const
  480. {
  481. return node.getPropertyAsValue (Ids::name, getUndoManager());
  482. }
  483. void Project::Item::addChild (const Item& newChild, int insertIndex)
  484. {
  485. node.addChild (newChild.getNode(), insertIndex, getUndoManager());
  486. }
  487. void Project::Item::removeItemFromProject()
  488. {
  489. node.getParent().removeChild (node, getUndoManager());
  490. }
  491. Project::Item Project::Item::getParent() const
  492. {
  493. if (isMainGroup() || ! isGroup())
  494. return *this;
  495. return Item (project, node.getParent());
  496. }
  497. struct ItemSorter
  498. {
  499. static int compareElements (const ValueTree& first, const ValueTree& second)
  500. {
  501. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  502. }
  503. };
  504. void Project::Item::sortAlphabetically()
  505. {
  506. ItemSorter sorter;
  507. node.sort (sorter);
  508. }
  509. bool Project::Item::addFile (const File& file, int insertIndex)
  510. {
  511. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  512. return false;
  513. if (file.isDirectory())
  514. {
  515. Item group (project.createNewGroup());
  516. group.getName() = file.getFileNameWithoutExtension();
  517. jassert (canContain (group));
  518. addChild (group, insertIndex);
  519. //group.setFile (file);
  520. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  521. while (iter.next())
  522. group.addFile (iter.getFile(), -1);
  523. group.sortAlphabetically();
  524. }
  525. else if (file.existsAsFile())
  526. {
  527. Item item (project.createNewItem (file));
  528. if (canContain (item))
  529. {
  530. item.setFile (file);
  531. addChild (item, insertIndex);
  532. }
  533. }
  534. else
  535. {
  536. jassertfalse;
  537. }
  538. return true;
  539. }
  540. const Image Project::Item::getIcon() const
  541. {
  542. if (isFile())
  543. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  544. else if (isMainGroup())
  545. return ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize);
  546. else
  547. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  548. }
  549. //==============================================================================
  550. ValueTree Project::getJuceConfigNode()
  551. {
  552. ValueTree configNode = projectRoot.getChildWithName (Tags::configGroup);
  553. if (! configNode.isValid())
  554. {
  555. configNode = ValueTree (Tags::configGroup);
  556. projectRoot.addChild (configNode, -1, 0);
  557. }
  558. return configNode;
  559. }
  560. void Project::getJuceConfigFlags (OwnedArray <JuceConfigFlag>& flags)
  561. {
  562. ValueTree configNode (getJuceConfigNode());
  563. File juceConfigH (getLocalJuceFolder().getChildFile ("juce_Config.h"));
  564. StringArray lines;
  565. lines.addLines (juceConfigH.loadFileAsString());
  566. for (int i = 0; i < lines.size(); ++i)
  567. {
  568. String line (lines[i].trim());
  569. if (line.startsWith ("/** ") && line.containsChar (':'))
  570. {
  571. ScopedPointer <JuceConfigFlag> config (new JuceConfigFlag());
  572. config->symbol = line.substring (4).upToFirstOccurrenceOf (":", false, false).trim();
  573. if (config->symbol.length() > 4)
  574. {
  575. config->description = line.fromFirstOccurrenceOf (":", false, false).trimStart();
  576. ++i;
  577. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  578. {
  579. if (lines[i].trim().isNotEmpty())
  580. config->description = config->description.trim() + " " + lines[i].trim();
  581. ++i;
  582. }
  583. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  584. config->value.referTo (getJuceConfigFlag (config->symbol));
  585. flags.add (config.release());
  586. }
  587. }
  588. }
  589. }
  590. const char* const Project::configFlagDefault = "default";
  591. const char* const Project::configFlagEnabled = "enabled";
  592. const char* const Project::configFlagDisabled = "disabled";
  593. Value Project::getJuceConfigFlag (const String& name)
  594. {
  595. const ValueTree configNode (getJuceConfigNode());
  596. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  597. if (v.getValue().toString().isEmpty())
  598. v = configFlagDefault;
  599. return v;
  600. }
  601. //==============================================================================
  602. ValueTree Project::getConfigurations() const
  603. {
  604. return projectRoot.getChildWithName (Tags::configurations);
  605. }
  606. int Project::getNumConfigurations() const
  607. {
  608. return getConfigurations().getNumChildren();
  609. }
  610. Project::BuildConfiguration Project::getConfiguration (int index)
  611. {
  612. jassert (index < getConfigurations().getNumChildren());
  613. return BuildConfiguration (this, getConfigurations().getChild (index));
  614. }
  615. bool Project::hasConfigurationNamed (const String& name) const
  616. {
  617. const ValueTree configs (getConfigurations());
  618. for (int i = configs.getNumChildren(); --i >= 0;)
  619. if (configs.getChild(i) [Ids::name].toString() == name)
  620. return true;
  621. return false;
  622. }
  623. const String Project::getUniqueConfigName (String name) const
  624. {
  625. String nameRoot (name);
  626. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  627. nameRoot = nameRoot.dropLastCharacters (1);
  628. nameRoot = nameRoot.trim();
  629. int suffix = 2;
  630. while (hasConfigurationNamed (name))
  631. name = nameRoot + " " + String (suffix++);
  632. return name;
  633. }
  634. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  635. {
  636. const String configName (getUniqueConfigName (configToCopy != 0 ? configToCopy->config [Ids::name].toString()
  637. : "New Build Configuration"));
  638. ValueTree configs (getConfigurations());
  639. if (! configs.isValid())
  640. {
  641. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  642. configs = getConfigurations();
  643. }
  644. ValueTree newConfig (Tags::configuration);
  645. if (configToCopy != 0)
  646. newConfig = configToCopy->config.createCopy();
  647. newConfig.setProperty (Ids::name, configName, 0);
  648. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  649. }
  650. void Project::deleteConfiguration (int index)
  651. {
  652. ValueTree configs (getConfigurations());
  653. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  654. }
  655. void Project::createDefaultConfigs()
  656. {
  657. for (int i = 0; i < 2; ++i)
  658. {
  659. addNewConfiguration (0);
  660. BuildConfiguration config = getConfiguration (i);
  661. const bool debugConfig = i == 0;
  662. config.getName() = debugConfig ? "Debug" : "Release";
  663. config.isDebug() = debugConfig;
  664. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  665. config.getTargetBinaryName() = getProjectFilenameRoot();
  666. }
  667. }
  668. //==============================================================================
  669. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  670. : project (project_),
  671. config (configNode)
  672. {
  673. }
  674. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  675. : project (other.project),
  676. config (other.config)
  677. {
  678. }
  679. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  680. {
  681. project = other.project;
  682. config = other.config;
  683. return *this;
  684. }
  685. Project::BuildConfiguration::~BuildConfiguration()
  686. {
  687. }
  688. const String Project::BuildConfiguration::getGCCOptimisationFlag() const
  689. {
  690. const int level = (int) getOptimisationLevel().getValue();
  691. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  692. }
  693. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  694. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  695. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  696. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  697. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  698. {
  699. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  700. props.getLast()->setTooltip ("The name of this configuration.");
  701. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  702. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  703. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  704. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  705. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  706. props.getLast()->setTooltip ("The optimisation level for this configuration");
  707. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  708. 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.");
  709. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  710. 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.");
  711. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  712. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  713. props.add (new TextPropertyComponent (getPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  714. props.getLast()->setTooltip ("Extra preprocessor definitions. Use whitespace or commas as a delimiter.");
  715. if (getMacSDKVersion().toString().isEmpty())
  716. getMacSDKVersion() = osxVersionDefault;
  717. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  718. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  719. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  720. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  721. if (getMacCompatibilityVersion().toString().isEmpty())
  722. getMacCompatibilityVersion() = osxVersionDefault;
  723. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  724. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  725. for (int i = props.size(); --i >= 0;)
  726. props.getUnchecked(i)->setPreferredHeight (22);
  727. }
  728. const StringArray Project::BuildConfiguration::parsePreprocessorDefs() const
  729. {
  730. StringArray defines;
  731. defines.addTokens (getPreprocessorDefs().toString(), " ,;", String::empty);
  732. defines.removeEmptyStrings (true);
  733. return defines;
  734. }
  735. const StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  736. {
  737. StringArray s;
  738. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  739. return s;
  740. }
  741. //==============================================================================
  742. ValueTree Project::getExporters()
  743. {
  744. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  745. if (! exporters.isValid())
  746. {
  747. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  748. exporters = getExporters();
  749. }
  750. return exporters;
  751. }
  752. int Project::getNumExporters()
  753. {
  754. return getExporters().getNumChildren();
  755. }
  756. ProjectExporter* Project::createExporter (int index)
  757. {
  758. jassert (index >= 0 && index < getNumExporters());
  759. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  760. }
  761. void Project::addNewExporter (int exporterIndex)
  762. {
  763. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterIndex));
  764. ValueTree exporters (getExporters());
  765. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  766. }
  767. void Project::deleteExporter (int index)
  768. {
  769. ValueTree exporters (getExporters());
  770. exporters.removeChild (index, getUndoManagerFor (exporters));
  771. }
  772. void Project::createDefaultExporters()
  773. {
  774. ValueTree exporters (getExporters());
  775. exporters.removeAllChildren (getUndoManagerFor (exporters));
  776. for (int i = 0; i < ProjectExporter::getNumExporters(); ++i)
  777. addNewExporter (i);
  778. }
  779. //==============================================================================
  780. const String Project::getFileTemplate (const String& templateName)
  781. {
  782. int dataSize;
  783. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  784. if (data == 0)
  785. {
  786. jassertfalse;
  787. return String::empty;
  788. }
  789. return String::fromUTF8 (data, dataSize);
  790. }
  791. //==============================================================================
  792. void Project::resaveJucerFile (const File& file)
  793. {
  794. if (! file.exists())
  795. {
  796. std::cout << "The file " << file.getFullPathName() << " doesn't exist!" << std::endl;
  797. return;
  798. }
  799. if (! file.hasFileExtension (Project::projectFileExtension))
  800. {
  801. std::cout << file.getFullPathName() << " isn't a valid jucer project file!" << std::endl;
  802. return;
  803. }
  804. Project newDoc (file);
  805. if (! newDoc.loadFrom (file, true))
  806. {
  807. std::cout << "Failed to load the project file: " << file.getFullPathName() << std::endl;
  808. return;
  809. }
  810. std::cout << "The Jucer - Re-saving file: " << file.getFullPathName() << std::endl;
  811. String error (newDoc.saveDocument (file));
  812. if (error.isNotEmpty())
  813. {
  814. std::cout << "Error when writing project: " << error << std::endl;
  815. return;
  816. }
  817. }