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.

280 lines
8.6KB

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