1 /********************************************************************************
2 *                                                                               *
3 *                          R e c t a n g l e    C l a s s                       *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1994,2005 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or                 *
9 * modify it under the terms of the GNU Lesser General Public                    *
10 * License as published by the Free Software Foundation; either                  *
11 * version 2.1 of the License, or (at your option) any later version.            *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
16 * Lesser General Public License for more details.                               *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public              *
19 * License along with this library; if not, write to the Free Software           *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
21 *********************************************************************************
22 * $Id: FXRectangle.cpp,v 1.13 2005/01/16 16:06:07 fox Exp $                     *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "fxpriv.h"
28 #include "FXHash.h"
29 #include "FXStream.h"
30 #include "FXSize.h"
31 #include "FXPoint.h"
32 #include "FXRectangle.h"
33 
34 using namespace FX;
35 
36 /*******************************************************************************/
37 
38 namespace FX {
39 
40 // Fast inlines
_max(FXshort a,FXshort b)41 static inline FXshort _max(FXshort a,FXshort b){ return a>b?a:b; }
_min(FXshort a,FXshort b)42 static inline FXshort _min(FXshort a,FXshort b){ return a<b?a:b; }
43 
44 
45 // Grow by amount
grow(FXshort margin)46 FXRectangle& FXRectangle::grow(FXshort margin){
47   x-=margin;
48   y-=margin;
49   w+=(margin+margin);
50   h+=(margin+margin);
51   return *this;
52   }
53 
54 
grow(FXshort hormargin,FXshort vermargin)55 FXRectangle& FXRectangle::grow(FXshort hormargin,FXshort vermargin){
56   x-=hormargin;
57   y-=vermargin;
58   w+=(hormargin+hormargin);
59   h+=(vermargin+vermargin);
60   return *this;
61   }
62 
63 
grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin)64 FXRectangle& FXRectangle::grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin){
65   x-=leftmargin;
66   y-=topmargin;
67   w+=(leftmargin+rightmargin);
68   h+=(topmargin+bottommargin);
69   return *this;
70   }
71 
72 
73 // Shrink by amount
shrink(FXshort margin)74 FXRectangle& FXRectangle::shrink(FXshort margin){
75   x+=margin;
76   y+=margin;
77   w-=(margin+margin);
78   h-=(margin+margin);
79   return *this;
80   }
81 
82 
shrink(FXshort hormargin,FXshort vermargin)83 FXRectangle& FXRectangle::shrink(FXshort hormargin,FXshort vermargin){
84   x+=hormargin;
85   y+=vermargin;
86   w-=(hormargin+hormargin);
87   h-=(vermargin+vermargin);
88   return *this;
89   }
90 
91 
shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin)92 FXRectangle& FXRectangle::shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin){
93   x+=leftmargin;
94   y+=topmargin;
95   w-=(leftmargin+rightmargin);
96   h-=(topmargin+bottommargin);
97   return *this;
98   }
99 
100 
101 // Union with rectangle
operator +=(const FXRectangle & r)102 FXRectangle& FXRectangle::operator+=(const FXRectangle &r){
103   w=_max(x+w,r.x+r.w); x=_min(x,r.x); w-=x;
104   h=_max(y+h,r.y+r.h); y=_min(y,r.y); h-=y;
105   return *this;
106   }
107 
108 
109 // Intersection with rectangle
operator *=(const FXRectangle & r)110 FXRectangle& FXRectangle::operator*=(const FXRectangle &r){
111   w=_min(x+w,r.x+r.w); x=_max(x,r.x); w-=x;
112   h=_min(y+h,r.y+r.h); y=_max(y,r.y); h-=y;
113   return *this;
114   }
115 
116 
117 // Union between rectangles
operator +(const FXRectangle & p,const FXRectangle & q)118 FXRectangle operator+(const FXRectangle& p,const FXRectangle& q){
119   register FXshort xx=_min(p.x,q.x);
120   register FXshort ww=_max(p.x+p.w,q.x+q.w)-xx;
121   register FXshort yy=_min(p.y,q.y);
122   register FXshort hh=_max(p.y+p.h,q.y+q.h)-yy;
123   return FXRectangle(xx,yy,ww,hh);
124   }
125 
126 
127 // Intersection between rectangles
operator *(const FXRectangle & p,const FXRectangle & q)128 FXRectangle operator*(const FXRectangle& p,const FXRectangle& q){
129   register FXshort xx=_max(p.x,q.x);
130   register FXshort ww=_min(p.x+p.w,q.x+q.w)-xx;
131   register FXshort yy=_max(p.y,q.y);
132   register FXshort hh=_min(p.y+p.h,q.y+q.h)-yy;
133   return FXRectangle(xx,yy,ww,hh);
134   }
135 
136 
137 
138 // Save object to a stream
operator <<(FXStream & store,const FXRectangle & r)139 FXStream& operator<<(FXStream& store,const FXRectangle& r){
140   store << r.x << r.y << r.w << r.h;
141   return store;
142   }
143 
144 
145 // Load object from a stream
operator >>(FXStream & store,FXRectangle & r)146 FXStream& operator>>(FXStream& store,FXRectangle& r){
147   store >> r.x >> r.y >> r.w >> r.h;
148   return store;
149   }
150 
151 }
152