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.

576 lines
17KB

  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_DrawableDocument.h"
  19. #include "jucer_DrawableTypeHandler.h"
  20. //==============================================================================
  21. namespace Tags
  22. {
  23. const Identifier drawableTag ("DRAWABLE");
  24. const Identifier markersGroupXTag ("MARKERS_X");
  25. const Identifier markersGroupYTag ("MARKERS_Y");
  26. }
  27. //==============================================================================
  28. DrawableDocument::DrawableDocument (Project* project_)
  29. : project (project_),
  30. root (Tags::drawableTag),
  31. saveAsXml (true),
  32. needsSaving (false)
  33. {
  34. DrawableComposite dc;
  35. root.addChild (dc.createValueTree (0), -1, 0);
  36. setName ("Drawable");
  37. checkRootObject();
  38. root.addListener (this);
  39. }
  40. DrawableDocument::~DrawableDocument()
  41. {
  42. root.removeListener (this);
  43. }
  44. void DrawableDocument::recursivelyUpdateIDs (Drawable::ValueTreeWrapperBase& d, StringArray& recentlyUsedIdCache)
  45. {
  46. if (d.getID().isEmpty())
  47. d.setID (createUniqueID (d.getState().getType().toString().toLowerCase() + "1", recentlyUsedIdCache), 0);
  48. if (d.getState().getType() == DrawableComposite::valueTreeType)
  49. {
  50. const DrawableComposite::ValueTreeWrapper composite (d.getState());
  51. for (int i = 0; i < composite.getNumDrawables(); ++i)
  52. {
  53. Drawable::ValueTreeWrapperBase child (composite.getDrawableState (i));
  54. recursivelyUpdateIDs (child, recentlyUsedIdCache);
  55. }
  56. }
  57. }
  58. void DrawableDocument::checkRootObject()
  59. {
  60. if (! root.hasProperty (Ids::id_))
  61. root.setProperty (Ids::id_, createAlphaNumericUID(), 0);
  62. if (markersX == 0)
  63. markersX = new MarkerList (*this, true);
  64. if (markersY == 0)
  65. markersY = new MarkerList (*this, false);
  66. DrawableComposite::ValueTreeWrapper rootObject (getRootDrawableNode());
  67. StringArray idCache;
  68. recursivelyUpdateIDs (rootObject, idCache);
  69. }
  70. const String DrawableDocument::getUniqueId() const
  71. {
  72. return root [Ids::id_];
  73. }
  74. //==============================================================================
  75. void DrawableDocument::setName (const String& name)
  76. {
  77. root.setProperty (Ids::name, name, getUndoManager());
  78. }
  79. const String DrawableDocument::getName() const
  80. {
  81. return root [Ids::name];
  82. }
  83. bool DrawableDocument::hasChangedSinceLastSave() const
  84. {
  85. return needsSaving;
  86. }
  87. bool DrawableDocument::reload (const File& drawableFile)
  88. {
  89. ScopedPointer <InputStream> stream (drawableFile.createInputStream());
  90. if (stream != 0 && load (*stream))
  91. {
  92. checkRootObject();
  93. undoManager.clearUndoHistory();
  94. needsSaving = false;
  95. return true;
  96. }
  97. return false;
  98. }
  99. bool DrawableDocument::save (const File& drawableFile)
  100. {
  101. TemporaryFile tempFile (drawableFile);
  102. ScopedPointer <OutputStream> out (tempFile.getFile().createOutputStream());
  103. if (out == 0)
  104. return false;
  105. save (*out);
  106. needsSaving = ! tempFile.overwriteTargetFileWithTemporary();
  107. return ! needsSaving;
  108. }
  109. void DrawableDocument::save (OutputStream& output)
  110. {
  111. if (saveAsXml)
  112. {
  113. ScopedPointer <XmlElement> xml (root.createXml());
  114. jassert (xml != 0);
  115. if (xml != 0)
  116. xml->writeToStream (output, String::empty, false, false);
  117. }
  118. else
  119. {
  120. root.writeToStream (output);
  121. }
  122. }
  123. bool DrawableDocument::load (InputStream& input)
  124. {
  125. int64 originalPos = input.getPosition();
  126. ValueTree loadedTree;
  127. XmlDocument xmlDoc (input.readEntireStreamAsString());
  128. ScopedPointer <XmlElement> xml (xmlDoc.getDocumentElement());
  129. if (xml != 0)
  130. {
  131. loadedTree = ValueTree::fromXml (*xml);
  132. }
  133. else
  134. {
  135. input.setPosition (originalPos);
  136. loadedTree = ValueTree::readFromStream (input);
  137. }
  138. if (loadedTree.hasType (Tags::drawableTag))
  139. {
  140. root.removeListener (this);
  141. root = loadedTree;
  142. root.addListener (this);
  143. markersX = 0;
  144. markersY = 0;
  145. valueTreeParentChanged (loadedTree);
  146. needsSaving = false;
  147. undoManager.clearUndoHistory();
  148. return true;
  149. }
  150. return false;
  151. }
  152. void DrawableDocument::changed()
  153. {
  154. needsSaving = true;
  155. }
  156. DrawableComposite::ValueTreeWrapper DrawableDocument::getRootDrawableNode() const
  157. {
  158. return DrawableComposite::ValueTreeWrapper (root.getChild (0));
  159. }
  160. ValueTree DrawableDocument::findDrawableState (const String& objectId, bool recursive) const
  161. {
  162. return getRootDrawableNode().getDrawableWithId (objectId, recursive);
  163. }
  164. const String DrawableDocument::createUniqueID (const String& name, StringArray& recentlyUsedIdCache) const
  165. {
  166. String n (CodeHelpers::makeValidIdentifier (name, false, true, false));
  167. int suffix = 2;
  168. int cacheIndex = -1;
  169. const String withoutNumbers (n.trimCharactersAtEnd ("0123456789"));
  170. for (int i = 0; i < recentlyUsedIdCache.size(); ++i)
  171. {
  172. if (recentlyUsedIdCache[i].startsWith (withoutNumbers))
  173. {
  174. cacheIndex = i;
  175. suffix = jmax (suffix, recentlyUsedIdCache[i].substring (withoutNumbers.length()).getIntValue() + 1);
  176. n = withoutNumbers + String (suffix++);
  177. break;
  178. }
  179. }
  180. while (markersX->getMarkerNamed (n).isValid() || markersY->getMarkerNamed (n).isValid()
  181. || findDrawableState (n, true).isValid())
  182. n = withoutNumbers + String (suffix++);
  183. if (cacheIndex >= 0)
  184. recentlyUsedIdCache.set (cacheIndex, n);
  185. else
  186. recentlyUsedIdCache.add (n);
  187. return n;
  188. }
  189. bool DrawableDocument::createItemProperties (Array <PropertyComponent*>& props, const String& itemId)
  190. {
  191. ValueTree drawable (findDrawableState (itemId.upToFirstOccurrenceOf ("/", false, false), false));
  192. if (drawable.isValid())
  193. {
  194. DrawableTypeInstance item (*this, drawable);
  195. if (itemId.containsChar ('/'))
  196. {
  197. OwnedArray <ControlPoint> points;
  198. item.getAllControlPoints (points);
  199. for (int i = 0; i < points.size(); ++i)
  200. if (points.getUnchecked(i)->getID() == itemId)
  201. points.getUnchecked(i)->createProperties (*this, props);
  202. }
  203. else
  204. {
  205. item.createProperties (props);
  206. }
  207. return true;
  208. }
  209. if (markersX->createProperties (props, itemId)
  210. || markersY->createProperties (props, itemId))
  211. return true;
  212. return false;
  213. }
  214. void DrawableDocument::createItemProperties (Array <PropertyComponent*>& props, const StringArray& selectedItemIds)
  215. {
  216. if (selectedItemIds.size() != 1)
  217. return; //xxx
  218. for (int i = 0; i < selectedItemIds.size(); ++i)
  219. createItemProperties (props, selectedItemIds[i]);
  220. }
  221. //==============================================================================
  222. const int menuItemOffset = 0x63451fa4;
  223. void DrawableDocument::addNewItemMenuItems (PopupMenu& menu) const
  224. {
  225. const StringArray newItems (DrawableTypeManager::getInstance()->getNewItemList());
  226. for (int i = 0; i < newItems.size(); ++i)
  227. menu.addItem (i + menuItemOffset, newItems[i]);
  228. }
  229. const ValueTree DrawableDocument::performNewItemMenuItem (int menuResultCode)
  230. {
  231. const StringArray newItems (DrawableTypeManager::getInstance()->getNewItemList());
  232. int index = menuResultCode - menuItemOffset;
  233. if (index >= 0 && index < newItems.size())
  234. {
  235. ValueTree state (DrawableTypeManager::getInstance()
  236. ->createNewItem (index, *this,
  237. Point<float> (Random::getSystemRandom().nextFloat() * 100.0f + 100.0f,
  238. Random::getSystemRandom().nextFloat() * 100.0f + 100.0f)));
  239. Drawable::ValueTreeWrapperBase wrapper (state);
  240. StringArray idCache;
  241. recursivelyUpdateIDs (wrapper, idCache);
  242. getRootDrawableNode().addDrawable (state, -1, getUndoManager());
  243. return state;
  244. }
  245. return ValueTree::invalid;
  246. }
  247. const ValueTree DrawableDocument::insertSVG (const File& file, const Point<float>& position)
  248. {
  249. ScopedPointer<Drawable> d (Drawable::createFromImageFile (file));
  250. DrawableComposite* dc = dynamic_cast <DrawableComposite*> (static_cast <Drawable*> (d));
  251. if (dc != 0)
  252. {
  253. ValueTree state (dc->createValueTree (this));
  254. if (state.isValid())
  255. {
  256. Drawable::ValueTreeWrapperBase wrapper (state);
  257. getRootDrawableNode().addDrawable (state, -1, getUndoManager());
  258. StringArray idCache;
  259. recursivelyUpdateIDs (wrapper, idCache);
  260. return state;
  261. }
  262. }
  263. return ValueTree::invalid;
  264. }
  265. //==============================================================================
  266. const Image DrawableDocument::getImageForIdentifier (const var& imageIdentifier)
  267. {
  268. const String s (imageIdentifier.toString());
  269. if (s.startsWithIgnoreCase ("id:"))
  270. {
  271. jassert (project != 0);
  272. if (project != 0)
  273. {
  274. Project::Item item (project->getMainGroup().findItemWithID (s.substring (3).trim()));
  275. if (item.isValid())
  276. {
  277. Image im (ImageCache::getFromFile (item.getFile()));
  278. if (im.isValid())
  279. {
  280. im.setTag (imageIdentifier);
  281. return im;
  282. }
  283. }
  284. }
  285. }
  286. static Image dummy;
  287. if (dummy.isNull())
  288. {
  289. dummy = Image (Image::ARGB, 128, 128, true);
  290. Graphics g (dummy);
  291. g.fillAll (Colours::khaki.withAlpha (0.51f));
  292. g.setColour (Colours::grey);
  293. g.drawRect (0, 0, 128, 128);
  294. for (int i = -128; i < 128; i += 16)
  295. g.drawLine (i, 0, i + 128, 128);
  296. g.setColour (Colours::darkgrey);
  297. g.drawRect (0, 0, 128, 128);
  298. g.setFont (16.0f, Font::bold);
  299. g.drawText ("(Image Missing)", 0, 0, 128, 128, Justification::centred, false);
  300. }
  301. return dummy;
  302. }
  303. const var DrawableDocument::getIdentifierForImage (const Image& image)
  304. {
  305. return image.getTag();
  306. }
  307. //==============================================================================
  308. void DrawableDocument::valueTreePropertyChanged (ValueTree& tree, const Identifier& name)
  309. {
  310. changed();
  311. }
  312. void DrawableDocument::valueTreeChildrenChanged (ValueTree& tree)
  313. {
  314. changed();
  315. }
  316. void DrawableDocument::valueTreeParentChanged (ValueTree& tree)
  317. {
  318. changed();
  319. }
  320. //==============================================================================
  321. const RelativeCoordinate DrawableDocument::findNamedCoordinate (const String& objectName, const String& edge) const
  322. {
  323. if (objectName == "parent")
  324. {
  325. jassert (edge != "right" && edge != "bottom"); // drawables don't have a canvas size..
  326. }
  327. if (objectName.isNotEmpty() && edge.isNotEmpty())
  328. {
  329. }
  330. {
  331. const ValueTree marker (getMarkerListX().getMarkerNamed (objectName));
  332. if (marker.isValid())
  333. return getMarkerListX().getCoordinate (marker);
  334. }
  335. {
  336. const ValueTree marker (getMarkerListY().getMarkerNamed (objectName));
  337. if (marker.isValid())
  338. return getMarkerListY().getCoordinate (marker);
  339. }
  340. return RelativeCoordinate();
  341. }
  342. //==============================================================================
  343. DrawableDocument::MarkerList::MarkerList (DrawableDocument& document_, bool isX_)
  344. : MarkerListBase (isX_),
  345. document (document_),
  346. object (document_.getRootDrawableNode())
  347. {
  348. }
  349. const String DrawableDocument::MarkerList::getId (const ValueTree& markerState)
  350. {
  351. return markerState [DrawableComposite::ValueTreeWrapper::nameProperty];
  352. }
  353. int DrawableDocument::MarkerList::size() const
  354. {
  355. return object.getNumMarkers (isX);
  356. }
  357. ValueTree DrawableDocument::MarkerList::getMarker (int index) const
  358. {
  359. return object.getMarkerState (isX, index);
  360. }
  361. ValueTree DrawableDocument::MarkerList::getMarkerNamed (const String& name) const
  362. {
  363. return object.getMarkerState (isX, name);
  364. }
  365. bool DrawableDocument::MarkerList::contains (const ValueTree& markerState) const
  366. {
  367. return object.containsMarker (isX, markerState);
  368. }
  369. void DrawableDocument::MarkerList::createMarker (const String& name, int position)
  370. {
  371. object.setMarker (isX, DrawableComposite::Marker (name, RelativeCoordinate ((double) position, isX)),
  372. getUndoManager());
  373. }
  374. void DrawableDocument::MarkerList::deleteMarker (ValueTree& markerState)
  375. {
  376. object.removeMarker (isX, markerState, getUndoManager());
  377. }
  378. const RelativeCoordinate DrawableDocument::MarkerList::findNamedCoordinate (const String& objectName, const String& edge) const
  379. {
  380. if (objectName == "parent")
  381. {
  382. jassert (edge != "right" && edge != "bottom"); // drawables don't have a canvas size..
  383. }
  384. const ValueTree marker (getMarkerNamed (objectName));
  385. if (marker.isValid())
  386. return getCoordinate (marker);
  387. return RelativeCoordinate();
  388. }
  389. bool DrawableDocument::MarkerList::createProperties (Array <PropertyComponent*>& props, const String& itemId)
  390. {
  391. ValueTree marker (getMarkerNamed (itemId));
  392. if (marker.isValid())
  393. {
  394. props.add (new TextPropertyComponent (marker.getPropertyAsValue (DrawableComposite::ValueTreeWrapper::nameProperty, getUndoManager()),
  395. "Marker Name", 256, false));
  396. props.add (new MarkerListBase::PositionPropertyComponent (*this, "Position", marker,
  397. marker.getPropertyAsValue (DrawableComposite::ValueTreeWrapper::posProperty, getUndoManager())));
  398. return true;
  399. }
  400. return false;
  401. }
  402. void DrawableDocument::MarkerList::addMarkerMenuItem (int i, const RelativeCoordinate& coord, const String& name, const String& edge, PopupMenu& menu,
  403. bool isAnchor1, const String& fullCoordName)
  404. {
  405. RelativeCoordinate requestedCoord (findNamedCoordinate (name, edge));
  406. menu.addItem (i, edge.isEmpty() ? name : (name + "." + edge),
  407. ! (name == fullCoordName || (fullCoordName.isNotEmpty() && requestedCoord.references (fullCoordName, this))),
  408. name == (isAnchor1 ? coord.getAnchorName1() : coord.getAnchorName2()));
  409. }
  410. void DrawableDocument::MarkerList::addMarkerMenuItems (const ValueTree& markerState, const RelativeCoordinate& coord, PopupMenu& menu, bool isAnchor1)
  411. {
  412. const String fullCoordName (getName (markerState));
  413. if (isHorizontal())
  414. addMarkerMenuItem (1, coord, "parent", "left", menu, isAnchor1, fullCoordName);
  415. else
  416. addMarkerMenuItem (1, coord, "parent", "top", menu, isAnchor1, fullCoordName);
  417. menu.addSeparator();
  418. for (int i = 0; i < size(); ++i)
  419. addMarkerMenuItem (100 + i, coord, getName (getMarker (i)),
  420. String::empty, menu, isAnchor1, fullCoordName);
  421. }
  422. const String DrawableDocument::MarkerList::getChosenMarkerMenuItem (const RelativeCoordinate& coord, int i) const
  423. {
  424. if (i == 1) return isHorizontal() ? "parent.left" : "parent.top";
  425. if (i >= 100 && i < 10000)
  426. return getName (getMarker (i - 100));
  427. jassertfalse;
  428. return String::empty;
  429. }
  430. UndoManager* DrawableDocument::MarkerList::getUndoManager() const
  431. {
  432. return document.getUndoManager();
  433. }
  434. const String DrawableDocument::MarkerList::getNonexistentMarkerName (const String& name)
  435. {
  436. return document.getNonexistentMarkerName (name);
  437. }
  438. const String DrawableDocument::getNonexistentMarkerName (const String& name)
  439. {
  440. String n (CodeHelpers::makeValidIdentifier (name, false, true, false));
  441. int suffix = 2;
  442. while (markersX->getMarkerNamed (n).isValid() || markersY->getMarkerNamed (n).isValid())
  443. n = n.trimCharactersAtEnd ("0123456789") + String (suffix++);
  444. return n;
  445. }
  446. void DrawableDocument::MarkerList::renameAnchor (const String& oldName, const String& newName)
  447. {
  448. document.renameAnchor (oldName, newName);
  449. }
  450. void DrawableDocument::renameAnchor (const String& oldName, const String& newName)
  451. {
  452. }