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.

1112 lines
40KB

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