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.

447 lines
15KB

  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. BEGIN_JUCE_NAMESPACE
  19. //=============================================================================
  20. namespace ComponentBuilderHelpers
  21. {
  22. String getStateId (const ValueTree& state)
  23. {
  24. return state [ComponentBuilder::idProperty].toString();
  25. }
  26. Component* removeComponentWithID (OwnedArray<Component>& components, const String& compId)
  27. {
  28. jassert (compId.isNotEmpty());
  29. for (int i = components.size(); --i >= 0;)
  30. {
  31. Component* const c = components.getUnchecked (i);
  32. if (c->getComponentID() == compId)
  33. return components.removeAndReturn (i);
  34. }
  35. return nullptr;
  36. }
  37. Component* findComponentWithID (Component& c, const String& compId)
  38. {
  39. jassert (compId.isNotEmpty());
  40. if (c.getComponentID() == compId)
  41. return &c;
  42. for (int i = c.getNumChildComponents(); --i >= 0;)
  43. {
  44. Component* const child = findComponentWithID (*c.getChildComponent (i), compId);
  45. if (child != nullptr)
  46. return child;
  47. }
  48. return nullptr;
  49. }
  50. Component* createNewComponent (ComponentBuilder::TypeHandler& type,
  51. const ValueTree& state, Component* parent)
  52. {
  53. Component* const c = type.addNewComponentFromState (state, parent);
  54. jassert (c != nullptr && c->getParentComponent() == parent);
  55. c->setComponentID (getStateId (state));
  56. return c;
  57. }
  58. void updateComponent (ComponentBuilder& builder, const ValueTree& state)
  59. {
  60. Component* topLevelComp = builder.getManagedComponent();
  61. if (topLevelComp != nullptr)
  62. {
  63. ComponentBuilder::TypeHandler* const type = builder.getHandlerForState (state);
  64. const String uid (getStateId (state));
  65. if (type == nullptr || uid.isEmpty())
  66. {
  67. // ..handle the case where a child of the actual state node has changed.
  68. if (state.getParent().isValid())
  69. updateComponent (builder, state.getParent());
  70. }
  71. else
  72. {
  73. Component* const changedComp = findComponentWithID (*topLevelComp, uid);
  74. if (changedComp != nullptr)
  75. type->updateComponentFromState (changedComp, state);
  76. }
  77. }
  78. }
  79. void updateComponentColours (Component& component, const ValueTree& colourState)
  80. {
  81. NamedValueSet& properties = component.getProperties();
  82. for (int i = properties.size(); --i >= 0;)
  83. {
  84. const Identifier name (properties.getName (i));
  85. if (name.toString().startsWith ("jcclr_"))
  86. {
  87. const String colourName (name.toString().substring (6));
  88. if (colourState [colourName].isVoid())
  89. component.removeColour (colourName.getHexValue32());
  90. }
  91. }
  92. for (int i = 0; i < colourState.getNumProperties(); ++i)
  93. {
  94. const Identifier colourName (colourState.getPropertyName (i));
  95. const String colour (colourState [colourName].toString());
  96. if (colour.isNotEmpty())
  97. component.setColour (colourName.toString().getHexValue32(), Colour::fromString (colour));
  98. }
  99. }
  100. template <class ComponentClass>
  101. class StandardTypeHandler : public ComponentBuilder::TypeHandler
  102. {
  103. public:
  104. StandardTypeHandler() : ComponentBuilder::TypeHandler (ComponentClass::Ids::tagType)
  105. {}
  106. Component* addNewComponentFromState (const ValueTree& state, Component* parent)
  107. {
  108. ComponentClass* const c = new ComponentClass();
  109. if (parent != nullptr)
  110. parent->addAndMakeVisible (c);
  111. updateComponentFromState (c, state);
  112. return c;
  113. }
  114. void updateComponentFromState (Component* component, const ValueTree& state)
  115. {
  116. ComponentClass* const c = dynamic_cast <ComponentClass*> (component);
  117. jassert (c != nullptr);
  118. c->setComponentID (state [ComponentBuilder::idProperty]);
  119. c->refreshFromValueTree (state, *this->getBuilder());
  120. }
  121. };
  122. }
  123. //=============================================================================
  124. const Identifier ComponentBuilder::idProperty ("id");
  125. const Identifier ComponentBuilder::positionID ("position");
  126. ComponentBuilder::ComponentBuilder()
  127. : imageProvider (nullptr)
  128. {
  129. }
  130. ComponentBuilder::ComponentBuilder (const ValueTree& state_)
  131. : state (state_), imageProvider (nullptr)
  132. {
  133. state.addListener (this);
  134. }
  135. ComponentBuilder::~ComponentBuilder()
  136. {
  137. state.removeListener (this);
  138. #if JUCE_DEBUG
  139. // Don't delete the managed component!! The builder owns that component, and will delete
  140. // it automatically when it gets deleted.
  141. jassert (componentRef.get() == static_cast <Component*> (component));
  142. #endif
  143. }
  144. Component* ComponentBuilder::getManagedComponent()
  145. {
  146. if (component == nullptr)
  147. {
  148. component = createComponent();
  149. #if JUCE_DEBUG
  150. componentRef = component;
  151. #endif
  152. }
  153. return component;
  154. }
  155. Component* ComponentBuilder::createComponent()
  156. {
  157. jassert (types.size() > 0); // You need to register all the necessary types before you can load a component!
  158. TypeHandler* const type = getHandlerForState (state);
  159. jassert (type != nullptr); // trying to create a component from an unknown type of ValueTree
  160. return type != nullptr ? ComponentBuilderHelpers::createNewComponent (*type, state, nullptr) : nullptr;
  161. }
  162. void ComponentBuilder::registerTypeHandler (ComponentBuilder::TypeHandler* const type)
  163. {
  164. jassert (type != nullptr);
  165. // Don't try to move your types around! Once a type has been added to a builder, the
  166. // builder owns it, and you should leave it alone!
  167. jassert (type->builder == nullptr);
  168. types.add (type);
  169. type->builder = this;
  170. }
  171. ComponentBuilder::TypeHandler* ComponentBuilder::getHandlerForState (const ValueTree& s) const
  172. {
  173. const Identifier targetType (s.getType());
  174. for (int i = 0; i < types.size(); ++i)
  175. {
  176. TypeHandler* const t = types.getUnchecked(i);
  177. if (t->type == targetType)
  178. return t;
  179. }
  180. return nullptr;
  181. }
  182. int ComponentBuilder::getNumHandlers() const noexcept
  183. {
  184. return types.size();
  185. }
  186. ComponentBuilder::TypeHandler* ComponentBuilder::getHandler (const int index) const noexcept
  187. {
  188. return types [index];
  189. }
  190. void ComponentBuilder::registerStandardComponentTypes()
  191. {
  192. Drawable::registerDrawableTypeHandlers (*this);
  193. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <ComboBox>());
  194. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <Slider>());
  195. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <Label>());
  196. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <Slider>());
  197. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <TextEditor>());
  198. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <GroupComponent>());
  199. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <TextButton>());
  200. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <ToggleButton>());
  201. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <ImageButton>());
  202. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <ImageComponent>());
  203. registerTypeHandler (new ComponentBuilderHelpers::StandardTypeHandler <HyperlinkButton>());
  204. }
  205. void ComponentBuilder::setImageProvider (ImageProvider* newImageProvider) noexcept
  206. {
  207. imageProvider = newImageProvider;
  208. }
  209. ComponentBuilder::ImageProvider* ComponentBuilder::getImageProvider() const noexcept
  210. {
  211. return imageProvider;
  212. }
  213. void ComponentBuilder::valueTreePropertyChanged (ValueTree& tree, const Identifier&)
  214. {
  215. ComponentBuilderHelpers::updateComponent (*this, tree);
  216. }
  217. void ComponentBuilder::valueTreeChildAdded (ValueTree& tree, ValueTree&)
  218. {
  219. ComponentBuilderHelpers::updateComponent (*this, tree);
  220. }
  221. void ComponentBuilder::valueTreeChildRemoved (ValueTree& tree, ValueTree&)
  222. {
  223. ComponentBuilderHelpers::updateComponent (*this, tree);
  224. }
  225. void ComponentBuilder::valueTreeChildOrderChanged (ValueTree& tree)
  226. {
  227. ComponentBuilderHelpers::updateComponent (*this, tree);
  228. }
  229. void ComponentBuilder::valueTreeParentChanged (ValueTree& tree)
  230. {
  231. ComponentBuilderHelpers::updateComponent (*this, tree);
  232. }
  233. //==============================================================================
  234. ComponentBuilder::TypeHandler::TypeHandler (const Identifier& valueTreeType)
  235. : type (valueTreeType), builder (nullptr)
  236. {
  237. }
  238. ComponentBuilder::TypeHandler::~TypeHandler()
  239. {
  240. }
  241. ComponentBuilder* ComponentBuilder::TypeHandler::getBuilder() const noexcept
  242. {
  243. // A type handler needs to be registered with a ComponentBuilder before using it!
  244. jassert (builder != nullptr);
  245. return builder;
  246. }
  247. void ComponentBuilder::updateChildComponents (Component& parent, const ValueTree& children)
  248. {
  249. using namespace ComponentBuilderHelpers;
  250. const int numExistingChildComps = parent.getNumChildComponents();
  251. Array <Component*> componentsInOrder;
  252. componentsInOrder.ensureStorageAllocated (numExistingChildComps);
  253. {
  254. OwnedArray<Component> existingComponents;
  255. existingComponents.ensureStorageAllocated (numExistingChildComps);
  256. int i;
  257. for (i = 0; i < numExistingChildComps; ++i)
  258. existingComponents.add (parent.getChildComponent (i));
  259. const int newNumChildren = children.getNumChildren();
  260. for (i = 0; i < newNumChildren; ++i)
  261. {
  262. const ValueTree childState (children.getChild (i));
  263. Component* c = removeComponentWithID (existingComponents, getStateId (childState));
  264. if (c == nullptr)
  265. {
  266. TypeHandler* const type = getHandlerForState (childState);
  267. jassert (type != nullptr);
  268. if (type != nullptr)
  269. c = ComponentBuilderHelpers::createNewComponent (*type, childState, &parent);
  270. }
  271. if (c != nullptr)
  272. componentsInOrder.add (c);
  273. }
  274. // (remaining unused items in existingComponents get deleted here as it goes out of scope)
  275. }
  276. // Make sure the z-order is correct..
  277. if (componentsInOrder.size() > 0)
  278. {
  279. componentsInOrder.getLast()->toFront (false);
  280. for (int i = componentsInOrder.size() - 1; --i >= 0;)
  281. componentsInOrder.getUnchecked(i)->toBehind (componentsInOrder.getUnchecked (i + 1));
  282. }
  283. }
  284. static void updateMarkers (MarkerList* const list, const ValueTree& state)
  285. {
  286. if (list != nullptr)
  287. MarkerList::ValueTreeWrapper (state).applyTo (*list);
  288. }
  289. void ComponentBuilder::initialiseRecursively (Component& comp, const ValueTree& state)
  290. {
  291. refreshBasicComponentProperties (comp, state);
  292. updateMarkers (comp.getMarkers (true), state.getChildWithName ("MARKERS_X"));
  293. updateMarkers (comp.getMarkers (false), state.getChildWithName ("MARKERS_Y"));
  294. const ValueTree childList (state.getChildWithName ("COMPONENTS"));
  295. if (childList.isValid())
  296. {
  297. updateChildComponents (comp, childList);
  298. for (int i = 0; i < childList.getNumChildren(); ++i)
  299. {
  300. const ValueTree childState (childList.getChild(i));
  301. Component* const c = ComponentBuilderHelpers::findComponentWithID (comp, ComponentBuilderHelpers::getStateId (childState));
  302. if (c != nullptr)
  303. {
  304. ComponentBuilder::TypeHandler* const type = getHandlerForState (childState);
  305. if (type != nullptr)
  306. type->updateComponentFromState (c, childState);
  307. else
  308. initialiseRecursively (*c, childState);
  309. }
  310. }
  311. }
  312. }
  313. void ComponentBuilder::initialiseFromValueTree (Component& comp,
  314. const ValueTree& state,
  315. ImageProvider* const imageProvider)
  316. {
  317. ComponentBuilder builder;
  318. builder.setImageProvider (imageProvider);
  319. builder.registerStandardComponentTypes();
  320. builder.initialiseRecursively (comp, state);
  321. }
  322. RelativeRectangle ComponentBuilder::getComponentBounds (const ValueTree& state)
  323. {
  324. try
  325. {
  326. return RelativeRectangle (state [positionID].toString());
  327. }
  328. catch (Expression::ParseError&)
  329. {}
  330. return RelativeRectangle();
  331. }
  332. void ComponentBuilder::refreshBasicComponentProperties (Component& comp, const ValueTree& state)
  333. {
  334. static const Identifier focusOrderID ("focusOrder");
  335. static const Identifier tooltipID ("tooltip");
  336. static const Identifier nameID ("name");
  337. comp.setName (state [nameID].toString());
  338. if (state.hasProperty (positionID))
  339. getComponentBounds (state).applyToComponent (comp);
  340. comp.setExplicitFocusOrder (state [focusOrderID]);
  341. const var tip (state [tooltipID]);
  342. if (! tip.isVoid())
  343. {
  344. SettableTooltipClient* tooltipClient = dynamic_cast <SettableTooltipClient*> (&comp);
  345. if (tooltipClient != nullptr)
  346. tooltipClient->setTooltip (tip.toString());
  347. }
  348. ComponentBuilderHelpers::updateComponentColours (comp, state.getChildWithName ("COLOURS"));
  349. }
  350. END_JUCE_NAMESPACE