2001-09-26 04:16:04 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 01:52:36 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-01-26 01:16:47 +03:00
|
|
|
*
|
2004-04-18 01:52:36 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-01-26 01:16:47 +03:00
|
|
|
*
|
2001-09-26 04:16:04 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-01-26 01:16:47 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 01:52:36 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 04:16:04 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-26 04:16:04 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 01:52:36 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-26 04:16:04 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 01:52:36 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 04:16:04 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 01:52:36 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-26 04:16:04 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-01-26 01:16:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef NSRECT_H
|
|
|
|
#define NSRECT_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "nsCoord.h"
|
|
|
|
#include "nsPoint.h"
|
|
|
|
#include "nsSize.h"
|
|
|
|
#include "nsMargin.h"
|
|
|
|
#include "nsUnitConversion.h"
|
2004-05-18 22:09:16 +04:00
|
|
|
#include "gfxCore.h"
|
2006-03-01 06:57:24 +03:00
|
|
|
#include "nsTraceRefcnt.h"
|
1999-01-26 01:16:47 +03:00
|
|
|
|
|
|
|
struct NS_GFX nsRect {
|
|
|
|
nscoord x, y;
|
|
|
|
nscoord width, height;
|
|
|
|
|
|
|
|
// Constructors
|
2006-03-01 06:57:24 +03:00
|
|
|
nsRect() : x(0), y(0), width(0), height(0) {
|
|
|
|
MOZ_COUNT_CTOR(nsRect);
|
|
|
|
}
|
|
|
|
nsRect(const nsRect& aRect) {
|
|
|
|
MOZ_COUNT_CTOR(nsRect);
|
|
|
|
*this = aRect;
|
|
|
|
}
|
|
|
|
nsRect(const nsPoint& aOrigin, const nsSize &aSize) {
|
|
|
|
MOZ_COUNT_CTOR(nsRect);
|
|
|
|
x = aOrigin.x; y = aOrigin.y;
|
|
|
|
width = aSize.width; height = aSize.height;
|
|
|
|
}
|
2004-11-03 05:48:04 +03:00
|
|
|
nsRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) {
|
2006-03-01 06:57:24 +03:00
|
|
|
MOZ_COUNT_CTOR(nsRect);
|
2004-11-03 05:48:04 +03:00
|
|
|
x = aX; y = aY; width = aWidth; height = aHeight;
|
|
|
|
VERIFY_COORD(x); VERIFY_COORD(y); VERIFY_COORD(width); VERIFY_COORD(height);
|
|
|
|
}
|
1999-01-26 01:16:47 +03:00
|
|
|
|
2006-03-01 06:57:24 +03:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
~nsRect() {
|
|
|
|
MOZ_COUNT_DTOR(nsRect);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-11-06 04:48:36 +03:00
|
|
|
// Emptiness. An empty rect is one that has no area, i.e. its height or width
|
1999-01-26 01:16:47 +03:00
|
|
|
// is <= 0
|
|
|
|
PRBool IsEmpty() const {
|
2001-11-06 04:48:36 +03:00
|
|
|
return (PRBool) ((height <= 0) || (width <= 0));
|
1999-01-26 01:16:47 +03:00
|
|
|
}
|
|
|
|
void Empty() {width = height = 0;}
|
|
|
|
|
|
|
|
// Containment
|
|
|
|
PRBool Contains(const nsRect& aRect) const;
|
|
|
|
PRBool Contains(nscoord aX, nscoord aY) const;
|
|
|
|
PRBool Contains(const nsPoint& aPoint) const {return Contains(aPoint.x, aPoint.y);}
|
|
|
|
|
|
|
|
// Intersection. Returns TRUE if the receiver overlaps aRect and
|
|
|
|
// FALSE otherwise
|
|
|
|
PRBool Intersects(const nsRect& aRect) const;
|
|
|
|
|
|
|
|
// Computes the area in which aRect1 and aRect2 overlap, and fills 'this' with
|
|
|
|
// the result. Returns FALSE if the rectangles don't intersect, and sets 'this'
|
|
|
|
// rect to be an empty rect.
|
|
|
|
//
|
|
|
|
// 'this' can be the same object as either aRect1 or aRect2
|
|
|
|
PRBool IntersectRect(const nsRect& aRect1, const nsRect& aRect2);
|
|
|
|
|
|
|
|
// Computes the smallest rectangle that contains both aRect1 and aRect2 and
|
|
|
|
// fills 'this' with the result. Returns FALSE and sets 'this' rect to be an
|
|
|
|
// empty rect if both aRect1 and aRect2 are empty
|
|
|
|
//
|
|
|
|
// 'this' can be the same object as either aRect1 or aRect2
|
|
|
|
PRBool UnionRect(const nsRect& aRect1, const nsRect& aRect2);
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
void SetRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) {
|
|
|
|
x = aX; y = aY; width = aWidth; height = aHeight;
|
|
|
|
}
|
2006-01-26 09:10:52 +03:00
|
|
|
void SetRect(const nsPoint& aPt, const nsSize& aSize) {
|
2006-01-26 05:29:17 +03:00
|
|
|
SetRect(aPt.x, aPt.y, aSize.width, aSize.height);
|
|
|
|
}
|
1999-01-26 01:16:47 +03:00
|
|
|
void MoveTo(nscoord aX, nscoord aY) {x = aX; y = aY;}
|
1999-11-12 18:10:40 +03:00
|
|
|
void MoveTo(const nsPoint& aPoint) {x = aPoint.x; y = aPoint.y;}
|
1999-01-26 01:16:47 +03:00
|
|
|
void MoveBy(nscoord aDx, nscoord aDy) {x += aDx; y += aDy;}
|
2003-09-13 20:21:41 +04:00
|
|
|
void MoveBy(const nsPoint& aPoint) {x += aPoint.x; y += aPoint.y;}
|
1999-01-26 01:16:47 +03:00
|
|
|
void SizeTo(nscoord aWidth, nscoord aHeight) {width = aWidth; height = aHeight;}
|
|
|
|
void SizeTo(const nsSize& aSize) {SizeTo(aSize.width, aSize.height);}
|
|
|
|
void SizeBy(nscoord aDeltaWidth, nscoord aDeltaHeight) {width += aDeltaWidth;
|
|
|
|
height += aDeltaHeight;}
|
|
|
|
|
|
|
|
// Inflate the rect by the specified width/height or margin
|
|
|
|
void Inflate(nscoord aDx, nscoord aDy);
|
|
|
|
void Inflate(const nsSize& aSize) {Inflate(aSize.width, aSize.height);}
|
|
|
|
void Inflate(const nsMargin& aMargin);
|
|
|
|
|
|
|
|
// Deflate the rect by the specified width/height or margin
|
|
|
|
void Deflate(nscoord aDx, nscoord aDy);
|
|
|
|
void Deflate(const nsSize& aSize) {Deflate(aSize.width, aSize.height);}
|
|
|
|
void Deflate(const nsMargin& aMargin);
|
|
|
|
|
|
|
|
// Overloaded operators. Note that '=' isn't defined so we'll get the
|
|
|
|
// compiler generated default assignment operator.
|
|
|
|
PRBool operator==(const nsRect& aRect) const {
|
|
|
|
return (PRBool) ((IsEmpty() && aRect.IsEmpty()) ||
|
|
|
|
((x == aRect.x) && (y == aRect.y) &&
|
|
|
|
(width == aRect.width) && (height == aRect.height)));
|
|
|
|
}
|
|
|
|
PRBool operator!=(const nsRect& aRect) const {
|
|
|
|
return (PRBool) !operator==(aRect);
|
|
|
|
}
|
2004-02-22 06:31:30 +03:00
|
|
|
|
|
|
|
nsRect operator+(const nsPoint& aPoint) const {
|
|
|
|
return nsRect(x + aPoint.x, y + aPoint.y, width, height);
|
|
|
|
}
|
|
|
|
nsRect operator-(const nsPoint& aPoint) const {
|
|
|
|
return nsRect(x - aPoint.x, y - aPoint.y, width, height);
|
|
|
|
}
|
1999-01-26 01:16:47 +03:00
|
|
|
nsRect& operator+=(const nsPoint& aPoint) {x += aPoint.x; y += aPoint.y; return *this;}
|
|
|
|
nsRect& operator-=(const nsPoint& aPoint) {x -= aPoint.x; y -= aPoint.y; return *this;}
|
|
|
|
|
|
|
|
nsRect& operator*=(const float aScale) {x = NSToCoordRound(x * aScale);
|
|
|
|
y = NSToCoordRound(y * aScale);
|
|
|
|
width = NSToCoordRound(width * aScale);
|
|
|
|
height = NSToCoordRound(height * aScale);
|
|
|
|
return *this;}
|
|
|
|
|
|
|
|
nsRect& ScaleRoundOut(const float aScale);
|
|
|
|
nsRect& ScaleRoundIn(const float aScale);
|
|
|
|
|
2004-03-10 04:59:16 +03:00
|
|
|
// Helpers for accessing the vertices
|
|
|
|
nsPoint TopLeft() const { return nsPoint(x, y); }
|
|
|
|
nsPoint TopRight() const { return nsPoint(XMost(), y); }
|
|
|
|
nsPoint BottomLeft() const { return nsPoint(x, YMost()); }
|
|
|
|
nsPoint BottomRight() const { return nsPoint(XMost(), YMost()); }
|
|
|
|
|
2004-10-12 00:35:51 +04:00
|
|
|
nsSize Size() const { return nsSize(width, height); }
|
|
|
|
|
1999-01-26 01:16:47 +03:00
|
|
|
// Helper methods for computing the extents
|
|
|
|
nscoord XMost() const {return x + width;}
|
|
|
|
nscoord YMost() const {return y + height;}
|
|
|
|
};
|
|
|
|
|
2004-11-03 05:48:04 +03:00
|
|
|
#ifdef NS_COORD_IS_FLOAT
|
|
|
|
struct NS_GFX nsIntRect {
|
|
|
|
PRInt32 x, y;
|
|
|
|
PRInt32 width, height;
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
nsIntRect() : x(0), y(0), width(0), height(0) {}
|
|
|
|
nsIntRect(const nsIntRect& aRect) {*this = aRect;}
|
|
|
|
nsIntRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) {
|
|
|
|
x = aX; y = aY; width = aWidth; height = aHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emptiness. An empty rect is one that has no area, i.e. its height or width
|
|
|
|
// is <= 0
|
|
|
|
PRBool IsEmpty() const {
|
|
|
|
return (PRBool) ((height <= 0) || (width <= 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) {
|
|
|
|
x = aX; y = aY; width = aWidth; height = aHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Computes the area in which aRect1 and aRect2 overlap, and fills 'this' with
|
|
|
|
// the result. Returns FALSE if the rectangles don't intersect, and sets 'this'
|
|
|
|
// rect to be an empty rect.
|
|
|
|
//
|
|
|
|
// 'this' can be the same object as either aRect1 or aRect2
|
|
|
|
PRBool IntersectRect(const nsIntRect& aRect1, const nsIntRect& aRect2);
|
|
|
|
|
|
|
|
// Computes the smallest rectangle that contains both aRect1 and aRect2 and
|
|
|
|
// fills 'this' with the result. Returns FALSE and sets 'this' rect to be an
|
|
|
|
// empty rect if both aRect1 and aRect2 are empty
|
|
|
|
//
|
|
|
|
// 'this' can be the same object as either aRect1 or aRect2
|
|
|
|
PRBool UnionRect(const nsIntRect& aRect1, const nsIntRect& aRect2);
|
|
|
|
|
|
|
|
// Helper methods for computing the extents
|
|
|
|
PRInt32 XMost() const {return x + width;}
|
|
|
|
PRInt32 YMost() const {return y + height;}
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
typedef nsRect nsIntRect;
|
|
|
|
#endif
|
|
|
|
|
2004-04-23 01:36:53 +04:00
|
|
|
#ifdef DEBUG
|
1999-01-26 01:16:47 +03:00
|
|
|
// Diagnostics
|
|
|
|
extern NS_GFX FILE* operator<<(FILE* out, const nsRect& rect);
|
2004-04-23 01:36:53 +04:00
|
|
|
#endif // DEBUG
|
1999-01-26 01:16:47 +03:00
|
|
|
|
|
|
|
#endif /* NSRECT_H */
|