juce_ComponentBuilder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  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. @tags{GUI}
  35. */
  36. class JUCE_API ComponentBuilder : private ValueTree::Listener
  37. {
  38. public:
  39. /** Creates a ComponentBuilder that will use the given state.
  40. Once you've created your builder, you should use registerTypeHandler() to register some
  41. type handlers for it, and then you can call createComponent() or getManagedComponent()
  42. to get the actual component.
  43. */
  44. explicit ComponentBuilder (const ValueTree& state);
  45. /** Creates a builder that doesn't have a state object. */
  46. ComponentBuilder();
  47. /** Destructor. */
  48. ~ComponentBuilder() override;
  49. /** This is the ValueTree data object that the builder is working with. */
  50. ValueTree state;
  51. //==============================================================================
  52. /** Returns the builder's component (creating it if necessary).
  53. The first time that this method is called, the builder will attempt to create a component
  54. from the ValueTree, so you must have registered some suitable type handlers before calling
  55. this. If there's a problem and the component can't be created, this method returns nullptr.
  56. The component that is returned is owned by this ComponentBuilder, so you can put it inside
  57. your own parent components, but don't delete it! The ComponentBuilder will delete it automatically
  58. when the builder is destroyed. If you want to get a component that you can delete yourself,
  59. call createComponent() instead.
  60. The ComponentBuilder will update this component if any changes are made to the ValueTree, so if
  61. there's a chance that the tree might change, be careful not to keep any pointers to sub-components,
  62. as they may be changed or removed.
  63. */
  64. Component* getManagedComponent();
  65. /** Creates and returns a new instance of the component that the ValueTree represents.
  66. The caller is responsible for using and deleting the object that is returned. Unlike
  67. getManagedComponent(), the component that is returned will not be updated by the builder.
  68. */
  69. Component* createComponent();
  70. //==============================================================================
  71. /**
  72. The class is a base class for objects that manage the loading of a type of component
  73. from a ValueTree.
  74. To store and re-load a tree of components as a ValueTree, each component type must have
  75. a TypeHandler to represent it.
  76. @see ComponentBuilder::registerTypeHandler(), Drawable::registerDrawableTypeHandlers()
  77. */
  78. class JUCE_API TypeHandler
  79. {
  80. public:
  81. //==============================================================================
  82. /** Creates a TypeHandler.
  83. The valueTreeType must be the type name of the ValueTrees that this handler can parse.
  84. */
  85. explicit TypeHandler (const Identifier& valueTreeType);
  86. /** Destructor. */
  87. virtual ~TypeHandler();
  88. /** Returns the type of the ValueTrees that this handler can parse. */
  89. const Identifier type;
  90. /** Returns the builder that this type is registered with. */
  91. ComponentBuilder* getBuilder() const noexcept;
  92. //==============================================================================
  93. /** This method must create a new component from the given state, add it to the specified
  94. parent component (which may be null), and return it.
  95. The ValueTree will have been pre-checked to make sure that its type matches the type
  96. that this handler supports.
  97. There's no need to set the new Component's ID to match that of the state - the builder
  98. will take care of that itself.
  99. */
  100. virtual Component* addNewComponentFromState (const ValueTree& state, Component* parent) = 0;
  101. /** This method must update an existing component from a new ValueTree state.
  102. A component that has been created with addNewComponentFromState() may need to be updated
  103. if the ValueTree changes, so this method is used to do that. Your implementation must do
  104. whatever's necessary to update the component from the new state provided.
  105. The ValueTree will have been pre-checked to make sure that its type matches the type
  106. that this handler supports, and the component will have been created by this type's
  107. addNewComponentFromState() method.
  108. */
  109. virtual void updateComponentFromState (Component* component, const ValueTree& state) = 0;
  110. private:
  111. //==============================================================================
  112. friend class ComponentBuilder;
  113. ComponentBuilder* builder;
  114. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TypeHandler)
  115. };
  116. //==============================================================================
  117. /** Adds a type handler that the builder can use when trying to load components.
  118. @see Drawable::registerDrawableTypeHandlers()
  119. */
  120. void registerTypeHandler (TypeHandler* type);
  121. /** Tries to find a registered type handler that can load a component from the given ValueTree. */
  122. TypeHandler* getHandlerForState (const ValueTree& state) const;
  123. /** Returns the number of registered type handlers.
  124. @see getHandler, registerTypeHandler
  125. */
  126. int getNumHandlers() const noexcept;
  127. /** Returns one of the registered type handlers.
  128. @see getNumHandlers, registerTypeHandler
  129. */
  130. TypeHandler* getHandler (int index) const noexcept;
  131. /** Registers handlers for various standard juce components. */
  132. void registerStandardComponentTypes();
  133. //==============================================================================
  134. /** This class is used when references to images need to be stored in ValueTrees.
  135. An instance of an ImageProvider provides a mechanism for converting an Image to/from
  136. a reference, which may be a file, URL, ID string, or whatever system is appropriate in
  137. your app.
  138. When you're loading components from a ValueTree that may need a way of loading images, you
  139. should call ComponentBuilder::setImageProvider() to supply a suitable provider before
  140. trying to load the component.
  141. @see ComponentBuilder::setImageProvider()
  142. */
  143. class JUCE_API ImageProvider
  144. {
  145. public:
  146. ImageProvider() = default;
  147. virtual ~ImageProvider() = default;
  148. /** Retrieves the image associated with this identifier, which could be any
  149. kind of string, number, filename, etc.
  150. The image that is returned will be owned by the caller, but it may come
  151. from the ImageCache.
  152. */
  153. virtual Image getImageForIdentifier (const var& imageIdentifier) = 0;
  154. /** Returns an identifier to be used to refer to a given image.
  155. This is used when a reference to an image is stored in a ValueTree.
  156. */
  157. virtual var getIdentifierForImage (const Image& image) = 0;
  158. };
  159. //==============================================================================
  160. /** Gives the builder an ImageProvider object that the type handlers can use when
  161. loading images from stored references.
  162. The object that is passed in is not owned by the builder, so the caller must delete
  163. it when it is no longer needed, but not while the builder may still be using it. To
  164. clear the image provider, just call setImageProvider (nullptr).
  165. */
  166. void setImageProvider (ImageProvider* newImageProvider) noexcept;
  167. /** Returns the current image provider that this builder is using, or nullptr if none has been set. */
  168. ImageProvider* getImageProvider() const noexcept;
  169. //==============================================================================
  170. /** Updates the children of a parent component by updating them from the children of
  171. a given ValueTree.
  172. */
  173. void updateChildComponents (Component& parent, const ValueTree& children);
  174. /** An identifier for the property of the ValueTrees that is used to store a unique ID
  175. for that component.
  176. */
  177. static const Identifier idProperty;
  178. private:
  179. //==============================================================================
  180. OwnedArray<TypeHandler> types;
  181. std::unique_ptr<Component> component;
  182. ImageProvider* imageProvider;
  183. #if JUCE_DEBUG
  184. WeakReference<Component> componentRef;
  185. #endif
  186. void valueTreePropertyChanged (ValueTree&, const Identifier&) override;
  187. void valueTreeChildAdded (ValueTree&, ValueTree&) override;
  188. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override;
  189. void valueTreeChildOrderChanged (ValueTree&, int, int) override;
  190. void valueTreeParentChanged (ValueTree&) override;
  191. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentBuilder)
  192. };
  193. } // namespace juce