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.

761 lines
30KB

  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. #pragma once
  20. class ComponentLayout;
  21. //==============================================================================
  22. /**
  23. A rectangle whose coordinates can be defined in terms of absolute or
  24. proportional distances.
  25. Designed mainly for storing component positions, this gives you a lot of
  26. control over how each coordinate is stored, either as an absolute position,
  27. or as a proportion of the size of a parent rectangle.
  28. It also allows you to define the anchor points by which the rectangle is
  29. positioned, so for example you could specify that the top right of the
  30. rectangle should be an absolute distance from its parent's bottom-right corner.
  31. This object can be stored as a string, which takes the form "x y w h", including
  32. symbols like '%' and letters to indicate the anchor point. See its toString()
  33. method for more info.
  34. Example usage:
  35. @code
  36. class MyComponent
  37. {
  38. void resized()
  39. {
  40. // this will set the child component's x to be 20% of our width, its y
  41. // to be 30, its width to be 150, and its height to be 50% of our
  42. // height..
  43. const PositionedRectangle pos1 ("20% 30 150 50%");
  44. pos1.applyToComponent (*myChildComponent1);
  45. // this will inset the child component with a gap of 10 pixels
  46. // around each of its edges..
  47. const PositionedRectangle pos2 ("10 10 20M 20M");
  48. pos2.applyToComponent (*myChildComponent2);
  49. }
  50. };
  51. @endcode
  52. */
  53. class PositionedRectangle
  54. {
  55. public:
  56. //==============================================================================
  57. /** Creates an empty rectangle with all coordinates set to zero.
  58. The default anchor point is top-left; the default
  59. */
  60. PositionedRectangle() noexcept
  61. : x (0.0), y (0.0), w (0.0), h (0.0),
  62. xMode (anchorAtLeftOrTop | absoluteFromParentTopLeft),
  63. yMode (anchorAtLeftOrTop | absoluteFromParentTopLeft),
  64. wMode (absoluteSize), hMode (absoluteSize)
  65. {
  66. }
  67. /** Initialises a PositionedRectangle from a saved string version.
  68. The string must be in the format generated by toString().
  69. */
  70. PositionedRectangle (const String& stringVersion) noexcept
  71. {
  72. StringArray tokens;
  73. tokens.addTokens (stringVersion, false);
  74. decodePosString (tokens [0], xMode, x);
  75. decodePosString (tokens [1], yMode, y);
  76. decodeSizeString (tokens [2], wMode, w);
  77. decodeSizeString (tokens [3], hMode, h);
  78. }
  79. /** Creates a copy of another PositionedRectangle. */
  80. PositionedRectangle (const PositionedRectangle& other) noexcept
  81. : x (other.x), y (other.y), w (other.w), h (other.h),
  82. xMode (other.xMode), yMode (other.yMode),
  83. wMode (other.wMode), hMode (other.hMode)
  84. {
  85. }
  86. /** Copies another PositionedRectangle. */
  87. PositionedRectangle& operator= (const PositionedRectangle& other) noexcept
  88. {
  89. x = other.x;
  90. y = other.y;
  91. w = other.w;
  92. h = other.h;
  93. xMode = other.xMode;
  94. yMode = other.yMode;
  95. wMode = other.wMode;
  96. hMode = other.hMode;
  97. return *this;
  98. }
  99. //==============================================================================
  100. /** Returns a string version of this position, from which it can later be
  101. re-generated.
  102. The format is four coordinates, "x y w h".
  103. - If a coordinate is absolute, it is stored as an integer, e.g. "100".
  104. - If a coordinate is proportional to its parent's width or height, it is stored
  105. as a percentage, e.g. "80%".
  106. - If the X or Y coordinate is relative to the parent's right or bottom edge, the
  107. number has "R" appended to it, e.g. "100R" means a distance of 100 pixels from
  108. the parent's right-hand edge.
  109. - If the X or Y coordinate is relative to the parent's centre, the number has "C"
  110. appended to it, e.g. "-50C" would be 50 pixels left of the parent's centre.
  111. - If the X or Y coordinate should be anchored at the component's right or bottom
  112. edge, then it has "r" appended to it. So "-50Rr" would mean that this component's
  113. right-hand edge should be 50 pixels left of the parent's right-hand edge.
  114. - If the X or Y coordinate should be anchored at the component's centre, then it
  115. has "c" appended to it. So "-50Rc" would mean that this component's
  116. centre should be 50 pixels left of the parent's right-hand edge. "40%c" means that
  117. this component's centre should be placed 40% across the parent's width.
  118. - If it's a width or height that should use the parentSizeMinusAbsolute mode, then
  119. the number has "M" appended to it.
  120. To reload a stored string, use the constructor that takes a string parameter.
  121. */
  122. String toString() const
  123. {
  124. String s;
  125. s.preallocateBytes (32);
  126. addPosDescription (s, xMode, x); s << ' ';
  127. addPosDescription (s, yMode, y); s << ' ';
  128. addSizeDescription (s, wMode, w); s << ' ';
  129. addSizeDescription (s, hMode, h);
  130. return s;
  131. }
  132. //==============================================================================
  133. /** Calculates the absolute position, given the size of the space that
  134. it should go in.
  135. This will work out any proportional distances and sizes relative to the
  136. target rectangle, and will return the absolute position.
  137. @see applyToComponent
  138. */
  139. Rectangle<int> getRectangle (const Rectangle<int>& target) const noexcept
  140. {
  141. jassert (! target.isEmpty());
  142. double x_, y_, w_, h_;
  143. applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
  144. applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());
  145. return Rectangle<int> (roundToInt (x_), roundToInt (y_), roundToInt (w_), roundToInt (h_));
  146. }
  147. /** Same as getRectangle(), but returning the values as doubles rather than ints. */
  148. void getRectangleDouble (const Rectangle<int>& target,
  149. double& x_, double& y_, double& w_, double& h_) const noexcept
  150. {
  151. jassert (! target.isEmpty());
  152. applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
  153. applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());
  154. }
  155. /** This sets the bounds of the given component to this position.
  156. This is equivalent to writing:
  157. @code
  158. comp.setBounds (getRectangle (Rectangle<int> (0, 0, comp.getParentWidth(), comp.getParentHeight())));
  159. @endcode
  160. @see getRectangle, updateFromComponent
  161. */
  162. void applyToComponent (Component& comp) const noexcept
  163. {
  164. comp.setBounds (getRectangle (Rectangle<int> (comp.getParentWidth(), comp.getParentHeight())));
  165. }
  166. //==============================================================================
  167. /** Updates this object's coordinates to match the given rectangle.
  168. This will set all coordinates based on the given rectangle, re-calculating
  169. any proportional distances, and using the current anchor points.
  170. So for example if the x coordinate mode is currently proportional, this will
  171. re-calculate x based on the rectangle's relative position within the target
  172. rectangle's width.
  173. If the target rectangle's width or height are zero then it may not be possible
  174. to re-calculate some proportional coordinates. In this case, those coordinates
  175. will not be changed.
  176. */
  177. void updateFrom (const Rectangle<int>& newPosition,
  178. const Rectangle<int>& targetSpaceToBeRelativeTo) noexcept
  179. {
  180. updatePosAndSize (x, w, newPosition.getX(), newPosition.getWidth(), xMode, wMode, targetSpaceToBeRelativeTo.getX(), targetSpaceToBeRelativeTo.getWidth());
  181. updatePosAndSize (y, h, newPosition.getY(), newPosition.getHeight(), yMode, hMode, targetSpaceToBeRelativeTo.getY(), targetSpaceToBeRelativeTo.getHeight());
  182. }
  183. /** Same functionality as updateFrom(), but taking doubles instead of ints.
  184. */
  185. void updateFromDouble (const double newX, const double newY,
  186. const double newW, const double newH,
  187. const Rectangle<int>& target) noexcept
  188. {
  189. updatePosAndSize (x, w, newX, newW, xMode, wMode, target.getX(), target.getWidth());
  190. updatePosAndSize (y, h, newY, newH, yMode, hMode, target.getY(), target.getHeight());
  191. }
  192. /** Updates this object's coordinates to match the bounds of this component.
  193. This is equivalent to calling updateFrom() with the component's bounds and
  194. it parent size.
  195. If the component doesn't currently have a parent, then proportional coordinates
  196. might not be updated because it would need to know the parent's size to do the
  197. maths for this.
  198. */
  199. void updateFromComponent (const Component& comp) noexcept
  200. {
  201. if (comp.getParentComponent() == nullptr && ! comp.isOnDesktop())
  202. updateFrom (comp.getBounds(), Rectangle<int>());
  203. else
  204. updateFrom (comp.getBounds(), Rectangle<int> (comp.getParentWidth(), comp.getParentHeight()));
  205. }
  206. //==============================================================================
  207. /** Specifies the point within the rectangle, relative to which it should be positioned. */
  208. enum AnchorPoint
  209. {
  210. anchorAtLeftOrTop = 1 << 0, /**< The x or y coordinate specifies where the left or top edge of the rectangle should be. */
  211. anchorAtRightOrBottom = 1 << 1, /**< The x or y coordinate specifies where the right or bottom edge of the rectangle should be. */
  212. anchorAtCentre = 1 << 2 /**< The x or y coordinate specifies where the centre of the rectangle should be. */
  213. };
  214. /** Specifies how an x or y coordinate should be interpreted. */
  215. enum PositionMode
  216. {
  217. absoluteFromParentTopLeft = 1 << 3, /**< The x or y coordinate specifies an absolute distance from the parent's top or left edge. */
  218. absoluteFromParentBottomRight = 1 << 4, /**< The x or y coordinate specifies an absolute distance from the parent's bottom or right edge. */
  219. absoluteFromParentCentre = 1 << 5, /**< The x or y coordinate specifies an absolute distance from the parent's centre. */
  220. 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. */
  221. };
  222. /** Specifies how the width or height should be interpreted. */
  223. enum SizeMode
  224. {
  225. absoluteSize = 1 << 0, /**< The width or height specifies an absolute size. */
  226. parentSizeMinusAbsolute = 1 << 1, /**< The width or height is an amount that should be subtracted from the parent's width or height. */
  227. proportionalSize = 1 << 2, /**< The width or height specifies a proportion of the parent's width or height. */
  228. };
  229. //==============================================================================
  230. /** Sets all options for all coordinates.
  231. This requires a reference rectangle to be specified, because if you're changing any
  232. of the modes from proportional to absolute or vice-versa, then it'll need to convert
  233. the coordinates, and will need to know the parent size so it can calculate this.
  234. */
  235. void setModes (const AnchorPoint xAnchor, const PositionMode xMode_,
  236. const AnchorPoint yAnchor, const PositionMode yMode_,
  237. const SizeMode widthMode, const SizeMode heightMode,
  238. const Rectangle<int>& target) noexcept
  239. {
  240. if (xMode != (xAnchor | xMode_) || wMode != widthMode)
  241. {
  242. double tx, tw;
  243. applyPosAndSize (tx, tw, x, w, xMode, wMode, target.getX(), target.getWidth());
  244. xMode = (uint8) (xAnchor | xMode_);
  245. wMode = (uint8) widthMode;
  246. updatePosAndSize (x, w, tx, tw, xMode, wMode, target.getX(), target.getWidth());
  247. }
  248. if (yMode != (yAnchor | yMode_) || hMode != heightMode)
  249. {
  250. double ty, th;
  251. applyPosAndSize (ty, th, y, h, yMode, hMode, target.getY(), target.getHeight());
  252. yMode = (uint8) (yAnchor | yMode_);
  253. hMode = (uint8) heightMode;
  254. updatePosAndSize (y, h, ty, th, yMode, hMode, target.getY(), target.getHeight());
  255. }
  256. }
  257. /** Returns the anchoring mode for the x coordinate.
  258. To change any of the modes, use setModes().
  259. */
  260. AnchorPoint getAnchorPointX() const noexcept
  261. {
  262. return (AnchorPoint) (xMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
  263. }
  264. /** Returns the positioning mode for the x coordinate.
  265. To change any of the modes, use setModes().
  266. */
  267. PositionMode getPositionModeX() const noexcept
  268. {
  269. return (PositionMode) (xMode & (absoluteFromParentTopLeft | absoluteFromParentBottomRight
  270. | absoluteFromParentCentre | proportionOfParentSize));
  271. }
  272. /** Returns the raw x coordinate.
  273. If the x position mode is absolute, then this will be the absolute value. If it's
  274. proportional, then this will be a fractional proportion, where 1.0 means the full
  275. width of the parent space.
  276. */
  277. double getX() const noexcept { return x; }
  278. /** Sets the raw value of the x coordinate.
  279. See getX() for the meaning of this value.
  280. */
  281. void setX (const double newX) noexcept { x = newX; }
  282. /** Returns the anchoring mode for the y coordinate.
  283. To change any of the modes, use setModes().
  284. */
  285. AnchorPoint getAnchorPointY() const noexcept
  286. {
  287. return (AnchorPoint) (yMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
  288. }
  289. /** Returns the positioning mode for the y coordinate.
  290. To change any of the modes, use setModes().
  291. */
  292. PositionMode getPositionModeY() const noexcept
  293. {
  294. return (PositionMode) (yMode & (absoluteFromParentTopLeft | absoluteFromParentBottomRight
  295. | absoluteFromParentCentre | proportionOfParentSize));
  296. }
  297. /** Returns the raw y coordinate.
  298. If the y position mode is absolute, then this will be the absolute value. If it's
  299. proportional, then this will be a fractional proportion, where 1.0 means the full
  300. height of the parent space.
  301. */
  302. double getY() const noexcept { return y; }
  303. /** Sets the raw value of the y coordinate.
  304. See getY() for the meaning of this value.
  305. */
  306. void setY (const double newY) noexcept { y = newY; }
  307. /** Returns the mode used to calculate the width.
  308. To change any of the modes, use setModes().
  309. */
  310. SizeMode getWidthMode() const noexcept { return (SizeMode) wMode; }
  311. /** Returns the raw width value.
  312. If the width mode is absolute, then this will be the absolute value. If the mode is
  313. proportional, then this will be a fractional proportion, where 1.0 means the full
  314. width of the parent space.
  315. */
  316. double getWidth() const noexcept { return w; }
  317. /** Sets the raw width value.
  318. See getWidth() for the details about what this value means.
  319. */
  320. void setWidth (const double newWidth) noexcept { w = newWidth; }
  321. /** Returns the mode used to calculate the height.
  322. To change any of the modes, use setModes().
  323. */
  324. SizeMode getHeightMode() const noexcept { return (SizeMode) hMode; }
  325. /** Returns the raw height value.
  326. If the height mode is absolute, then this will be the absolute value. If the mode is
  327. proportional, then this will be a fractional proportion, where 1.0 means the full
  328. height of the parent space.
  329. */
  330. double getHeight() const noexcept { return h; }
  331. /** Sets the raw height value.
  332. See getHeight() for the details about what this value means.
  333. */
  334. void setHeight (const double newHeight) noexcept { h = newHeight; }
  335. //==============================================================================
  336. /** If the size and position are constance, and wouldn't be affected by changes
  337. in the parent's size, then this will return true.
  338. */
  339. bool isPositionAbsolute() const noexcept
  340. {
  341. return (xMode & ~anchorAtLeftOrTop) == absoluteFromParentTopLeft
  342. && (yMode & ~anchorAtLeftOrTop) == absoluteFromParentTopLeft
  343. && wMode == absoluteSize
  344. && hMode == absoluteSize;
  345. }
  346. //==============================================================================
  347. /** Compares two objects. */
  348. bool operator== (const PositionedRectangle& other) const noexcept
  349. {
  350. return x == other.x && y == other.y
  351. && w == other.w && h == other.h
  352. && xMode == other.xMode && yMode == other.yMode
  353. && wMode == other.wMode && hMode == other.hMode;
  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 Point<float> ((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. };