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.

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