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.

335 lines
11KB

  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. struct MarkerListScope : public Expression::Scope
  22. {
  23. MarkerListScope (Component& comp) : component (comp) {}
  24. Expression getSymbolValue (const String& symbol) const override
  25. {
  26. switch (RelativeCoordinate::StandardStrings::getTypeOf (symbol))
  27. {
  28. case RelativeCoordinate::StandardStrings::width: return Expression ((double) component.getWidth());
  29. case RelativeCoordinate::StandardStrings::height: return Expression ((double) component.getHeight());
  30. default: break;
  31. }
  32. MarkerList* list;
  33. if (auto* marker = findMarker (component, symbol, list))
  34. return Expression (marker->position.getExpression().evaluate (*this));
  35. return Expression::Scope::getSymbolValue (symbol);
  36. }
  37. void visitRelativeScope (const String& scopeName, Visitor& visitor) const override
  38. {
  39. if (scopeName == RelativeCoordinate::Strings::parent)
  40. {
  41. if (auto* parent = component.getParentComponent())
  42. {
  43. visitor.visit (MarkerListScope (*parent));
  44. return;
  45. }
  46. }
  47. Expression::Scope::visitRelativeScope (scopeName, visitor);
  48. }
  49. String getScopeUID() const override
  50. {
  51. return String::toHexString ((pointer_sized_int) (void*) &component) + "m";
  52. }
  53. static const MarkerList::Marker* findMarker (Component& component, const String& name, MarkerList*& list)
  54. {
  55. const MarkerList::Marker* marker = nullptr;
  56. auto* mlh = dynamic_cast<MarkerList::MarkerListHolder*> (&component);
  57. if (mlh != nullptr)
  58. {
  59. list = mlh->getMarkers (true);
  60. if (list != nullptr)
  61. marker = list->getMarker (name);
  62. }
  63. if (marker == nullptr)
  64. {
  65. if (mlh != nullptr)
  66. {
  67. list = mlh->getMarkers (false);
  68. if (list != nullptr)
  69. marker = list->getMarker (name);
  70. }
  71. }
  72. return marker;
  73. }
  74. Component& component;
  75. };
  76. //==============================================================================
  77. RelativeCoordinatePositionerBase::ComponentScope::ComponentScope (Component& comp)
  78. : component (comp)
  79. {
  80. }
  81. Expression RelativeCoordinatePositionerBase::ComponentScope::getSymbolValue (const String& symbol) const
  82. {
  83. switch (RelativeCoordinate::StandardStrings::getTypeOf (symbol))
  84. {
  85. case RelativeCoordinate::StandardStrings::x:
  86. case RelativeCoordinate::StandardStrings::left: return Expression ((double) component.getX());
  87. case RelativeCoordinate::StandardStrings::y:
  88. case RelativeCoordinate::StandardStrings::top: return Expression ((double) component.getY());
  89. case RelativeCoordinate::StandardStrings::width: return Expression ((double) component.getWidth());
  90. case RelativeCoordinate::StandardStrings::height: return Expression ((double) component.getHeight());
  91. case RelativeCoordinate::StandardStrings::right: return Expression ((double) component.getRight());
  92. case RelativeCoordinate::StandardStrings::bottom: return Expression ((double) component.getBottom());
  93. default: break;
  94. }
  95. if (Component* const parent = component.getParentComponent())
  96. {
  97. MarkerList* list;
  98. if (auto* marker = MarkerListScope::findMarker (*parent, symbol, list))
  99. {
  100. MarkerListScope scope (*parent);
  101. return Expression (marker->position.getExpression().evaluate (scope));
  102. }
  103. }
  104. return Expression::Scope::getSymbolValue (symbol);
  105. }
  106. void RelativeCoordinatePositionerBase::ComponentScope::visitRelativeScope (const String& scopeName, Visitor& visitor) const
  107. {
  108. if (auto* targetComp = (scopeName == RelativeCoordinate::Strings::parent)
  109. ? component.getParentComponent()
  110. : findSiblingComponent (scopeName))
  111. visitor.visit (ComponentScope (*targetComp));
  112. else
  113. Expression::Scope::visitRelativeScope (scopeName, visitor);
  114. }
  115. String RelativeCoordinatePositionerBase::ComponentScope::getScopeUID() const
  116. {
  117. return String::toHexString ((pointer_sized_int) (void*) &component);
  118. }
  119. Component* RelativeCoordinatePositionerBase::ComponentScope::findSiblingComponent (const String& componentID) const
  120. {
  121. if (Component* const parent = component.getParentComponent())
  122. return parent->findChildWithID (componentID);
  123. return nullptr;
  124. }
  125. //==============================================================================
  126. class RelativeCoordinatePositionerBase::DependencyFinderScope : public ComponentScope
  127. {
  128. public:
  129. DependencyFinderScope (Component& comp, RelativeCoordinatePositionerBase& p, bool& result)
  130. : ComponentScope (comp), positioner (p), ok (result)
  131. {
  132. }
  133. Expression getSymbolValue (const String& symbol) const override
  134. {
  135. switch (RelativeCoordinate::StandardStrings::getTypeOf (symbol))
  136. {
  137. case RelativeCoordinate::StandardStrings::x:
  138. case RelativeCoordinate::StandardStrings::left:
  139. case RelativeCoordinate::StandardStrings::y:
  140. case RelativeCoordinate::StandardStrings::top:
  141. case RelativeCoordinate::StandardStrings::width:
  142. case RelativeCoordinate::StandardStrings::height:
  143. case RelativeCoordinate::StandardStrings::right:
  144. case RelativeCoordinate::StandardStrings::bottom:
  145. positioner.registerComponentListener (component);
  146. break;
  147. default:
  148. if (auto* parent = component.getParentComponent())
  149. {
  150. MarkerList* list;
  151. if (MarkerListScope::findMarker (*parent, symbol, list) != nullptr)
  152. {
  153. positioner.registerMarkerListListener (list);
  154. }
  155. else
  156. {
  157. // The marker we want doesn't exist, so watch all lists in case they change and the marker appears later..
  158. if (auto* mlh = dynamic_cast<MarkerList::MarkerListHolder*> (parent))
  159. {
  160. positioner.registerMarkerListListener (mlh->getMarkers (true));
  161. positioner.registerMarkerListListener (mlh->getMarkers (false));
  162. }
  163. ok = false;
  164. }
  165. }
  166. break;
  167. }
  168. return ComponentScope::getSymbolValue (symbol);
  169. }
  170. void visitRelativeScope (const String& scopeName, Visitor& visitor) const override
  171. {
  172. if (Component* const targetComp = (scopeName == RelativeCoordinate::Strings::parent)
  173. ? component.getParentComponent()
  174. : findSiblingComponent (scopeName))
  175. {
  176. visitor.visit (DependencyFinderScope (*targetComp, positioner, ok));
  177. }
  178. else
  179. {
  180. // The named component doesn't exist, so we'll watch the parent for changes in case it appears later..
  181. if (Component* const parent = component.getParentComponent())
  182. positioner.registerComponentListener (*parent);
  183. positioner.registerComponentListener (component);
  184. ok = false;
  185. }
  186. }
  187. private:
  188. RelativeCoordinatePositionerBase& positioner;
  189. bool& ok;
  190. };
  191. //==============================================================================
  192. RelativeCoordinatePositionerBase::RelativeCoordinatePositionerBase (Component& comp)
  193. : Component::Positioner (comp), registeredOk (false)
  194. {
  195. }
  196. RelativeCoordinatePositionerBase::~RelativeCoordinatePositionerBase()
  197. {
  198. unregisterListeners();
  199. }
  200. void RelativeCoordinatePositionerBase::componentMovedOrResized (Component&, bool /*wasMoved*/, bool /*wasResized*/)
  201. {
  202. apply();
  203. }
  204. void RelativeCoordinatePositionerBase::componentParentHierarchyChanged (Component&)
  205. {
  206. apply();
  207. }
  208. void RelativeCoordinatePositionerBase::componentChildrenChanged (Component& changed)
  209. {
  210. if (getComponent().getParentComponent() == &changed && ! registeredOk)
  211. apply();
  212. }
  213. void RelativeCoordinatePositionerBase::componentBeingDeleted (Component& comp)
  214. {
  215. jassert (sourceComponents.contains (&comp));
  216. sourceComponents.removeFirstMatchingValue (&comp);
  217. registeredOk = false;
  218. }
  219. void RelativeCoordinatePositionerBase::markersChanged (MarkerList*)
  220. {
  221. apply();
  222. }
  223. void RelativeCoordinatePositionerBase::markerListBeingDeleted (MarkerList* markerList)
  224. {
  225. jassert (sourceMarkerLists.contains (markerList));
  226. sourceMarkerLists.removeFirstMatchingValue (markerList);
  227. }
  228. void RelativeCoordinatePositionerBase::apply()
  229. {
  230. if (! registeredOk)
  231. {
  232. unregisterListeners();
  233. registeredOk = registerCoordinates();
  234. }
  235. applyToComponentBounds();
  236. }
  237. bool RelativeCoordinatePositionerBase::addCoordinate (const RelativeCoordinate& coord)
  238. {
  239. bool ok = true;
  240. DependencyFinderScope finderScope (getComponent(), *this, ok);
  241. coord.getExpression().evaluate (finderScope);
  242. return ok;
  243. }
  244. bool RelativeCoordinatePositionerBase::addPoint (const RelativePoint& point)
  245. {
  246. const bool ok = addCoordinate (point.x);
  247. return addCoordinate (point.y) && ok;
  248. }
  249. void RelativeCoordinatePositionerBase::registerComponentListener (Component& comp)
  250. {
  251. if (! sourceComponents.contains (&comp))
  252. {
  253. comp.addComponentListener (this);
  254. sourceComponents.add (&comp);
  255. }
  256. }
  257. void RelativeCoordinatePositionerBase::registerMarkerListListener (MarkerList* const list)
  258. {
  259. if (list != nullptr && ! sourceMarkerLists.contains (list))
  260. {
  261. list->addListener (this);
  262. sourceMarkerLists.add (list);
  263. }
  264. }
  265. void RelativeCoordinatePositionerBase::unregisterListeners()
  266. {
  267. for (int i = sourceComponents.size(); --i >= 0;)
  268. sourceComponents.getUnchecked(i)->removeComponentListener (this);
  269. for (int i = sourceMarkerLists.size(); --i >= 0;)
  270. sourceMarkerLists.getUnchecked(i)->removeListener (this);
  271. sourceComponents.clear();
  272. sourceMarkerLists.clear();
  273. }
  274. } // namespace juce