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.

264 lines
12KB

  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. #ifndef __JUCE_COMPONENTBUILDER_JUCEHEADER__
  19. #define __JUCE_COMPONENTBUILDER_JUCEHEADER__
  20. #include "../components/juce_Component.h"
  21. //==============================================================================
  22. /**
  23. Loads and maintains a tree of Components from a ValueTree that represents them.
  24. To allow the state of a tree of components to be saved as a ValueTree and re-loaded,
  25. this class lets you register a set of type-handlers for the different components that
  26. are involved, and then uses these types to re-create a set of components from its
  27. stored state.
  28. Essentially, to use this, you need to create a ComponentBuilder with your ValueTree,
  29. then use registerTypeHandler() to give it a set of type handlers that can cope with
  30. all the items in your tree. Then you can call getComponent() to build the component.
  31. Once you've got the component you can either take it and delete the ComponentBuilder
  32. object, or if you keep the ComponentBuilder around, it'll monitor any changes in the
  33. ValueTree and automatically update the component to reflect these changes.
  34. */
  35. class JUCE_API ComponentBuilder : private ValueTree::Listener
  36. {
  37. public:
  38. /** Creates a ComponentBuilder that will use the given state.
  39. Once you've created your builder, you should use registerTypeHandler() to register some
  40. type handlers for it, and then you can call createComponent() or getManagedComponent()
  41. to get the actual component.
  42. */
  43. explicit ComponentBuilder (const ValueTree& state);
  44. /** Creates a builder that doesn't have a state object. */
  45. ComponentBuilder();
  46. /** Destructor. */
  47. ~ComponentBuilder();
  48. /** This is the ValueTree data object that the builder is working with. */
  49. ValueTree state;
  50. //==============================================================================
  51. /** Returns the builder's component (creating it if necessary).
  52. The first time that this method is called, the builder will attempt to create a component
  53. from the ValueTree, so you must have registered some suitable type handlers before calling
  54. this. If there's a problem and the component can't be created, this method returns 0.
  55. The component that is returned is owned by this ComponentBuilder, so you can put it inside
  56. your own parent components, but don't delete it! The ComponentBuilder will delete it automatically
  57. when the builder is destroyed. If you want to get a component that you can delete yourself,
  58. call createComponent() instead.
  59. The ComponentBuilder will update this component if any changes are made to the ValueTree, so if
  60. there's a chance that the tree might change, be careful not to keep any pointers to sub-components,
  61. as they may be changed or removed.
  62. */
  63. Component* getManagedComponent();
  64. /** Creates and returns a new instance of the component that the ValueTree represents.
  65. The caller is responsible for using and deleting the object that is returned. Unlike
  66. getManagedComponent(), the component that is returned will not be updated by the builder.
  67. */
  68. Component* createComponent();
  69. //==============================================================================
  70. /**
  71. The class is a base class for objects that manage the loading of a type of component
  72. from a ValueTree.
  73. To store and re-load a tree of components as a ValueTree, each component type must have
  74. a TypeHandler to represent it.
  75. @see ComponentBuilder::registerTypeHandler(), Drawable::registerDrawableTypeHandlers()
  76. */
  77. class JUCE_API TypeHandler
  78. {
  79. public:
  80. //==============================================================================
  81. /** Creates a TypeHandler.
  82. The valueTreeType must be the type name of the ValueTrees that this handler can parse.
  83. */
  84. explicit TypeHandler (const Identifier& valueTreeType);
  85. /** Destructor. */
  86. virtual ~TypeHandler();
  87. /** Returns the type of the ValueTrees that this handler can parse. */
  88. const Identifier type;
  89. /** Returns the builder that this type is registered with. */
  90. ComponentBuilder* getBuilder() const noexcept;
  91. //==============================================================================
  92. /** This method must create a new component from the given state, add it to the specified
  93. parent component (which may be null), and return it.
  94. The ValueTree will have been pre-checked to make sure that its type matches the type
  95. that this handler supports.
  96. There's no need to set the new Component's ID to match that of the state - the builder
  97. will take care of that itself.
  98. */
  99. virtual Component* addNewComponentFromState (const ValueTree& state, Component* parent) = 0;
  100. /** This method must update an existing component from a new ValueTree state.
  101. A component that has been created with addNewComponentFromState() may need to be updated
  102. if the ValueTree changes, so this method is used to do that. Your implementation must do
  103. whatever's necessary to update the component from the new state provided.
  104. The ValueTree will have been pre-checked to make sure that its type matches the type
  105. that this handler supports, and the component will have been created by this type's
  106. addNewComponentFromState() method.
  107. */
  108. virtual void updateComponentFromState (Component* component, const ValueTree& state) = 0;
  109. private:
  110. //==============================================================================
  111. friend class ComponentBuilder;
  112. ComponentBuilder* builder;
  113. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TypeHandler);
  114. };
  115. //==============================================================================
  116. /** Adds a type handler that the builder can use when trying to load components.
  117. @see Drawable::registerDrawableTypeHandlers()
  118. */
  119. void registerTypeHandler (TypeHandler* type);
  120. /** Tries to find a registered type handler that can load a component from the given ValueTree. */
  121. TypeHandler* getHandlerForState (const ValueTree& state) const;
  122. /** Returns the number of registered type handlers.
  123. @see getHandler, registerTypeHandler
  124. */
  125. int getNumHandlers() const noexcept;
  126. /** Returns one of the registered type handlers.
  127. @see getNumHandlers, registerTypeHandler
  128. */
  129. TypeHandler* getHandler (int index) const noexcept;
  130. /** Registers handlers for various standard juce components. */
  131. void registerStandardComponentTypes();
  132. //=============================================================================
  133. /** This class is used when references to images need to be stored in ValueTrees.
  134. An instance of an ImageProvider provides a mechanism for converting an Image to/from
  135. a reference, which may be a file, URL, ID string, or whatever system is appropriate in
  136. your app.
  137. When you're loading components from a ValueTree that may need a way of loading images, you
  138. should call ComponentBuilder::setImageProvider() to supply a suitable provider before
  139. trying to load the component.
  140. @see ComponentBuilder::setImageProvider()
  141. */
  142. class JUCE_API ImageProvider
  143. {
  144. public:
  145. ImageProvider() {}
  146. virtual ~ImageProvider() {}
  147. /** Retrieves the image associated with this identifier, which could be any
  148. kind of string, number, filename, etc.
  149. The image that is returned will be owned by the caller, but it may come
  150. from the ImageCache.
  151. */
  152. virtual Image getImageForIdentifier (const var& imageIdentifier) = 0;
  153. /** Returns an identifier to be used to refer to a given image.
  154. This is used when a reference to an image is stored in a ValueTree.
  155. */
  156. virtual var getIdentifierForImage (const Image& image) = 0;
  157. };
  158. //==============================================================================
  159. /** Gives the builder an ImageProvider object that the type handlers can use when
  160. loading images from stored references.
  161. The object that is passed in is not owned by the builder, so the caller must delete
  162. it when it is no longer needed, but not while the builder may still be using it. To
  163. clear the image provider, just call setImageProvider (nullptr).
  164. */
  165. void setImageProvider (ImageProvider* newImageProvider) noexcept;
  166. /** Returns the current image provider that this builder is using, or 0 if none has been set. */
  167. ImageProvider* getImageProvider() const noexcept;
  168. //=============================================================================
  169. /** Updates the children of a parent component by updating them from the children of
  170. a given ValueTree.
  171. */
  172. void updateChildComponents (Component& parent, const ValueTree& children);
  173. /** An identifier for the property of the ValueTrees that is used to store a unique ID
  174. for that component.
  175. */
  176. static const Identifier idProperty;
  177. /**
  178. */
  179. static void initialiseFromValueTree (Component& component,
  180. const ValueTree& state,
  181. ImageProvider* imageProvider);
  182. //=============================================================================
  183. /** @internal */
  184. static void refreshBasicComponentProperties (Component&, const ValueTree&);
  185. /** @internal */
  186. static RelativeRectangle getComponentBounds (const ValueTree&);
  187. private:
  188. //=============================================================================
  189. OwnedArray <TypeHandler> types;
  190. ScopedPointer<Component> component;
  191. ImageProvider* imageProvider;
  192. #if JUCE_DEBUG
  193. WeakReference<Component> componentRef;
  194. #endif
  195. void valueTreePropertyChanged (ValueTree&, const Identifier&);
  196. void valueTreeChildAdded (ValueTree&, ValueTree&);
  197. void valueTreeChildRemoved (ValueTree&, ValueTree&);
  198. void valueTreeChildOrderChanged (ValueTree&);
  199. void valueTreeParentChanged (ValueTree&);
  200. static const Identifier positionID;
  201. void initialiseRecursively (Component&, const ValueTree&);
  202. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentBuilder);
  203. };
  204. #endif // __JUCE_COMPONENTBUILDER_JUCEHEADER__