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.

1152 lines
36KB

  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. class ValueTree::SharedObject : public ReferenceCountedObject
  18. {
  19. public:
  20. typedef ReferenceCountedObjectPtr<SharedObject> Ptr;
  21. explicit SharedObject (const Identifier& t) noexcept
  22. : type (t), parent (nullptr)
  23. {
  24. }
  25. SharedObject (const SharedObject& other)
  26. : ReferenceCountedObject(),
  27. type (other.type), properties (other.properties), parent (nullptr)
  28. {
  29. for (int i = 0; i < other.children.size(); ++i)
  30. {
  31. SharedObject* const child = new SharedObject (*other.children.getObjectPointerUnchecked(i));
  32. child->parent = this;
  33. children.add (child);
  34. }
  35. }
  36. ~SharedObject()
  37. {
  38. jassert (parent == nullptr); // this should never happen unless something isn't obeying the ref-counting!
  39. for (int i = children.size(); --i >= 0;)
  40. {
  41. const Ptr c (children.getObjectPointerUnchecked(i));
  42. c->parent = nullptr;
  43. children.remove (i);
  44. c->sendParentChangeMessage();
  45. }
  46. }
  47. template <typename Method>
  48. void callListeners (Method method, ValueTree& tree) const
  49. {
  50. const int numListeners = valueTreesWithListeners.size();
  51. if (numListeners == 1)
  52. {
  53. valueTreesWithListeners.getUnchecked(0)->listeners.call (method, tree);
  54. }
  55. else if (numListeners > 0)
  56. {
  57. const SortedSet<ValueTree*> listenersCopy (valueTreesWithListeners);
  58. for (int i = 0; i < numListeners; ++i)
  59. {
  60. ValueTree* const v = listenersCopy.getUnchecked(i);
  61. if (i == 0 || valueTreesWithListeners.contains (v))
  62. v->listeners.call (method, tree);
  63. }
  64. }
  65. }
  66. template <typename Method, typename ParamType>
  67. void callListeners (Method method, ValueTree& tree, ParamType& param2) const
  68. {
  69. const int numListeners = valueTreesWithListeners.size();
  70. if (numListeners == 1)
  71. {
  72. valueTreesWithListeners.getUnchecked(0)->listeners.call (method, tree, param2);
  73. }
  74. else if (numListeners > 0)
  75. {
  76. const SortedSet<ValueTree*> listenersCopy (valueTreesWithListeners);
  77. for (int i = 0; i < numListeners; ++i)
  78. {
  79. ValueTree* const v = listenersCopy.getUnchecked(i);
  80. if (i == 0 || valueTreesWithListeners.contains (v))
  81. v->listeners.call (method, tree, param2);
  82. }
  83. }
  84. }
  85. template <typename Method, typename ParamType1, typename ParamType2>
  86. void callListeners (Method method, ValueTree& tree, ParamType1& param2, ParamType2& param3) const
  87. {
  88. const int numListeners = valueTreesWithListeners.size();
  89. if (numListeners == 1)
  90. {
  91. valueTreesWithListeners.getUnchecked(0)->listeners.call (method, tree, param2, param3);
  92. }
  93. else if (numListeners > 0)
  94. {
  95. const SortedSet<ValueTree*> listenersCopy (valueTreesWithListeners);
  96. for (int i = 0; i < numListeners; ++i)
  97. {
  98. ValueTree* const v = listenersCopy.getUnchecked(i);
  99. if (i == 0 || valueTreesWithListeners.contains (v))
  100. v->listeners.call (method, tree, param2, param3);
  101. }
  102. }
  103. }
  104. void sendPropertyChangeMessage (const Identifier& property)
  105. {
  106. ValueTree tree (this);
  107. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  108. t->callListeners (&ValueTree::Listener::valueTreePropertyChanged, tree, property);
  109. }
  110. void sendChildAddedMessage (ValueTree child)
  111. {
  112. ValueTree tree (this);
  113. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  114. t->callListeners (&ValueTree::Listener::valueTreeChildAdded, tree, child);
  115. }
  116. void sendChildRemovedMessage (ValueTree child, int index)
  117. {
  118. ValueTree tree (this);
  119. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  120. t->callListeners (&ValueTree::Listener::valueTreeChildRemoved, tree, child, index);
  121. }
  122. void sendChildOrderChangedMessage (int oldIndex, int newIndex)
  123. {
  124. ValueTree tree (this);
  125. for (ValueTree::SharedObject* t = this; t != nullptr; t = t->parent)
  126. t->callListeners (&ValueTree::Listener::valueTreeChildOrderChanged, tree, oldIndex, newIndex);
  127. }
  128. void sendParentChangeMessage()
  129. {
  130. ValueTree tree (this);
  131. for (int j = children.size(); --j >= 0;)
  132. if (SharedObject* const child = children.getObjectPointer (j))
  133. child->sendParentChangeMessage();
  134. callListeners (&ValueTree::Listener::valueTreeParentChanged, tree);
  135. }
  136. void setProperty (const Identifier& name, const var& newValue, UndoManager* const undoManager)
  137. {
  138. if (undoManager == nullptr)
  139. {
  140. if (properties.set (name, newValue))
  141. sendPropertyChangeMessage (name);
  142. }
  143. else
  144. {
  145. if (const var* const existingValue = properties.getVarPointer (name))
  146. {
  147. if (*existingValue != newValue)
  148. undoManager->perform (new SetPropertyAction (this, name, newValue, *existingValue, false, false));
  149. }
  150. else
  151. {
  152. undoManager->perform (new SetPropertyAction (this, name, newValue, var(), true, false));
  153. }
  154. }
  155. }
  156. bool hasProperty (const Identifier& name) const noexcept
  157. {
  158. return properties.contains (name);
  159. }
  160. void removeProperty (const Identifier& name, UndoManager* const undoManager)
  161. {
  162. if (undoManager == nullptr)
  163. {
  164. if (properties.remove (name))
  165. sendPropertyChangeMessage (name);
  166. }
  167. else
  168. {
  169. if (properties.contains (name))
  170. undoManager->perform (new SetPropertyAction (this, name, var(), properties [name], false, true));
  171. }
  172. }
  173. void removeAllProperties (UndoManager* const undoManager)
  174. {
  175. if (undoManager == nullptr)
  176. {
  177. while (properties.size() > 0)
  178. {
  179. const Identifier name (properties.getName (properties.size() - 1));
  180. properties.remove (name);
  181. sendPropertyChangeMessage (name);
  182. }
  183. }
  184. else
  185. {
  186. for (int i = properties.size(); --i >= 0;)
  187. undoManager->perform (new SetPropertyAction (this, properties.getName(i), var(),
  188. properties.getValueAt(i), false, true));
  189. }
  190. }
  191. void copyPropertiesFrom (const SharedObject& source, UndoManager* const undoManager)
  192. {
  193. for (int i = properties.size(); --i >= 0;)
  194. if (! source.properties.contains (properties.getName (i)))
  195. removeProperty (properties.getName (i), undoManager);
  196. for (int i = 0; i < source.properties.size(); ++i)
  197. setProperty (source.properties.getName(i), source.properties.getValueAt(i), undoManager);
  198. }
  199. ValueTree getChildWithName (const Identifier& typeToMatch) const
  200. {
  201. for (int i = 0; i < children.size(); ++i)
  202. {
  203. SharedObject* const s = children.getObjectPointerUnchecked (i);
  204. if (s->type == typeToMatch)
  205. return ValueTree (s);
  206. }
  207. return ValueTree();
  208. }
  209. ValueTree getOrCreateChildWithName (const Identifier& typeToMatch, UndoManager* undoManager)
  210. {
  211. for (int i = 0; i < children.size(); ++i)
  212. {
  213. SharedObject* const s = children.getObjectPointerUnchecked (i);
  214. if (s->type == typeToMatch)
  215. return ValueTree (s);
  216. }
  217. SharedObject* const newObject = new SharedObject (typeToMatch);
  218. addChild (newObject, -1, undoManager);
  219. return ValueTree (newObject);
  220. }
  221. ValueTree getChildWithProperty (const Identifier& propertyName, const var& propertyValue) const
  222. {
  223. for (int i = 0; i < children.size(); ++i)
  224. {
  225. SharedObject* const s = children.getObjectPointerUnchecked (i);
  226. if (s->properties[propertyName] == propertyValue)
  227. return ValueTree (s);
  228. }
  229. return ValueTree();
  230. }
  231. bool isAChildOf (const SharedObject* const possibleParent) const noexcept
  232. {
  233. for (const SharedObject* p = parent; p != nullptr; p = p->parent)
  234. if (p == possibleParent)
  235. return true;
  236. return false;
  237. }
  238. int indexOf (const ValueTree& child) const noexcept
  239. {
  240. return children.indexOf (child.object);
  241. }
  242. void addChild (SharedObject* child, int index, UndoManager* const undoManager)
  243. {
  244. if (child != nullptr && child->parent != this)
  245. {
  246. if (child != this && ! isAChildOf (child))
  247. {
  248. // You should always make sure that a child is removed from its previous parent before
  249. // adding it somewhere else - otherwise, it's ambiguous as to whether a different
  250. // undomanager should be used when removing it from its current parent..
  251. jassert (child->parent == nullptr);
  252. if (child->parent != nullptr)
  253. {
  254. jassert (child->parent->children.indexOf (child) >= 0);
  255. child->parent->removeChild (child->parent->children.indexOf (child), undoManager);
  256. }
  257. if (undoManager == nullptr)
  258. {
  259. children.insert (index, child);
  260. child->parent = this;
  261. sendChildAddedMessage (ValueTree (child));
  262. child->sendParentChangeMessage();
  263. }
  264. else
  265. {
  266. if (! isPositiveAndBelow (index, children.size()))
  267. index = children.size();
  268. undoManager->perform (new AddOrRemoveChildAction (this, index, child));
  269. }
  270. }
  271. else
  272. {
  273. // You're attempting to create a recursive loop! A node
  274. // can't be a child of one of its own children!
  275. jassertfalse;
  276. }
  277. }
  278. }
  279. void removeChild (const int childIndex, UndoManager* const undoManager)
  280. {
  281. if (const Ptr child = children.getObjectPointer (childIndex))
  282. {
  283. if (undoManager == nullptr)
  284. {
  285. children.remove (childIndex);
  286. child->parent = nullptr;
  287. sendChildRemovedMessage (ValueTree (child), childIndex);
  288. child->sendParentChangeMessage();
  289. }
  290. else
  291. {
  292. undoManager->perform (new AddOrRemoveChildAction (this, childIndex, nullptr));
  293. }
  294. }
  295. }
  296. void removeAllChildren (UndoManager* const undoManager)
  297. {
  298. while (children.size() > 0)
  299. removeChild (children.size() - 1, undoManager);
  300. }
  301. void moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
  302. {
  303. // The source index must be a valid index!
  304. jassert (isPositiveAndBelow (currentIndex, children.size()));
  305. if (currentIndex != newIndex
  306. && isPositiveAndBelow (currentIndex, children.size()))
  307. {
  308. if (undoManager == nullptr)
  309. {
  310. children.move (currentIndex, newIndex);
  311. sendChildOrderChangedMessage (currentIndex, newIndex);
  312. }
  313. else
  314. {
  315. if (! isPositiveAndBelow (newIndex, children.size()))
  316. newIndex = children.size() - 1;
  317. undoManager->perform (new MoveChildAction (this, currentIndex, newIndex));
  318. }
  319. }
  320. }
  321. void reorderChildren (const OwnedArray<ValueTree>& newOrder, UndoManager* undoManager)
  322. {
  323. jassert (newOrder.size() == children.size());
  324. for (int i = 0; i < children.size(); ++i)
  325. {
  326. SharedObject* const child = newOrder.getUnchecked(i)->object;
  327. if (children.getObjectPointerUnchecked (i) != child)
  328. {
  329. const int oldIndex = children.indexOf (child);
  330. jassert (oldIndex >= 0);
  331. moveChild (oldIndex, i, undoManager);
  332. }
  333. }
  334. }
  335. bool isEquivalentTo (const SharedObject& other) const noexcept
  336. {
  337. if (type != other.type
  338. || properties.size() != other.properties.size()
  339. || children.size() != other.children.size()
  340. || properties != other.properties)
  341. return false;
  342. for (int i = 0; i < children.size(); ++i)
  343. if (! children.getObjectPointerUnchecked(i)->isEquivalentTo (*other.children.getObjectPointerUnchecked(i)))
  344. return false;
  345. return true;
  346. }
  347. XmlElement* createXml() const
  348. {
  349. XmlElement* const xml = new XmlElement (type);
  350. properties.copyToXmlAttributes (*xml);
  351. // (NB: it's faster to add nodes to XML elements in reverse order)
  352. for (int i = children.size(); --i >= 0;)
  353. xml->prependChildElement (children.getObjectPointerUnchecked(i)->createXml());
  354. return xml;
  355. }
  356. void writeToStream (OutputStream& output) const
  357. {
  358. output.writeString (type.toString());
  359. output.writeCompressedInt (properties.size());
  360. for (int j = 0; j < properties.size(); ++j)
  361. {
  362. output.writeString (properties.getName (j).toString());
  363. properties.getValueAt(j).writeToStream (output);
  364. }
  365. output.writeCompressedInt (children.size());
  366. for (int i = 0; i < children.size(); ++i)
  367. writeObjectToStream (output, children.getObjectPointerUnchecked(i));
  368. }
  369. static void writeObjectToStream (OutputStream& output, const SharedObject* const object)
  370. {
  371. if (object != nullptr)
  372. {
  373. object->writeToStream (output);
  374. }
  375. else
  376. {
  377. output.writeString (String());
  378. output.writeCompressedInt (0);
  379. output.writeCompressedInt (0);
  380. }
  381. }
  382. //==============================================================================
  383. class SetPropertyAction : public UndoableAction
  384. {
  385. public:
  386. SetPropertyAction (SharedObject* const so, const Identifier& propertyName,
  387. const var& newVal, const var& oldVal, bool isAdding, bool isDeleting)
  388. : target (so), name (propertyName), newValue (newVal), oldValue (oldVal),
  389. isAddingNewProperty (isAdding), isDeletingProperty (isDeleting)
  390. {
  391. }
  392. bool perform() override
  393. {
  394. jassert (! (isAddingNewProperty && target->hasProperty (name)));
  395. if (isDeletingProperty)
  396. target->removeProperty (name, nullptr);
  397. else
  398. target->setProperty (name, newValue, nullptr);
  399. return true;
  400. }
  401. bool undo() override
  402. {
  403. if (isAddingNewProperty)
  404. target->removeProperty (name, nullptr);
  405. else
  406. target->setProperty (name, oldValue, nullptr);
  407. return true;
  408. }
  409. int getSizeInUnits() override
  410. {
  411. return (int) sizeof (*this); //xxx should be more accurate
  412. }
  413. UndoableAction* createCoalescedAction (UndoableAction* nextAction) override
  414. {
  415. if (! (isAddingNewProperty || isDeletingProperty))
  416. {
  417. if (SetPropertyAction* const next = dynamic_cast<SetPropertyAction*> (nextAction))
  418. if (next->target == target && next->name == name
  419. && ! (next->isAddingNewProperty || next->isDeletingProperty))
  420. return new SetPropertyAction (target, name, next->newValue, oldValue, false, false);
  421. }
  422. return nullptr;
  423. }
  424. private:
  425. const Ptr target;
  426. const Identifier name;
  427. const var newValue;
  428. var oldValue;
  429. const bool isAddingNewProperty : 1, isDeletingProperty : 1;
  430. JUCE_DECLARE_NON_COPYABLE (SetPropertyAction)
  431. };
  432. //==============================================================================
  433. class AddOrRemoveChildAction : public UndoableAction
  434. {
  435. public:
  436. AddOrRemoveChildAction (SharedObject* parentObject, int index, SharedObject* newChild)
  437. : target (parentObject),
  438. child (newChild != nullptr ? newChild : parentObject->children.getObjectPointer (index)),
  439. childIndex (index),
  440. isDeleting (newChild == nullptr)
  441. {
  442. jassert (child != nullptr);
  443. }
  444. bool perform() override
  445. {
  446. if (isDeleting)
  447. target->removeChild (childIndex, nullptr);
  448. else
  449. target->addChild (child, childIndex, nullptr);
  450. return true;
  451. }
  452. bool undo() override
  453. {
  454. if (isDeleting)
  455. {
  456. target->addChild (child, childIndex, nullptr);
  457. }
  458. else
  459. {
  460. // If you hit this, it seems that your object's state is getting confused - probably
  461. // because you've interleaved some undoable and non-undoable operations?
  462. jassert (childIndex < target->children.size());
  463. target->removeChild (childIndex, nullptr);
  464. }
  465. return true;
  466. }
  467. int getSizeInUnits() override
  468. {
  469. return (int) sizeof (*this); //xxx should be more accurate
  470. }
  471. private:
  472. const Ptr target, child;
  473. const int childIndex;
  474. const bool isDeleting;
  475. JUCE_DECLARE_NON_COPYABLE (AddOrRemoveChildAction)
  476. };
  477. //==============================================================================
  478. class MoveChildAction : public UndoableAction
  479. {
  480. public:
  481. MoveChildAction (SharedObject* parentObject, int fromIndex, int toIndex) noexcept
  482. : parent (parentObject), startIndex (fromIndex), endIndex (toIndex)
  483. {
  484. }
  485. bool perform() override
  486. {
  487. parent->moveChild (startIndex, endIndex, nullptr);
  488. return true;
  489. }
  490. bool undo() override
  491. {
  492. parent->moveChild (endIndex, startIndex, nullptr);
  493. return true;
  494. }
  495. int getSizeInUnits() override
  496. {
  497. return (int) sizeof (*this); //xxx should be more accurate
  498. }
  499. UndoableAction* createCoalescedAction (UndoableAction* nextAction) override
  500. {
  501. if (MoveChildAction* next = dynamic_cast<MoveChildAction*> (nextAction))
  502. if (next->parent == parent && next->startIndex == endIndex)
  503. return new MoveChildAction (parent, startIndex, next->endIndex);
  504. return nullptr;
  505. }
  506. private:
  507. const Ptr parent;
  508. const int startIndex, endIndex;
  509. JUCE_DECLARE_NON_COPYABLE (MoveChildAction)
  510. };
  511. //==============================================================================
  512. const Identifier type;
  513. NamedValueSet properties;
  514. ReferenceCountedArray<SharedObject> children;
  515. SortedSet<ValueTree*> valueTreesWithListeners;
  516. SharedObject* parent;
  517. private:
  518. SharedObject& operator= (const SharedObject&);
  519. JUCE_LEAK_DETECTOR (SharedObject)
  520. };
  521. //==============================================================================
  522. ValueTree::ValueTree() noexcept
  523. {
  524. }
  525. const ValueTree ValueTree::invalid;
  526. ValueTree::ValueTree (const Identifier& type) : object (new ValueTree::SharedObject (type))
  527. {
  528. jassert (type.toString().isNotEmpty()); // All objects must be given a sensible type name!
  529. }
  530. ValueTree::ValueTree (SharedObject* so) noexcept : object (so)
  531. {
  532. }
  533. ValueTree::ValueTree (const ValueTree& other) noexcept : object (other.object)
  534. {
  535. }
  536. ValueTree& ValueTree::operator= (const ValueTree& other)
  537. {
  538. if (object != other.object)
  539. {
  540. if (listeners.isEmpty())
  541. {
  542. object = other.object;
  543. }
  544. else
  545. {
  546. if (object != nullptr)
  547. object->valueTreesWithListeners.removeValue (this);
  548. if (other.object != nullptr)
  549. other.object->valueTreesWithListeners.add (this);
  550. object = other.object;
  551. listeners.call (&ValueTree::Listener::valueTreeRedirected, *this);
  552. }
  553. }
  554. return *this;
  555. }
  556. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  557. ValueTree::ValueTree (ValueTree&& other) noexcept
  558. : object (static_cast<SharedObject::Ptr&&> (other.object))
  559. {
  560. }
  561. #endif
  562. ValueTree::~ValueTree()
  563. {
  564. if (listeners.size() > 0 && object != nullptr)
  565. object->valueTreesWithListeners.removeValue (this);
  566. }
  567. bool ValueTree::operator== (const ValueTree& other) const noexcept
  568. {
  569. return object == other.object;
  570. }
  571. bool ValueTree::operator!= (const ValueTree& other) const noexcept
  572. {
  573. return object != other.object;
  574. }
  575. bool ValueTree::isEquivalentTo (const ValueTree& other) const
  576. {
  577. return object == other.object
  578. || (object != nullptr && other.object != nullptr
  579. && object->isEquivalentTo (*other.object));
  580. }
  581. ValueTree ValueTree::createCopy() const
  582. {
  583. return ValueTree (createCopyIfNotNull (object.get()));
  584. }
  585. bool ValueTree::hasType (const Identifier& typeName) const noexcept
  586. {
  587. return object != nullptr && object->type == typeName;
  588. }
  589. Identifier ValueTree::getType() const noexcept
  590. {
  591. return object != nullptr ? object->type : Identifier();
  592. }
  593. ValueTree ValueTree::getParent() const noexcept
  594. {
  595. return ValueTree (object != nullptr ? object->parent
  596. : static_cast<SharedObject*> (nullptr));
  597. }
  598. ValueTree ValueTree::getSibling (const int delta) const noexcept
  599. {
  600. if (object == nullptr || object->parent == nullptr)
  601. return invalid;
  602. const int index = object->parent->indexOf (*this) + delta;
  603. return ValueTree (object->parent->children.getObjectPointer (index));
  604. }
  605. const var& ValueTree::operator[] (const Identifier& name) const noexcept
  606. {
  607. return object == nullptr ? var::null : object->properties[name];
  608. }
  609. const var& ValueTree::getProperty (const Identifier& name) const noexcept
  610. {
  611. return object == nullptr ? var::null : object->properties[name];
  612. }
  613. var ValueTree::getProperty (const Identifier& name, const var& defaultReturnValue) const
  614. {
  615. return object == nullptr ? defaultReturnValue
  616. : object->properties.getWithDefault (name, defaultReturnValue);
  617. }
  618. const var* ValueTree::getPropertyPointer (const Identifier& name) const noexcept
  619. {
  620. return object == nullptr ? nullptr
  621. : object->properties.getVarPointer (name);
  622. }
  623. ValueTree& ValueTree::setProperty (const Identifier& name, const var& newValue, UndoManager* undoManager)
  624. {
  625. jassert (name.toString().isNotEmpty()); // Must have a valid property name!
  626. jassert (object != nullptr); // Trying to add a property to a null ValueTree will fail!
  627. if (object != nullptr)
  628. object->setProperty (name, newValue, undoManager);
  629. return *this;
  630. }
  631. bool ValueTree::hasProperty (const Identifier& name) const noexcept
  632. {
  633. return object != nullptr && object->hasProperty (name);
  634. }
  635. void ValueTree::removeProperty (const Identifier& name, UndoManager* const undoManager)
  636. {
  637. if (object != nullptr)
  638. object->removeProperty (name, undoManager);
  639. }
  640. void ValueTree::removeAllProperties (UndoManager* const undoManager)
  641. {
  642. if (object != nullptr)
  643. object->removeAllProperties (undoManager);
  644. }
  645. int ValueTree::getNumProperties() const noexcept
  646. {
  647. return object == nullptr ? 0 : object->properties.size();
  648. }
  649. Identifier ValueTree::getPropertyName (const int index) const noexcept
  650. {
  651. return object == nullptr ? Identifier()
  652. : object->properties.getName (index);
  653. }
  654. void ValueTree::copyPropertiesFrom (const ValueTree& source, UndoManager* const undoManager)
  655. {
  656. jassert (object != nullptr || source.object == nullptr); // Trying to add properties to a null ValueTree will fail!
  657. if (source.object == nullptr)
  658. removeAllProperties (undoManager);
  659. else if (object != nullptr)
  660. object->copyPropertiesFrom (*(source.object), undoManager);
  661. }
  662. int ValueTree::getReferenceCount() const noexcept
  663. {
  664. return object != nullptr ? object->getReferenceCount() : 0;
  665. }
  666. //==============================================================================
  667. class ValueTreePropertyValueSource : public Value::ValueSource,
  668. private ValueTree::Listener
  669. {
  670. public:
  671. ValueTreePropertyValueSource (const ValueTree& vt, const Identifier& prop, UndoManager* um)
  672. : tree (vt), property (prop), undoManager (um)
  673. {
  674. tree.addListener (this);
  675. }
  676. ~ValueTreePropertyValueSource()
  677. {
  678. tree.removeListener (this);
  679. }
  680. var getValue() const override { return tree [property]; }
  681. void setValue (const var& newValue) override { tree.setProperty (property, newValue, undoManager); }
  682. private:
  683. ValueTree tree;
  684. const Identifier property;
  685. UndoManager* const undoManager;
  686. void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override
  687. {
  688. if (tree == changedTree && property == changedProperty)
  689. sendChangeMessage (false);
  690. }
  691. void valueTreeChildAdded (ValueTree&, ValueTree&) override {}
  692. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override {}
  693. void valueTreeChildOrderChanged (ValueTree&, int, int) override {}
  694. void valueTreeParentChanged (ValueTree&) override {}
  695. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueTreePropertyValueSource)
  696. };
  697. Value ValueTree::getPropertyAsValue (const Identifier& name, UndoManager* const undoManager)
  698. {
  699. return Value (new ValueTreePropertyValueSource (*this, name, undoManager));
  700. }
  701. //==============================================================================
  702. int ValueTree::getNumChildren() const noexcept
  703. {
  704. return object == nullptr ? 0 : object->children.size();
  705. }
  706. ValueTree ValueTree::getChild (int index) const
  707. {
  708. return ValueTree (object != nullptr ? object->children.getObjectPointer (index)
  709. : static_cast<SharedObject*> (nullptr));
  710. }
  711. ValueTree ValueTree::getChildWithName (const Identifier& type) const
  712. {
  713. return object != nullptr ? object->getChildWithName (type) : ValueTree();
  714. }
  715. ValueTree ValueTree::getOrCreateChildWithName (const Identifier& type, UndoManager* undoManager)
  716. {
  717. return object != nullptr ? object->getOrCreateChildWithName (type, undoManager) : ValueTree();
  718. }
  719. ValueTree ValueTree::getChildWithProperty (const Identifier& propertyName, const var& propertyValue) const
  720. {
  721. return object != nullptr ? object->getChildWithProperty (propertyName, propertyValue) : ValueTree();
  722. }
  723. bool ValueTree::isAChildOf (const ValueTree& possibleParent) const noexcept
  724. {
  725. return object != nullptr && object->isAChildOf (possibleParent.object);
  726. }
  727. int ValueTree::indexOf (const ValueTree& child) const noexcept
  728. {
  729. return object != nullptr ? object->indexOf (child) : -1;
  730. }
  731. void ValueTree::addChild (const ValueTree& child, int index, UndoManager* const undoManager)
  732. {
  733. jassert (object != nullptr); // Trying to add a child to a null ValueTree!
  734. if (object != nullptr)
  735. object->addChild (child.object, index, undoManager);
  736. }
  737. void ValueTree::removeChild (const int childIndex, UndoManager* const undoManager)
  738. {
  739. if (object != nullptr)
  740. object->removeChild (childIndex, undoManager);
  741. }
  742. void ValueTree::removeChild (const ValueTree& child, UndoManager* const undoManager)
  743. {
  744. if (object != nullptr)
  745. object->removeChild (object->children.indexOf (child.object), undoManager);
  746. }
  747. void ValueTree::removeAllChildren (UndoManager* const undoManager)
  748. {
  749. if (object != nullptr)
  750. object->removeAllChildren (undoManager);
  751. }
  752. void ValueTree::moveChild (int currentIndex, int newIndex, UndoManager* undoManager)
  753. {
  754. if (object != nullptr)
  755. object->moveChild (currentIndex, newIndex, undoManager);
  756. }
  757. //==============================================================================
  758. void ValueTree::createListOfChildren (OwnedArray<ValueTree>& list) const
  759. {
  760. jassert (object != nullptr);
  761. for (int i = 0; i < object->children.size(); ++i)
  762. list.add (new ValueTree (object->children.getObjectPointerUnchecked(i)));
  763. }
  764. void ValueTree::reorderChildren (const OwnedArray<ValueTree>& newOrder, UndoManager* undoManager)
  765. {
  766. jassert (object != nullptr);
  767. object->reorderChildren (newOrder, undoManager);
  768. }
  769. //==============================================================================
  770. void ValueTree::addListener (Listener* listener)
  771. {
  772. if (listener != nullptr)
  773. {
  774. if (listeners.isEmpty() && object != nullptr)
  775. object->valueTreesWithListeners.add (this);
  776. listeners.add (listener);
  777. }
  778. }
  779. void ValueTree::removeListener (Listener* listener)
  780. {
  781. listeners.remove (listener);
  782. if (listeners.isEmpty() && object != nullptr)
  783. object->valueTreesWithListeners.removeValue (this);
  784. }
  785. void ValueTree::sendPropertyChangeMessage (const Identifier& property)
  786. {
  787. if (object != nullptr)
  788. object->sendPropertyChangeMessage (property);
  789. }
  790. //==============================================================================
  791. XmlElement* ValueTree::createXml() const
  792. {
  793. return object != nullptr ? object->createXml() : nullptr;
  794. }
  795. ValueTree ValueTree::fromXml (const XmlElement& xml)
  796. {
  797. if (! xml.isTextElement())
  798. {
  799. ValueTree v (xml.getTagName());
  800. v.object->properties.setFromXmlAttributes (xml);
  801. forEachXmlChildElement (xml, e)
  802. v.addChild (fromXml (*e), -1, nullptr);
  803. return v;
  804. }
  805. // ValueTrees don't have any equivalent to XML text elements!
  806. jassertfalse;
  807. return ValueTree();
  808. }
  809. String ValueTree::toXmlString() const
  810. {
  811. const ScopedPointer<XmlElement> xml (createXml());
  812. return xml != nullptr ? xml->createDocument (StringRef()) : String();
  813. }
  814. //==============================================================================
  815. void ValueTree::writeToStream (OutputStream& output) const
  816. {
  817. SharedObject::writeObjectToStream (output, object);
  818. }
  819. ValueTree ValueTree::readFromStream (InputStream& input)
  820. {
  821. const String type (input.readString());
  822. if (type.isEmpty())
  823. return ValueTree();
  824. ValueTree v (type);
  825. const int numProps = input.readCompressedInt();
  826. if (numProps < 0)
  827. {
  828. jassertfalse; // trying to read corrupted data!
  829. return v;
  830. }
  831. for (int i = 0; i < numProps; ++i)
  832. {
  833. const String name (input.readString());
  834. if (name.isNotEmpty())
  835. {
  836. const var value (var::readFromStream (input));
  837. v.object->properties.set (name, value);
  838. }
  839. else
  840. {
  841. jassertfalse; // trying to read corrupted data!
  842. }
  843. }
  844. const int numChildren = input.readCompressedInt();
  845. v.object->children.ensureStorageAllocated (numChildren);
  846. for (int i = 0; i < numChildren; ++i)
  847. {
  848. ValueTree child (readFromStream (input));
  849. if (! child.isValid())
  850. return v;
  851. v.object->children.add (child.object);
  852. child.object->parent = v.object;
  853. }
  854. return v;
  855. }
  856. ValueTree ValueTree::readFromData (const void* const data, const size_t numBytes)
  857. {
  858. MemoryInputStream in (data, numBytes, false);
  859. return readFromStream (in);
  860. }
  861. ValueTree ValueTree::readFromGZIPData (const void* const data, const size_t numBytes)
  862. {
  863. MemoryInputStream in (data, numBytes, false);
  864. GZIPDecompressorInputStream gzipStream (in);
  865. return readFromStream (gzipStream);
  866. }
  867. void ValueTree::Listener::valueTreeRedirected (ValueTree&) {}
  868. //==============================================================================
  869. #if JUCE_UNIT_TESTS
  870. class ValueTreeTests : public UnitTest
  871. {
  872. public:
  873. ValueTreeTests() : UnitTest ("ValueTrees") {}
  874. static String createRandomIdentifier (Random& r)
  875. {
  876. char buffer[50] = { 0 };
  877. const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-:";
  878. for (int i = 1 + r.nextInt (numElementsInArray (buffer) - 2); --i >= 0;)
  879. buffer[i] = chars [r.nextInt (sizeof (chars) - 1)];
  880. String result (buffer);
  881. if (! XmlElement::isValidXmlName (result))
  882. result = createRandomIdentifier (r);
  883. return result;
  884. }
  885. static String createRandomWideCharString (Random& r)
  886. {
  887. juce_wchar buffer[50] = { 0 };
  888. for (int i = r.nextInt (numElementsInArray (buffer) - 1); --i >= 0;)
  889. {
  890. if (r.nextBool())
  891. {
  892. do
  893. {
  894. buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
  895. }
  896. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  897. }
  898. else
  899. buffer[i] = (juce_wchar) (1 + r.nextInt (0x7e));
  900. }
  901. return CharPointer_UTF32 (buffer);
  902. }
  903. static ValueTree createRandomTree (UndoManager* undoManager, int depth, Random& r)
  904. {
  905. ValueTree v (createRandomIdentifier (r));
  906. for (int i = r.nextInt (10); --i >= 0;)
  907. {
  908. switch (r.nextInt (5))
  909. {
  910. case 0: v.setProperty (createRandomIdentifier (r), createRandomWideCharString (r), undoManager); break;
  911. case 1: v.setProperty (createRandomIdentifier (r), r.nextInt(), undoManager); break;
  912. case 2: if (depth < 5) v.addChild (createRandomTree (undoManager, depth + 1, r), r.nextInt (v.getNumChildren() + 1), undoManager); break;
  913. case 3: v.setProperty (createRandomIdentifier (r), r.nextBool(), undoManager); break;
  914. case 4: v.setProperty (createRandomIdentifier (r), r.nextDouble(), undoManager); break;
  915. default: break;
  916. }
  917. }
  918. return v;
  919. }
  920. void runTest() override
  921. {
  922. beginTest ("ValueTree");
  923. Random r = getRandom();
  924. for (int i = 10; --i >= 0;)
  925. {
  926. MemoryOutputStream mo;
  927. ValueTree v1 (createRandomTree (nullptr, 0, r));
  928. v1.writeToStream (mo);
  929. MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
  930. ValueTree v2 = ValueTree::readFromStream (mi);
  931. expect (v1.isEquivalentTo (v2));
  932. ScopedPointer<XmlElement> xml1 (v1.createXml());
  933. ScopedPointer<XmlElement> xml2 (v2.createCopy().createXml());
  934. expect (xml1->isEquivalentTo (xml2, false));
  935. ValueTree v4 = v2.createCopy();
  936. expect (v1.isEquivalentTo (v4));
  937. }
  938. }
  939. };
  940. static ValueTreeTests valueTreeTests;
  941. #endif