LMMS
Loading...
Searching...
No Matches
juce_ComponentBuilder.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30{
31 static String getStateId (const ValueTree& state)
32 {
33 return state [ComponentBuilder::idProperty].toString();
34 }
35
36 static Component* removeComponentWithID (OwnedArray<Component>& components, const String& compId)
37 {
38 jassert (compId.isNotEmpty());
39
40 for (int i = components.size(); --i >= 0;)
41 {
42 Component* const c = components.getUnchecked (i);
43
44 if (c->getComponentID() == compId)
45 return components.removeAndReturn (i);
46 }
47
48 return nullptr;
49 }
50
52 {
53 jassert (compId.isNotEmpty());
54 if (c.getComponentID() == compId)
55 return &c;
56
57 for (auto* child : c.getChildren())
58 if (auto* found = findComponentWithID (*child, compId))
59 return found;
60
61 return nullptr;
62 }
63
65 const ValueTree& state, Component* parent)
66 {
67 Component* const c = type.addNewComponentFromState (state, parent);
68 jassert (c != nullptr && c->getParentComponent() == parent);
69 c->setComponentID (getStateId (state));
70 return c;
71 }
72
73 static void updateComponent (ComponentBuilder& builder, const ValueTree& state)
74 {
75 if (Component* topLevelComp = builder.getManagedComponent())
76 {
78 const String uid (getStateId (state));
79
80 if (type == nullptr || uid.isEmpty())
81 {
82 // ..handle the case where a child of the actual state node has changed.
83 if (state.getParent().isValid())
84 updateComponent (builder, state.getParent());
85 }
86 else
87 {
88 if (Component* const changedComp = findComponentWithID (*topLevelComp, uid))
89 type->updateComponentFromState (changedComp, state);
90 }
91 }
92 }
93}
94
95//==============================================================================
96const Identifier ComponentBuilder::idProperty ("id");
97
102
104 : state (state_), imageProvider (nullptr)
105{
106 state.addListener (this);
107}
108
110{
111 state.removeListener (this);
112
113 #if JUCE_DEBUG
114 // Don't delete the managed component!! The builder owns that component, and will delete
115 // it automatically when it gets deleted.
116 jassert (componentRef.get() == component.get());
117 #endif
118}
119
121{
122 if (component == nullptr)
123 {
124 component.reset (createComponent());
125
126 #if JUCE_DEBUG
127 componentRef = component.get();
128 #endif
129 }
130
131 return component.get();
132}
133
135{
136 jassert (types.size() > 0); // You need to register all the necessary types before you can load a component!
137
140
141 jassertfalse; // trying to create a component from an unknown type of ValueTree
142 return nullptr;
143}
144
146{
147 jassert (type != nullptr);
148
149 // Don't try to move your types around! Once a type has been added to a builder, the
150 // builder owns it, and you should leave it alone!
151 jassert (type->builder == nullptr);
152
153 types.add (type);
154 type->builder = this;
155}
156
158{
159 const Identifier targetType (s.getType());
160
161 for (int i = 0; i < types.size(); ++i)
162 {
163 TypeHandler* const t = types.getUnchecked(i);
164
165 if (t->type == targetType)
166 return t;
167 }
168
169 return nullptr;
170}
171
173{
174 return types.size();
175}
176
178{
179 return types [index];
180}
181
185
186void ComponentBuilder::setImageProvider (ImageProvider* newImageProvider) noexcept
187{
188 imageProvider = newImageProvider;
189}
190
195
200
205
210
215
220
221//==============================================================================
223 : type (valueTreeType), builder (nullptr)
224{
225}
226
230
232{
233 // A type handler needs to be registered with a ComponentBuilder before using it!
234 jassert (builder != nullptr);
235 return builder;
236}
237
239{
240 using namespace ComponentBuilderHelpers;
241
242 auto numExistingChildComps = parent.getNumChildComponents();
243
244 Array<Component*> componentsInOrder;
245 componentsInOrder.ensureStorageAllocated (numExistingChildComps);
246
247 {
248 OwnedArray<Component> existingComponents;
249 existingComponents.ensureStorageAllocated (numExistingChildComps);
250
251 for (int i = 0; i < numExistingChildComps; ++i)
252 existingComponents.add (parent.getChildComponent (i));
253
254 auto newNumChildren = children.getNumChildren();
255
256 for (int i = 0; i < newNumChildren; ++i)
257 {
258 auto childState = children.getChild (i);
259 auto* c = removeComponentWithID (existingComponents, getStateId (childState));
260
261 if (c == nullptr)
262 {
263 if (auto* type = getHandlerForState (childState))
265 else
267 }
268
269 if (c != nullptr)
270 componentsInOrder.add (c);
271 }
272
273 // (remaining unused items in existingComponents get deleted here as it goes out of scope)
274 }
275
276 // Make sure the z-order is correct..
277 if (componentsInOrder.size() > 0)
278 {
279 componentsInOrder.getLast()->toFront (false);
280
281 for (int i = componentsInOrder.size() - 1; --i >= 0;)
282 componentsInOrder.getUnchecked(i)->toBehind (componentsInOrder.getUnchecked (i + 1));
283 }
284}
285
286} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
#define nullptr
Definition DistrhoDefines.h:75
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
Definition juce_Array.h:56
ElementType getUnchecked(int index) const
Definition juce_Array.h:252
void ensureStorageAllocated(int minNumElements)
Definition juce_Array.h:1065
int size() const noexcept
Definition juce_Array.h:215
void add(const ElementType &newElement)
Definition juce_Array.h:418
ElementType getLast() const noexcept
Definition juce_Array.h:300
Definition juce_ComponentBuilder.h:186
Definition juce_ComponentBuilder.h:101
ComponentBuilder * builder
Definition juce_ComponentBuilder.h:145
const Identifier type
Definition juce_ComponentBuilder.h:113
friend class ComponentBuilder
Definition juce_ComponentBuilder.h:144
ComponentBuilder * getBuilder() const noexcept
Definition juce_ComponentBuilder.cpp:231
virtual ~TypeHandler()
Definition juce_ComponentBuilder.cpp:227
TypeHandler(const Identifier &valueTreeType)
Definition juce_ComponentBuilder.cpp:222
Definition juce_ComponentBuilder.h:48
void valueTreeParentChanged(ValueTree &) override
Definition juce_ComponentBuilder.cpp:216
void valueTreePropertyChanged(ValueTree &, const Identifier &) override
Definition juce_ComponentBuilder.cpp:196
ComponentBuilder(const ValueTree &state)
Definition juce_ComponentBuilder.cpp:103
int getNumHandlers() const noexcept
Definition juce_ComponentBuilder.cpp:172
ValueTree state
Definition juce_ComponentBuilder.h:64
void registerStandardComponentTypes()
Definition juce_ComponentBuilder.cpp:182
void valueTreeChildOrderChanged(ValueTree &, int, int) override
Definition juce_ComponentBuilder.cpp:211
std::unique_ptr< Component > component
Definition juce_ComponentBuilder.h:232
ImageProvider * imageProvider
Definition juce_ComponentBuilder.h:233
OwnedArray< TypeHandler > types
Definition juce_ComponentBuilder.h:231
ImageProvider * getImageProvider() const noexcept
Definition juce_ComponentBuilder.cpp:191
void updateChildComponents(Component &parent, const ValueTree &children)
Definition juce_ComponentBuilder.cpp:238
void setImageProvider(ImageProvider *newImageProvider) noexcept
Definition juce_ComponentBuilder.cpp:186
TypeHandler * getHandler(int index) const noexcept
Definition juce_ComponentBuilder.cpp:177
static const Identifier idProperty
Definition juce_ComponentBuilder.h:227
~ComponentBuilder() override
Definition juce_ComponentBuilder.cpp:109
TypeHandler * getHandlerForState(const ValueTree &state) const
Definition juce_ComponentBuilder.cpp:157
Component * createComponent()
Definition juce_ComponentBuilder.cpp:134
void valueTreeChildRemoved(ValueTree &, ValueTree &, int) override
Definition juce_ComponentBuilder.cpp:206
Component * getManagedComponent()
Definition juce_ComponentBuilder.cpp:120
void registerTypeHandler(TypeHandler *type)
Definition juce_ComponentBuilder.cpp:145
void valueTreeChildAdded(ValueTree &, ValueTree &) override
Definition juce_ComponentBuilder.cpp:201
Definition juce_Component.h:36
Definition juce_Identifier.h:39
Definition juce_OwnedArray.h:51
int size() const noexcept
Definition juce_OwnedArray.h:130
ObjectClass * getUnchecked(int index) const noexcept
Definition juce_OwnedArray.h:160
ObjectClass * removeAndReturn(int indexToRemove)
Definition juce_OwnedArray.h:629
void ensureStorageAllocated(int minNumElements) noexcept
Definition juce_OwnedArray.h:788
ObjectClass * add(ObjectClass *newObject)
Definition juce_OwnedArray.h:294
Definition juce_String.h:53
bool isEmpty() const noexcept
Definition juce_String.h:310
bool isNotEmpty() const noexcept
Definition juce_String.h:316
Definition juce_ValueTree.h:72
ValueTree getChild(int index) const
Definition juce_ValueTree.cpp:854
int getNumChildren() const noexcept
Definition juce_ValueTree.cpp:849
bool isValid() const noexcept
Definition juce_ValueTree.h:174
ValueTree getParent() const noexcept
Definition juce_ValueTree.cpp:700
struct huft * t
Definition inflate.c:943
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
static uintptr_t parent
Definition pugl.h:1644
#define jassert(expression)
#define jassertfalse
Definition juce_ComponentBuilder.cpp:30
static Component * createNewComponent(ComponentBuilder::TypeHandler &type, const ValueTree &state, Component *parent)
Definition juce_ComponentBuilder.cpp:64
static Component * findComponentWithID(Component &c, const String &compId)
Definition juce_ComponentBuilder.cpp:51
static Component * removeComponentWithID(OwnedArray< Component > &components, const String &compId)
Definition juce_ComponentBuilder.cpp:36
static String getStateId(const ValueTree &state)
Definition juce_ComponentBuilder.cpp:31
static void updateComponent(ComponentBuilder &builder, const ValueTree &state)
Definition juce_ComponentBuilder.cpp:73
Definition carla_juce.cpp:31
@ tree
Definition juce_AccessibilityRole.h:58
return c
Definition crypt.c:175
#define const
Definition zconf.h:137