2017-10-27 23:10:06 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 11:12:37 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#ifndef MOZILLA_GFX_BASEMARGIN_H_
|
|
|
|
#define MOZILLA_GFX_BASEMARGIN_H_
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2016-06-03 21:23:40 +00:00
|
|
|
#include <ostream>
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#include "Types.h"
|
2011-05-26 19:41:33 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-06-28 10:13:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sides represents a set of physical sides.
|
|
|
|
*/
|
2015-03-21 16:28:04 +00:00
|
|
|
struct Sides final {
|
2014-06-28 10:13:13 +00:00
|
|
|
Sides() : mBits(0) {}
|
|
|
|
explicit Sides(SideBits aSideBits) {
|
|
|
|
MOZ_ASSERT((aSideBits & ~eSideBitsAll) == 0, "illegal side bits");
|
|
|
|
mBits = aSideBits;
|
|
|
|
}
|
|
|
|
bool IsEmpty() const { return mBits == 0; }
|
2016-02-03 18:44:41 +00:00
|
|
|
bool Top() const { return (mBits & eSideBitsTop) != 0; }
|
|
|
|
bool Right() const { return (mBits & eSideBitsRight) != 0; }
|
|
|
|
bool Bottom() const { return (mBits & eSideBitsBottom) != 0; }
|
|
|
|
bool Left() const { return (mBits & eSideBitsLeft) != 0; }
|
2014-06-28 10:13:13 +00:00
|
|
|
bool Contains(SideBits aSideBits) const {
|
|
|
|
MOZ_ASSERT((aSideBits & ~eSideBitsAll) == 0, "illegal side bits");
|
|
|
|
return (mBits & aSideBits) == aSideBits;
|
|
|
|
}
|
|
|
|
Sides operator|(Sides aOther) const {
|
|
|
|
return Sides(SideBits(mBits | aOther.mBits));
|
|
|
|
}
|
|
|
|
Sides operator|(SideBits aSideBits) const { return *this | Sides(aSideBits); }
|
|
|
|
Sides& operator|=(Sides aOther) {
|
|
|
|
mBits |= aOther.mBits;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Sides& operator|=(SideBits aSideBits) { return *this |= Sides(aSideBits); }
|
|
|
|
bool operator==(Sides aOther) const { return mBits == aOther.mBits; }
|
|
|
|
bool operator!=(Sides aOther) const { return !(*this == aOther); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t mBits;
|
|
|
|
};
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
namespace gfx {
|
2011-05-26 19:41:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not use this class directly. Subclass it, pass that subclass as the
|
|
|
|
* Sub parameter, and only use that subclass.
|
|
|
|
*/
|
|
|
|
template <class T, class Sub>
|
|
|
|
struct BaseMargin {
|
2014-06-28 10:13:13 +00:00
|
|
|
typedef mozilla::Side SideT; // because we have a method named Side
|
2011-05-26 19:41:33 +00:00
|
|
|
|
|
|
|
// Do not change the layout of these members; the Side() methods below
|
|
|
|
// depend on this order.
|
|
|
|
T top, right, bottom, left;
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
BaseMargin() : top(0), right(0), bottom(0), left(0) {}
|
2013-03-06 08:05:55 +00:00
|
|
|
BaseMargin(T aTop, T aRight, T aBottom, T aLeft)
|
2011-05-26 19:41:33 +00:00
|
|
|
: top(aTop), right(aRight), bottom(aBottom), left(aLeft) {}
|
|
|
|
|
2013-03-06 08:05:55 +00:00
|
|
|
void SizeTo(T aTop, T aRight, T aBottom, T aLeft) {
|
|
|
|
top = aTop;
|
|
|
|
right = aRight;
|
|
|
|
bottom = aBottom;
|
|
|
|
left = aLeft;
|
2011-05-26 19:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T LeftRight() const { return left + right; }
|
|
|
|
T TopBottom() const { return top + bottom; }
|
|
|
|
|
|
|
|
T& Side(SideT aSide) {
|
|
|
|
// This is ugly!
|
2015-04-20 13:35:00 +00:00
|
|
|
return *(&top + int(aSide));
|
2011-05-26 19:41:33 +00:00
|
|
|
}
|
|
|
|
T Side(SideT aSide) const {
|
|
|
|
// This is ugly!
|
2015-04-20 13:35:00 +00:00
|
|
|
return *(&top + int(aSide));
|
2011-05-26 19:41:33 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 10:13:13 +00:00
|
|
|
void ApplySkipSides(Sides aSkipSides) {
|
|
|
|
if (aSkipSides.Top()) {
|
2014-06-24 17:52:19 +00:00
|
|
|
top = 0;
|
|
|
|
}
|
2014-06-28 10:13:13 +00:00
|
|
|
if (aSkipSides.Right()) {
|
2014-06-24 17:52:19 +00:00
|
|
|
right = 0;
|
|
|
|
}
|
2014-06-28 10:13:13 +00:00
|
|
|
if (aSkipSides.Bottom()) {
|
2014-06-24 17:52:19 +00:00
|
|
|
bottom = 0;
|
|
|
|
}
|
2014-06-28 10:13:13 +00:00
|
|
|
if (aSkipSides.Left()) {
|
2014-06-24 17:52:19 +00:00
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 19:41:33 +00:00
|
|
|
// Overloaded operators. Note that '=' isn't defined so we'll get the
|
|
|
|
// compiler generated default assignment operator
|
|
|
|
bool operator==(const Sub& aMargin) const {
|
2013-03-06 08:05:55 +00:00
|
|
|
return top == aMargin.top && right == aMargin.right &&
|
|
|
|
bottom == aMargin.bottom && left == aMargin.left;
|
2011-05-26 19:41:33 +00:00
|
|
|
}
|
|
|
|
bool operator!=(const Sub& aMargin) const { return !(*this == aMargin); }
|
|
|
|
Sub operator+(const Sub& aMargin) const {
|
2013-03-06 08:05:55 +00:00
|
|
|
return Sub(top + aMargin.top, right + aMargin.right,
|
|
|
|
bottom + aMargin.bottom, left + aMargin.left);
|
2011-05-26 19:41:33 +00:00
|
|
|
}
|
|
|
|
Sub operator-(const Sub& aMargin) const {
|
2013-03-06 08:05:55 +00:00
|
|
|
return Sub(top - aMargin.top, right - aMargin.right,
|
|
|
|
bottom - aMargin.bottom, left - aMargin.left);
|
2011-05-26 19:41:33 +00:00
|
|
|
}
|
|
|
|
Sub& operator+=(const Sub& aMargin) {
|
|
|
|
top += aMargin.top;
|
|
|
|
right += aMargin.right;
|
|
|
|
bottom += aMargin.bottom;
|
2013-03-06 08:05:55 +00:00
|
|
|
left += aMargin.left;
|
2011-05-26 19:41:33 +00:00
|
|
|
return *static_cast<Sub*>(this);
|
|
|
|
}
|
2016-06-03 21:23:40 +00:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const BaseMargin& aMargin) {
|
|
|
|
return aStream << '(' << aMargin.top << ',' << aMargin.right << ','
|
|
|
|
<< aMargin.bottom << ',' << aMargin.left << ')';
|
|
|
|
}
|
2011-05-26 19:41:33 +00:00
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2011-05-26 19:41:33 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
#endif /* MOZILLA_GFX_BASEMARGIN_H_ */
|