Audio plugin host https://kx.studio/carla
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.

juce_ComponentBuilder.cpp 8.6KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. if (TypeHandler* const type = getHandlerForState (state))
  107. return ComponentBuilderHelpers::createNewComponent (*type, state, nullptr);
  108. jassertfalse; // trying to create a component from an unknown type of ValueTree
  109. return nullptr;
  110. }
  111. void ComponentBuilder::registerTypeHandler (ComponentBuilder::TypeHandler* const type)
  112. {
  113. jassert (type != nullptr);
  114. // Don't try to move your types around! Once a type has been added to a builder, the
  115. // builder owns it, and you should leave it alone!
  116. jassert (type->builder == nullptr);
  117. types.add (type);
  118. type->builder = this;
  119. }
  120. ComponentBuilder::TypeHandler* ComponentBuilder::getHandlerForState (const ValueTree& s) const
  121. {
  122. const Identifier targetType (s.getType());
  123. for (int i = 0; i < types.size(); ++i)
  124. {
  125. TypeHandler* const t = types.getUnchecked(i);
  126. if (t->type == targetType)
  127. return t;
  128. }
  129. return nullptr;
  130. }
  131. int ComponentBuilder::getNumHandlers() const noexcept
  132. {
  133. return types.size();
  134. }
  135. ComponentBuilder::TypeHandler* ComponentBuilder::getHandler (const int index) const noexcept
  136. {
  137. return types [index];
  138. }
  139. void ComponentBuilder::registerStandardComponentTypes()
  140. {
  141. Drawable::registerDrawableTypeHandlers (*this);
  142. }
  143. void ComponentBuilder::setImageProvider (ImageProvider* newImageProvider) noexcept
  144. {
  145. imageProvider = newImageProvider;
  146. }
  147. ComponentBuilder::ImageProvider* ComponentBuilder::getImageProvider() const noexcept
  148. {
  149. return imageProvider;
  150. }
  151. void ComponentBuilder::valueTreePropertyChanged (ValueTree& tree, const Identifier&)
  152. {
  153. ComponentBuilderHelpers::updateComponent (*this, tree);
  154. }
  155. void ComponentBuilder::valueTreeChildAdded (ValueTree& tree, ValueTree&)
  156. {
  157. ComponentBuilderHelpers::updateComponent (*this, tree);
  158. }
  159. void ComponentBuilder::valueTreeChildRemoved (ValueTree& tree, ValueTree&)
  160. {
  161. ComponentBuilderHelpers::updateComponent (*this, tree);
  162. }
  163. void ComponentBuilder::valueTreeChildOrderChanged (ValueTree& tree)
  164. {
  165. ComponentBuilderHelpers::updateComponent (*this, tree);
  166. }
  167. void ComponentBuilder::valueTreeParentChanged (ValueTree& tree)
  168. {
  169. ComponentBuilderHelpers::updateComponent (*this, tree);
  170. }
  171. //==============================================================================
  172. ComponentBuilder::TypeHandler::TypeHandler (const Identifier& valueTreeType)
  173. : type (valueTreeType), builder (nullptr)
  174. {
  175. }
  176. ComponentBuilder::TypeHandler::~TypeHandler()
  177. {
  178. }
  179. ComponentBuilder* ComponentBuilder::TypeHandler::getBuilder() const noexcept
  180. {
  181. // A type handler needs to be registered with a ComponentBuilder before using it!
  182. jassert (builder != nullptr);
  183. return builder;
  184. }
  185. void ComponentBuilder::updateChildComponents (Component& parent, const ValueTree& children)
  186. {
  187. using namespace ComponentBuilderHelpers;
  188. const int numExistingChildComps = parent.getNumChildComponents();
  189. Array<Component*> componentsInOrder;
  190. componentsInOrder.ensureStorageAllocated (numExistingChildComps);
  191. {
  192. OwnedArray<Component> existingComponents;
  193. existingComponents.ensureStorageAllocated (numExistingChildComps);
  194. for (int i = 0; i < numExistingChildComps; ++i)
  195. existingComponents.add (parent.getChildComponent (i));
  196. const int newNumChildren = children.getNumChildren();
  197. for (int i = 0; i < newNumChildren; ++i)
  198. {
  199. const ValueTree childState (children.getChild (i));
  200. Component* c = removeComponentWithID (existingComponents, getStateId (childState));
  201. if (c == nullptr)
  202. {
  203. if (TypeHandler* const type = getHandlerForState (childState))
  204. c = ComponentBuilderHelpers::createNewComponent (*type, childState, &parent);
  205. else
  206. jassertfalse;
  207. }
  208. if (c != nullptr)
  209. componentsInOrder.add (c);
  210. }
  211. // (remaining unused items in existingComponents get deleted here as it goes out of scope)
  212. }
  213. // Make sure the z-order is correct..
  214. if (componentsInOrder.size() > 0)
  215. {
  216. componentsInOrder.getLast()->toFront (false);
  217. for (int i = componentsInOrder.size() - 1; --i >= 0;)
  218. componentsInOrder.getUnchecked(i)->toBehind (componentsInOrder.getUnchecked (i + 1));
  219. }
  220. }