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.

753 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #pragma once
  14. class ComponentLayout;
  15. //==============================================================================
  16. /**
  17. A rectangle whose coordinates can be defined in terms of absolute or
  18. proportional distances.
  19. Designed mainly for storing component positions, this gives you a lot of
  20. control over how each coordinate is stored, either as an absolute position,
  21. or as a proportion of the size of a parent rectangle.
  22. It also allows you to define the anchor points by which the rectangle is
  23. positioned, so for example you could specify that the top right of the
  24. rectangle should be an absolute distance from its parent's bottom-right corner.
  25. This object can be stored as a string, which takes the form "x y w h", including
  26. symbols like '%' and letters to indicate the anchor point. See its toString()
  27. method for more info.
  28. Example usage:
  29. @code
  30. class MyComponent
  31. {
  32. void resized()
  33. {
  34. // this will set the child component's x to be 20% of our width, its y
  35. // to be 30, its width to be 150, and its height to be 50% of our
  36. // height..
  37. const PositionedRectangle pos1 ("20% 30 150 50%");
  38. pos1.applyToComponent (*myChildComponent1);
  39. // this will inset the child component with a gap of 10 pixels
  40. // around each of its edges..
  41. const PositionedRectangle pos2 ("10 10 20M 20M");
  42. pos2.applyToComponent (*myChildComponent2);
  43. }
  44. };
  45. @endcode
  46. */
  47. class PositionedRectangle
  48. {
  49. public:
  50. //==============================================================================
  51. /** Creates an empty rectangle with all coordinates set to zero.
  52. The default anchor point is top-left; the default
  53. */
  54. PositionedRectangle() noexcept
  55. : x (0.0), y (0.0), w (0.0), h (0.0),
  56. xMode (anchorAtLeftOrTop | absoluteFromParentTopLeft),
  57. yMode (anchorAtLeftOrTop | absoluteFromParentTopLeft),
  58. wMode (absoluteSize), hMode (absoluteSize)
  59. {
  60. }
  61. /** Initialises a PositionedRectangle from a saved string version.
  62. The string must be in the format generated by toString().
  63. */
  64. PositionedRectangle (const String& stringVersion) noexcept
  65. {
  66. StringArray tokens;
  67. tokens.addTokens (stringVersion, false);
  68. decodePosString (tokens [0], xMode, x);
  69. decodePosString (tokens [1], yMode, y);
  70. decodeSizeString (tokens [2], wMode, w);
  71. decodeSizeString (tokens [3], hMode, h);
  72. }
  73. /** Creates a copy of another PositionedRectangle. */
  74. PositionedRectangle (const PositionedRectangle& other) noexcept
  75. : x (other.x), y (other.y), w (other.w), h (other.h),
  76. xMode (other.xMode), yMode (other.yMode),
  77. wMode (other.wMode), hMode (other.hMode)
  78. {
  79. }
  80. /** Copies another PositionedRectangle. */
  81. PositionedRectangle& operator= (const PositionedRectangle& other) noexcept
  82. {
  83. x = other.x;
  84. y = other.y;
  85. w = other.w;
  86. h = other.h;
  87. xMode = other.xMode;
  88. yMode = other.yMode;
  89. wMode = other.wMode;
  90. hMode = other.hMode;
  91. return *this;
  92. }
  93. //==============================================================================
  94. /** Returns a string version of this position, from which it can later be
  95. re-generated.
  96. The format is four coordinates, "x y w h".
  97. - If a coordinate is absolute, it is stored as an integer, e.g. "100".
  98. - If a coordinate is proportional to its parent's width or height, it is stored
  99. as a percentage, e.g. "80%".
  100. - If the X or Y coordinate is relative to the parent's right or bottom edge, the
  101. number has "R" appended to it, e.g. "100R" means a distance of 100 pixels from
  102. the parent's right-hand edge.
  103. - If the X or Y coordinate is relative to the parent's centre, the number has "C"
  104. appended to it, e.g. "-50C" would be 50 pixels left of the parent's centre.
  105. - If the X or Y coordinate should be anchored at the component's right or bottom
  106. edge, then it has "r" appended to it. So "-50Rr" would mean that this component's
  107. right-hand edge should be 50 pixels left of the parent's right-hand edge.
  108. - If the X or Y coordinate should be anchored at the component's centre, then it
  109. has "c" appended to it. So "-50Rc" would mean that this component's
  110. centre should be 50 pixels left of the parent's right-hand edge. "40%c" means that
  111. this component's centre should be placed 40% across the parent's width.
  112. - If it's a width or height that should use the parentSizeMinusAbsolute mode, then
  113. the number has "M" appended to it.
  114. To reload a stored string, use the constructor that takes a string parameter.
  115. */
  116. String toString() const
  117. {
  118. String s;
  119. s.preallocateBytes (32);
  120. addPosDescription (s, xMode, x); s << ' ';
  121. addPosDescription (s, yMode, y); s << ' ';
  122. addSizeDescription (s, wMode, w); s << ' ';
  123. addSizeDescription (s, hMode, h);
  124. return s;
  125. }
  126. //==============================================================================
  127. /** Calculates the absolute position, given the size of the space that
  128. it should go in.
  129. This will work out any proportional distances and sizes relative to the
  130. target rectangle, and will return the absolute position.
  131. @see applyToComponent
  132. */
  133. Rectangle<int> getRectangle (const Rectangle<int>& target) const noexcept
  134. {
  135. jassert (! target.isEmpty());
  136. double x_, y_, w_, h_;
  137. applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
  138. applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());
  139. return Rectangle<int> (roundToInt (x_), roundToInt (y_), roundToInt (w_), roundToInt (h_));
  140. }
  141. /** Same as getRectangle(), but returning the values as doubles rather than ints. */
  142. void getRectangleDouble (const Rectangle<int>& target,
  143. double& x_, double& y_, double& w_, double& h_) const noexcept
  144. {
  145. jassert (! target.isEmpty());
  146. applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
  147. applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());
  148. }
  149. /** This sets the bounds of the given component to this position.
  150. This is equivalent to writing:
  151. @code
  152. comp.setBounds (getRectangle (Rectangle<int> (0, 0, comp.getParentWidth(), comp.getParentHeight())));
  153. @endcode
  154. @see getRectangle, updateFromComponent
  155. */
  156. void applyToComponent (Component& comp) const noexcept
  157. {
  158. comp.setBounds (getRectangle (Rectangle<int> (comp.getParentWidth(), comp.getParentHeight())));
  159. }
  160. //==============================================================================
  161. /** Updates this object's coordinates to match the given rectangle.
  162. This will set all coordinates based on the given rectangle, re-calculating
  163. any proportional distances, and using the current anchor points.
  164. So for example if the x coordinate mode is currently proportional, this will
  165. re-calculate x based on the rectangle's relative position within the target
  166. rectangle's width.
  167. If the target rectangle's width or height are zero then it may not be possible
  168. to re-calculate some proportional coordinates. In this case, those coordinates
  169. will not be changed.
  170. */
  171. void updateFrom (const Rectangle<int>& newPosition,
  172. const Rectangle<int>& targetSpaceToBeRelativeTo) noexcept
  173. {
  174. updatePosAndSize (x, w, newPosition.getX(), newPosition.getWidth(), xMode, wMode, targetSpaceToBeRelativeTo.getX(), targetSpaceToBeRelativeTo.getWidth());
  175. updatePosAndSize (y, h, newPosition.getY(), newPosition.getHeight(), yMode, hMode, targetSpaceToBeRelativeTo.getY(), targetSpaceToBeRelativeTo.getHeight());
  176. }
  177. /** Same functionality as updateFrom(), but taking doubles instead of ints.
  178. */
  179. void updateFromDouble (const double newX, const double newY,
  180. const double newW, const double newH,
  181. const Rectangle<int>& target) noexcept
  182. {
  183. updatePosAndSize (x, w, newX, newW, xMode, wMode, target.getX(), target.getWidth());
  184. updatePosAndSize (y, h, newY, newH, yMode, hMode, target.getY(), target.getHeight());
  185. }
  186. /** Updates this object's coordinates to match the bounds of this component.
  187. This is equivalent to calling updateFrom() with the component's bounds and
  188. it parent size.
  189. If the component doesn't currently have a parent, then proportional coordinates
  190. might not be updated because it would need to know the parent's size to do the
  191. maths for this.
  192. */
  193. void updateFromComponent (const Component& comp) noexcept
  194. {
  195. if (comp.getParentComponent() == nullptr && ! comp.isOnDesktop())
  196. updateFrom (comp.getBounds(), Rectangle<int>());
  197. else
  198. updateFrom (comp.getBounds(), Rectangle<int> (comp.getParentWidth(), comp.getParentHeight()));
  199. }
  200. //==============================================================================
  201. /** Specifies the point within the rectangle, relative to which it should be positioned. */
  202. enum AnchorPoint
  203. {
  204. anchorAtLeftOrTop = 1 << 0, /**< The x or y coordinate specifies where the left or top edge of the rectangle should be. */
  205. anchorAtRightOrBottom = 1 << 1, /**< The x or y coordinate specifies where the right or bottom edge of the rectangle should be. */
  206. anchorAtCentre = 1 << 2 /**< The x or y coordinate specifies where the centre of the rectangle should be. */
  207. };
  208. /** Specifies how an x or y coordinate should be interpreted. */
  209. enum PositionMode
  210. {
  211. absoluteFromParentTopLeft = 1 << 3, /**< The x or y coordinate specifies an absolute distance from the parent's top or left edge. */
  212. absoluteFromParentBottomRight = 1 << 4, /**< The x or y coordinate specifies an absolute distance from the parent's bottom or right edge. */
  213. absoluteFromParentCentre = 1 << 5, /**< The x or y coordinate specifies an absolute distance from the parent's centre. */
  214. proportionOfParentSize = 1 << 6 /**< The x or y coordinate specifies a proportion of the parent's width or height, measured from the parent's top or left. */
  215. };
  216. /** Specifies how the width or height should be interpreted. */
  217. enum SizeMode
  218. {
  219. absoluteSize = 1 << 0, /**< The width or height specifies an absolute size. */
  220. parentSizeMinusAbsolute = 1 << 1, /**< The width or height is an amount that should be subtracted from the parent's width or height. */
  221. proportionalSize = 1 << 2, /**< The width or height specifies a proportion of the parent's width or height. */
  222. };
  223. //==============================================================================
  224. /** Sets all options for all coordinates.
  225. This requires a reference rectangle to be specified, because if you're changing any
  226. of the modes from proportional to absolute or vice-versa, then it'll need to convert
  227. the coordinates, and will need to know the parent size so it can calculate this.
  228. */
  229. void setModes (const AnchorPoint xAnchor, const PositionMode xMode_,
  230. const AnchorPoint yAnchor, const PositionMode yMode_,
  231. const SizeMode widthMode, const SizeMode heightMode,
  232. const Rectangle<int>& target) noexcept
  233. {
  234. if (xMode != (xAnchor | xMode_) || wMode != widthMode)
  235. {
  236. double tx, tw;
  237. applyPosAndSize (tx, tw, x, w, xMode, wMode, target.getX(), target.getWidth());
  238. xMode = (uint8) (xAnchor | xMode_);
  239. wMode = (uint8) widthMode;
  240. updatePosAndSize (x, w, tx, tw, xMode, wMode, target.getX(), target.getWidth());
  241. }
  242. if (yMode != (yAnchor | yMode_) || hMode != heightMode)
  243. {
  244. double ty, th;
  245. applyPosAndSize (ty, th, y, h, yMode, hMode, target.getY(), target.getHeight());
  246. yMode = (uint8) (yAnchor | yMode_);
  247. hMode = (uint8) heightMode;
  248. updatePosAndSize (y, h, ty, th, yMode, hMode, target.getY(), target.getHeight());
  249. }
  250. }
  251. /** Returns the anchoring mode for the x coordinate.
  252. To change any of the modes, use setModes().
  253. */
  254. AnchorPoint getAnchorPointX() const noexcept
  255. {
  256. return (AnchorPoint) (xMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
  257. }
  258. /** Returns the positioning mode for the x coordinate.
  259. To change any of the modes, use setModes().
  260. */
  261. PositionMode getPositionModeX() const noexcept
  262. {
  263. return (PositionMode) (xMode & (absoluteFromParentTopLeft | absoluteFromParentBottomRight
  264. | absoluteFromParentCentre | proportionOfParentSize));
  265. }
  266. /** Returns the raw x coordinate.
  267. If the x position mode is absolute, then this will be the absolute value. If it's
  268. proportional, then this will be a fractional proportion, where 1.0 means the full
  269. width of the parent space.
  270. */
  271. double getX() const noexcept { return x; }
  272. /** Sets the raw value of the x coordinate.
  273. See getX() for the meaning of this value.
  274. */
  275. void setX (const double newX) noexcept { x = newX; }
  276. /** Returns the anchoring mode for the y coordinate.
  277. To change any of the modes, use setModes().
  278. */
  279. AnchorPoint getAnchorPointY() const noexcept
  280. {
  281. return (AnchorPoint) (yMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
  282. }
  283. /** Returns the positioning mode for the y coordinate.
  284. To change any of the modes, use setModes().
  285. */
  286. PositionMode getPositionModeY() const noexcept
  287. {
  288. return (PositionMode) (yMode & (absoluteFromParentTopLeft | absoluteFromParentBottomRight
  289. | absoluteFromParentCentre | proportionOfParentSize));
  290. }
  291. /** Returns the raw y coordinate.
  292. If the y position mode is absolute, then this will be the absolute value. If it's
  293. proportional, then this will be a fractional proportion, where 1.0 means the full
  294. height of the parent space.
  295. */
  296. double getY() const noexcept { return y; }
  297. /** Sets the raw value of the y coordinate.
  298. See getY() for the meaning of this value.
  299. */
  300. void setY (const double newY) noexcept { y = newY; }
  301. /** Returns the mode used to calculate the width.
  302. To change any of the modes, use setModes().
  303. */
  304. SizeMode getWidthMode() const noexcept { return (SizeMode) wMode; }
  305. /** Returns the raw width value.
  306. If the width mode is absolute, then this will be the absolute value. If the mode is
  307. proportional, then this will be a fractional proportion, where 1.0 means the full
  308. width of the parent space.
  309. */
  310. double getWidth() const noexcept { return w; }
  311. /** Sets the raw width value.
  312. See getWidth() for the details about what this value means.
  313. */
  314. void setWidth (const double newWidth) noexcept { w = newWidth; }
  315. /** Returns the mode used to calculate the height.
  316. To change any of the modes, use setModes().
  317. */
  318. SizeMode getHeightMode() const noexcept { return (SizeMode) hMode; }
  319. /** Returns the raw height value.
  320. If the height mode is absolute, then this will be the absolute value. If the mode is
  321. proportional, then this will be a fractional proportion, where 1.0 means the full
  322. height of the parent space.
  323. */
  324. double getHeight() const noexcept { return h; }
  325. /** Sets the raw height value.
  326. See getHeight() for the details about what this value means.
  327. */
  328. void setHeight (const double newHeight) noexcept { h = newHeight; }
  329. //==============================================================================
  330. /** If the size and position are constance, and wouldn't be affected by changes
  331. in the parent's size, then this will return true.
  332. */
  333. bool isPositionAbsolute() const noexcept
  334. {
  335. return (xMode & ~anchorAtLeftOrTop) == absoluteFromParentTopLeft
  336. && (yMode & ~anchorAtLeftOrTop) == absoluteFromParentTopLeft
  337. && wMode == absoluteSize
  338. && hMode == absoluteSize;
  339. }
  340. //==============================================================================
  341. /** Compares two objects. */
  342. bool operator== (const PositionedRectangle& other) const noexcept
  343. {
  344. return x == other.x && y == other.y
  345. && w == other.w && h == other.h
  346. && xMode == other.xMode && yMode == other.yMode
  347. && wMode == other.wMode && hMode == other.hMode;
  348. }
  349. /** Compares two objects. */
  350. bool operator!= (const PositionedRectangle& other) const noexcept
  351. {
  352. return ! operator== (other);
  353. }
  354. private:
  355. //==============================================================================
  356. double x, y, w, h;
  357. uint8 xMode, yMode, wMode, hMode;
  358. void addPosDescription (String& s, const uint8 mode, const double value) const noexcept
  359. {
  360. if ((mode & proportionOfParentSize) != 0)
  361. {
  362. s << (roundToInt (value * 100000.0) / 1000.0) << '%';
  363. }
  364. else
  365. {
  366. s << (roundToInt (value * 100.0) / 100.0);
  367. if ((mode & absoluteFromParentBottomRight) != 0)
  368. s << 'R';
  369. else if ((mode & absoluteFromParentCentre) != 0)
  370. s << 'C';
  371. }
  372. if ((mode & anchorAtRightOrBottom) != 0)
  373. s << 'r';
  374. else if ((mode & anchorAtCentre) != 0)
  375. s << 'c';
  376. }
  377. void addSizeDescription (String& s, const uint8 mode, const double value) const noexcept
  378. {
  379. if (mode == proportionalSize)
  380. s << (roundToInt (value * 100000.0) / 1000.0) << '%';
  381. else if (mode == parentSizeMinusAbsolute)
  382. s << (roundToInt (value * 100.0) / 100.0) << 'M';
  383. else
  384. s << (roundToInt (value * 100.0) / 100.0);
  385. }
  386. void decodePosString (const String& s, uint8& mode, double& value) noexcept
  387. {
  388. if (s.containsChar ('r'))
  389. mode = anchorAtRightOrBottom;
  390. else if (s.containsChar ('c'))
  391. mode = anchorAtCentre;
  392. else
  393. mode = anchorAtLeftOrTop;
  394. if (s.containsChar ('%'))
  395. {
  396. mode |= proportionOfParentSize;
  397. value = s.removeCharacters ("%rcRC").getDoubleValue() / 100.0;
  398. }
  399. else
  400. {
  401. if (s.containsChar ('R'))
  402. mode |= absoluteFromParentBottomRight;
  403. else if (s.containsChar ('C'))
  404. mode |= absoluteFromParentCentre;
  405. else
  406. mode |= absoluteFromParentTopLeft;
  407. value = s.removeCharacters ("rcRC").getDoubleValue();
  408. }
  409. }
  410. void decodeSizeString (const String& s, uint8& mode, double& value) noexcept
  411. {
  412. if (s.containsChar ('%'))
  413. {
  414. mode = proportionalSize;
  415. value = s.upToFirstOccurrenceOf ("%", false, false).getDoubleValue() / 100.0;
  416. }
  417. else if (s.containsChar ('M'))
  418. {
  419. mode = parentSizeMinusAbsolute;
  420. value = s.getDoubleValue();
  421. }
  422. else
  423. {
  424. mode = absoluteSize;
  425. value = s.getDoubleValue();
  426. }
  427. }
  428. void applyPosAndSize (double& xOut, double& wOut, const double x_, const double w_,
  429. const uint8 xMode_, const uint8 wMode_,
  430. const int parentPos, const int parentSize) const noexcept
  431. {
  432. if (wMode_ == proportionalSize)
  433. wOut = roundToInt (w_ * parentSize);
  434. else if (wMode_ == parentSizeMinusAbsolute)
  435. wOut = jmax (0, parentSize - roundToInt (w_));
  436. else
  437. wOut = roundToInt (w_);
  438. if ((xMode_ & proportionOfParentSize) != 0)
  439. xOut = parentPos + x_ * parentSize;
  440. else if ((xMode_ & absoluteFromParentBottomRight) != 0)
  441. xOut = (parentPos + parentSize) - x_;
  442. else if ((xMode_ & absoluteFromParentCentre) != 0)
  443. xOut = x_ + (parentPos + parentSize / 2);
  444. else
  445. xOut = x_ + parentPos;
  446. if ((xMode_ & anchorAtRightOrBottom) != 0)
  447. xOut -= wOut;
  448. else if ((xMode_ & anchorAtCentre) != 0)
  449. xOut -= wOut / 2;
  450. }
  451. void updatePosAndSize (double& xOut, double& wOut, double x_, const double w_,
  452. const uint8 xMode_, const uint8 wMode_,
  453. const int parentPos, const int parentSize) const noexcept
  454. {
  455. if (wMode_ == proportionalSize)
  456. {
  457. if (parentSize > 0)
  458. wOut = w_ / parentSize;
  459. }
  460. else if (wMode_ == parentSizeMinusAbsolute)
  461. wOut = parentSize - w_;
  462. else
  463. wOut = w_;
  464. if ((xMode_ & anchorAtRightOrBottom) != 0)
  465. x_ += w_;
  466. else if ((xMode_ & anchorAtCentre) != 0)
  467. x_ += w_ / 2;
  468. if ((xMode_ & proportionOfParentSize) != 0)
  469. {
  470. if (parentSize > 0)
  471. xOut = (x_ - parentPos) / parentSize;
  472. }
  473. else if ((xMode_ & absoluteFromParentBottomRight) != 0)
  474. xOut = (parentPos + parentSize) - x_;
  475. else if ((xMode_ & absoluteFromParentCentre) != 0)
  476. xOut = x_ - (parentPos + parentSize / 2);
  477. else
  478. xOut = x_ - parentPos;
  479. }
  480. };
  481. //==============================================================================
  482. struct RelativePositionedRectangle
  483. {
  484. //==============================================================================
  485. RelativePositionedRectangle()
  486. : relativeToX (0),
  487. relativeToY (0),
  488. relativeToW (0),
  489. relativeToH (0)
  490. {
  491. }
  492. RelativePositionedRectangle (const RelativePositionedRectangle& other)
  493. : rect (other.rect),
  494. relativeToX (other.relativeToX),
  495. relativeToY (other.relativeToY),
  496. relativeToW (other.relativeToW),
  497. relativeToH (other.relativeToH)
  498. {
  499. }
  500. RelativePositionedRectangle& operator= (const RelativePositionedRectangle& other)
  501. {
  502. rect = other.rect;
  503. relativeToX = other.relativeToX;
  504. relativeToY = other.relativeToY;
  505. relativeToW = other.relativeToW;
  506. relativeToH = other.relativeToH;
  507. return *this;
  508. }
  509. //==============================================================================
  510. bool operator== (const RelativePositionedRectangle& other) const noexcept
  511. {
  512. return rect == other.rect
  513. && relativeToX == other.relativeToX
  514. && relativeToY == other.relativeToY
  515. && relativeToW == other.relativeToW
  516. && relativeToH == other.relativeToH;
  517. }
  518. bool operator!= (const RelativePositionedRectangle& other) const noexcept
  519. {
  520. return ! operator== (other);
  521. }
  522. template <typename LayoutType>
  523. void getRelativeTargetBounds (const Rectangle<int>& parentArea,
  524. const LayoutType* layout,
  525. int& x, int& xw, int& y, int& yh,
  526. int& w, int& h) const
  527. {
  528. Component* rx = {};
  529. Component* ry = {};
  530. Component* rw = {};
  531. Component* rh = {};
  532. if (layout != nullptr)
  533. {
  534. rx = layout->findComponentWithId (relativeToX);
  535. ry = layout->findComponentWithId (relativeToY);
  536. rw = layout->findComponentWithId (relativeToW);
  537. rh = layout->findComponentWithId (relativeToH);
  538. }
  539. x = parentArea.getX() + (rx != nullptr ? rx->getX() : 0);
  540. y = parentArea.getY() + (ry != nullptr ? ry->getY() : 0);
  541. w = rw != nullptr ? rw->getWidth() : parentArea.getWidth();
  542. h = rh != nullptr ? rh->getHeight() : parentArea.getHeight();
  543. xw = rx != nullptr ? rx->getWidth() : parentArea.getWidth();
  544. yh = ry != nullptr ? ry->getHeight() : parentArea.getHeight();
  545. }
  546. Rectangle<int> getRectangle (const Rectangle<int>& parentArea,
  547. const ComponentLayout* layout) const
  548. {
  549. int x, xw, y, yh, w, h;
  550. getRelativeTargetBounds (parentArea, layout, x, xw, y, yh, w, h);
  551. const Rectangle<int> xyRect ((xw <= 0 || yh <= 0) ? Rectangle<int>()
  552. : rect.getRectangle (Rectangle<int> (x, y, xw, yh)));
  553. const Rectangle<int> whRect ((w <= 0 || h <= 0) ? Rectangle<int>()
  554. : rect.getRectangle (Rectangle<int> (x, y, w, h)));
  555. return Rectangle<int> (xyRect.getX(), xyRect.getY(),
  556. whRect.getWidth(), whRect.getHeight());
  557. }
  558. void getRectangleDouble (double& x, double& y, double& w, double& h,
  559. const Rectangle<int>& parentArea,
  560. const ComponentLayout* layout) const
  561. {
  562. int rx, rxw, ry, ryh, rw, rh;
  563. getRelativeTargetBounds (parentArea, layout, rx, rxw, ry, ryh, rw, rh);
  564. double dummy1, dummy2;
  565. rect.getRectangleDouble (Rectangle<int> (rx, ry, rxw, ryh), x, y, dummy1, dummy2);
  566. rect.getRectangleDouble (Rectangle<int> (rx, ry, rw, rh), dummy1, dummy2, w, h);
  567. }
  568. void updateFromComponent (const Component& comp, const ComponentLayout* layout)
  569. {
  570. int x, xw, y, yh, w, h;
  571. getRelativeTargetBounds (Rectangle<int> (0, 0, comp.getParentWidth(), comp.getParentHeight()),
  572. layout, x, xw, y, yh, w, h);
  573. PositionedRectangle xyRect (rect), whRect (rect);
  574. xyRect.updateFrom (comp.getBounds(), Rectangle<int> (x, y, xw, yh));
  575. whRect.updateFrom (comp.getBounds(), Rectangle<int> (x, y, w, h));
  576. rect.setX (xyRect.getX());
  577. rect.setY (xyRect.getY());
  578. rect.setWidth (whRect.getWidth());
  579. rect.setHeight (whRect.getHeight());
  580. }
  581. void updateFrom (double newX, double newY, double newW, double newH,
  582. const Rectangle<int>& parentArea, const ComponentLayout* layout)
  583. {
  584. int x, xw, y, yh, w, h;
  585. getRelativeTargetBounds (parentArea, layout, x, xw, y, yh, w, h);
  586. PositionedRectangle xyRect (rect), whRect (rect);
  587. xyRect.updateFromDouble (newX, newY, newW, newH, Rectangle<int> (x, y, xw, yh));
  588. whRect.updateFromDouble (newX, newY, newW, newH, Rectangle<int> (x, y, w, h));
  589. rect.setX (xyRect.getX());
  590. rect.setY (xyRect.getY());
  591. rect.setWidth (whRect.getWidth());
  592. rect.setHeight (whRect.getHeight());
  593. }
  594. void applyToXml (XmlElement& e) const
  595. {
  596. e.setAttribute ("pos", rect.toString());
  597. if (relativeToX != 0) e.setAttribute ("posRelativeX", String::toHexString (relativeToX));
  598. if (relativeToY != 0) e.setAttribute ("posRelativeY", String::toHexString (relativeToY));
  599. if (relativeToW != 0) e.setAttribute ("posRelativeW", String::toHexString (relativeToW));
  600. if (relativeToH != 0) e.setAttribute ("posRelativeH", String::toHexString (relativeToH));
  601. }
  602. void restoreFromXml (const XmlElement& e, const RelativePositionedRectangle& defaultPos)
  603. {
  604. rect = PositionedRectangle (e.getStringAttribute ("pos", defaultPos.rect.toString()));
  605. relativeToX = e.getStringAttribute ("posRelativeX", String::toHexString (defaultPos.relativeToX)).getHexValue64();
  606. relativeToY = e.getStringAttribute ("posRelativeY", String::toHexString (defaultPos.relativeToY)).getHexValue64();
  607. relativeToW = e.getStringAttribute ("posRelativeW", String::toHexString (defaultPos.relativeToW)).getHexValue64();
  608. relativeToH = e.getStringAttribute ("posRelativeH", String::toHexString (defaultPos.relativeToH)).getHexValue64();
  609. }
  610. String toString() const
  611. {
  612. StringArray toks;
  613. toks.addTokens (rect.toString(), false);
  614. return toks[0] + " " + toks[1];
  615. }
  616. Point<float> toXY (const Rectangle<int>& parentArea,
  617. const ComponentLayout* layout) const
  618. {
  619. double x, y, w, h;
  620. getRectangleDouble (x, y, w, h, parentArea, layout);
  621. return { (float) x, (float) y };
  622. }
  623. void getXY (double& x, double& y,
  624. const Rectangle<int>& parentArea,
  625. const ComponentLayout* layout) const
  626. {
  627. double w, h;
  628. getRectangleDouble (x, y, w, h, parentArea, layout);
  629. }
  630. //==============================================================================
  631. PositionedRectangle rect;
  632. int64 relativeToX;
  633. int64 relativeToY;
  634. int64 relativeToW;
  635. int64 relativeToH;
  636. };