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.

425 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. Displays::Displays (Desktop& desktop)
  21. {
  22. init (desktop);
  23. }
  24. void Displays::init (Desktop& desktop)
  25. {
  26. findDisplays (desktop.getGlobalScaleFactor());
  27. }
  28. const Displays::Display* Displays::getDisplayForRect (Rectangle<int> rect, bool isPhysical) const noexcept
  29. {
  30. int maxArea = -1;
  31. const Display* foundDisplay = nullptr;
  32. for (auto& display : displays)
  33. {
  34. auto displayArea = display.totalArea;
  35. if (isPhysical)
  36. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  37. displayArea = displayArea.getIntersection (rect);
  38. auto area = displayArea.getWidth() * displayArea.getHeight();
  39. if (area >= maxArea)
  40. {
  41. maxArea = area;
  42. foundDisplay = &display;
  43. }
  44. }
  45. return foundDisplay;
  46. }
  47. const Displays::Display* Displays::getDisplayForPoint (Point<int> point, bool isPhysical) const noexcept
  48. {
  49. auto minDistance = std::numeric_limits<int>::max();
  50. const Display* foundDisplay = nullptr;
  51. for (auto& display : displays)
  52. {
  53. auto displayArea = display.totalArea;
  54. if (isPhysical)
  55. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  56. if (displayArea.contains (point))
  57. return &display;
  58. auto distance = displayArea.getCentre().getDistanceFrom (point);
  59. if (distance <= minDistance)
  60. {
  61. minDistance = distance;
  62. foundDisplay = &display;
  63. }
  64. }
  65. return foundDisplay;
  66. }
  67. Rectangle<int> Displays::physicalToLogical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  68. {
  69. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  70. : getDisplayForRect (rect, true);
  71. if (display == nullptr)
  72. return rect;
  73. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  74. return ((rect.toFloat() - display->topLeftPhysical.toFloat()) / (display->scale / globalScale)).toNearestInt() + (display->totalArea.getTopLeft() * globalScale);
  75. }
  76. Rectangle<int> Displays::logicalToPhysical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  77. {
  78. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  79. : getDisplayForRect (rect, false);
  80. if (display == nullptr)
  81. return rect;
  82. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  83. return ((rect.toFloat() - (display->totalArea.getTopLeft().toFloat() * globalScale)) * (display->scale / globalScale)).toNearestInt() + display->topLeftPhysical;
  84. }
  85. template <typename ValueType>
  86. Point<ValueType> Displays::physicalToLogical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  87. {
  88. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  89. : getDisplayForPoint (point.roundToInt(), true);
  90. if (display == nullptr)
  91. return point;
  92. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  93. Point<ValueType> logicalTopLeft (static_cast<ValueType> (display->totalArea.getX()), static_cast<ValueType> (display->totalArea.getY()));
  94. Point<ValueType> physicalTopLeft (static_cast<ValueType> (display->topLeftPhysical.getX()), static_cast<ValueType> (display->topLeftPhysical.getY()));
  95. return ((point - physicalTopLeft) / (display->scale / globalScale)) + (logicalTopLeft * globalScale);
  96. }
  97. template <typename ValueType>
  98. Point<ValueType> Displays::logicalToPhysical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  99. {
  100. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  101. : getDisplayForPoint (point.roundToInt(), false);
  102. if (display == nullptr)
  103. return point;
  104. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  105. Point<ValueType> logicalTopLeft (static_cast<ValueType> (display->totalArea.getX()), static_cast<ValueType> (display->totalArea.getY()));
  106. Point<ValueType> physicalTopLeft (static_cast<ValueType> (display->topLeftPhysical.getX()), static_cast<ValueType> (display->topLeftPhysical.getY()));
  107. return ((point - (logicalTopLeft * globalScale)) * (display->scale / globalScale)) + physicalTopLeft;
  108. }
  109. const Displays::Display* Displays::getPrimaryDisplay() const noexcept
  110. {
  111. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  112. for (auto& d : displays)
  113. if (d.isMain)
  114. return &d;
  115. return nullptr;
  116. }
  117. RectangleList<int> Displays::getRectangleList (bool userAreasOnly) const
  118. {
  119. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  120. RectangleList<int> rl;
  121. for (auto& d : displays)
  122. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  123. return rl;
  124. }
  125. Rectangle<int> Displays::getTotalBounds (bool userAreasOnly) const
  126. {
  127. return getRectangleList (userAreasOnly).getBounds();
  128. }
  129. void Displays::refresh()
  130. {
  131. Array<Display> oldDisplays;
  132. oldDisplays.swapWith (displays);
  133. init (Desktop::getInstance());
  134. if (oldDisplays != displays)
  135. {
  136. for (auto i = ComponentPeer::getNumPeers(); --i >= 0;)
  137. if (auto* peer = ComponentPeer::getPeer (i))
  138. peer->handleScreenSizeChange();
  139. }
  140. }
  141. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  142. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept
  143. {
  144. return d1.isMain == d2.isMain
  145. && d1.totalArea == d2.totalArea
  146. && d1.userArea == d2.userArea
  147. && d1.topLeftPhysical == d2.topLeftPhysical
  148. && d1.scale == d2.scale
  149. && d1.dpi == d2.dpi;
  150. }
  151. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  152. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept { return ! (d1 == d2); }
  153. //==============================================================================
  154. // These methods are used for converting the totalArea and userArea Rectangles in Display from physical to logical
  155. // pixels. We do this by constructing a graph of connected displays where the root node has position (0, 0); this can be
  156. // safely converted to logical pixels using its scale factor and we can then traverse the graph and work out the logical pixels
  157. // for all the other connected displays. We need to do this as the logical bounds of a display depend not only on its scale
  158. // factor but also the scale factor of the displays connected to it.
  159. /**
  160. Represents a node in our graph of displays.
  161. */
  162. struct DisplayNode
  163. {
  164. /** The Display object that this represents. */
  165. Displays::Display* display;
  166. /** True if this represents the 'root' display with position (0, 0). */
  167. bool isRoot = false;
  168. /** The parent node of this node in our display graph. This will have a correct logicalArea. */
  169. DisplayNode* parent = nullptr;
  170. /** The logical area to be calculated. This will be valid after processDisplay() has
  171. been called on this node.
  172. */
  173. Rectangle<double> logicalArea;
  174. };
  175. /** Recursive - will calculate and set the logicalArea member of current. */
  176. static void processDisplay (DisplayNode* currentNode, Array<DisplayNode>& allNodes)
  177. {
  178. const auto physicalArea = currentNode->display->totalArea.toDouble();
  179. const auto scale = currentNode->display->scale;
  180. if (! currentNode->isRoot)
  181. {
  182. const auto logicalWidth = physicalArea.getWidth() / scale;
  183. const auto logicalHeight = physicalArea.getHeight() / scale;
  184. const auto physicalParentArea = currentNode->parent->display->totalArea.toDouble();
  185. const auto logicalParentArea = currentNode->parent->logicalArea; // logical area of parent has already been calculated
  186. const auto parentScale = currentNode->parent->display->scale;
  187. Rectangle<double> logicalArea (0.0, 0.0, logicalWidth, logicalHeight);
  188. if (physicalArea.getRight() == physicalParentArea.getX()) logicalArea.setPosition ({ logicalParentArea.getX() - logicalWidth, physicalArea.getY() / parentScale }); // on left
  189. else if (physicalArea.getX() == physicalParentArea.getRight()) logicalArea.setPosition ({ logicalParentArea.getRight(), physicalArea.getY() / parentScale }); // on right
  190. else if (physicalArea.getBottom() == physicalParentArea.getY()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getY() - logicalHeight }); // on top
  191. else if (physicalArea.getY() == physicalParentArea.getBottom()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getBottom() }); // on bottom
  192. else jassertfalse;
  193. currentNode->logicalArea = logicalArea;
  194. }
  195. else
  196. {
  197. // If currentNode is the root (position (0, 0)) then we can just scale the physical area
  198. currentNode->logicalArea = physicalArea / scale;
  199. currentNode->parent = currentNode;
  200. }
  201. // Find child nodes
  202. Array<DisplayNode*> children;
  203. for (auto& node : allNodes)
  204. {
  205. // Already calculated
  206. if (node.parent != nullptr)
  207. continue;
  208. const auto otherPhysicalArea = node.display->totalArea.toDouble();
  209. // If the displays are touching on any side
  210. if (otherPhysicalArea.getX() == physicalArea.getRight() || otherPhysicalArea.getRight() == physicalArea.getX()
  211. || otherPhysicalArea.getY() == physicalArea.getBottom() || otherPhysicalArea.getBottom() == physicalArea.getY())
  212. {
  213. node.parent = currentNode;
  214. children.add (&node);
  215. }
  216. }
  217. // Recursively process all child nodes
  218. for (auto child : children)
  219. processDisplay (child, allNodes);
  220. }
  221. /** This is called when the displays Array has been filled out with the info for all connected displays and the
  222. totalArea and userArea Rectangles need to be converted from physical to logical coordinates.
  223. */
  224. void Displays::updateToLogical()
  225. {
  226. if (displays.size() == 1)
  227. {
  228. auto& display = displays.getReference (0);
  229. display.totalArea = (display.totalArea.toDouble() / display.scale).toNearestInt();
  230. display.userArea = (display.userArea.toDouble() / display.scale).toNearestInt();
  231. return;
  232. }
  233. Array<DisplayNode> displayNodes;
  234. for (auto& d : displays)
  235. {
  236. DisplayNode node;
  237. node.display = &d;
  238. if (d.totalArea.getTopLeft() == Point<int>())
  239. node.isRoot = true;
  240. displayNodes.add (node);
  241. }
  242. auto* root = [&displayNodes]() -> DisplayNode*
  243. {
  244. for (auto& node : displayNodes)
  245. if (node.isRoot)
  246. return &node;
  247. auto minDistance = std::numeric_limits<int>::max();
  248. DisplayNode* retVal = nullptr;
  249. for (auto& node : displayNodes)
  250. {
  251. auto distance = node.display->totalArea.getTopLeft().getDistanceFrom ({});
  252. if (distance < minDistance)
  253. {
  254. minDistance = distance;
  255. retVal = &node;
  256. }
  257. }
  258. retVal->isRoot = true;
  259. return retVal;
  260. }();
  261. // Must have a root node!
  262. jassert (root != nullptr);
  263. // Recursively traverse the display graph from the root and work out logical bounds
  264. processDisplay (root, displayNodes);
  265. for (auto& node : displayNodes)
  266. {
  267. // All of the nodes should have a parent
  268. jassert (node.parent != nullptr);
  269. auto relativeUserArea = (node.display->userArea.toDouble() - node.display->totalArea.toDouble().getTopLeft()) / node.display->scale;
  270. // Now set Display::totalArea and ::userArea using the logical area that we have calculated
  271. node.display->topLeftPhysical = node.display->totalArea.getTopLeft();
  272. node.display->totalArea = node.logicalArea.toNearestInt();
  273. node.display->userArea = (relativeUserArea + node.logicalArea.getTopLeft()).toNearestInt();
  274. }
  275. }
  276. #ifndef DOXYGEN
  277. // explicit template instantiations
  278. template Point<int> Displays::physicalToLogical (Point<int>, const Display*) const noexcept;
  279. template Point<float> Displays::physicalToLogical (Point<float>, const Display*) const noexcept;
  280. template Point<int> Displays::logicalToPhysical (Point<int>, const Display*) const noexcept;
  281. template Point<float> Displays::logicalToPhysical (Point<float>, const Display*) const noexcept;
  282. #endif
  283. //==============================================================================
  284. // Deprecated methods
  285. const Displays::Display& Displays::getDisplayContaining (Point<int> position) const noexcept
  286. {
  287. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  288. const auto* best = &displays.getReference (0);
  289. auto bestDistance = std::numeric_limits<int>::max();
  290. for (auto& d : displays)
  291. {
  292. if (d.totalArea.contains (position))
  293. {
  294. best = &d;
  295. break;
  296. }
  297. auto distance = d.totalArea.getCentre().getDistanceFrom (position);
  298. if (distance < bestDistance)
  299. {
  300. bestDistance = distance;
  301. best = &d;
  302. }
  303. }
  304. return *best;
  305. }
  306. const Displays::Display& Displays::findDisplayForRect (Rectangle<int> rect, bool isPhysical) const noexcept
  307. {
  308. if (auto* display = getDisplayForRect (rect, isPhysical))
  309. return *display;
  310. return emptyDisplay;
  311. }
  312. const Displays::Display& Displays::findDisplayForPoint (Point<int> point, bool isPhysical) const noexcept
  313. {
  314. if (auto* display = getDisplayForPoint (point, isPhysical))
  315. return *display;
  316. return emptyDisplay;
  317. }
  318. const Displays::Display& Displays::getMainDisplay() const noexcept
  319. {
  320. if (auto* display = getPrimaryDisplay())
  321. return *display;
  322. return emptyDisplay;
  323. }
  324. } // namespace juce