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.

283 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace ComponentBuilderHelpers
  20. {
  21. static String getStateId (const ValueTree& state)
  22. {
  23. return state [ComponentBuilder::idProperty].toString();
  24. }
  25. static Component* removeComponentWithID (OwnedArray<Component>& components, const String& compId)
  26. {
  27. jassert (compId.isNotEmpty());
  28. for (int i = components.size(); --i >= 0;)
  29. {
  30. Component* const c = components.getUnchecked (i);
  31. if (c->getComponentID() == compId)
  32. return components.removeAndReturn (i);
  33. }
  34. return nullptr;
  35. }
  36. static Component* findComponentWithID (Component& c, const String& compId)
  37. {
  38. jassert (compId.isNotEmpty());
  39. if (c.getComponentID() == compId)
  40. return &c;
  41. for (int i = c.getNumChildComponents(); --i >= 0;)
  42. if (Component* const child = findComponentWithID (*c.getChildComponent (i), compId))
  43. return child;
  44. return nullptr;
  45. }
  46. static Component* createNewComponent (ComponentBuilder::TypeHandler& type,
  47. const ValueTree& state, Component* parent)
  48. {
  49. Component* const c = type.addNewComponentFromState (state, parent);
  50. jassert (c != nullptr && c->getParentComponent() == parent);
  51. c->setComponentID (getStateId (state));
  52. return c;
  53. }
  54. static void updateComponent (ComponentBuilder& builder, const ValueTree& state)
  55. {
  56. if (Component* topLevelComp = builder.getManagedComponent())
  57. {
  58. ComponentBuilder::TypeHandler* const type = builder.getHandlerForState (state);
  59. const String uid (getStateId (state));
  60. if (type == nullptr || uid.isEmpty())
  61. {
  62. // ..handle the case where a child of the actual state node has changed.
  63. if (state.getParent().isValid())
  64. updateComponent (builder, state.getParent());
  65. }
  66. else
  67. {
  68. if (Component* const changedComp = findComponentWithID (*topLevelComp, uid))
  69. type->updateComponentFromState (changedComp, state);
  70. }
  71. }
  72. }
  73. }
  74. //==============================================================================
  75. const Identifier ComponentBuilder::idProperty ("id");
  76. ComponentBuilder::ComponentBuilder()
  77. : imageProvider (nullptr)
  78. {
  79. }
  80. ComponentBuilder::ComponentBuilder (const ValueTree& state_)
  81. : state (state_), imageProvider (nullptr)
  82. {
  83. state.addListener (this);
  84. }
  85. ComponentBuilder::~ComponentBuilder()
  86. {
  87. state.removeListener (this);
  88. #if JUCE_DEBUG
  89. // Don't delete the managed component!! The builder owns that component, and will delete
  90. // it automatically when it gets deleted.
  91. jassert (componentRef.get() == static_cast<Component*> (component));
  92. #endif
  93. }
  94. Component* ComponentBuilder::getManagedComponent()
  95. {
  96. if (component == nullptr)
  97. {
  98. component = createComponent();
  99. #if JUCE_DEBUG
  100. componentRef = component;
  101. #endif
  102. }
  103. return component;
  104. }
  105. Component* ComponentBuilder::createComponent()
  106. {
  107. jassert (types.size() > 0); // You need to register all the necessary types before you can load a component!
  108. if (TypeHandler* const type = getHandlerForState (state))
  109. return ComponentBuilderHelpers::createNewComponent (*type, state, nullptr);
  110. jassertfalse; // trying to create a component from an unknown type of ValueTree
  111. return nullptr;
  112. }
  113. void ComponentBuilder::registerTypeHandler (ComponentBuilder::TypeHandler* const type)
  114. {
  115. jassert (type != nullptr);
  116. // Don't try to move your types around! Once a type has been added to a builder, the
  117. // builder owns it, and you should leave it alone!
  118. jassert (type->builder == nullptr);
  119. types.add (type);
  120. type->builder = this;
  121. }
  122. ComponentBuilder::TypeHandler* ComponentBuilder::getHandlerForState (const ValueTree& s) const
  123. {
  124. const Identifier targetType (s.getType());
  125. for (int i = 0; i < types.size(); ++i)
  126. {
  127. TypeHandler* const t = types.getUnchecked(i);
  128. if (t->type == targetType)
  129. return t;
  130. }
  131. return nullptr;
  132. }
  133. int ComponentBuilder::getNumHandlers() const noexcept
  134. {
  135. return types.size();
  136. }
  137. ComponentBuilder::TypeHandler* ComponentBuilder::getHandler (const int index) const noexcept
  138. {
  139. return types [index];
  140. }
  141. void ComponentBuilder::registerStandardComponentTypes()
  142. {
  143. Drawable::registerDrawableTypeHandlers (*this);
  144. }
  145. void ComponentBuilder::setImageProvider (ImageProvider* newImageProvider) noexcept
  146. {
  147. imageProvider = newImageProvider;
  148. }
  149. ComponentBuilder::ImageProvider* ComponentBuilder::getImageProvider() const noexcept
  150. {
  151. return imageProvider;
  152. }
  153. void ComponentBuilder::valueTreePropertyChanged (ValueTree& tree, const Identifier&)
  154. {
  155. ComponentBuilderHelpers::updateComponent (*this, tree);
  156. }
  157. void ComponentBuilder::valueTreeChildAdded (ValueTree& tree, ValueTree&)
  158. {
  159. ComponentBuilderHelpers::updateComponent (*this, tree);
  160. }
  161. void ComponentBuilder::valueTreeChildRemoved (ValueTree& tree, ValueTree&, int)
  162. {
  163. ComponentBuilderHelpers::updateComponent (*this, tree);
  164. }
  165. void ComponentBuilder::valueTreeChildOrderChanged (ValueTree& tree, int, int)
  166. {
  167. ComponentBuilderHelpers::updateComponent (*this, tree);
  168. }
  169. void ComponentBuilder::valueTreeParentChanged (ValueTree& tree)
  170. {
  171. ComponentBuilderHelpers::updateComponent (*this, tree);
  172. }
  173. //==============================================================================
  174. ComponentBuilder::TypeHandler::TypeHandler (const Identifier& valueTreeType)
  175. : type (valueTreeType), builder (nullptr)
  176. {
  177. }
  178. ComponentBuilder::TypeHandler::~TypeHandler()
  179. {
  180. }
  181. ComponentBuilder* ComponentBuilder::TypeHandler::getBuilder() const noexcept
  182. {
  183. // A type handler needs to be registered with a ComponentBuilder before using it!
  184. jassert (builder != nullptr);
  185. return builder;
  186. }
  187. void ComponentBuilder::updateChildComponents (Component& parent, const ValueTree& children)
  188. {
  189. using namespace ComponentBuilderHelpers;
  190. const int numExistingChildComps = parent.getNumChildComponents();
  191. Array<Component*> componentsInOrder;
  192. componentsInOrder.ensureStorageAllocated (numExistingChildComps);
  193. {
  194. OwnedArray<Component> existingComponents;
  195. existingComponents.ensureStorageAllocated (numExistingChildComps);
  196. for (int i = 0; i < numExistingChildComps; ++i)
  197. existingComponents.add (parent.getChildComponent (i));
  198. const int newNumChildren = children.getNumChildren();
  199. for (int i = 0; i < newNumChildren; ++i)
  200. {
  201. const ValueTree childState (children.getChild (i));
  202. Component* c = removeComponentWithID (existingComponents, getStateId (childState));
  203. if (c == nullptr)
  204. {
  205. if (TypeHandler* const type = getHandlerForState (childState))
  206. c = ComponentBuilderHelpers::createNewComponent (*type, childState, &parent);
  207. else
  208. jassertfalse;
  209. }
  210. if (c != nullptr)
  211. componentsInOrder.add (c);
  212. }
  213. // (remaining unused items in existingComponents get deleted here as it goes out of scope)
  214. }
  215. // Make sure the z-order is correct..
  216. if (componentsInOrder.size() > 0)
  217. {
  218. componentsInOrder.getLast()->toFront (false);
  219. for (int i = componentsInOrder.size() - 1; --i >= 0;)
  220. componentsInOrder.getUnchecked(i)->toBehind (componentsInOrder.getUnchecked (i + 1));
  221. }
  222. }