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.

288 lines
8.8KB

  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. BEGIN_JUCE_NAMESPACE
  19. //=============================================================================
  20. namespace ComponentBuilderHelpers
  21. {
  22. String getStateId (const ValueTree& state)
  23. {
  24. return state [ComponentBuilder::idProperty].toString();
  25. }
  26. Component* findComponentWithID (OwnedArray<Component>& components, const String& compId)
  27. {
  28. jassert (compId.isNotEmpty());
  29. for (int i = components.size(); --i >= 0;)
  30. {
  31. Component* const c = components.getUnchecked (i);
  32. if (c->getComponentID() == compId)
  33. return components.removeAndReturn (i);
  34. }
  35. return nullptr;
  36. }
  37. Component* findComponentWithID (Component* const c, const String& compId)
  38. {
  39. jassert (compId.isNotEmpty());
  40. if (c->getComponentID() == compId)
  41. return c;
  42. for (int i = c->getNumChildComponents(); --i >= 0;)
  43. {
  44. Component* const child = findComponentWithID (c->getChildComponent (i), compId);
  45. if (child != nullptr)
  46. return child;
  47. }
  48. return nullptr;
  49. }
  50. Component* createNewComponent (ComponentBuilder::TypeHandler& type,
  51. const ValueTree& state, Component* parent)
  52. {
  53. Component* const c = type.addNewComponentFromState (state, parent);
  54. jassert (c != nullptr && c->getParentComponent() == parent);
  55. c->setComponentID (getStateId (state));
  56. return c;
  57. }
  58. void updateComponent (ComponentBuilder& builder, const ValueTree& state)
  59. {
  60. Component* topLevelComp = builder.getManagedComponent();
  61. if (topLevelComp != nullptr)
  62. {
  63. ComponentBuilder::TypeHandler* const type = builder.getHandlerForState (state);
  64. const String uid (getStateId (state));
  65. if (type == nullptr || uid.isEmpty())
  66. {
  67. // ..handle the case where a child of the actual state node has changed.
  68. if (state.getParent().isValid())
  69. updateComponent (builder, state.getParent());
  70. }
  71. else
  72. {
  73. Component* const changedComp = findComponentWithID (topLevelComp, uid);
  74. if (changedComp != nullptr)
  75. type->updateComponentFromState (changedComp, state);
  76. }
  77. }
  78. }
  79. }
  80. //=============================================================================
  81. const Identifier ComponentBuilder::idProperty ("id");
  82. ComponentBuilder::ComponentBuilder (const ValueTree& state_)
  83. : state (state_), imageProvider (nullptr)
  84. {
  85. state.addListener (this);
  86. }
  87. ComponentBuilder::~ComponentBuilder()
  88. {
  89. state.removeListener (this);
  90. #if JUCE_DEBUG
  91. // Don't delete the managed component!! The builder owns that component, and will delete
  92. // it automatically when it gets deleted.
  93. jassert (componentRef.get() == static_cast <Component*> (component));
  94. #endif
  95. }
  96. Component* ComponentBuilder::getManagedComponent()
  97. {
  98. if (component == nullptr)
  99. {
  100. component = createComponent();
  101. #if JUCE_DEBUG
  102. componentRef = component;
  103. #endif
  104. }
  105. return component;
  106. }
  107. Component* ComponentBuilder::createComponent()
  108. {
  109. jassert (types.size() > 0); // You need to register all the necessary types before you can load a component!
  110. TypeHandler* const type = getHandlerForState (state);
  111. jassert (type != nullptr); // trying to create a component from an unknown type of ValueTree
  112. return type != nullptr ? ComponentBuilderHelpers::createNewComponent (*type, state, nullptr) : nullptr;
  113. }
  114. void ComponentBuilder::registerTypeHandler (ComponentBuilder::TypeHandler* const type)
  115. {
  116. jassert (type != nullptr);
  117. // Don't try to move your types around! Once a type has been added to a builder, the
  118. // builder owns it, and you should leave it alone!
  119. jassert (type->builder == nullptr);
  120. types.add (type);
  121. type->builder = this;
  122. }
  123. ComponentBuilder::TypeHandler* ComponentBuilder::getHandlerForState (const ValueTree& s) const
  124. {
  125. const Identifier targetType (s.getType());
  126. for (int i = 0; i < types.size(); ++i)
  127. {
  128. TypeHandler* const t = types.getUnchecked(i);
  129. if (t->getType() == targetType)
  130. return t;
  131. }
  132. return nullptr;
  133. }
  134. int ComponentBuilder::getNumHandlers() const noexcept
  135. {
  136. return types.size();
  137. }
  138. ComponentBuilder::TypeHandler* ComponentBuilder::getHandler (const int index) const noexcept
  139. {
  140. return types [index];
  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. : builder (nullptr),
  173. valueTreeType (valueTreeType_)
  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. int i;
  195. for (i = 0; i < numExistingChildComps; ++i)
  196. existingComponents.add (parent.getChildComponent (i));
  197. const int newNumChildren = children.getNumChildren();
  198. for (i = 0; i < newNumChildren; ++i)
  199. {
  200. const ValueTree childState (children.getChild (i));
  201. ComponentBuilder::TypeHandler* const type = getHandlerForState (childState);
  202. jassert (type != nullptr);
  203. if (type != nullptr)
  204. {
  205. Component* c = findComponentWithID (existingComponents, getStateId (childState));
  206. if (c == nullptr)
  207. c = createNewComponent (*type, childState, &parent);
  208. componentsInOrder.add (c);
  209. }
  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. }
  221. END_JUCE_NAMESPACE