Back to index

lightning-sunbird  0.9+nobinonly
nsRect.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 
00039 #ifndef NSRECT_H
00040 #define NSRECT_H
00041 
00042 #include <stdio.h>
00043 #include "nsCoord.h"
00044 #include "nsPoint.h"
00045 #include "nsSize.h"
00046 #include "nsMargin.h"
00047 #include "nsUnitConversion.h"
00048 #include "gfxCore.h"
00049 
00050 struct NS_GFX nsRect {
00051   nscoord x, y;
00052   nscoord width, height;
00053 
00054   // Constructors
00055   nsRect() : x(0), y(0), width(0), height(0) {}
00056   nsRect(const nsRect& aRect) {*this = aRect;}
00057   nsRect(const nsPoint& aOrigin, const nsSize &aSize) {x = aOrigin.x; y = aOrigin.y;
00058                                                        width = aSize.width; height = aSize.height;}
00059   nsRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) {
00060     x = aX; y = aY; width = aWidth; height = aHeight;
00061     VERIFY_COORD(x); VERIFY_COORD(y); VERIFY_COORD(width); VERIFY_COORD(height);
00062   }
00063 
00064   // Emptiness. An empty rect is one that has no area, i.e. its height or width
00065   // is <= 0
00066   PRBool IsEmpty() const {
00067     return (PRBool) ((height <= 0) || (width <= 0));
00068   }
00069   void   Empty() {width = height = 0;}
00070 
00071   // Containment
00072   PRBool Contains(const nsRect& aRect) const;
00073   PRBool Contains(nscoord aX, nscoord aY) const;
00074   PRBool Contains(const nsPoint& aPoint) const {return Contains(aPoint.x, aPoint.y);}
00075 
00076   // Intersection. Returns TRUE if the receiver overlaps aRect and
00077   // FALSE otherwise
00078   PRBool Intersects(const nsRect& aRect) const;
00079 
00080   // Computes the area in which aRect1 and aRect2 overlap, and fills 'this' with
00081   // the result. Returns FALSE if the rectangles don't intersect, and sets 'this'
00082   // rect to be an empty rect.
00083   //
00084   // 'this' can be the same object as either aRect1 or aRect2
00085   PRBool IntersectRect(const nsRect& aRect1, const nsRect& aRect2);
00086 
00087   // Computes the smallest rectangle that contains both aRect1 and aRect2 and
00088   // fills 'this' with the result. Returns FALSE and sets 'this' rect to be an
00089   // empty rect if both aRect1 and aRect2 are empty
00090   //
00091   // 'this' can be the same object as either aRect1 or aRect2
00092   PRBool UnionRect(const nsRect& aRect1, const nsRect& aRect2);
00093 
00094   // Accessors
00095   void SetRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) {
00096     x = aX; y = aY; width = aWidth; height = aHeight;
00097   }
00098   void MoveTo(nscoord aX, nscoord aY) {x = aX; y = aY;}
00099   void MoveTo(const nsPoint& aPoint) {x = aPoint.x; y = aPoint.y;}
00100   void MoveBy(nscoord aDx, nscoord aDy) {x += aDx; y += aDy;}
00101   void MoveBy(const nsPoint& aPoint) {x += aPoint.x; y += aPoint.y;}
00102   void SizeTo(nscoord aWidth, nscoord aHeight) {width = aWidth; height = aHeight;}
00103   void SizeTo(const nsSize& aSize) {SizeTo(aSize.width, aSize.height);}
00104   void SizeBy(nscoord aDeltaWidth, nscoord aDeltaHeight) {width += aDeltaWidth;
00105                                                           height += aDeltaHeight;}
00106 
00107   // Inflate the rect by the specified width/height or margin
00108   void Inflate(nscoord aDx, nscoord aDy);
00109   void Inflate(const nsSize& aSize) {Inflate(aSize.width, aSize.height);}
00110   void Inflate(const nsMargin& aMargin);
00111 
00112   // Deflate the rect by the specified width/height or margin
00113   void Deflate(nscoord aDx, nscoord aDy);
00114   void Deflate(const nsSize& aSize) {Deflate(aSize.width, aSize.height);}
00115   void Deflate(const nsMargin& aMargin);
00116 
00117   // Overloaded operators. Note that '=' isn't defined so we'll get the
00118   // compiler generated default assignment operator.
00119   PRBool  operator==(const nsRect& aRect) const {
00120     return (PRBool) ((IsEmpty() && aRect.IsEmpty()) ||
00121                      ((x == aRect.x) && (y == aRect.y) &&
00122                       (width == aRect.width) && (height == aRect.height)));
00123   }
00124   PRBool  operator!=(const nsRect& aRect) const {
00125     return (PRBool) !operator==(aRect);
00126   }
00127 
00128   nsRect  operator+(const nsPoint& aPoint) const {
00129     return nsRect(x + aPoint.x, y + aPoint.y, width, height);
00130   }
00131   nsRect  operator-(const nsPoint& aPoint) const {
00132     return nsRect(x - aPoint.x, y - aPoint.y, width, height);
00133   }
00134   nsRect& operator+=(const nsPoint& aPoint) {x += aPoint.x; y += aPoint.y; return *this;}
00135   nsRect& operator-=(const nsPoint& aPoint) {x -= aPoint.x; y -= aPoint.y; return *this;}
00136 
00137   nsRect& operator*=(const float aScale) {x = NSToCoordRound(x * aScale); 
00138                                           y = NSToCoordRound(y * aScale); 
00139                                           width = NSToCoordRound(width * aScale); 
00140                                           height = NSToCoordRound(height * aScale); 
00141                                           return *this;}
00142 
00143   nsRect& ScaleRoundOut(const float aScale);
00144   nsRect& ScaleRoundIn(const float aScale);
00145 
00146   // Helpers for accessing the vertices
00147   nsPoint TopLeft() const { return nsPoint(x, y); }
00148   nsPoint TopRight() const { return nsPoint(XMost(), y); }
00149   nsPoint BottomLeft() const { return nsPoint(x, YMost()); }
00150   nsPoint BottomRight() const { return nsPoint(XMost(), YMost()); }
00151 
00152   nsSize Size() const { return nsSize(width, height); }
00153 
00154   // Helper methods for computing the extents
00155   nscoord XMost() const {return x + width;}
00156   nscoord YMost() const {return y + height;}
00157 };
00158 
00159 #ifdef NS_COORD_IS_FLOAT
00160 struct NS_GFX nsIntRect {
00161   PRInt32 x, y;
00162   PRInt32 width, height;
00163 
00164   // Constructors
00165   nsIntRect() : x(0), y(0), width(0), height(0) {}
00166   nsIntRect(const nsIntRect& aRect) {*this = aRect;}
00167   nsIntRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) {
00168     x = aX; y = aY; width = aWidth; height = aHeight;
00169   }
00170 
00171   // Emptiness. An empty rect is one that has no area, i.e. its height or width
00172   // is <= 0
00173   PRBool IsEmpty() const {
00174     return (PRBool) ((height <= 0) || (width <= 0));
00175   }
00176 
00177   void SetRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) {
00178     x = aX; y = aY; width = aWidth; height = aHeight;
00179   }
00180 
00181   // Computes the area in which aRect1 and aRect2 overlap, and fills 'this' with
00182   // the result. Returns FALSE if the rectangles don't intersect, and sets 'this'
00183   // rect to be an empty rect.
00184   //
00185   // 'this' can be the same object as either aRect1 or aRect2
00186   PRBool IntersectRect(const nsIntRect& aRect1, const nsIntRect& aRect2);
00187 
00188   // Computes the smallest rectangle that contains both aRect1 and aRect2 and
00189   // fills 'this' with the result. Returns FALSE and sets 'this' rect to be an
00190   // empty rect if both aRect1 and aRect2 are empty
00191   //
00192   // 'this' can be the same object as either aRect1 or aRect2
00193   PRBool UnionRect(const nsIntRect& aRect1, const nsIntRect& aRect2);
00194 
00195   // Helper methods for computing the extents
00196   PRInt32 XMost() const {return x + width;}
00197   PRInt32 YMost() const {return y + height;}
00198 };
00199 #else
00200 typedef nsRect nsIntRect;
00201 #endif
00202 
00203 #ifdef DEBUG
00204 // Diagnostics
00205 extern NS_GFX FILE* operator<<(FILE* out, const nsRect& rect);
00206 #endif // DEBUG
00207 
00208 #endif /* NSRECT_H */