2011-05-26 23:41:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#ifndef MOZILLA_GFX_BASEMARGIN_H_
|
|
|
|
#define MOZILLA_GFX_BASEMARGIN_H_
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2016-06-04 00:23:40 +03:00
|
|
|
#include <ostream>
|
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#include "Types.h"
|
2011-05-26 23:41:33 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-06-28 14:13:13 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sides represents a set of physical sides.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
struct Sides final {
|
2014-06-28 14:13:13 +04: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 21:44:41 +03: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 14:13:13 +04: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 21:41:16 +04:00
|
|
|
namespace gfx {
|
2011-05-26 23:41:33 +04: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 14:13:13 +04:00
|
|
|
typedef mozilla::Side SideT; // because we have a method named Side
|
2011-05-26 23:41:33 +04: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 12:05:55 +04:00
|
|
|
BaseMargin(T aTop, T aRight, T aBottom, T aLeft) :
|
2011-05-26 23:41:33 +04:00
|
|
|
top(aTop), right(aRight), bottom(aBottom), left(aLeft) {}
|
|
|
|
|
2013-03-06 12:05:55 +04:00
|
|
|
void SizeTo(T aTop, T aRight, T aBottom, T aLeft)
|
2011-05-26 23:41:33 +04:00
|
|
|
{
|
2013-03-06 12:05:55 +04:00
|
|
|
top = aTop; right = aRight; bottom = aBottom; left = aLeft;
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
T LeftRight() const { return left + right; }
|
|
|
|
T TopBottom() const { return top + bottom; }
|
|
|
|
|
|
|
|
T& Side(SideT aSide) {
|
|
|
|
// This is ugly!
|
2015-04-20 16:35:00 +03:00
|
|
|
return *(&top + int(aSide));
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
T Side(SideT aSide) const {
|
|
|
|
// This is ugly!
|
2015-04-20 16:35:00 +03:00
|
|
|
return *(&top + int(aSide));
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:13:13 +04:00
|
|
|
void ApplySkipSides(Sides aSkipSides)
|
2014-06-24 21:52:19 +04:00
|
|
|
{
|
2014-06-28 14:13:13 +04:00
|
|
|
if (aSkipSides.Top()) {
|
2014-06-24 21:52:19 +04:00
|
|
|
top = 0;
|
|
|
|
}
|
2014-06-28 14:13:13 +04:00
|
|
|
if (aSkipSides.Right()) {
|
2014-06-24 21:52:19 +04:00
|
|
|
right = 0;
|
|
|
|
}
|
2014-06-28 14:13:13 +04:00
|
|
|
if (aSkipSides.Bottom()) {
|
2014-06-24 21:52:19 +04:00
|
|
|
bottom = 0;
|
|
|
|
}
|
2014-06-28 14:13:13 +04:00
|
|
|
if (aSkipSides.Left()) {
|
2014-06-24 21:52:19 +04:00
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 23:41:33 +04: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 12:05:55 +04:00
|
|
|
return top == aMargin.top && right == aMargin.right &&
|
|
|
|
bottom == aMargin.bottom && left == aMargin.left;
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
bool operator!=(const Sub& aMargin) const {
|
|
|
|
return !(*this == aMargin);
|
|
|
|
}
|
|
|
|
Sub operator+(const Sub& aMargin) const {
|
2013-03-06 12:05:55 +04:00
|
|
|
return Sub(top + aMargin.top, right + aMargin.right,
|
|
|
|
bottom + aMargin.bottom, left + aMargin.left);
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
Sub operator-(const Sub& aMargin) const {
|
2013-03-06 12:05:55 +04:00
|
|
|
return Sub(top - aMargin.top, right - aMargin.right,
|
|
|
|
bottom - aMargin.bottom, left - aMargin.left);
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
Sub& operator+=(const Sub& aMargin) {
|
|
|
|
top += aMargin.top;
|
|
|
|
right += aMargin.right;
|
|
|
|
bottom += aMargin.bottom;
|
2013-03-06 12:05:55 +04:00
|
|
|
left += aMargin.left;
|
2011-05-26 23:41:33 +04:00
|
|
|
return *static_cast<Sub*>(this);
|
|
|
|
}
|
2016-06-04 00:23:40 +03:00
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& aStream,
|
|
|
|
const BaseMargin& aMargin) {
|
|
|
|
return aStream << '(' << aMargin.top << ',' << aMargin.right << ','
|
|
|
|
<< aMargin.bottom << ',' << aMargin.left << ')';
|
|
|
|
}
|
2011-05-26 23:41:33 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#endif /* MOZILLA_GFX_BASEMARGIN_H_ */
|