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.

1105 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. 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() = 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() = "Manu";
  107. getPluginCode() = "Plug";
  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. ScopedPointer <XmlElement> xml (XmlDocument::parse (file));
  126. if (xml == 0 || ! xml->hasTagName (Tags::projectRoot.toString()))
  127. return "Not a valid Jucer project!";
  128. ValueTree newTree (ValueTree::fromXml (*xml));
  129. if (! newTree.hasType (Tags::projectRoot))
  130. return "The document contains errors and couldn't be parsed!";
  131. StoredSettings::getInstance()->recentFiles.addFile (file);
  132. StoredSettings::getInstance()->flush();
  133. projectRoot = newTree;
  134. setMissingDefaultValues();
  135. return String::empty;
  136. }
  137. const String Project::saveDocument (const File& file)
  138. {
  139. updateProjectSettings();
  140. {
  141. // (getting these forces the values to be sanitised)
  142. OwnedArray <Project::JuceConfigFlag> flags;
  143. getJuceConfigFlags (flags);
  144. }
  145. if (FileHelpers::isJuceFolder (getLocalJuceFolder()))
  146. StoredSettings::getInstance()->setLastKnownJuceFolder (getLocalJuceFolder().getFullPathName());
  147. StoredSettings::getInstance()->recentFiles.addFile (file);
  148. ProjectSaver saver (*this, file);
  149. return saver.save();
  150. }
  151. //==============================================================================
  152. File Project::lastDocumentOpened;
  153. const File Project::getLastDocumentOpened()
  154. {
  155. return lastDocumentOpened;
  156. }
  157. void Project::setLastDocumentOpened (const File& file)
  158. {
  159. lastDocumentOpened = file;
  160. }
  161. //==============================================================================
  162. void Project::valueTreePropertyChanged (ValueTree& tree, const Identifier& property)
  163. {
  164. if (isLibrary())
  165. getJuceLinkageModeValue() = notLinkedToJuce;
  166. changed();
  167. }
  168. void Project::valueTreeChildrenChanged (ValueTree& tree)
  169. {
  170. changed();
  171. }
  172. void Project::valueTreeParentChanged (ValueTree& tree)
  173. {
  174. }
  175. //==============================================================================
  176. const File Project::resolveFilename (String filename) const
  177. {
  178. if (filename.isEmpty())
  179. return File::nonexistent;
  180. filename = replacePreprocessorDefs (getPreprocessorDefs(), filename);
  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 (sourceOrHeaderFileExtensions);
  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. {
  251. OwnedArray<Project::Item> images;
  252. findAllImageItems (images);
  253. StringArray choices;
  254. Array<var> ids;
  255. choices.add ("<None>");
  256. ids.add (var::null);
  257. choices.add (String::empty);
  258. ids.add (var::null);
  259. for (int i = 0; i < images.size(); ++i)
  260. {
  261. choices.add (images.getUnchecked(i)->getName().toString());
  262. ids.add (images.getUnchecked(i)->getID());
  263. }
  264. props.add (new ChoicePropertyComponent (getSmallIconImageItemID(), "Icon (small)", choices, ids));
  265. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  266. props.add (new ChoicePropertyComponent (getBigIconImageItemID(), "Icon (large)", choices, ids));
  267. props.getLast()->setTooltip ("Sets an icon to use for the executable.");
  268. }
  269. props.add (new TextPropertyComponent (getObjectiveCClassSuffix(), "Objective-C Name Suffix", 256, false));
  270. props.getLast()->setTooltip ("An optional string which will be appended to objective-C class names. If you're building a plugin, it's important to define this, to avoid name clashes between multiple plugin modules that are dynamically loaded into the same address space.");
  271. if (isAudioPlugin())
  272. {
  273. props.add (new BooleanPropertyComponent (shouldBuildVST(), "Build VST", "Enabled"));
  274. props.getLast()->setTooltip ("Whether the project should produce a VST plugin.");
  275. props.add (new BooleanPropertyComponent (shouldBuildAU(), "Build AudioUnit", "Enabled"));
  276. props.getLast()->setTooltip ("Whether the project should produce an AudioUnit plugin.");
  277. props.add (new BooleanPropertyComponent (shouldBuildRTAS(), "Build RTAS", "Enabled"));
  278. props.getLast()->setTooltip ("Whether the project should produce an RTAS plugin.");
  279. }
  280. if (isAudioPlugin())
  281. {
  282. props.add (new TextPropertyComponent (getPluginName(), "Plugin Name", 128, false));
  283. props.getLast()->setTooltip ("The name of your plugin (keep it short!)");
  284. props.add (new TextPropertyComponent (getPluginDesc(), "Plugin Description", 256, false));
  285. props.getLast()->setTooltip ("A short description of your plugin.");
  286. props.add (new TextPropertyComponent (getPluginManufacturer(), "Plugin Manufacturer", 256, false));
  287. props.getLast()->setTooltip ("The name of your company (cannot be blank).");
  288. props.add (new TextPropertyComponent (getPluginManufacturerCode(), "Plugin Manufacturer Code", 4, false));
  289. 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!");
  290. props.add (new TextPropertyComponent (getPluginCode(), "Plugin Code", 4, false));
  291. 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!");
  292. props.add (new TextPropertyComponent (getPluginChannelConfigs(), "Plugin Channel Configurations", 256, false));
  293. 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 "
  294. "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 "
  295. "and 1 output, or with 2 inputs and 2 outputs.");
  296. props.add (new BooleanPropertyComponent (getPluginIsSynth(), "Plugin is a Synth", "Is a Synth"));
  297. 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.");
  298. props.add (new BooleanPropertyComponent (getPluginWantsMidiInput(), "Plugin Midi Input", "Plugin wants midi input"));
  299. props.getLast()->setTooltip ("Enable this if you want your plugin to accept midi messages.");
  300. props.add (new BooleanPropertyComponent (getPluginProducesMidiOut(), "Plugin Midi Output", "Plugin produces midi output"));
  301. props.getLast()->setTooltip ("Enable this if your plugin is going to produce midi messages.");
  302. props.add (new BooleanPropertyComponent (getPluginSilenceInProducesSilenceOut(), "Silence", "Silence in produces silence out"));
  303. 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.");
  304. props.add (new TextPropertyComponent (getPluginTailLengthSeconds(), "Tail Length (in seconds)", 12, false));
  305. 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.");
  306. props.add (new BooleanPropertyComponent (getPluginEditorNeedsKeyFocus(), "Key Focus", "Plugin editor requires keyboard focus"));
  307. props.getLast()->setTooltip ("Enable this if your plugin needs keyboard input - some hosts can be a bit funny about keyboard focus..");
  308. props.add (new TextPropertyComponent (getPluginAUExportPrefix(), "Plugin AU Export Prefix", 64, false));
  309. 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.");
  310. props.add (new TextPropertyComponent (getPluginAUCocoaViewClassName(), "Plugin AU Cocoa View Name", 64, false));
  311. 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 "
  312. "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.");
  313. props.add (new TextPropertyComponent (getPluginRTASCategory(), "Plugin RTAS Category", 64, false));
  314. 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, "
  315. "ePlugInCategory_PitchShift, ePlugInCategory_Reverb, ePlugInCategory_Delay, "
  316. "ePlugInCategory_Modulation, ePlugInCategory_Harmonic, ePlugInCategory_NoiseReduction, "
  317. "ePlugInCategory_Dither, ePlugInCategory_SoundField");
  318. }
  319. props.add (new TextPropertyComponent (getProjectPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  320. 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.");
  321. for (int i = props.size(); --i >= 0;)
  322. props.getUnchecked(i)->setPreferredHeight (22);
  323. }
  324. const Image Project::getBigIcon()
  325. {
  326. Item icon (getMainGroup().findItemWithID (getBigIconImageItemID().toString()));
  327. if (icon.isValid())
  328. return ImageCache::getFromFile (icon.getFile());
  329. return Image();
  330. }
  331. const Image Project::getSmallIcon()
  332. {
  333. Item icon (getMainGroup().findItemWithID (getSmallIconImageItemID().toString()));
  334. if (icon.isValid())
  335. return ImageCache::getFromFile (icon.getFile());
  336. return Image();
  337. }
  338. const StringPairArray Project::getPreprocessorDefs() const
  339. {
  340. return parsePreprocessorDefs (getProjectPreprocessorDefs().toString());
  341. }
  342. //==============================================================================
  343. Project::Item Project::getMainGroup()
  344. {
  345. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  346. }
  347. Project::Item Project::createNewGroup()
  348. {
  349. Item item (*this, ValueTree (Tags::group));
  350. item.initialiseNodeValues();
  351. item.getName() = "New Group";
  352. return item;
  353. }
  354. Project::Item Project::createNewItem (const File& file)
  355. {
  356. Item item (*this, ValueTree (Tags::file));
  357. item.initialiseNodeValues();
  358. item.getName() = file.getFileName();
  359. item.getShouldCompileValue() = file.hasFileExtension ("cpp;mm;c;m;cc;cxx");
  360. item.getShouldAddToResourceValue() = shouldBeAddedToBinaryResourcesByDefault (file);
  361. return item;
  362. }
  363. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  364. {
  365. if (item.isImageFile())
  366. {
  367. found.add (new Project::Item (item));
  368. }
  369. else if (item.isGroup())
  370. {
  371. for (int i = 0; i < item.getNumChildren(); ++i)
  372. findImages (item.getChild (i), found);
  373. }
  374. }
  375. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  376. {
  377. findImages (getMainGroup(), items);
  378. }
  379. //==============================================================================
  380. Project::Item::Item (Project& project_, const ValueTree& node_)
  381. : project (project_), node (node_)
  382. {
  383. }
  384. Project::Item::Item (const Item& other)
  385. : project (other.project), node (other.node)
  386. {
  387. }
  388. Project::Item::~Item()
  389. {
  390. }
  391. const String Project::Item::getID() const { return node [Ids::id_]; }
  392. const String Project::Item::getImageFileID() const { return "id:" + getID(); }
  393. bool Project::Item::isFile() const { return node.hasType (Tags::file); }
  394. bool Project::Item::isGroup() const { return node.hasType (Tags::group) || isMainGroup(); }
  395. bool Project::Item::isMainGroup() const { return node.hasType (Tags::projectMainGroup); }
  396. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  397. Project::Item Project::Item::findItemWithID (const String& targetId) const
  398. {
  399. if (node [Ids::id_] == targetId)
  400. return *this;
  401. if (isGroup())
  402. {
  403. for (int i = getNumChildren(); --i >= 0;)
  404. {
  405. Item found (getChild(i).findItemWithID (targetId));
  406. if (found.isValid())
  407. return found;
  408. }
  409. }
  410. return Item (project, ValueTree::invalid);
  411. }
  412. bool Project::Item::canContain (const Item& child) const
  413. {
  414. if (isFile())
  415. return false;
  416. if (isGroup())
  417. return child.isFile() || child.isGroup();
  418. jassertfalse
  419. return false;
  420. }
  421. bool Project::Item::shouldBeAddedToTargetProject() const
  422. {
  423. return isFile();
  424. }
  425. bool Project::Item::shouldBeCompiled() const
  426. {
  427. return getShouldCompileValue().getValue();
  428. }
  429. Value Project::Item::getShouldCompileValue() const
  430. {
  431. return node.getPropertyAsValue (Ids::compile, getUndoManager());
  432. }
  433. bool Project::Item::shouldBeAddedToBinaryResources() const
  434. {
  435. return getShouldAddToResourceValue().getValue();
  436. }
  437. Value Project::Item::getShouldAddToResourceValue() const
  438. {
  439. return node.getPropertyAsValue (Ids::resource, getUndoManager());
  440. }
  441. const File Project::Item::getFile() const
  442. {
  443. if (isFile())
  444. return project.resolveFilename (node [Ids::file].toString());
  445. else
  446. return File::nonexistent;
  447. }
  448. void Project::Item::setFile (const File& file)
  449. {
  450. jassert (isFile());
  451. node.setProperty (Ids::file, project.getRelativePathForFile (file), getUndoManager());
  452. node.setProperty (Ids::name, file.getFileName(), getUndoManager());
  453. jassert (getFile() == file);
  454. }
  455. bool Project::Item::renameFile (const File& newFile)
  456. {
  457. const File oldFile (getFile());
  458. if (oldFile.moveFileTo (newFile))
  459. {
  460. setFile (newFile);
  461. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  462. return true;
  463. }
  464. return false;
  465. }
  466. Project::Item Project::Item::findItemForFile (const File& file) const
  467. {
  468. if (getFile() == file)
  469. return *this;
  470. if (isGroup())
  471. {
  472. for (int i = getNumChildren(); --i >= 0;)
  473. {
  474. Item found (getChild(i).findItemForFile (file));
  475. if (found.isValid())
  476. return found;
  477. }
  478. }
  479. return Item (project, ValueTree::invalid);
  480. }
  481. const File Project::Item::determineGroupFolder() const
  482. {
  483. jassert (isGroup());
  484. File f;
  485. for (int i = 0; i < getNumChildren(); ++i)
  486. {
  487. f = getChild(i).getFile();
  488. if (f.exists())
  489. return f.getParentDirectory();
  490. }
  491. Item parent (getParent());
  492. if (parent != *this)
  493. {
  494. f = parent.determineGroupFolder();
  495. if (f.getChildFile (getName().toString()).isDirectory())
  496. f = f.getChildFile (getName().toString());
  497. }
  498. else
  499. {
  500. f = project.getFile().getParentDirectory();
  501. if (f.getChildFile ("Source").isDirectory())
  502. f = f.getChildFile ("Source");
  503. }
  504. return f;
  505. }
  506. void Project::Item::initialiseNodeValues()
  507. {
  508. if (! node.hasProperty (Ids::id_))
  509. node.setProperty (Ids::id_, createAlphaNumericUID(), 0);
  510. if (isFile())
  511. {
  512. node.setProperty (Ids::name, getFile().getFileName(), 0);
  513. }
  514. else if (isGroup())
  515. {
  516. for (int i = getNumChildren(); --i >= 0;)
  517. getChild(i).initialiseNodeValues();
  518. }
  519. }
  520. Value Project::Item::getName() const
  521. {
  522. return node.getPropertyAsValue (Ids::name, getUndoManager());
  523. }
  524. void Project::Item::addChild (const Item& newChild, int insertIndex)
  525. {
  526. node.addChild (newChild.getNode(), insertIndex, getUndoManager());
  527. }
  528. void Project::Item::removeItemFromProject()
  529. {
  530. node.getParent().removeChild (node, getUndoManager());
  531. }
  532. Project::Item Project::Item::getParent() const
  533. {
  534. if (isMainGroup() || ! isGroup())
  535. return *this;
  536. return Item (project, node.getParent());
  537. }
  538. struct ItemSorter
  539. {
  540. static int compareElements (const ValueTree& first, const ValueTree& second)
  541. {
  542. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  543. }
  544. };
  545. void Project::Item::sortAlphabetically()
  546. {
  547. ItemSorter sorter;
  548. node.sort (sorter, getUndoManager(), true);
  549. }
  550. bool Project::Item::addFile (const File& file, int insertIndex)
  551. {
  552. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  553. return false;
  554. if (file.isDirectory())
  555. {
  556. Item group (project.createNewGroup());
  557. group.getName() = file.getFileNameWithoutExtension();
  558. jassert (canContain (group));
  559. addChild (group, insertIndex);
  560. //group.setFile (file);
  561. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  562. while (iter.next())
  563. {
  564. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  565. group.addFile (iter.getFile(), -1);
  566. }
  567. group.sortAlphabetically();
  568. }
  569. else if (file.existsAsFile())
  570. {
  571. if (! project.getMainGroup().findItemForFile (file).isValid())
  572. {
  573. Item item (project.createNewItem (file));
  574. if (canContain (item))
  575. {
  576. item.setFile (file);
  577. addChild (item, insertIndex);
  578. }
  579. }
  580. }
  581. else
  582. {
  583. jassertfalse;
  584. }
  585. return true;
  586. }
  587. const Drawable* Project::Item::getIcon() const
  588. {
  589. if (isFile())
  590. {
  591. if (isImageFile())
  592. return StoredSettings::getInstance()->getImageFileIcon();
  593. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  594. }
  595. else if (isMainGroup())
  596. {
  597. static DrawableImage im;
  598. im.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize));
  599. return &im;
  600. }
  601. else
  602. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  603. }
  604. //==============================================================================
  605. ValueTree Project::getJuceConfigNode()
  606. {
  607. ValueTree configNode = projectRoot.getChildWithName (Tags::configGroup);
  608. if (! configNode.isValid())
  609. {
  610. configNode = ValueTree (Tags::configGroup);
  611. projectRoot.addChild (configNode, -1, 0);
  612. }
  613. return configNode;
  614. }
  615. void Project::getJuceConfigFlags (OwnedArray <JuceConfigFlag>& flags)
  616. {
  617. ValueTree configNode (getJuceConfigNode());
  618. File juceConfigH (getLocalJuceFolder().getChildFile ("juce_Config.h"));
  619. StringArray lines;
  620. lines.addLines (juceConfigH.loadFileAsString());
  621. for (int i = 0; i < lines.size(); ++i)
  622. {
  623. String line (lines[i].trim());
  624. if (line.startsWith ("/** ") && line.containsChar (':'))
  625. {
  626. ScopedPointer <JuceConfigFlag> config (new JuceConfigFlag());
  627. config->symbol = line.substring (4).upToFirstOccurrenceOf (":", false, false).trim();
  628. if (config->symbol.length() > 4)
  629. {
  630. config->description = line.fromFirstOccurrenceOf (":", false, false).trimStart();
  631. ++i;
  632. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  633. {
  634. if (lines[i].trim().isNotEmpty())
  635. config->description = config->description.trim() + " " + lines[i].trim();
  636. ++i;
  637. }
  638. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  639. config->value.referTo (getJuceConfigFlag (config->symbol));
  640. flags.add (config.release());
  641. }
  642. }
  643. }
  644. }
  645. const char* const Project::configFlagDefault = "default";
  646. const char* const Project::configFlagEnabled = "enabled";
  647. const char* const Project::configFlagDisabled = "disabled";
  648. Value Project::getJuceConfigFlag (const String& name)
  649. {
  650. const ValueTree configNode (getJuceConfigNode());
  651. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  652. if (v.getValue().toString().isEmpty())
  653. v = configFlagDefault;
  654. return v;
  655. }
  656. //==============================================================================
  657. ValueTree Project::getConfigurations() const
  658. {
  659. return projectRoot.getChildWithName (Tags::configurations);
  660. }
  661. int Project::getNumConfigurations() const
  662. {
  663. return getConfigurations().getNumChildren();
  664. }
  665. Project::BuildConfiguration Project::getConfiguration (int index)
  666. {
  667. jassert (index < getConfigurations().getNumChildren());
  668. return BuildConfiguration (this, getConfigurations().getChild (index));
  669. }
  670. bool Project::hasConfigurationNamed (const String& name) const
  671. {
  672. const ValueTree configs (getConfigurations());
  673. for (int i = configs.getNumChildren(); --i >= 0;)
  674. if (configs.getChild(i) [Ids::name].toString() == name)
  675. return true;
  676. return false;
  677. }
  678. const String Project::getUniqueConfigName (String name) const
  679. {
  680. String nameRoot (name);
  681. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  682. nameRoot = nameRoot.dropLastCharacters (1);
  683. nameRoot = nameRoot.trim();
  684. int suffix = 2;
  685. while (hasConfigurationNamed (name))
  686. name = nameRoot + " " + String (suffix++);
  687. return name;
  688. }
  689. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  690. {
  691. const String configName (getUniqueConfigName (configToCopy != 0 ? configToCopy->config [Ids::name].toString()
  692. : "New Build Configuration"));
  693. ValueTree configs (getConfigurations());
  694. if (! configs.isValid())
  695. {
  696. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  697. configs = getConfigurations();
  698. }
  699. ValueTree newConfig (Tags::configuration);
  700. if (configToCopy != 0)
  701. newConfig = configToCopy->config.createCopy();
  702. newConfig.setProperty (Ids::name, configName, 0);
  703. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  704. }
  705. void Project::deleteConfiguration (int index)
  706. {
  707. ValueTree configs (getConfigurations());
  708. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  709. }
  710. void Project::createDefaultConfigs()
  711. {
  712. for (int i = 0; i < 2; ++i)
  713. {
  714. addNewConfiguration (0);
  715. BuildConfiguration config = getConfiguration (i);
  716. const bool debugConfig = i == 0;
  717. config.getName() = debugConfig ? "Debug" : "Release";
  718. config.isDebug() = debugConfig;
  719. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  720. config.getTargetBinaryName() = getProjectFilenameRoot();
  721. }
  722. }
  723. //==============================================================================
  724. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  725. : project (project_),
  726. config (configNode)
  727. {
  728. }
  729. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  730. : project (other.project),
  731. config (other.config)
  732. {
  733. }
  734. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  735. {
  736. project = other.project;
  737. config = other.config;
  738. return *this;
  739. }
  740. Project::BuildConfiguration::~BuildConfiguration()
  741. {
  742. }
  743. const String Project::BuildConfiguration::getGCCOptimisationFlag() const
  744. {
  745. const int level = (int) getOptimisationLevel().getValue();
  746. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  747. }
  748. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  749. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  750. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  751. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  752. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  753. {
  754. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  755. props.getLast()->setTooltip ("The name of this configuration.");
  756. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  757. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  758. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  759. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  760. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  761. props.getLast()->setTooltip ("The optimisation level for this configuration");
  762. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  763. 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.");
  764. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  765. 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.");
  766. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  767. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  768. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  769. 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.");
  770. if (getMacSDKVersion().toString().isEmpty())
  771. getMacSDKVersion() = osxVersionDefault;
  772. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  773. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  774. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  775. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  776. if (getMacCompatibilityVersion().toString().isEmpty())
  777. getMacCompatibilityVersion() = osxVersionDefault;
  778. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  779. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  780. for (int i = props.size(); --i >= 0;)
  781. props.getUnchecked(i)->setPreferredHeight (22);
  782. }
  783. const StringPairArray Project::BuildConfiguration::getAllPreprocessorDefs() const
  784. {
  785. return mergePreprocessorDefs (project->getPreprocessorDefs(),
  786. parsePreprocessorDefs (getBuildConfigPreprocessorDefs().toString()));
  787. }
  788. const StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  789. {
  790. StringArray s;
  791. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  792. return s;
  793. }
  794. //==============================================================================
  795. ValueTree Project::getExporters()
  796. {
  797. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  798. if (! exporters.isValid())
  799. {
  800. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  801. exporters = getExporters();
  802. }
  803. return exporters;
  804. }
  805. int Project::getNumExporters()
  806. {
  807. return getExporters().getNumChildren();
  808. }
  809. ProjectExporter* Project::createExporter (int index)
  810. {
  811. jassert (index >= 0 && index < getNumExporters());
  812. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  813. }
  814. void Project::addNewExporter (int exporterIndex)
  815. {
  816. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterIndex));
  817. ValueTree exporters (getExporters());
  818. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  819. }
  820. void Project::deleteExporter (int index)
  821. {
  822. ValueTree exporters (getExporters());
  823. exporters.removeChild (index, getUndoManagerFor (exporters));
  824. }
  825. void Project::createDefaultExporters()
  826. {
  827. ValueTree exporters (getExporters());
  828. exporters.removeAllChildren (getUndoManagerFor (exporters));
  829. for (int i = 0; i < ProjectExporter::getNumExporters(); ++i)
  830. addNewExporter (i);
  831. }
  832. //==============================================================================
  833. const String Project::getFileTemplate (const String& templateName)
  834. {
  835. int dataSize;
  836. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  837. if (data == 0)
  838. {
  839. jassertfalse;
  840. return String::empty;
  841. }
  842. return String::fromUTF8 (data, dataSize);
  843. }
  844. //==============================================================================
  845. void Project::resaveJucerFile (const File& file)
  846. {
  847. if (! file.exists())
  848. {
  849. std::cout << "The file " << file.getFullPathName() << " doesn't exist!" << std::endl;
  850. return;
  851. }
  852. if (! file.hasFileExtension (Project::projectFileExtension))
  853. {
  854. std::cout << file.getFullPathName() << " isn't a valid jucer project file!" << std::endl;
  855. return;
  856. }
  857. Project newDoc (file);
  858. if (! newDoc.loadFrom (file, true))
  859. {
  860. std::cout << "Failed to load the project file: " << file.getFullPathName() << std::endl;
  861. return;
  862. }
  863. std::cout << "The Jucer - Re-saving file: " << file.getFullPathName() << std::endl;
  864. String error (newDoc.saveDocument (file));
  865. if (error.isNotEmpty())
  866. {
  867. std::cout << "Error when writing project: " << error << std::endl;
  868. return;
  869. }
  870. }