michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_BASEMARGIN_H_ michael@0: #define MOZILLA_GFX_BASEMARGIN_H_ michael@0: michael@0: #include "Types.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: /** michael@0: * Do not use this class directly. Subclass it, pass that subclass as the michael@0: * Sub parameter, and only use that subclass. michael@0: */ michael@0: template michael@0: struct BaseMargin { michael@0: typedef mozilla::css::Side SideT; michael@0: michael@0: // Do not change the layout of these members; the Side() methods below michael@0: // depend on this order. michael@0: T top, right, bottom, left; michael@0: michael@0: // Constructors michael@0: BaseMargin() : top(0), right(0), bottom(0), left(0) {} michael@0: BaseMargin(T aTop, T aRight, T aBottom, T aLeft) : michael@0: top(aTop), right(aRight), bottom(aBottom), left(aLeft) {} michael@0: michael@0: void SizeTo(T aTop, T aRight, T aBottom, T aLeft) michael@0: { michael@0: top = aTop; right = aRight; bottom = aBottom; left = aLeft; michael@0: } michael@0: michael@0: T LeftRight() const { return left + right; } michael@0: T TopBottom() const { return top + bottom; } michael@0: michael@0: T& Side(SideT aSide) { michael@0: // This is ugly! michael@0: return *(&top + T(aSide)); michael@0: } michael@0: T Side(SideT aSide) const { michael@0: // This is ugly! michael@0: return *(&top + T(aSide)); michael@0: } michael@0: michael@0: // Overloaded operators. Note that '=' isn't defined so we'll get the michael@0: // compiler generated default assignment operator michael@0: bool operator==(const Sub& aMargin) const { michael@0: return top == aMargin.top && right == aMargin.right && michael@0: bottom == aMargin.bottom && left == aMargin.left; michael@0: } michael@0: bool operator!=(const Sub& aMargin) const { michael@0: return !(*this == aMargin); michael@0: } michael@0: Sub operator+(const Sub& aMargin) const { michael@0: return Sub(top + aMargin.top, right + aMargin.right, michael@0: bottom + aMargin.bottom, left + aMargin.left); michael@0: } michael@0: Sub operator-(const Sub& aMargin) const { michael@0: return Sub(top - aMargin.top, right - aMargin.right, michael@0: bottom - aMargin.bottom, left - aMargin.left); michael@0: } michael@0: Sub& operator+=(const Sub& aMargin) { michael@0: top += aMargin.top; michael@0: right += aMargin.right; michael@0: bottom += aMargin.bottom; michael@0: left += aMargin.left; michael@0: return *static_cast(this); michael@0: } michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /* MOZILLA_GFX_BASEMARGIN_H_ */