1 /********************************************************************************
2 *                                                                               *
3 *                               S i z e    C l a s s                            *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1994,2021 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (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                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #ifndef FXSIZE_H
22 #define FXSIZE_H
23 
24 namespace FX {
25 
26 
27 /// Size
28 class FXAPI FXSize {
29 public:
30   FXshort w;
31   FXshort h;
32 public:
33 
34   /// Constructors
FXSize()35   FXSize(){ }
FXSize(const FXSize & s)36   FXSize(const FXSize& s):w(s.w),h(s.h){ }
FXSize(FXshort ww,FXshort hh)37   FXSize(FXshort ww,FXshort hh):w(ww),h(hh){ }
38 
39   /// Return a non-const reference to the ith element
40   FXshort& operator[](FXint i){return (&w)[i];}
41 
42   /// Return a const reference to the ith element
43   const FXshort& operator[](FXint i) const {return (&w)[i];}
44 
45   /// Test if empty
empty()46   FXbool empty() const { return w<=0 || h<=0; }
47 
48   /// Grow by amount
49   FXSize& grow(FXshort margin);
50   FXSize& grow(FXshort hormargin,FXshort vermargin);
51   FXSize& grow(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
52 
53   /// Shrink by amount
54   FXSize& shrink(FXshort margin);
55   FXSize& shrink(FXshort hormargin,FXshort vermargin);
56   FXSize& shrink(FXshort leftmargin,FXshort rightmargin,FXshort topmargin,FXshort bottommargin);
57 
58   /// Assignment
59   FXSize& operator=(const FXSize& s){ w=s.w; h=s.h; return *this; }
60 
61   /// Set value from another size
set(const FXSize & s)62   FXSize& set(const FXSize& s){ w=s.w; h=s.h; return *this; }
63 
64   /// Set value from components
set(FXshort ww,FXshort hh)65   FXSize& set(FXshort ww,FXshort hh){ w=ww; h=hh; return *this; }
66 
67   /// Assignment operators
68   FXSize& operator*=(FXshort c){ w*=c; h*=c; return *this; }
69   FXSize& operator/=(FXshort c){ w/=c; h/=c; return *this; }
70   FXSize& operator+=(const FXSize& s){ w+=s.w; h+=s.h; return *this; }
71   FXSize& operator-=(const FXSize& s){ w-=s.w; h-=s.h; return *this; }
72 
73   /// Test if zero
74   FXbool operator!() const { return w==0 && h==0; }
75 
76   /// Unary
77   FXSize operator+() const { return *this; }
78   FXSize operator-(){ return FXSize(-w,-h); }
79   };
80 
81 
82 /// Scale operators
83 inline FXSize operator*(const FXSize& s,FXshort c){ return FXSize(s.w*c,s.h*c); }
84 inline FXSize operator*(FXshort c,const FXSize& s){ return FXSize(c*s.w,c*s.h); }
85 inline FXSize operator/(const FXSize& s,FXshort c){ return FXSize(s.w/c,s.h/c); }
86 inline FXSize operator/(FXshort c,const FXSize& s){ return FXSize(c/s.w,c/s.h); }
87 
88 /// Addition operators
89 inline FXSize operator+(const FXSize& a,const FXSize& b){ return FXSize(a.w+b.w,a.h+b.h); }
90 inline FXSize operator-(const FXSize& a,const FXSize& b){ return FXSize(a.w-b.w,a.h-b.h); }
91 
92 /// Equality tests
93 inline FXbool operator==(const FXSize& a,FXshort n){ return a.w==n && a.h==n; }
94 inline FXbool operator!=(const FXSize& a,FXshort n){ return a.w!=n || a.h!=n; }
95 inline FXbool operator==(FXshort n,const FXSize& a){ return n==a.w && n==a.h; }
96 inline FXbool operator!=(FXshort n,const FXSize& a){ return n!=a.w || n!=a.h; }
97 
98 /// Equality tests
99 inline FXbool operator==(const FXSize& a,const FXSize& b){ return a.w==b.w && a.h==b.h; }
100 inline FXbool operator!=(const FXSize& a,const FXSize& b){ return a.w!=b.w || a.h!=b.h; }
101 
102 /// Inequality tests
103 inline FXbool operator<(const FXSize& a,FXshort n){ return a.w<n && a.h<n; }
104 inline FXbool operator<=(const FXSize& a,FXshort n){ return a.w<=n && a.h<=n; }
105 inline FXbool operator>(const FXSize& a,FXshort n){ return a.w>n && a.h>n; }
106 inline FXbool operator>=(const FXSize& a,FXshort n){ return a.w>=n && a.h>=n; }
107 
108 /// Inequality tests
109 inline FXbool operator<(FXshort n,const FXSize& a){ return n<a.w && n<a.h; }
110 inline FXbool operator<=(FXshort n,const FXSize& a){ return n<=a.w && n<=a.h; }
111 inline FXbool operator>(FXshort n,const FXSize& a){ return n>a.w && n>a.h; }
112 inline FXbool operator>=(FXshort n,const FXSize& a){ return n>=a.w && n>=a.h; }
113 
114 /// Inequality tests
115 inline FXbool operator<(const FXSize& a,const FXSize& b){ return a.w<b.w && a.h<b.h; }
116 inline FXbool operator<=(const FXSize& a,const FXSize& b){ return a.w<=b.w && a.h<=b.h; }
117 inline FXbool operator>(const FXSize& a,const FXSize& b){ return a.w>b.w && a.h>b.h; }
118 inline FXbool operator>=(const FXSize& a,const FXSize& b){ return a.w>=b.w && a.h>=b.h; }
119 
120 /// Lowest or highest components
lo(const FXSize & a,const FXSize & b)121 inline FXSize lo(const FXSize& a,const FXSize& b){ return FXSize(FXMIN(a.w,b.w),FXMIN(a.h,b.h)); }
hi(const FXSize & a,const FXSize & b)122 inline FXSize hi(const FXSize& a,const FXSize& b){ return FXSize(FXMAX(a.w,b.w),FXMAX(a.h,b.h)); }
123 
124 /// Save object to a stream
125 extern FXAPI FXStream& operator<<(FXStream& store,const FXSize& s);
126 
127 /// Load object from a stream
128 extern FXAPI FXStream& operator>>(FXStream& store,FXSize& s);
129 
130 }
131 
132 #endif
133