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.

281 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. namespace ComponentBuilderHelpers
  19. {
  20. static String getStateId (const ValueTree& state)
  21. {
  22. return state [ComponentBuilder::idProperty].toString();
  23. }
  24. static Component* removeComponentWithID (OwnedArray<Component>& components, const String& compId)
  25. {
  26. jassert (compId.isNotEmpty());
  27. for (int i = components.size(); --i >= 0;)
  28. {
  29. Component* const c = components.getUnchecked (i);
  30. if (c->getComponentID() == compId)
  31. return components.removeAndReturn (i);
  32. }
  33. return nullptr;
  34. }
  35. static Component* findComponentWithID (Component& c, const String& compId)
  36. {
  37. jassert (compId.isNotEmpty());
  38. if (c.getComponentID() == compId)
  39. return &c;
  40. for (int i = c.getNumChildComponents(); --i >= 0;)
  41. if (Component* const child = findComponentWithID (*c.getChildComponent (i), compId))
  42. return child;
  43. return nullptr;
  44. }
  45. static Component* createNewComponent (ComponentBuilder::TypeHandler& type,
  46. const ValueTree& state, Component* parent)
  47. {
  48. Component* const c = type.addNewComponentFromState (state, parent);
  49. jassert (c != nullptr && c->getParentComponent() == parent);
  50. c->setComponentID (getStateId (state));
  51. return c;
  52. }
  53. static void updateComponent (ComponentBuilder& builder, const ValueTree& state)
  54. {
  55. if (Component* topLevelComp = builder.getManagedComponent())
  56. {
  57. ComponentBuilder::TypeHandler* const type = builder.getHandlerForState (state);
  58. const String uid (getStateId (state));
  59. if (type == nullptr || uid.isEmpty())
  60. {
  61. // ..handle the case where a child of the actual state node has changed.
  62. if (state.getParent().isValid())
  63. updateComponent (builder, state.getParent());
  64. }
  65. else
  66. {
  67. if (Component* const changedComp = findComponentWithID (*topLevelComp, uid))
  68. type->updateComponentFromState (changedComp, state);
  69. }
  70. }
  71. }
  72. }
  73. //=============================================================================
  74. const Identifier ComponentBuilder::idProperty ("id");
  75. ComponentBuilder::ComponentBuilder()
  76. : imageProvider (nullptr)
  77. {
  78. }
  79. ComponentBuilder::ComponentBuilder (const ValueTree& state_)
  80. : state (state_), imageProvider (nullptr)
  81. {
  82. state.addListener (this);
  83. }
  84. ComponentBuilder::~ComponentBuilder()
  85. {
  86. state.removeListener (this);
  87. #if JUCE_DEBUG
  88. // Don't delete the managed component!! The builder owns that component, and will delete
  89. // it automatically when it gets deleted.
  90. jassert (componentRef.get() == static_cast <Component*> (component));
  91. #endif
  92. }
  93. Component* ComponentBuilder::getManagedComponent()
  94. {
  95. if (component == nullptr)
  96. {
  97. component = createComponent();
  98. #if JUCE_DEBUG
  99. componentRef = component;
  100. #endif
  101. }
  102. return component;
  103. }
  104. Component* ComponentBuilder::createComponent()
  105. {
  106. jassert (types.size() > 0); // You need to register all the necessary types before you can load a component!
  107. TypeHandler* const type = getHandlerForState (state);
  108. jassert (type != nullptr); // trying to create a component from an unknown type of ValueTree
  109. return type != nullptr ? ComponentBuilderHelpers::createNewComponent (*type, state, nullptr) : 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. }