File: jucer_RelativePositionedRectangle.h

package info (click to toggle)
juce 6.1.3~ds0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,612 kB
  • sloc: cpp: 431,694; java: 2,592; ansic: 797; xml: 259; sh: 164; python: 126; makefile: 64
file content (759 lines) | stat: -rw-r--r-- 31,221 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
  ==============================================================================

   This file is part of the JUCE library.
   Copyright (c) 2020 - Raw Material Software Limited

   JUCE is an open source library subject to commercial or open-source
   licensing.

   By using JUCE, you agree to the terms of both the JUCE 6 End-User License
   Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).

   End User License Agreement: www.juce.com/juce-6-licence
   Privacy Policy: www.juce.com/juce-privacy-policy

   Or: You may also use this code under the terms of the GPL v3 (see
   www.gnu.org/licenses).

   JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
   EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
   DISCLAIMED.

  ==============================================================================
*/

#pragma once

class ComponentLayout;

//==============================================================================
/**
    A rectangle whose coordinates can be defined in terms of absolute or
    proportional distances.

    Designed mainly for storing component positions, this gives you a lot of
    control over how each coordinate is stored, either as an absolute position,
    or as a proportion of the size of a parent rectangle.

    It also allows you to define the anchor points by which the rectangle is
    positioned, so for example you could specify that the top right of the
    rectangle should be an absolute distance from its parent's bottom-right corner.

    This object can be stored as a string, which takes the form "x y w h", including
    symbols like '%' and letters to indicate the anchor point. See its toString()
    method for more info.

    Example usage:
    @code
    class MyComponent
    {
        void resized()
        {
            // this will set the child component's x to be 20% of our width, its y
            // to be 30, its width to be 150, and its height to be 50% of our
            // height..
            const PositionedRectangle pos1 ("20% 30 150 50%");
            pos1.applyToComponent (*myChildComponent1);

            // this will inset the child component with a gap of 10 pixels
            // around each of its edges..
            const PositionedRectangle pos2 ("10 10 20M 20M");
            pos2.applyToComponent (*myChildComponent2);
        }
    };
    @endcode
*/
class PositionedRectangle
{
public:
    //==============================================================================
    /** Creates an empty rectangle with all coordinates set to zero.

        The default anchor point is top-left; the default
    */
    PositionedRectangle() noexcept
        : x (0.0), y (0.0), w (0.0), h (0.0),
          xMode ((int) anchorAtLeftOrTop | (int) absoluteFromParentTopLeft),
          yMode ((int) anchorAtLeftOrTop | (int) absoluteFromParentTopLeft),
          wMode (absoluteSize), hMode (absoluteSize)
    {
    }

    /** Initialises a PositionedRectangle from a saved string version.

        The string must be in the format generated by toString().
    */
    PositionedRectangle (const String& stringVersion) noexcept
    {
        StringArray tokens;
        tokens.addTokens (stringVersion, false);

        decodePosString (tokens [0], xMode, x);
        decodePosString (tokens [1], yMode, y);
        decodeSizeString (tokens [2], wMode, w);
        decodeSizeString (tokens [3], hMode, h);
    }

    /** Creates a copy of another PositionedRectangle. */
    PositionedRectangle (const PositionedRectangle& other) noexcept
        : x (other.x), y (other.y), w (other.w), h (other.h),
          xMode (other.xMode), yMode (other.yMode),
          wMode (other.wMode), hMode (other.hMode)
    {
    }

    /** Copies another PositionedRectangle. */
    PositionedRectangle& operator= (const PositionedRectangle& other) noexcept
    {
        x = other.x;
        y = other.y;
        w = other.w;
        h = other.h;
        xMode = other.xMode;
        yMode = other.yMode;
        wMode = other.wMode;
        hMode = other.hMode;

        return *this;
    }

    //==============================================================================
    /** Returns a string version of this position, from which it can later be
        re-generated.

        The format is four coordinates, "x y w h".

        - If a coordinate is absolute, it is stored as an integer, e.g. "100".
        - If a coordinate is proportional to its parent's width or height, it is stored
          as a percentage, e.g. "80%".
        - If the X or Y coordinate is relative to the parent's right or bottom edge, the
          number has "R" appended to it, e.g. "100R" means a distance of 100 pixels from
          the parent's right-hand edge.
        - If the X or Y coordinate is relative to the parent's centre, the number has "C"
          appended to it, e.g. "-50C" would be 50 pixels left of the parent's centre.
        - If the X or Y coordinate should be anchored at the component's right or bottom
          edge, then it has "r" appended to it. So "-50Rr" would mean that this component's
          right-hand edge should be 50 pixels left of the parent's right-hand edge.
        - If the X or Y coordinate should be anchored at the component's centre, then it
          has "c" appended to it. So "-50Rc" would mean that this component's
          centre should be 50 pixels left of the parent's right-hand edge. "40%c" means that
          this component's centre should be placed 40% across the parent's width.
        - If it's a width or height that should use the parentSizeMinusAbsolute mode, then
          the number has "M" appended to it.

        To reload a stored string, use the constructor that takes a string parameter.
    */
    String toString() const
    {
        String s;
        s.preallocateBytes (32);
        addPosDescription (s, xMode, x);  s << ' ';
        addPosDescription (s, yMode, y);  s << ' ';
        addSizeDescription (s, wMode, w); s << ' ';
        addSizeDescription (s, hMode, h);
        return s;
    }

    //==============================================================================
    /** Calculates the absolute position, given the size of the space that
        it should go in.

        This will work out any proportional distances and sizes relative to the
        target rectangle, and will return the absolute position.

        @see applyToComponent
    */
    Rectangle<int> getRectangle (const Rectangle<int>& target) const noexcept
    {
        jassert (! target.isEmpty());

        double x_, y_, w_, h_;
        applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
        applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());
        return Rectangle<int> (roundToInt (x_), roundToInt (y_), roundToInt (w_), roundToInt (h_));
    }

    /** Same as getRectangle(), but returning the values as doubles rather than ints. */
    void getRectangleDouble (const Rectangle<int>& target,
                             double& x_, double& y_, double& w_, double& h_) const noexcept
    {
        jassert (! target.isEmpty());
        applyPosAndSize (x_, w_, x, w, xMode, wMode, target.getX(), target.getWidth());
        applyPosAndSize (y_, h_, y, h, yMode, hMode, target.getY(), target.getHeight());
    }

    /** This sets the bounds of the given component to this position.

        This is equivalent to writing:
        @code
        comp.setBounds (getRectangle (Rectangle<int> (0, 0, comp.getParentWidth(), comp.getParentHeight())));
        @endcode

        @see getRectangle, updateFromComponent
    */
    void applyToComponent (Component& comp) const noexcept
    {
        comp.setBounds (getRectangle (Rectangle<int> (comp.getParentWidth(), comp.getParentHeight())));
    }

    //==============================================================================
    /** Updates this object's coordinates to match the given rectangle.

        This will set all coordinates based on the given rectangle, re-calculating
        any proportional distances, and using the current anchor points.

        So for example if the x coordinate mode is currently proportional, this will
        re-calculate x based on the rectangle's relative position within the target
        rectangle's width.

        If the target rectangle's width or height are zero then it may not be possible
        to re-calculate some proportional coordinates. In this case, those coordinates
        will not be changed.
    */
    void updateFrom (const Rectangle<int>& newPosition,
                     const Rectangle<int>& targetSpaceToBeRelativeTo) noexcept
    {
        updatePosAndSize (x, w, newPosition.getX(), newPosition.getWidth(), xMode, wMode, targetSpaceToBeRelativeTo.getX(), targetSpaceToBeRelativeTo.getWidth());
        updatePosAndSize (y, h, newPosition.getY(), newPosition.getHeight(), yMode, hMode, targetSpaceToBeRelativeTo.getY(), targetSpaceToBeRelativeTo.getHeight());
    }

    /** Same functionality as updateFrom(), but taking doubles instead of ints.
    */
    void updateFromDouble (const double newX, const double newY,
                           const double newW, const double newH,
                           const Rectangle<int>& target) noexcept
    {
        updatePosAndSize (x, w, newX, newW, xMode, wMode, target.getX(), target.getWidth());
        updatePosAndSize (y, h, newY, newH, yMode, hMode, target.getY(), target.getHeight());
    }

    /** Updates this object's coordinates to match the bounds of this component.

        This is equivalent to calling updateFrom() with the component's bounds and
        it parent size.

        If the component doesn't currently have a parent, then proportional coordinates
        might not be updated because it would need to know the parent's size to do the
        maths for this.
    */
    void updateFromComponent (const Component& comp) noexcept
    {
        if (comp.getParentComponent() == nullptr && ! comp.isOnDesktop())
            updateFrom (comp.getBounds(), Rectangle<int>());
        else
            updateFrom (comp.getBounds(), Rectangle<int> (comp.getParentWidth(), comp.getParentHeight()));
    }

    //==============================================================================
    /** Specifies the point within the rectangle, relative to which it should be positioned. */
    enum AnchorPoint
    {
        anchorAtLeftOrTop              = 1 << 0,    /**< The x or y coordinate specifies where the left or top edge of the rectangle should be. */
        anchorAtRightOrBottom          = 1 << 1,    /**< The x or y coordinate specifies where the right or bottom edge of the rectangle should be. */
        anchorAtCentre                 = 1 << 2     /**< The x or y coordinate specifies where the centre of the rectangle should be. */
    };

    /** Specifies how an x or y coordinate should be interpreted. */
    enum PositionMode
    {
        absoluteFromParentTopLeft       = 1 << 3,   /**< The x or y coordinate specifies an absolute distance from the parent's top or left edge. */
        absoluteFromParentBottomRight   = 1 << 4,   /**< The x or y coordinate specifies an absolute distance from the parent's bottom or right edge. */
        absoluteFromParentCentre        = 1 << 5,   /**< The x or y coordinate specifies an absolute distance from the parent's centre. */
        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. */
    };

    /** Specifies how the width or height should be interpreted. */
    enum SizeMode
    {
        absoluteSize                    = 1 << 0,   /**< The width or height specifies an absolute size. */
        parentSizeMinusAbsolute         = 1 << 1,   /**< The width or height is an amount that should be subtracted from the parent's width or height. */
        proportionalSize                = 1 << 2,   /**< The width or height specifies a proportion of the parent's width or height. */
    };

    //==============================================================================
    /** Sets all options for all coordinates.

        This requires a reference rectangle to be specified, because if you're changing any
        of the modes from proportional to absolute or vice-versa, then it'll need to convert
        the coordinates, and will need to know the parent size so it can calculate this.
    */
    void setModes (const AnchorPoint xAnchor, const PositionMode xMode_,
                   const AnchorPoint yAnchor, const PositionMode yMode_,
                   const SizeMode widthMode, const SizeMode heightMode,
                   const Rectangle<int>& target) noexcept
    {
        if (xMode != ((int) xAnchor | (int) xMode_) || wMode != widthMode)
        {
            double tx, tw;
            applyPosAndSize (tx, tw, x, w, xMode, wMode, target.getX(), target.getWidth());

            xMode = (uint8) ((int) xAnchor | (int) xMode_);
            wMode = (uint8) widthMode;

            updatePosAndSize (x, w, tx, tw, xMode, wMode, target.getX(), target.getWidth());
        }

        if (yMode != ((int) yAnchor | (int) yMode_) || hMode != heightMode)
        {
            double ty, th;
            applyPosAndSize (ty, th, y, h, yMode, hMode, target.getY(), target.getHeight());

            yMode = (uint8) ((int) yAnchor | (int) yMode_);
            hMode = (uint8) heightMode;

            updatePosAndSize (y, h, ty, th, yMode, hMode, target.getY(), target.getHeight());
        }
    }

    /** Returns the anchoring mode for the x coordinate.
        To change any of the modes, use setModes().
    */
    AnchorPoint getAnchorPointX() const noexcept
    {
        return (AnchorPoint) (xMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
    }

    /** Returns the positioning mode for the x coordinate.
        To change any of the modes, use setModes().
    */
    PositionMode getPositionModeX() const noexcept
    {
        return (PositionMode) (xMode & (absoluteFromParentTopLeft | absoluteFromParentBottomRight
                                         | absoluteFromParentCentre | proportionOfParentSize));
    }

    /** Returns the raw x coordinate.

        If the x position mode is absolute, then this will be the absolute value. If it's
        proportional, then this will be a fractional proportion, where 1.0 means the full
        width of the parent space.
    */
    double getX() const noexcept                    { return x; }

    /** Sets the raw value of the x coordinate.
        See getX() for the meaning of this value.
    */
    void setX (const double newX) noexcept          { x = newX; }

    /** Returns the anchoring mode for the y coordinate.
        To change any of the modes, use setModes().
    */
    AnchorPoint getAnchorPointY() const noexcept
    {
        return (AnchorPoint) (yMode & (anchorAtLeftOrTop | anchorAtRightOrBottom | anchorAtCentre));
    }

    /** Returns the positioning mode for the y coordinate.
        To change any of the modes, use setModes().
    */
    PositionMode getPositionModeY() const noexcept
    {
        return (PositionMode) (yMode & (absoluteFromParentTopLeft | absoluteFromParentBottomRight
                                         | absoluteFromParentCentre | proportionOfParentSize));
    }

    /** Returns the raw y coordinate.

        If the y position mode is absolute, then this will be the absolute value. If it's
        proportional, then this will be a fractional proportion, where 1.0 means the full
        height of the parent space.
    */
    double getY() const noexcept                    { return y; }

    /** Sets the raw value of the y coordinate.
        See getY() for the meaning of this value.
    */
    void setY (const double newY) noexcept          { y = newY; }

    /** Returns the mode used to calculate the width.
        To change any of the modes, use setModes().
    */
    SizeMode getWidthMode() const noexcept          { return (SizeMode) wMode; }

    /** Returns the raw width value.

        If the width mode is absolute, then this will be the absolute value. If the mode is
        proportional, then this will be a fractional proportion, where 1.0 means the full
        width of the parent space.
    */
    double getWidth() const noexcept                { return w; }

    /** Sets the raw width value.

        See getWidth() for the details about what this value means.
    */
    void setWidth (const double newWidth) noexcept  { w = newWidth; }

    /** Returns the mode used to calculate the height.
        To change any of the modes, use setModes().
    */
    SizeMode getHeightMode() const noexcept         { return (SizeMode) hMode; }

    /** Returns the raw height value.

        If the height mode is absolute, then this will be the absolute value. If the mode is
        proportional, then this will be a fractional proportion, where 1.0 means the full
        height of the parent space.
    */
    double getHeight() const noexcept               { return h; }

    /** Sets the raw height value.

        See getHeight() for the details about what this value means.
    */
    void setHeight (const double newHeight) noexcept    { h = newHeight; }

    //==============================================================================
    /** If the size and position are constance, and wouldn't be affected by changes
        in the parent's size, then this will return true.
    */
    bool isPositionAbsolute() const noexcept
    {
        return (xMode & ~anchorAtLeftOrTop) == absoluteFromParentTopLeft
            && (yMode & ~anchorAtLeftOrTop) == absoluteFromParentTopLeft
            && wMode == absoluteSize
            && hMode == absoluteSize;
    }

    //==============================================================================
    /** Compares two objects. */
    bool operator== (const PositionedRectangle& other) const noexcept
    {
        return x == other.x && y == other.y
            && w == other.w && h == other.h
            && xMode == other.xMode && yMode == other.yMode
            && wMode == other.wMode && hMode == other.hMode;
    }

    /** Compares two objects. */
    bool operator!= (const PositionedRectangle& other) const noexcept
    {
        return ! operator== (other);
    }

private:
    //==============================================================================
    double x, y, w, h;
    uint8 xMode, yMode, wMode, hMode;

    void addPosDescription (String& s, const uint8 mode, const double value) const noexcept
    {
        if ((mode & proportionOfParentSize) != 0)
        {
            s << (roundToInt (value * 100000.0) / 1000.0) << '%';
        }
        else
        {
            s << (roundToInt (value * 100.0) / 100.0);

            if ((mode & absoluteFromParentBottomRight) != 0)
                s << 'R';
            else if ((mode & absoluteFromParentCentre) != 0)
                s << 'C';
        }

        if ((mode & anchorAtRightOrBottom) != 0)
            s << 'r';
        else if ((mode & anchorAtCentre) != 0)
            s << 'c';
    }

    void addSizeDescription (String& s, const uint8 mode, const double value) const noexcept
    {
        if (mode == proportionalSize)
            s << (roundToInt (value * 100000.0) / 1000.0) << '%';
        else if (mode == parentSizeMinusAbsolute)
            s << (roundToInt (value * 100.0) / 100.0) << 'M';
        else
            s << (roundToInt (value * 100.0) / 100.0);
    }

    void decodePosString (const String& s, uint8& mode, double& value) noexcept
    {
        if (s.containsChar ('r'))
            mode = anchorAtRightOrBottom;
        else if (s.containsChar ('c'))
            mode = anchorAtCentre;
        else
            mode = anchorAtLeftOrTop;

        if (s.containsChar ('%'))
        {
            mode |= proportionOfParentSize;
            value = s.removeCharacters ("%rcRC").getDoubleValue() / 100.0;
        }
        else
        {
            if (s.containsChar ('R'))
                mode |= absoluteFromParentBottomRight;
            else if (s.containsChar ('C'))
                mode |= absoluteFromParentCentre;
            else
                mode |= absoluteFromParentTopLeft;

            value = s.removeCharacters ("rcRC").getDoubleValue();
        }
    }

    void decodeSizeString (const String& s, uint8& mode, double& value) noexcept
    {
        if (s.containsChar ('%'))
        {
            mode = proportionalSize;
            value = s.upToFirstOccurrenceOf ("%", false, false).getDoubleValue() / 100.0;
        }
        else if (s.containsChar ('M'))
        {
            mode = parentSizeMinusAbsolute;
            value = s.getDoubleValue();
        }
        else
        {
            mode = absoluteSize;
            value = s.getDoubleValue();
        }
    }

    void applyPosAndSize (double& xOut, double& wOut, const double x_, const double w_,
                          const uint8 xMode_, const uint8 wMode_,
                          const int parentPos, const int parentSize) const noexcept
    {
        if (wMode_ == proportionalSize)
            wOut = roundToInt (w_ * parentSize);
        else if (wMode_ == parentSizeMinusAbsolute)
            wOut = jmax (0, parentSize - roundToInt (w_));
        else
            wOut = roundToInt (w_);

        if ((xMode_ & proportionOfParentSize) != 0)
            xOut = parentPos + x_ * parentSize;
        else if ((xMode_ & absoluteFromParentBottomRight) != 0)
            xOut = (parentPos + parentSize) - x_;
        else if ((xMode_ & absoluteFromParentCentre) != 0)
            xOut = x_ + (parentPos + parentSize / 2);
        else
            xOut = x_ + parentPos;

        if ((xMode_ & anchorAtRightOrBottom) != 0)
            xOut -= wOut;
        else if ((xMode_ & anchorAtCentre) != 0)
            xOut -= wOut / 2;
    }

    void updatePosAndSize (double& xOut, double& wOut, double x_, const double w_,
                           const uint8 xMode_, const uint8 wMode_,
                           const int parentPos, const int parentSize) const noexcept
    {
        if (wMode_ == proportionalSize)
        {
            if (parentSize > 0)
                wOut = w_ / parentSize;
        }
        else if (wMode_ == parentSizeMinusAbsolute)
            wOut = parentSize - w_;
        else
            wOut = w_;

        if ((xMode_ & anchorAtRightOrBottom) != 0)
            x_ += w_;
        else if ((xMode_ & anchorAtCentre) != 0)
            x_ += w_ / 2;

        if ((xMode_ & proportionOfParentSize) != 0)
        {
            if (parentSize > 0)
                xOut = (x_ - parentPos) / parentSize;
        }
        else if ((xMode_ & absoluteFromParentBottomRight) != 0)
            xOut = (parentPos + parentSize) - x_;
        else if ((xMode_ & absoluteFromParentCentre) != 0)
            xOut = x_ - (parentPos + parentSize / 2);
        else
            xOut = x_ - parentPos;
    }
};

//==============================================================================
struct RelativePositionedRectangle
{
    //==============================================================================
    RelativePositionedRectangle()
        : relativeToX (0),
          relativeToY (0),
          relativeToW (0),
          relativeToH (0)
    {
    }

    RelativePositionedRectangle (const RelativePositionedRectangle& other)
        : rect (other.rect),
          relativeToX (other.relativeToX),
          relativeToY (other.relativeToY),
          relativeToW (other.relativeToW),
          relativeToH (other.relativeToH)
    {
    }

    RelativePositionedRectangle& operator= (const RelativePositionedRectangle& other)
    {
        rect = other.rect;
        relativeToX = other.relativeToX;
        relativeToY = other.relativeToY;
        relativeToW = other.relativeToW;
        relativeToH = other.relativeToH;
        return *this;
    }

    //==============================================================================
    bool operator== (const RelativePositionedRectangle& other) const noexcept
    {
        return rect == other.rect
            && relativeToX == other.relativeToX
            && relativeToY == other.relativeToY
            && relativeToW == other.relativeToW
            && relativeToH == other.relativeToH;
    }

    bool operator!= (const RelativePositionedRectangle& other) const noexcept
    {
        return ! operator== (other);
    }

    template <typename LayoutType>
    void getRelativeTargetBounds (const Rectangle<int>& parentArea,
                                  const LayoutType* layout,
                                  int& x, int& xw, int& y, int& yh,
                                  int& w, int& h) const
    {
        Component* rx = {};
        Component* ry = {};
        Component* rw = {};
        Component* rh = {};

        if (layout != nullptr)
        {
            rx = layout->findComponentWithId (relativeToX);
            ry = layout->findComponentWithId (relativeToY);
            rw = layout->findComponentWithId (relativeToW);
            rh = layout->findComponentWithId (relativeToH);
        }

        x = parentArea.getX() + (rx != nullptr ? rx->getX() : 0);
        y = parentArea.getY() + (ry != nullptr ? ry->getY() : 0);
        w = rw != nullptr ? rw->getWidth() : parentArea.getWidth();
        h = rh != nullptr ? rh->getHeight() : parentArea.getHeight();
        xw = rx != nullptr ? rx->getWidth() : parentArea.getWidth();
        yh = ry != nullptr ? ry->getHeight() : parentArea.getHeight();
    }

    Rectangle<int> getRectangle (const Rectangle<int>& parentArea,
                                 const ComponentLayout* layout) const
    {
        int x, xw, y, yh, w, h;
        getRelativeTargetBounds (parentArea, layout, x, xw, y, yh, w, h);

        const Rectangle<int> xyRect ((xw <= 0 || yh <= 0) ? Rectangle<int>()
                                                          : rect.getRectangle (Rectangle<int> (x, y, xw, yh)));

        const Rectangle<int> whRect ((w <= 0 || h <= 0) ? Rectangle<int>()
                                                        : rect.getRectangle (Rectangle<int> (x, y, w, h)));

        return Rectangle<int> (xyRect.getX(), xyRect.getY(),
                               whRect.getWidth(), whRect.getHeight());
    }

    void getRectangleDouble (double& x, double& y, double& w, double& h,
                             const Rectangle<int>& parentArea,
                             const ComponentLayout* layout) const
    {
        int rx, rxw, ry, ryh, rw, rh;
        getRelativeTargetBounds (parentArea, layout, rx, rxw, ry, ryh, rw, rh);

        double dummy1, dummy2;
        rect.getRectangleDouble (Rectangle<int> (rx, ry, rxw, ryh), x, y, dummy1, dummy2);
        rect.getRectangleDouble (Rectangle<int> (rx, ry, rw, rh), dummy1, dummy2, w, h);
    }

    void updateFromComponent (const Component& comp, const ComponentLayout* layout)
    {
        int x, xw, y, yh, w, h;
        getRelativeTargetBounds (Rectangle<int> (0, 0, comp.getParentWidth(), comp.getParentHeight()),
                                 layout, x, xw, y, yh, w, h);

        PositionedRectangle xyRect (rect), whRect (rect);
        xyRect.updateFrom (comp.getBounds(), Rectangle<int> (x, y, xw, yh));
        whRect.updateFrom (comp.getBounds(), Rectangle<int> (x, y, w, h));

        rect.setX (xyRect.getX());
        rect.setY (xyRect.getY());
        rect.setWidth (whRect.getWidth());
        rect.setHeight (whRect.getHeight());
    }

    void updateFrom (double newX, double newY, double newW, double newH,
                     const Rectangle<int>& parentArea, const ComponentLayout* layout)
    {
        int x, xw, y, yh, w, h;
        getRelativeTargetBounds (parentArea, layout, x, xw, y, yh, w, h);

        PositionedRectangle xyRect (rect), whRect (rect);
        xyRect.updateFromDouble (newX, newY, newW, newH, Rectangle<int> (x, y, xw, yh));
        whRect.updateFromDouble (newX, newY, newW, newH, Rectangle<int> (x, y, w, h));

        rect.setX (xyRect.getX());
        rect.setY (xyRect.getY());
        rect.setWidth (whRect.getWidth());
        rect.setHeight (whRect.getHeight());
    }

    void applyToXml (XmlElement& e) const
    {
        e.setAttribute ("pos", rect.toString());

        if (relativeToX != 0)   e.setAttribute ("posRelativeX", String::toHexString (relativeToX));
        if (relativeToY != 0)   e.setAttribute ("posRelativeY", String::toHexString (relativeToY));
        if (relativeToW != 0)   e.setAttribute ("posRelativeW", String::toHexString (relativeToW));
        if (relativeToH != 0)   e.setAttribute ("posRelativeH", String::toHexString (relativeToH));
    }

    void restoreFromXml (const XmlElement& e, const RelativePositionedRectangle& defaultPos)
    {
        rect = PositionedRectangle (e.getStringAttribute ("pos", defaultPos.rect.toString()));
        relativeToX = e.getStringAttribute ("posRelativeX", String::toHexString (defaultPos.relativeToX)).getHexValue64();
        relativeToY = e.getStringAttribute ("posRelativeY", String::toHexString (defaultPos.relativeToY)).getHexValue64();
        relativeToW = e.getStringAttribute ("posRelativeW", String::toHexString (defaultPos.relativeToW)).getHexValue64();
        relativeToH = e.getStringAttribute ("posRelativeH", String::toHexString (defaultPos.relativeToH)).getHexValue64();
    }

    String toString() const
    {
        StringArray toks;
        toks.addTokens (rect.toString(), false);

        return toks[0] + " " + toks[1];
    }

    Point<float> toXY (const Rectangle<int>& parentArea,
                       const ComponentLayout* layout) const
    {
        double x, y, w, h;
        getRectangleDouble (x, y, w, h, parentArea, layout);
        return { (float) x, (float) y };
    }

    void getXY (double& x, double& y,
                const Rectangle<int>& parentArea,
                const ComponentLayout* layout) const
    {
        double w, h;
        getRectangleDouble (x, y, w, h, parentArea, layout);
    }

    //==============================================================================
    PositionedRectangle rect;
    int64 relativeToX;
    int64 relativeToY;
    int64 relativeToW;
    int64 relativeToH;
};