Back to index

lightning-sunbird  0.9+nobinonly
nsMargin.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef NSMARGIN_H
00039 #define NSMARGIN_H
00040 
00041 #include "nsCoord.h"
00042 #include "gfxCore.h"
00043 
00044 struct nsMargin {
00045   nscoord top, right, bottom, left;
00046 
00047   // Constructors
00048   nsMargin() {}
00049   nsMargin(const nsMargin& aMargin) {*this = aMargin;}
00050   nsMargin(nscoord aLeft,  nscoord aTop,
00051            nscoord aRight, nscoord aBottom) {left = aLeft; top = aTop;
00052                                              right = aRight; bottom = aBottom;}
00053 
00054   void SizeTo(nscoord aLeft,  nscoord aTop,
00055               nscoord aRight, nscoord aBottom) {left = aLeft; top = aTop;
00056                                                 right = aRight; bottom = aBottom;}
00057   void SizeBy(nscoord aLeft, nscoord  aTop,
00058               nscoord aRight, nscoord aBottom) {left += aLeft; top += aTop;
00059                                                 right += aRight; bottom += aBottom;}
00060 
00061   nscoord LeftRight() const { return left + right; }
00062   nscoord TopBottom() const { return top + bottom; }
00063 
00064 #if (NS_SIDE_TOP == 0) && (NS_SIDE_RIGHT == 1) && (NS_SIDE_BOTTOM == 2) && (NS_SIDE_LEFT == 3)
00065   nscoord& side(PRUint8 aSide) {
00066     NS_PRECONDITION(aSide <= NS_SIDE_LEFT, "Out of range side");
00067     return *(&top + aSide);
00068   }    
00069 
00070   nscoord side(PRUint8 aSide) const {
00071     NS_PRECONDITION(aSide <= NS_SIDE_LEFT, "Out of range side");
00072     return *(&top + aSide);
00073   }    
00074 #else
00075 #error "Somebody changed the side constants."
00076 #endif
00077 
00078   // Overloaded operators. Note that '=' isn't defined so we'll get the
00079   // compiler generated default assignment operator
00080   PRBool operator==(const nsMargin& aMargin) const {
00081     return (PRBool) ((left == aMargin.left) && (top == aMargin.top) &&
00082                      (right == aMargin.right) && (bottom == aMargin.bottom));
00083   }
00084   PRBool operator!=(const nsMargin& aMargin) const {
00085     return (PRBool) ((left != aMargin.left) || (top != aMargin.top) ||
00086                      (right != aMargin.right) || (bottom != aMargin.bottom));
00087   }
00088   nsMargin operator+(const nsMargin& aMargin) const {
00089     return nsMargin(left + aMargin.left, top + aMargin.top,
00090                     right + aMargin.right, bottom + aMargin.bottom);
00091   }
00092   nsMargin operator-(const nsMargin& aMargin) const {
00093     return nsMargin(left - aMargin.left, top - aMargin.top,
00094                     right - aMargin.right, bottom - aMargin.bottom);
00095   }
00096   nsMargin& operator+=(const nsMargin& aMargin) {left += aMargin.left;
00097                                                  top += aMargin.top;
00098                                                  right += aMargin.right;
00099                                                  bottom += aMargin.bottom;
00100                                                  return *this;}
00101   nsMargin& operator-=(const nsMargin& aMargin) {left -= aMargin.left;
00102                                                  top -= aMargin.top;
00103                                                  right -= aMargin.right;
00104                                                  bottom -= aMargin.bottom;
00105                                                  return *this;}
00106 };
00107 
00108 #ifdef NS_COORD_IS_FLOAT
00109 struct nsIntMargin {
00110   PRInt32 top, right, bottom, left;
00111 
00112   // Constructors
00113   nsIntMargin() {}
00114   nsIntMargin(const nsIntMargin& aMargin) {*this = aMargin;}
00115   nsIntMargin(PRInt32 aLeft,  PRInt32 aTop,
00116               PRInt32 aRight, PRInt32 aBottom) {left = aLeft; top = aTop;
00117                                                 right = aRight; bottom = aBottom;}
00118 };
00119 #else
00120 typedef nsMargin nsIntMargin;
00121 #endif
00122 
00123 #endif /* NSMARGIN_H */