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.

342 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_JucerDocument.h"
  19. //==============================================================================
  20. BinaryResources::BinaryResources()
  21. {
  22. }
  23. BinaryResources::~BinaryResources()
  24. {
  25. }
  26. BinaryResources& BinaryResources::operator= (const BinaryResources& other)
  27. {
  28. for (int i = 0; i < other.resources.size(); ++i)
  29. add (other.resources[i]->name,
  30. other.resources[i]->originalFilename,
  31. other.resources[i]->data);
  32. return *this;
  33. }
  34. void BinaryResources::changed()
  35. {
  36. if (document != nullptr)
  37. {
  38. document->changed();
  39. document->refreshAllPropertyComps();
  40. }
  41. }
  42. //==============================================================================
  43. void BinaryResources::clear()
  44. {
  45. if (resources.size() > 0)
  46. {
  47. resources.clear();
  48. changed();
  49. }
  50. }
  51. StringArray BinaryResources::getResourceNames() const
  52. {
  53. StringArray s;
  54. for (int i = 0; i < resources.size(); ++i)
  55. s.add (resources.getUnchecked(i)->name);
  56. return s;
  57. }
  58. BinaryResources::BinaryResource* BinaryResources::findResource (const String& name) const noexcept
  59. {
  60. for (int i = resources.size(); --i >= 0;)
  61. if (resources.getUnchecked(i)->name == name)
  62. return resources.getUnchecked(i);
  63. return nullptr;
  64. }
  65. const BinaryResources::BinaryResource* BinaryResources::getResource (const String& name) const
  66. {
  67. return findResource (name);
  68. }
  69. const BinaryResources::BinaryResource* BinaryResources::getResourceForFile (const File& file) const
  70. {
  71. for (int i = resources.size(); --i >= 0;)
  72. if (resources.getUnchecked(i)->originalFilename == file.getFullPathName())
  73. return resources.getUnchecked(i);
  74. return nullptr;
  75. }
  76. bool BinaryResources::add (const String& name, const File& file)
  77. {
  78. MemoryBlock mb;
  79. if (! file.loadFileAsData (mb))
  80. return false;
  81. add (name, file.getFullPathName(), mb);
  82. return true;
  83. }
  84. void BinaryResources::add (const String& name, const String& originalFileName, const MemoryBlock& data)
  85. {
  86. BinaryResource* r = findResource (name);
  87. if (r == nullptr)
  88. {
  89. resources.add (r = new BinaryResource());
  90. r->name = name;
  91. }
  92. r->originalFilename = originalFileName;
  93. r->data = data;
  94. r->drawable = nullptr;
  95. changed();
  96. }
  97. bool BinaryResources::reload (const int index)
  98. {
  99. return resources[index] != 0
  100. && add (resources [index]->name,
  101. File (resources [index]->originalFilename));
  102. }
  103. String BinaryResources::browseForResource (const String& title,
  104. const String& wildcard,
  105. const File& fileToStartFrom,
  106. const String& resourceToReplace)
  107. {
  108. FileChooser fc (title, fileToStartFrom, wildcard);
  109. if (fc.browseForFileToOpen())
  110. {
  111. String name (resourceToReplace);
  112. if (name.isEmpty())
  113. name = findUniqueName (fc.getResult().getFileName());
  114. if (! add (name, fc.getResult()))
  115. {
  116. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  117. TRANS("Adding Resource"),
  118. TRANS("Failed to load the file!"));
  119. name.clear();
  120. }
  121. return name;
  122. }
  123. return String();
  124. }
  125. String BinaryResources::findUniqueName (const String& rootName) const
  126. {
  127. String nameRoot (CodeHelpers::makeValidIdentifier (rootName, true, true, false));
  128. String name (nameRoot);
  129. const StringArray names (getResourceNames());
  130. int suffix = 1;
  131. while (names.contains (name))
  132. name = nameRoot + String (++suffix);
  133. return name;
  134. }
  135. void BinaryResources::remove (const int i)
  136. {
  137. if (resources[i] != 0)
  138. {
  139. resources.remove (i);
  140. changed();
  141. }
  142. }
  143. const Drawable* BinaryResources::getDrawable (const String& name) const
  144. {
  145. if (BinaryResources::BinaryResource* const res = const_cast<BinaryResources::BinaryResource*> (getResource (name)))
  146. {
  147. if (res->drawable == nullptr && res->data.getSize() > 0)
  148. res->drawable = Drawable::createFromImageData (res->data.getData(),
  149. res->data.getSize());
  150. return res->drawable;
  151. }
  152. return nullptr;
  153. }
  154. Image BinaryResources::getImageFromCache (const String& name) const
  155. {
  156. if (const BinaryResources::BinaryResource* const res = getResource (name))
  157. if (res->data.getSize() > 0)
  158. return ImageCache::getFromMemory (res->data.getData(), (int) res->data.getSize());
  159. return Image();
  160. }
  161. void BinaryResources::loadFromCpp (const File& cppFileLocation, const String& cppFile)
  162. {
  163. StringArray cpp;
  164. cpp.addLines (cppFile);
  165. clear();
  166. for (int i = 0; i < cpp.size(); ++i)
  167. {
  168. if (cpp[i].contains ("JUCER_RESOURCE:"))
  169. {
  170. StringArray tokens;
  171. tokens.addTokens (cpp[i].fromFirstOccurrenceOf (":", false, false), ",", "\"'");
  172. tokens.trim();
  173. tokens.removeEmptyStrings();
  174. const String resourceName (tokens[0]);
  175. const int resourceSize = tokens[1].getIntValue();
  176. const String originalFileName (cppFileLocation.getSiblingFile (tokens[2].unquoted()).getFullPathName());
  177. jassert (resourceName.isNotEmpty() && resourceSize > 0);
  178. if (resourceName.isNotEmpty() && resourceSize > 0)
  179. {
  180. const int firstLine = i;
  181. while (i < cpp.size())
  182. if (cpp [i++].contains ("}"))
  183. break;
  184. const String dataString (cpp.joinIntoString (" ", firstLine, i - firstLine)
  185. .fromFirstOccurrenceOf ("{", false, false));
  186. MemoryOutputStream out;
  187. String::CharPointerType t (dataString.getCharPointer());
  188. int n = 0;
  189. while (! t.isEmpty())
  190. {
  191. const juce_wchar c = t.getAndAdvance();
  192. if (c >= '0' && c <= '9')
  193. n = n * 10 + (c - '0');
  194. else if (c == ',')
  195. {
  196. out.writeByte ((char) n);
  197. n = 0;
  198. }
  199. else if (c == '}')
  200. break;
  201. }
  202. jassert (resourceSize < (int) out.getDataSize() && resourceSize > (int) out.getDataSize() - 2);
  203. MemoryBlock mb (out.getData(), out.getDataSize());
  204. mb.setSize ((size_t) resourceSize);
  205. add (resourceName, originalFileName, mb);
  206. }
  207. }
  208. }
  209. }
  210. //==============================================================================
  211. void BinaryResources::fillInGeneratedCode (GeneratedCode& code) const
  212. {
  213. if (resources.size() > 0)
  214. {
  215. code.publicMemberDeclarations << "// Binary resources:\n";
  216. MemoryOutputStream defs;
  217. defs << "//==============================================================================\n";
  218. defs << "// Binary resources - be careful not to edit any of these sections!\n\n";
  219. for (int i = 0; i < resources.size(); ++i)
  220. {
  221. code.publicMemberDeclarations
  222. << "static const char* "
  223. << resources[i]->name
  224. << ";\nstatic const int "
  225. << resources[i]->name
  226. << "Size;\n";
  227. const String name (resources[i]->name);
  228. const MemoryBlock& mb = resources[i]->data;
  229. defs << "// JUCER_RESOURCE: " << name << ", " << (int) mb.getSize()
  230. << ", \""
  231. << File (resources[i]->originalFilename)
  232. .getRelativePathFrom (code.document->getCppFile())
  233. .replaceCharacter ('\\', '/')
  234. << "\"\n";
  235. String line1;
  236. line1 << "static const unsigned char resource_"
  237. << code.className << "_" << name << "[] = { ";
  238. defs << line1;
  239. int charsOnLine = line1.length();
  240. for (size_t j = 0; j < mb.getSize(); ++j)
  241. {
  242. const int num = (int) (unsigned char) mb[j];
  243. defs << num << ',';
  244. charsOnLine += 2;
  245. if (num >= 10) ++charsOnLine;
  246. if (num >= 100) ++charsOnLine;
  247. if (charsOnLine >= 200)
  248. {
  249. charsOnLine = 0;
  250. defs << '\n';
  251. }
  252. }
  253. defs
  254. << "0,0};\n\n"
  255. "const char* " << code.className << "::" << name
  256. << " = (const char*) resource_" << code.className << "_" << name
  257. << ";\nconst int "
  258. << code.className << "::" << name << "Size = "
  259. << (int) mb.getSize()
  260. << ";\n\n";
  261. }
  262. code.staticMemberDefinitions << defs.toString();
  263. }
  264. }