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.h 11KB

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