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.

364 lines
13KB

  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. Displays::Displays (Desktop& desktop)
  22. {
  23. init (desktop);
  24. }
  25. void Displays::init (Desktop& desktop)
  26. {
  27. findDisplays (desktop.getGlobalScaleFactor());
  28. }
  29. const Displays::Display& Displays::findDisplayForRect (Rectangle<int> rect, bool isPhysical) const noexcept
  30. {
  31. int maxArea = -1;
  32. Display* retVal = nullptr;
  33. for (auto& display : displays)
  34. {
  35. auto displayArea = display.totalArea;
  36. if (isPhysical)
  37. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  38. displayArea = displayArea.getIntersection (rect);
  39. auto area = displayArea.getWidth() * displayArea.getHeight();
  40. if (area >= maxArea)
  41. {
  42. maxArea = area;
  43. retVal = &display;
  44. }
  45. }
  46. return *retVal;
  47. }
  48. const Displays::Display& Displays::findDisplayForPoint (Point<int> point, bool isPhysical) const noexcept
  49. {
  50. auto minDistance = std::numeric_limits<int>::max();
  51. Display* retVal = nullptr;
  52. for (auto& display : displays)
  53. {
  54. auto displayArea = display.totalArea;
  55. if (isPhysical)
  56. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  57. if (displayArea.contains (point))
  58. return display;
  59. auto distance = displayArea.getCentre().getDistanceFrom (point);
  60. if (distance <= minDistance)
  61. {
  62. minDistance = distance;
  63. retVal = &display;
  64. }
  65. }
  66. return *retVal;
  67. }
  68. Rectangle<int> Displays::physicalToLogical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  69. {
  70. auto& display = useScaleFactorOfDisplay != nullptr ? *useScaleFactorOfDisplay
  71. : findDisplayForRect (rect, true);
  72. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  73. return ((rect.toFloat() - display.topLeftPhysical.toFloat()) / (display.scale / globalScale)).toNearestInt() + (display.totalArea.getTopLeft() * globalScale);
  74. }
  75. Rectangle<int> Displays::logicalToPhysical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  76. {
  77. auto& display = useScaleFactorOfDisplay != nullptr ? *useScaleFactorOfDisplay
  78. : findDisplayForRect (rect, false);
  79. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  80. return ((rect.toFloat() - (display.totalArea.getTopLeft().toFloat() * globalScale)) * (display.scale / globalScale)).toNearestInt() + display.topLeftPhysical;
  81. }
  82. template <typename ValueType>
  83. Point<ValueType> Displays::physicalToLogical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  84. {
  85. auto& display = useScaleFactorOfDisplay != nullptr ? *useScaleFactorOfDisplay
  86. : findDisplayForPoint (point.roundToInt(), true);
  87. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  88. Point<ValueType> logicalTopLeft (display.totalArea.getX(), display.totalArea.getY());
  89. Point<ValueType> physicalTopLeft (display.topLeftPhysical.getX(), display.topLeftPhysical.getY());
  90. return ((point - physicalTopLeft) / (display.scale / globalScale)) + (logicalTopLeft * globalScale);
  91. }
  92. template <typename ValueType>
  93. Point<ValueType> Displays::logicalToPhysical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  94. {
  95. auto& display = useScaleFactorOfDisplay != nullptr ? *useScaleFactorOfDisplay
  96. : findDisplayForPoint (point.roundToInt(), false);
  97. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  98. Point<ValueType> logicalTopLeft (display.totalArea.getX(), display.totalArea.getY());
  99. Point<ValueType> physicalTopLeft (display.topLeftPhysical.getX(), display.topLeftPhysical.getY());
  100. return ((point - (logicalTopLeft * globalScale)) * (display.scale / globalScale)) + physicalTopLeft;
  101. }
  102. const Displays::Display& Displays::getMainDisplay() const noexcept
  103. {
  104. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  105. for (auto& d : displays)
  106. if (d.isMain)
  107. return d;
  108. // no main display!
  109. jassertfalse;
  110. return displays.getReference (0);
  111. }
  112. RectangleList<int> Displays::getRectangleList (bool userAreasOnly) const
  113. {
  114. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  115. RectangleList<int> rl;
  116. for (auto& d : displays)
  117. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  118. return rl;
  119. }
  120. Rectangle<int> Displays::getTotalBounds (bool userAreasOnly) const
  121. {
  122. return getRectangleList (userAreasOnly).getBounds();
  123. }
  124. void Displays::refresh()
  125. {
  126. Array<Display> oldDisplays;
  127. oldDisplays.swapWith (displays);
  128. init (Desktop::getInstance());
  129. if (oldDisplays != displays)
  130. {
  131. for (auto i = ComponentPeer::getNumPeers(); --i >= 0;)
  132. if (auto* peer = ComponentPeer::getPeer (i))
  133. peer->handleScreenSizeChange();
  134. }
  135. }
  136. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  137. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept
  138. {
  139. return d1.isMain == d2.isMain
  140. && d1.totalArea == d2.totalArea
  141. && d1.userArea == d2.userArea
  142. && d1.topLeftPhysical == d2.topLeftPhysical
  143. && d1.scale == d2.scale
  144. && d1.dpi == d2.dpi;
  145. }
  146. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  147. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept { return ! (d1 == d2); }
  148. // Deprecated method
  149. const Displays::Display& Displays::getDisplayContaining (Point<int> position) const noexcept
  150. {
  151. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  152. auto* best = &displays.getReference (0);
  153. auto bestDistance = std::numeric_limits<int>::max();
  154. for (auto& d : displays)
  155. {
  156. if (d.totalArea.contains (position))
  157. {
  158. best = &d;
  159. break;
  160. }
  161. auto distance = d.totalArea.getCentre().getDistanceFrom (position);
  162. if (distance < bestDistance)
  163. {
  164. bestDistance = distance;
  165. best = &d;
  166. }
  167. }
  168. return *best;
  169. }
  170. //==============================================================================
  171. // These methods are used for converting the totalArea and userArea Rectangles in Display from physical to logical
  172. // pixels. We do this by constructing a graph of connected displays where the root node has position (0, 0); this can be
  173. // safely converted to logical pixels using its scale factor and we can then traverse the graph and work out the logical pixels
  174. // for all the other connected displays. We need to do this as the logical bounds of a display depend not only on its scale
  175. // factor but also the scale factor of the displays connected to it.
  176. /**
  177. Represents a node in our graph of displays.
  178. */
  179. struct DisplayNode
  180. {
  181. /** The Display object that this represents. */
  182. Displays::Display* display;
  183. /** True if this represents the 'root' display with position (0, 0). */
  184. bool isRoot = false;
  185. /** The parent node of this node in our display graph. This will have a correct logicalArea. */
  186. DisplayNode* parent = nullptr;
  187. /** The logical area to be calculated. This will be valid after processDisplay() has
  188. been called on this node.
  189. */
  190. Rectangle<double> logicalArea;
  191. };
  192. /** Recursive - will calculate and set the logicalArea member of current. */
  193. static void processDisplay (DisplayNode* currentNode, const Array<DisplayNode>& allNodes)
  194. {
  195. const auto physicalArea = currentNode->display->totalArea.toDouble();
  196. const auto scale = currentNode->display->scale;
  197. if (! currentNode->isRoot)
  198. {
  199. const auto logicalWidth = physicalArea.getWidth() / scale;
  200. const auto logicalHeight = physicalArea.getHeight() / scale;
  201. const auto physicalParentArea = currentNode->parent->display->totalArea.toDouble();
  202. const auto logicalParentArea = currentNode->parent->logicalArea; // logical area of parent has already been calculated
  203. const auto parentScale = currentNode->parent->display->scale;
  204. Rectangle<double> logicalArea (0.0, 0.0, logicalWidth, logicalHeight);
  205. if (physicalArea.getRight() == physicalParentArea.getX()) logicalArea.setPosition ({ logicalParentArea.getX() - logicalWidth, physicalArea.getY() / parentScale }); // on left
  206. else if (physicalArea.getX() == physicalParentArea.getRight()) logicalArea.setPosition ({ logicalParentArea.getRight(), physicalArea.getY() / parentScale }); // on right
  207. else if (physicalArea.getBottom() == physicalParentArea.getY()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getY() - logicalHeight }); // on top
  208. else if (physicalArea.getY() == physicalParentArea.getBottom()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getBottom() }); // on bottom
  209. else jassertfalse;
  210. currentNode->logicalArea = logicalArea;
  211. }
  212. else
  213. {
  214. // If currentNode is the root (position (0, 0)) then we can just scale the physical area
  215. currentNode->logicalArea = physicalArea / scale;
  216. currentNode->parent = currentNode;
  217. }
  218. // Find child nodes
  219. Array<DisplayNode*> children;
  220. for (auto& node : allNodes)
  221. {
  222. // Already calculated
  223. if (node.parent != nullptr)
  224. continue;
  225. const auto otherPhysicalArea = node.display->totalArea.toDouble();
  226. // If the displays are touching on any side
  227. if (otherPhysicalArea.getX() == physicalArea.getRight() || otherPhysicalArea.getRight() == physicalArea.getX()
  228. || otherPhysicalArea.getY() == physicalArea.getBottom() || otherPhysicalArea.getBottom() == physicalArea.getY())
  229. {
  230. node.parent = currentNode;
  231. children.add (&node);
  232. }
  233. }
  234. // Recursively process all child nodes
  235. for (auto child : children)
  236. processDisplay (child, allNodes);
  237. }
  238. /** This is called when the displays Array has been filled out with the info for all connected displays and the
  239. totalArea and userArea Rectangles need to be converted from physical to logical coordinates.
  240. */
  241. void Displays::updateToLogical()
  242. {
  243. if (displays.size() == 1)
  244. {
  245. auto& display = displays.getReference (0);
  246. display.totalArea = (display.totalArea.toDouble() / display.scale).toNearestInt();
  247. display.userArea = (display.userArea.toDouble() / display.scale).toNearestInt();
  248. return;
  249. }
  250. Array<DisplayNode> displayNodes;
  251. for (auto& d : displays)
  252. {
  253. DisplayNode node;
  254. node.display = &d;
  255. displayNodes.add (node);
  256. }
  257. DisplayNode* root = nullptr;
  258. for (auto& node : displayNodes)
  259. {
  260. if (node.display->totalArea.getTopLeft() == Point<int>())
  261. {
  262. root = &node;
  263. root->isRoot = true;
  264. break;
  265. }
  266. }
  267. // Must have a root node!
  268. jassert (root != nullptr);
  269. // Recursively traverse the display graph from the root and work out logical bounds
  270. processDisplay (root, displayNodes);
  271. for (auto& node : displayNodes)
  272. {
  273. // All of the nodes should have a parent
  274. jassert (node.parent != nullptr);
  275. auto relativeUserArea = (node.display->userArea.toDouble() - node.display->totalArea.toDouble().getTopLeft()) / node.display->scale;
  276. // Now set Display::totalArea and ::userArea using the logical area that we have calculated
  277. node.display->topLeftPhysical = node.display->totalArea.getTopLeft();
  278. node.display->totalArea = node.logicalArea.toNearestInt();
  279. node.display->userArea = (relativeUserArea + node.logicalArea.getTopLeft()).toNearestInt();
  280. }
  281. }
  282. } // namespace juce