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.

762 lines
30KB

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