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,2004 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.10 2004/01/18 21:25:49 fox Exp $                        *
00023 ********************************************************************************/
00024 #ifndef FXRECTANGLE_H
00025 #define FXRECTANGLE_H
00026 
00027 
00028 namespace FX {
00029 
00030 /// Rectangle
00031 class FXAPI FXRectangle {
00032 public:
00033   FXshort x;
00034   FXshort y;
00035   FXshort w;
00036   FXshort h;
00037 public:
00038 
00039   /// Constructors
00040   FXRectangle(){ }
00041   FXRectangle(FXshort xx,FXshort yy,FXshort ww,FXshort hh):x(xx),y(yy),w(ww),h(hh){ }
00042   FXRectangle(const FXPoint& p,const FXSize& s):x(p.x),y(p.y),w(s.w),h(s.h){ }
00043   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){ }
00044 
00045   /// Equality
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   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; }
00048 
00049   /// Point in rectangle
00050   FXbool contains(const FXPoint& p) const { return x<=p.x && y<=p.y && p.x<x+w && p.y<y+h; }
00051   FXbool contains(FXshort xx,FXshort yy) const { return x<=xx && y<=yy && xx<x+w && yy<y+h; }
00052 
00053   /// Rectangle properly contained in rectangle
00054   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; }
00055 
00056   /// Rectangles overlap
00057   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; }
00058 
00059   /// Return moved rectangle
00060   FXRectangle& move(FXshort dx,FXshort dy){ x+=dx; y+=dy; return *this; }
00061 
00062   /// Grow by amount
00063   FXRectangle& grow(FXshort margin);
00064   FXRectangle& grow(FXshort hormargin,FXshort vermargin);
00065   FXRectangle& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00066 
00067   /// Shrink by amount
00068   FXRectangle& shrink(FXshort margin);
00069   FXRectangle& shrink(FXshort hormargin,FXshort vermargin);
00070   FXRectangle& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
00071 
00072   /// Corners
00073   FXPoint tl() const { return FXPoint(x,y); }
00074   FXPoint tr() const { return FXPoint(x+w-1,y); }
00075   FXPoint bl() const { return FXPoint(x,y+h-1); }
00076   FXPoint br() const { return FXPoint(x+w-1,y+h-1); }
00077 
00078   /// Union and intersection with rectangle
00079   FXRectangle& operator+=(const FXRectangle &r);
00080   FXRectangle& operator*=(const FXRectangle &r);
00081 
00082   /// Union and intersection between rectangles
00083   friend FXAPI FXRectangle operator+(const FXRectangle& p,const FXRectangle& q);
00084   friend FXAPI FXRectangle operator*(const FXRectangle& p,const FXRectangle& q);
00085 
00086   /// Save object to a stream
00087   friend FXAPI FXStream& operator<<(FXStream& store,const FXRectangle& r);
00088 
00089   /// Load object from a stream
00090   friend FXAPI FXStream& operator>>(FXStream& store,FXRectangle& r);
00091   };
00092 
00093 }
00094 
00095 #endif

Copyright © 1997-2004 Jeroen van der Zijp