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.

1115 lines
35KB

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