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.

1134 lines
36KB

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