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.

759 lines
30KB

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