The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1149 lines
41KB

  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 Image Project::getBestIconForSize (int size, bool returnNullIfNothingBigEnough)
  346. {
  347. Image im;
  348. const Image im1 (getSmallIcon());
  349. const Image im2 (getBigIcon());
  350. if (im1.isValid() && im2.isValid())
  351. {
  352. if (im1.getWidth() >= size && im2.getWidth() >= size)
  353. im = im1.getWidth() < im2.getWidth() ? im1 : im2;
  354. else if (im1.getWidth() >= size)
  355. im = im1;
  356. else if (im2.getWidth() >= size)
  357. im = im2;
  358. else
  359. return Image();
  360. }
  361. else
  362. {
  363. im = im1.isValid() ? im1 : im2;
  364. }
  365. if (size == im.getWidth() && size == im.getHeight())
  366. return im;
  367. if (returnNullIfNothingBigEnough && im.getWidth() < size && im.getHeight() < size)
  368. return Image::null;
  369. Image newIm (Image::ARGB, size, size, true);
  370. Graphics g (newIm);
  371. g.drawImageWithin (im, 0, 0, size, size,
  372. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
  373. return newIm;
  374. }
  375. const StringPairArray Project::getPreprocessorDefs() const
  376. {
  377. return parsePreprocessorDefs (getProjectPreprocessorDefs().toString());
  378. }
  379. //==============================================================================
  380. Project::Item Project::getMainGroup()
  381. {
  382. return Item (*this, projectRoot.getChildWithName (Tags::projectMainGroup));
  383. }
  384. Project::Item Project::createNewGroup()
  385. {
  386. Item item (*this, ValueTree (Tags::group));
  387. item.initialiseNodeValues();
  388. item.getName() = "New Group";
  389. return item;
  390. }
  391. Project::Item Project::createNewItem (const File& file)
  392. {
  393. Item item (*this, ValueTree (Tags::file));
  394. item.initialiseNodeValues();
  395. item.getName() = file.getFileName();
  396. item.getShouldCompileValue() = file.hasFileExtension ("cpp;mm;c;m;cc;cxx");
  397. item.getShouldAddToResourceValue() = shouldBeAddedToBinaryResourcesByDefault (file);
  398. return item;
  399. }
  400. static void findImages (const Project::Item& item, OwnedArray<Project::Item>& found)
  401. {
  402. if (item.isImageFile())
  403. {
  404. found.add (new Project::Item (item));
  405. }
  406. else if (item.isGroup())
  407. {
  408. for (int i = 0; i < item.getNumChildren(); ++i)
  409. findImages (item.getChild (i), found);
  410. }
  411. }
  412. void Project::findAllImageItems (OwnedArray<Project::Item>& items)
  413. {
  414. findImages (getMainGroup(), items);
  415. }
  416. //==============================================================================
  417. Project::Item::Item (Project& project_, const ValueTree& node_)
  418. : project (project_), node (node_)
  419. {
  420. }
  421. Project::Item::Item (const Item& other)
  422. : project (other.project), node (other.node)
  423. {
  424. }
  425. Project::Item::~Item()
  426. {
  427. }
  428. const String Project::Item::getID() const { return node [Ids::id_]; }
  429. const String Project::Item::getImageFileID() const { return "id:" + getID(); }
  430. bool Project::Item::isFile() const { return node.hasType (Tags::file); }
  431. bool Project::Item::isGroup() const { return node.hasType (Tags::group) || isMainGroup(); }
  432. bool Project::Item::isMainGroup() const { return node.hasType (Tags::projectMainGroup); }
  433. bool Project::Item::isImageFile() const { return isFile() && getFile().hasFileExtension ("png;jpg;jpeg;gif;drawable"); }
  434. Project::Item Project::Item::findItemWithID (const String& targetId) const
  435. {
  436. if (node [Ids::id_] == targetId)
  437. return *this;
  438. if (isGroup())
  439. {
  440. for (int i = getNumChildren(); --i >= 0;)
  441. {
  442. Item found (getChild(i).findItemWithID (targetId));
  443. if (found.isValid())
  444. return found;
  445. }
  446. }
  447. return Item (project, ValueTree::invalid);
  448. }
  449. bool Project::Item::canContain (const Item& child) const
  450. {
  451. if (isFile())
  452. return false;
  453. if (isGroup())
  454. return child.isFile() || child.isGroup();
  455. jassertfalse
  456. return false;
  457. }
  458. bool Project::Item::shouldBeAddedToTargetProject() const
  459. {
  460. return isFile();
  461. }
  462. bool Project::Item::shouldBeCompiled() const
  463. {
  464. return getShouldCompileValue().getValue();
  465. }
  466. Value Project::Item::getShouldCompileValue() const
  467. {
  468. return node.getPropertyAsValue (Ids::compile, getUndoManager());
  469. }
  470. bool Project::Item::shouldBeAddedToBinaryResources() const
  471. {
  472. return getShouldAddToResourceValue().getValue();
  473. }
  474. Value Project::Item::getShouldAddToResourceValue() const
  475. {
  476. return node.getPropertyAsValue (Ids::resource, getUndoManager());
  477. }
  478. const File Project::Item::getFile() const
  479. {
  480. if (isFile())
  481. return project.resolveFilename (node [Ids::file].toString());
  482. else
  483. return File::nonexistent;
  484. }
  485. void Project::Item::setFile (const File& file)
  486. {
  487. jassert (isFile());
  488. node.setProperty (Ids::file, project.getRelativePathForFile (file), getUndoManager());
  489. node.setProperty (Ids::name, file.getFileName(), getUndoManager());
  490. jassert (getFile() == file);
  491. }
  492. bool Project::Item::renameFile (const File& newFile)
  493. {
  494. const File oldFile (getFile());
  495. if (oldFile.moveFileTo (newFile))
  496. {
  497. setFile (newFile);
  498. OpenDocumentManager::getInstance()->fileHasBeenRenamed (oldFile, newFile);
  499. return true;
  500. }
  501. return false;
  502. }
  503. Project::Item Project::Item::findItemForFile (const File& file) const
  504. {
  505. if (getFile() == file)
  506. return *this;
  507. if (isGroup())
  508. {
  509. for (int i = getNumChildren(); --i >= 0;)
  510. {
  511. Item found (getChild(i).findItemForFile (file));
  512. if (found.isValid())
  513. return found;
  514. }
  515. }
  516. return Item (project, ValueTree::invalid);
  517. }
  518. const File Project::Item::determineGroupFolder() const
  519. {
  520. jassert (isGroup());
  521. File f;
  522. for (int i = 0; i < getNumChildren(); ++i)
  523. {
  524. f = getChild(i).getFile();
  525. if (f.exists())
  526. return f.getParentDirectory();
  527. }
  528. Item parent (getParent());
  529. if (parent != *this)
  530. {
  531. f = parent.determineGroupFolder();
  532. if (f.getChildFile (getName().toString()).isDirectory())
  533. f = f.getChildFile (getName().toString());
  534. }
  535. else
  536. {
  537. f = project.getFile().getParentDirectory();
  538. if (f.getChildFile ("Source").isDirectory())
  539. f = f.getChildFile ("Source");
  540. }
  541. return f;
  542. }
  543. void Project::Item::initialiseNodeValues()
  544. {
  545. if (! node.hasProperty (Ids::id_))
  546. node.setProperty (Ids::id_, createAlphaNumericUID(), 0);
  547. if (isFile())
  548. {
  549. node.setProperty (Ids::name, getFile().getFileName(), 0);
  550. }
  551. else if (isGroup())
  552. {
  553. for (int i = getNumChildren(); --i >= 0;)
  554. getChild(i).initialiseNodeValues();
  555. }
  556. }
  557. Value Project::Item::getName() const
  558. {
  559. return node.getPropertyAsValue (Ids::name, getUndoManager());
  560. }
  561. void Project::Item::addChild (const Item& newChild, int insertIndex)
  562. {
  563. node.addChild (newChild.getNode(), insertIndex, getUndoManager());
  564. }
  565. void Project::Item::removeItemFromProject()
  566. {
  567. node.getParent().removeChild (node, getUndoManager());
  568. }
  569. Project::Item Project::Item::getParent() const
  570. {
  571. if (isMainGroup() || ! isGroup())
  572. return *this;
  573. return Item (project, node.getParent());
  574. }
  575. struct ItemSorter
  576. {
  577. static int compareElements (const ValueTree& first, const ValueTree& second)
  578. {
  579. return first [Ids::name].toString().compareIgnoreCase (second [Ids::name].toString());
  580. }
  581. };
  582. void Project::Item::sortAlphabetically()
  583. {
  584. ItemSorter sorter;
  585. node.sort (sorter, getUndoManager(), true);
  586. }
  587. bool Project::Item::addFile (const File& file, int insertIndex)
  588. {
  589. if (file == File::nonexistent || file.isHidden() || file.getFileName().startsWithChar ('.'))
  590. return false;
  591. if (file.isDirectory())
  592. {
  593. Item group (project.createNewGroup());
  594. group.getName() = file.getFileNameWithoutExtension();
  595. jassert (canContain (group));
  596. addChild (group, insertIndex);
  597. //group.setFile (file);
  598. DirectoryIterator iter (file, false, "*", File::findFilesAndDirectories);
  599. while (iter.next())
  600. {
  601. if (! project.getMainGroup().findItemForFile (iter.getFile()).isValid())
  602. group.addFile (iter.getFile(), -1);
  603. }
  604. group.sortAlphabetically();
  605. }
  606. else if (file.existsAsFile())
  607. {
  608. if (! project.getMainGroup().findItemForFile (file).isValid())
  609. {
  610. Item item (project.createNewItem (file));
  611. if (canContain (item))
  612. {
  613. item.setFile (file);
  614. addChild (item, insertIndex);
  615. }
  616. }
  617. }
  618. else
  619. {
  620. jassertfalse;
  621. }
  622. return true;
  623. }
  624. const Drawable* Project::Item::getIcon() const
  625. {
  626. if (isFile())
  627. {
  628. if (isImageFile())
  629. return StoredSettings::getInstance()->getImageFileIcon();
  630. return LookAndFeel::getDefaultLookAndFeel().getDefaultDocumentFileImage();
  631. }
  632. else if (isMainGroup())
  633. {
  634. return &(getProject().mainProjectIcon);
  635. }
  636. return LookAndFeel::getDefaultLookAndFeel().getDefaultFolderImage();
  637. }
  638. //==============================================================================
  639. ValueTree Project::getJuceConfigNode()
  640. {
  641. ValueTree configNode = projectRoot.getChildWithName (Tags::configGroup);
  642. if (! configNode.isValid())
  643. {
  644. configNode = ValueTree (Tags::configGroup);
  645. projectRoot.addChild (configNode, -1, 0);
  646. }
  647. return configNode;
  648. }
  649. void Project::getJuceConfigFlags (OwnedArray <JuceConfigFlag>& flags)
  650. {
  651. ValueTree configNode (getJuceConfigNode());
  652. File juceConfigH (getLocalJuceFolder().getChildFile ("juce_Config.h"));
  653. StringArray lines;
  654. lines.addLines (juceConfigH.loadFileAsString());
  655. for (int i = 0; i < lines.size(); ++i)
  656. {
  657. String line (lines[i].trim());
  658. if (line.startsWith ("/** ") && line.containsChar (':'))
  659. {
  660. ScopedPointer <JuceConfigFlag> config (new JuceConfigFlag());
  661. config->symbol = line.substring (4).upToFirstOccurrenceOf (":", false, false).trim();
  662. if (config->symbol.length() > 4)
  663. {
  664. config->description = line.fromFirstOccurrenceOf (":", false, false).trimStart();
  665. ++i;
  666. while (! (lines[i].contains ("*/") || lines[i].contains ("@see")))
  667. {
  668. if (lines[i].trim().isNotEmpty())
  669. config->description = config->description.trim() + " " + lines[i].trim();
  670. ++i;
  671. }
  672. config->description = config->description.upToFirstOccurrenceOf ("*/", false, false);
  673. config->value.referTo (getJuceConfigFlag (config->symbol));
  674. flags.add (config.release());
  675. }
  676. }
  677. }
  678. }
  679. const char* const Project::configFlagDefault = "default";
  680. const char* const Project::configFlagEnabled = "enabled";
  681. const char* const Project::configFlagDisabled = "disabled";
  682. Value Project::getJuceConfigFlag (const String& name)
  683. {
  684. const ValueTree configNode (getJuceConfigNode());
  685. Value v (configNode.getPropertyAsValue (name, getUndoManagerFor (configNode)));
  686. if (v.getValue().toString().isEmpty())
  687. v = configFlagDefault;
  688. return v;
  689. }
  690. //==============================================================================
  691. ValueTree Project::getConfigurations() const
  692. {
  693. return projectRoot.getChildWithName (Tags::configurations);
  694. }
  695. int Project::getNumConfigurations() const
  696. {
  697. return getConfigurations().getNumChildren();
  698. }
  699. Project::BuildConfiguration Project::getConfiguration (int index)
  700. {
  701. jassert (index < getConfigurations().getNumChildren());
  702. return BuildConfiguration (this, getConfigurations().getChild (index));
  703. }
  704. bool Project::hasConfigurationNamed (const String& name) const
  705. {
  706. const ValueTree configs (getConfigurations());
  707. for (int i = configs.getNumChildren(); --i >= 0;)
  708. if (configs.getChild(i) [Ids::name].toString() == name)
  709. return true;
  710. return false;
  711. }
  712. const String Project::getUniqueConfigName (String name) const
  713. {
  714. String nameRoot (name);
  715. while (CharacterFunctions::isDigit (nameRoot.getLastCharacter()))
  716. nameRoot = nameRoot.dropLastCharacters (1);
  717. nameRoot = nameRoot.trim();
  718. int suffix = 2;
  719. while (hasConfigurationNamed (name))
  720. name = nameRoot + " " + String (suffix++);
  721. return name;
  722. }
  723. void Project::addNewConfiguration (BuildConfiguration* configToCopy)
  724. {
  725. const String configName (getUniqueConfigName (configToCopy != 0 ? configToCopy->config [Ids::name].toString()
  726. : "New Build Configuration"));
  727. ValueTree configs (getConfigurations());
  728. if (! configs.isValid())
  729. {
  730. projectRoot.addChild (ValueTree (Tags::configurations), 0, getUndoManagerFor (projectRoot));
  731. configs = getConfigurations();
  732. }
  733. ValueTree newConfig (Tags::configuration);
  734. if (configToCopy != 0)
  735. newConfig = configToCopy->config.createCopy();
  736. newConfig.setProperty (Ids::name, configName, 0);
  737. configs.addChild (newConfig, -1, getUndoManagerFor (configs));
  738. }
  739. void Project::deleteConfiguration (int index)
  740. {
  741. ValueTree configs (getConfigurations());
  742. configs.removeChild (index, getUndoManagerFor (getConfigurations()));
  743. }
  744. void Project::createDefaultConfigs()
  745. {
  746. for (int i = 0; i < 2; ++i)
  747. {
  748. addNewConfiguration (0);
  749. BuildConfiguration config = getConfiguration (i);
  750. const bool debugConfig = i == 0;
  751. config.getName() = debugConfig ? "Debug" : "Release";
  752. config.isDebug() = debugConfig;
  753. config.getOptimisationLevel() = debugConfig ? 1 : 2;
  754. config.getTargetBinaryName() = getProjectFilenameRoot();
  755. }
  756. }
  757. //==============================================================================
  758. Project::BuildConfiguration::BuildConfiguration (Project* project_, const ValueTree& configNode)
  759. : project (project_),
  760. config (configNode)
  761. {
  762. }
  763. Project::BuildConfiguration::BuildConfiguration (const BuildConfiguration& other)
  764. : project (other.project),
  765. config (other.config)
  766. {
  767. }
  768. const Project::BuildConfiguration& Project::BuildConfiguration::operator= (const BuildConfiguration& other)
  769. {
  770. project = other.project;
  771. config = other.config;
  772. return *this;
  773. }
  774. Project::BuildConfiguration::~BuildConfiguration()
  775. {
  776. }
  777. const String Project::BuildConfiguration::getGCCOptimisationFlag() const
  778. {
  779. const int level = (int) getOptimisationLevel().getValue();
  780. return String (level <= 1 ? "0" : (level == 2 ? "s" : "3"));
  781. }
  782. const char* const Project::BuildConfiguration::osxVersionDefault = "default";
  783. const char* const Project::BuildConfiguration::osxVersion10_4 = "10.4 SDK";
  784. const char* const Project::BuildConfiguration::osxVersion10_5 = "10.5 SDK";
  785. const char* const Project::BuildConfiguration::osxVersion10_6 = "10.6 SDK";
  786. void Project::BuildConfiguration::createPropertyEditors (Array <PropertyComponent*>& props)
  787. {
  788. props.add (new TextPropertyComponent (getName(), "Name", 96, false));
  789. props.getLast()->setTooltip ("The name of this configuration.");
  790. props.add (new BooleanPropertyComponent (isDebug(), "Debug mode", "Debugging enabled"));
  791. props.getLast()->setTooltip ("If enabled, this means that the configuration should be built with debug synbols.");
  792. const char* optimisationLevels[] = { "No optimisation", "Optimise for size and speed", "Optimise for maximum speed", 0 };
  793. const int optimisationLevelValues[] = { 1, 2, 3, 0 };
  794. props.add (new ChoicePropertyComponent (getOptimisationLevel(), "Optimisation", StringArray (optimisationLevels), Array<var> (optimisationLevelValues)));
  795. props.getLast()->setTooltip ("The optimisation level for this configuration");
  796. props.add (new TextPropertyComponent (getTargetBinaryName(), "Binary name", 256, false));
  797. 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.");
  798. props.add (new TextPropertyComponent (getTargetBinaryRelativePath(), "Binary location", 1024, false));
  799. 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.");
  800. props.add (new TextPropertyComponent (getHeaderSearchPath(), "Header search path", 16384, false));
  801. props.getLast()->setTooltip ("Extra header search paths. Use semi-colons to separate multiple paths.");
  802. props.add (new TextPropertyComponent (getBuildConfigPreprocessorDefs(), "Preprocessor definitions", 32768, false));
  803. 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.");
  804. if (getMacSDKVersion().toString().isEmpty())
  805. getMacSDKVersion() = osxVersionDefault;
  806. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  807. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, 0 };
  808. props.add (new ChoicePropertyComponent (getMacSDKVersion(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  809. props.getLast()->setTooltip ("The version of OSX to link against in the XCode build.");
  810. if (getMacCompatibilityVersion().toString().isEmpty())
  811. getMacCompatibilityVersion() = osxVersionDefault;
  812. props.add (new ChoicePropertyComponent (getMacCompatibilityVersion(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)));
  813. props.getLast()->setTooltip ("The minimum version of OSX that the target binary will be compatible with.");
  814. for (int i = props.size(); --i >= 0;)
  815. props.getUnchecked(i)->setPreferredHeight (22);
  816. }
  817. const StringPairArray Project::BuildConfiguration::getAllPreprocessorDefs() const
  818. {
  819. return mergePreprocessorDefs (project->getPreprocessorDefs(),
  820. parsePreprocessorDefs (getBuildConfigPreprocessorDefs().toString()));
  821. }
  822. const StringArray Project::BuildConfiguration::getHeaderSearchPaths() const
  823. {
  824. StringArray s;
  825. s.addTokens (getHeaderSearchPath().toString(), ";", String::empty);
  826. return s;
  827. }
  828. //==============================================================================
  829. ValueTree Project::getExporters()
  830. {
  831. ValueTree exporters (projectRoot.getChildWithName (Tags::exporters));
  832. if (! exporters.isValid())
  833. {
  834. projectRoot.addChild (ValueTree (Tags::exporters), 0, getUndoManagerFor (projectRoot));
  835. exporters = getExporters();
  836. }
  837. return exporters;
  838. }
  839. int Project::getNumExporters()
  840. {
  841. return getExporters().getNumChildren();
  842. }
  843. ProjectExporter* Project::createExporter (int index)
  844. {
  845. jassert (index >= 0 && index < getNumExporters());
  846. return ProjectExporter::createExporter (*this, getExporters().getChild (index));
  847. }
  848. void Project::addNewExporter (int exporterIndex)
  849. {
  850. ScopedPointer<ProjectExporter> exp (ProjectExporter::createNewExporter (*this, exporterIndex));
  851. ValueTree exporters (getExporters());
  852. exporters.addChild (exp->getSettings(), -1, getUndoManagerFor (exporters));
  853. }
  854. void Project::deleteExporter (int index)
  855. {
  856. ValueTree exporters (getExporters());
  857. exporters.removeChild (index, getUndoManagerFor (exporters));
  858. }
  859. void Project::createDefaultExporters()
  860. {
  861. ValueTree exporters (getExporters());
  862. exporters.removeAllChildren (getUndoManagerFor (exporters));
  863. for (int i = 0; i < ProjectExporter::getNumExporters(); ++i)
  864. addNewExporter (i);
  865. }
  866. //==============================================================================
  867. const String Project::getFileTemplate (const String& templateName)
  868. {
  869. int dataSize;
  870. const char* data = BinaryData::getNamedResource (templateName.toUTF8(), dataSize);
  871. if (data == 0)
  872. {
  873. jassertfalse;
  874. return String::empty;
  875. }
  876. return String::fromUTF8 (data, dataSize);
  877. }
  878. //==============================================================================
  879. void Project::resaveJucerFile (const File& file)
  880. {
  881. if (! file.exists())
  882. {
  883. std::cout << "The file " << file.getFullPathName() << " doesn't exist!" << std::endl;
  884. return;
  885. }
  886. if (! file.hasFileExtension (Project::projectFileExtension))
  887. {
  888. std::cout << file.getFullPathName() << " isn't a valid jucer project file!" << std::endl;
  889. return;
  890. }
  891. Project newDoc (file);
  892. if (! newDoc.loadFrom (file, true))
  893. {
  894. std::cout << "Failed to load the project file: " << file.getFullPathName() << std::endl;
  895. return;
  896. }
  897. std::cout << "The Jucer - Re-saving file: " << file.getFullPathName() << std::endl;
  898. String error (newDoc.saveDocument (file));
  899. if (error.isNotEmpty())
  900. {
  901. std::cout << "Error when writing project: " << error << std::endl;
  902. return;
  903. }
  904. }