1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef MOZILLA_GFX_BASEMARGIN_H_
8 #define MOZILLA_GFX_BASEMARGIN_H_
9 
10 #include <ostream>
11 
12 #include "Types.h"
13 
14 namespace mozilla {
15 
16 /**
17  * Sides represents a set of physical sides.
18  */
19 struct Sides final {
Sidesfinal20   Sides() : mBits(SideBits::eNone) {}
Sidesfinal21   explicit Sides(SideBits aSideBits) {
22     MOZ_ASSERT((aSideBits & ~SideBits::eAll) == SideBits::eNone,
23                "illegal side bits");
24     mBits = aSideBits;
25   }
IsEmptyfinal26   bool IsEmpty() const { return mBits == SideBits::eNone; }
Topfinal27   bool Top() const { return (mBits & SideBits::eTop) == SideBits::eTop; }
Rightfinal28   bool Right() const { return (mBits & SideBits::eRight) == SideBits::eRight; }
Bottomfinal29   bool Bottom() const {
30     return (mBits & SideBits::eBottom) == SideBits::eBottom;
31   }
Leftfinal32   bool Left() const { return (mBits & SideBits::eLeft) == SideBits::eLeft; }
Containsfinal33   bool Contains(SideBits aSideBits) const {
34     MOZ_ASSERT(!(aSideBits & ~SideBits::eAll), "illegal side bits");
35     return (mBits & aSideBits) == aSideBits;
36   }
37   Sides operator|(Sides aOther) const {
38     return Sides(SideBits(mBits | aOther.mBits));
39   }
40   Sides operator|(SideBits aSideBits) const { return *this | Sides(aSideBits); }
41   Sides& operator|=(Sides aOther) {
42     mBits |= aOther.mBits;
43     return *this;
44   }
45   Sides& operator|=(SideBits aSideBits) { return *this |= Sides(aSideBits); }
46   bool operator==(Sides aOther) const { return mBits == aOther.mBits; }
47   bool operator!=(Sides aOther) const { return !(*this == aOther); }
48 
49  private:
50   SideBits mBits;
51 };
52 
53 namespace gfx {
54 
55 /**
56  * Do not use this class directly. Subclass it, pass that subclass as the
57  * Sub parameter, and only use that subclass.
58  */
59 template <class T, class Sub>
60 struct BaseMargin {
61   typedef mozilla::Side SideT;  // because we have a method named Side
62 
63   // Do not change the layout of these members; the Side() methods below
64   // depend on this order.
65   T top, right, bottom, left;
66 
67   // Constructors
BaseMarginBaseMargin68   BaseMargin() : top(0), right(0), bottom(0), left(0) {}
BaseMarginBaseMargin69   BaseMargin(T aTop, T aRight, T aBottom, T aLeft)
70       : top(aTop), right(aRight), bottom(aBottom), left(aLeft) {}
71 
SizeToBaseMargin72   void SizeTo(T aTop, T aRight, T aBottom, T aLeft) {
73     top = aTop;
74     right = aRight;
75     bottom = aBottom;
76     left = aLeft;
77   }
78 
LeftRightBaseMargin79   T LeftRight() const { return left + right; }
TopBottomBaseMargin80   T TopBottom() const { return top + bottom; }
81 
SideBaseMargin82   T& Side(SideT aSide) {
83     // This is ugly!
84     return *(&top + int(aSide));
85   }
SideBaseMargin86   T Side(SideT aSide) const {
87     // This is ugly!
88     return *(&top + int(aSide));
89   }
90 
ApplySkipSidesBaseMargin91   Sub& ApplySkipSides(Sides aSkipSides) {
92     if (aSkipSides.Top()) {
93       top = 0;
94     }
95     if (aSkipSides.Right()) {
96       right = 0;
97     }
98     if (aSkipSides.Bottom()) {
99       bottom = 0;
100     }
101     if (aSkipSides.Left()) {
102       left = 0;
103     }
104     return *static_cast<Sub*>(this);
105   }
106 
107   // Overloaded operators. Note that '=' isn't defined so we'll get the
108   // compiler generated default assignment operator
109   bool operator==(const Sub& aMargin) const {
110     return top == aMargin.top && right == aMargin.right &&
111            bottom == aMargin.bottom && left == aMargin.left;
112   }
113   bool operator!=(const Sub& aMargin) const { return !(*this == aMargin); }
114   Sub operator+(const Sub& aMargin) const {
115     return Sub(top + aMargin.top, right + aMargin.right,
116                bottom + aMargin.bottom, left + aMargin.left);
117   }
118   Sub operator-(const Sub& aMargin) const {
119     return Sub(top - aMargin.top, right - aMargin.right,
120                bottom - aMargin.bottom, left - aMargin.left);
121   }
122   Sub& operator+=(const Sub& aMargin) {
123     top += aMargin.top;
124     right += aMargin.right;
125     bottom += aMargin.bottom;
126     left += aMargin.left;
127     return *static_cast<Sub*>(this);
128   }
129 
130   friend std::ostream& operator<<(std::ostream& aStream,
131                                   const BaseMargin& aMargin) {
132     return aStream << '(' << aMargin.top << ',' << aMargin.right << ','
133                    << aMargin.bottom << ',' << aMargin.left << ')';
134   }
135 };
136 
137 }  // namespace gfx
138 }  // namespace mozilla
139 
140 #endif /* MOZILLA_GFX_BASEMARGIN_H_ */
141