Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXRectangle.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                          R e c t a n g l e    C l a s s                       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1994,2002 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXRectangle.h,v 1.7 2002/03/12 07:11:30 fox Exp $                        *
00023 ********************************************************************************/
00024 #ifndef FXRECTANGLE_H
00025 #define FXRECTANGLE_H
00026 
00027 namespace FX {
00028 
00029 /// Rectangle
00030 class FXAPI FXRectangle {
00031 public:
00032   FXshort x;
00033   FXshort y;
00034   FXshort w;
00035   FXshort h;
00036 public:
00037 
00038   /// Constructors
00039   FXRectangle(){ }
00040   FXRectangle(FXshort xx,FXshort yy,FXshort ww,FXshort hh):x(xx),y(yy),w(ww),h(hh){ }
00041   FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
00042   FXRectangle(const FXPoint& topleft,const FXPoint& bottomright):x(topleft.x),y(topleft.y),w(bottomright.x-topleft.x+1),h(bottomright.y-topleft.y+1){ }
00043 
00044   /// Equality
00045   friend FXAPI FXbool operator==(const FXRectangle& p,const FXRectangle& q){ return p.x==q.x && p.y==q.y && p.w==q.w && p.h==q.h; }
00046   friend FXAPI FXbool operator!=(const FXRectangle& p,const FXRectangle& q){ return p.x!=q.x || p.y!=q.y || p.w!=q.w || p.h!=q.h; }
00047 
00048   /// Point in rectangle
00049   FXbool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
00050   FXbool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
00051 
00052   /// Rectangle properly contained in rectangle
00053   FXbool contains(const FXRectangle& r) const { return x<=r.x && y<=r.y && r.x+r.w<=x+w && r.y+r.h<=y+h; }
00054 
00055   /// Rectangles overlap
00056   friend FXAPI FXbool overlap(const FXRectangle& a,const FXRectangle& b){ return b.x<a.x+a.w && b.y<a.y+a.h && a.x<b.x+b.w && a.y<b.y+b.h; }
00057 
00058   /// Return moved rectangle
00059   FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
00060 
00061   /// Grow by amount
00062   FXRectangle& grow(FXshort margin);
00063   FXRectangle& grow(FXshort hormargin,FXshort vermargin);
00064   FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00065 
00066   /// Shrink by amount
00067   FXRectangle& shrink(FXshort margin);
00068   FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
00069   FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00070 
00071   /// Corners
00072   FXPoint tl() const { return FXPoint(x,y); }
00073   FXPoint tr() const { return FXPoint(x+w-1,y); }
00074   FXPoint bl() const { return FXPoint(x,y+h-1); }
00075   FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
00076 
00077   /// Union and intersection with rectangle
00078   FXRectangle& operator+=(const FXRectangle &r);
00079   FXRectangle& operator*=(const FXRectangle &r);
00080 
00081   /// Union and intersection between rectangles
00082   friend FXAPI FXRectangle operator+(const FXRectangle& p,const FXRectangle& q);
00083   friend FXAPI FXRectangle operator*(const FXRectangle& p,const FXRectangle& q);
00084 
00085   /// Save object to a stream
00086   friend FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
00087 
00088   /// Load object from a stream
00089   friend FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
00090   };
00091 
00092 }
00093 
00094 #endif