- Your Widget Set For OpenGL
Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | Related Pages

udimension.hpp

00001 /*************************************************************************** 00002 LibUFO - UI For OpenGL 00003 copyright : (C) 2001-2005 by Johannes Schmidt 00004 email : schmidtjf at users.sourceforge.net 00005 ------------------- 00006 00007 file : include/ufo/util/udimension.hpp 00008 begin : Sun May 13 2001 00009 $Id: udimension.hpp,v 1.12 2005/09/30 12:36:48 schmidtjf Exp $ 00010 ***************************************************************************/ 00011 00012 /*************************************************************************** 00013 * This library is free software; you can redistribute it and/or * 00014 * modify it under the terms of the GNU Lesser General Public * 00015 * License as published by the Free Software Foundation; either * 00016 * version 2.1 of the License, or (at your option) any later version. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00021 * Lesser General Public License for more details. * 00022 * * 00023 * You should have received a copy of the GNU Lesser General Public * 00024 * License along with this library; if not, write to the Free Software * 00025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 00026 ***************************************************************************/ 00027 00028 #ifndef UDIMENSION_HPP 00029 #define UDIMENSION_HPP 00030 00031 #include "../uobject.hpp" 00032 00033 #include "uinsets.hpp" 00034 00035 namespace ufo { 00036 00046 class UFO_EXPORT UDimension { 00047 public: 00050 inline UDimension(); 00055 inline UDimension(int w, int h); 00056 00058 inline int getWidth() const; 00060 inline int getHeight() const; 00061 00062 00066 inline bool isInvalid() const; 00070 inline bool isValid() const; 00072 inline bool isEmpty() const; 00073 00078 inline void clamp(const UDimension & maxDim); 00083 inline void expand(const UDimension & minDim); 00084 00090 inline void update(const UDimension & dim); 00097 inline void transcribe(const UDimension & dim); 00098 00103 inline void setSize(int w, int h); 00107 inline UDimension getSize() const; 00108 public: // Public operators 00112 inline bool operator()() const; 00116 inline bool operator!() const; 00117 00121 inline UDimension & operator+=(const UDimension & dim); 00125 inline UDimension & operator-=(const UDimension & dim); 00126 00130 inline UDimension & operator+=(const UInsets & insets); 00134 inline UDimension & operator-=(const UInsets & insets); 00135 00139 inline UDimension & operator*=(int c); 00143 inline UDimension & operator*=(double c); 00147 inline UDimension & operator/=(int c); 00151 inline UDimension & operator/=(double c); 00152 00153 inline friend std::ostream & operator<<(std::ostream & os, const UDimension & o); 00154 public: // Public attributes 00156 int w; 00158 int h; 00159 public: // Public static attributes 00160 static UDimension maxDimension; 00161 static UDimension invalid; 00162 }; 00163 00164 // 00165 // public operators 00166 // 00167 inline UDimension operator+(const UDimension & dim1, const UDimension & dim2); 00168 inline UDimension operator-(const UDimension & dim1, const UDimension & dim2); 00169 inline UDimension operator+(const UDimension & dim, const UInsets & in); 00170 inline UDimension operator-(const UDimension & dim, const UInsets & in); 00171 inline UDimension operator*(const UDimension & dim, int c); 00172 inline UDimension operator*(int c, const UDimension & p); 00173 inline UDimension operator*(const UDimension & dim, double c); 00174 inline UDimension operator*(double c, const UDimension & p); 00175 inline UDimension operator/(const UDimension & dim, int c); 00176 inline UDimension operator/(const UDimension & dim, double c); 00177 00179 inline bool operator==(const UDimension & dim1,const UDimension & dim2); 00180 inline bool operator!=(const UDimension & dim1,const UDimension & dim2); 00181 00182 00186 class UFO_EXPORT UDimensionObject : public UDimension, public UObject { 00187 UFO_DECLARE_DYNAMIC_CLASS(UDimensionObject) 00188 public: 00189 inline UDimensionObject(); 00190 inline UDimensionObject(const UDimension & dim); 00191 inline UDimensionObject(int w, int h); 00192 00193 // 00194 // overrides UObject 00195 // 00196 virtual unsigned int hashCode() const; 00197 virtual bool equals(const UObject * obj); 00198 virtual bool equals(const UDimension * obj); 00199 virtual UObject * clone() const; 00200 00201 protected: // Protected methods 00202 virtual std::ostream & paramString(std::ostream & os) const; 00203 }; 00204 00205 00206 // 00207 // UDimension 00208 // inline implementation 00209 // 00210 00211 inline 00212 UDimension::UDimension() : w(0), h(0) {} 00213 00214 inline 00215 UDimension::UDimension(int w, int h) : w(w), h(h) {} 00216 00217 00218 inline int 00219 UDimension::getWidth() const { return w; } 00220 00221 inline int 00222 UDimension::getHeight() const { return h; } 00223 00224 inline bool 00225 UDimension::isInvalid() const { 00226 return (*this == UDimension::invalid); 00227 } 00228 00229 inline bool 00230 UDimension::isValid() const { 00231 return (w != UDimension::invalid.w && 00232 h != UDimension::invalid.h); 00233 } 00234 00235 inline bool 00236 UDimension::isEmpty() const { 00237 return (!(w && h)); 00238 } 00239 00240 inline void 00241 UDimension::clamp(const UDimension & maxDim) { 00242 w = std::min(w, maxDim.w); 00243 h = std::min(h, maxDim.h); 00244 } 00245 00246 inline void 00247 UDimension::expand(const UDimension & minDim) { 00248 w = std::max(w, minDim.w); 00249 h = std::max(h, minDim.h); 00250 } 00251 00252 inline void 00253 UDimension::update(const UDimension & dim) { 00254 if (w == invalid.w) { 00255 w = dim.w; 00256 } 00257 if (h == invalid.h) { 00258 h = dim.h; 00259 } 00260 } 00261 00262 inline void 00263 UDimension::transcribe(const UDimension & dim) { 00264 if (dim.w != invalid.w) { 00265 w = dim.w; 00266 } 00267 if (dim.h != invalid.h) { 00268 h = dim.h; 00269 } 00270 } 00271 00272 inline void 00273 UDimension::setSize(int w, int h) { 00274 this->w = w; 00275 this->h = h; 00276 } 00277 00278 inline UDimension 00279 UDimension::getSize() const { 00280 return UDimension(w, h); 00281 } 00282 00283 inline bool 00284 UDimension::operator()() const { 00285 return isValid(); 00286 } 00287 00288 inline bool 00289 UDimension::operator!() const { 00290 return !isValid(); 00291 } 00292 00293 00294 inline UDimension & 00295 UDimension::operator+=(const UDimension & dim) { 00296 w += dim.w; 00297 h += dim.h; 00298 return *this; 00299 } 00300 00301 inline UDimension & 00302 UDimension::operator-=(const UDimension & dim) { 00303 w -= dim.w; 00304 h -= dim.h; 00305 return *this; 00306 } 00307 00308 inline UDimension & 00309 UDimension::operator+=(const UInsets & insets) { 00310 w += insets.getHorizontal(); 00311 h += insets.getVertical(); 00312 return *this; 00313 } 00314 00315 inline UDimension & 00316 UDimension::operator-=(const UInsets & insets) { 00317 w -= insets.getHorizontal(); 00318 h -= insets.getVertical(); 00319 return *this; 00320 } 00321 00322 00323 inline UDimension & 00324 UDimension::operator*=(int c) { 00325 w *= c; 00326 h *= c; 00327 return *this; 00328 } 00329 00330 inline UDimension & 00331 UDimension::operator*=(double c) { 00332 // FIXME: should we round or truncate? 00333 w = int(w * c); 00334 h = int(h * c); 00335 return *this; 00336 } 00337 00338 inline UDimension & 00339 UDimension::operator/=(int c) { 00340 // FIXME: should we round or truncate? 00341 w = int(w / c); 00342 h = int(h / c); 00343 return *this; 00344 } 00345 00346 inline UDimension & 00347 UDimension::operator/=(double c) { 00348 // FIXME: should we round or truncate? 00349 w = int(w / c); 00350 h = int(h / c); 00351 return *this; 00352 } 00353 00354 inline std::ostream & 00355 operator<<(std::ostream & os, const UDimension & o) { 00356 return os << "UDimension[" << o.w << "x" << o.h << "]"; 00357 } 00358 00359 // 00360 // public operators 00361 // inline implementation 00362 // 00363 inline UDimension operator+(const UDimension & dim1, const UDimension & dim2) { 00364 UDimension ret(dim1); 00365 return ret += dim2; 00366 } 00367 00368 inline UDimension operator-(const UDimension & dim1, const UDimension & dim2) { 00369 UDimension ret(dim1); 00370 return ret -= dim2; 00371 } 00372 00373 inline UDimension operator+(const UDimension & dim, const UInsets & in) { 00374 UDimension ret(dim); 00375 return ret += in; 00376 } 00377 00378 inline UDimension operator-(const UDimension & dim, const UInsets & in) { 00379 UDimension ret(dim); 00380 return ret -= in; 00381 } 00382 00383 inline UDimension operator*(const UDimension & dim, int c) { 00384 UDimension ret(dim); 00385 return ret *= c; 00386 } 00387 00388 inline UDimension operator*(int c, const UDimension & dim) { 00389 UDimension ret(dim); 00390 return ret *= c; 00391 } 00392 00393 inline UDimension operator*(const UDimension & dim, double c) { 00394 UDimension ret(dim); 00395 return ret *= c; 00396 } 00397 00398 inline UDimension operator*(double c, const UDimension & dim) { 00399 UDimension ret(dim); 00400 return ret *= c; 00401 } 00402 00403 inline UDimension operator/(const UDimension & dim, int c) { 00404 UDimension ret(dim); 00405 return ret /= c; 00406 } 00407 00408 inline UDimension operator/(const UDimension & dim, double c) { 00409 UDimension ret(dim); 00410 return ret /= c; 00411 } 00412 00413 00414 inline bool operator==(const UDimension & dim1,const UDimension & dim2) { 00415 return dim1.w == dim2.w && dim1.h == dim2.h; 00416 } 00417 00418 inline bool operator!=(const UDimension & dim1,const UDimension & dim2) { 00419 return dim1.w != dim2.w || dim1.h != dim2.h; 00420 } 00421 00422 // 00423 // UDimensionObject 00424 // inline implementation 00425 // 00426 00427 inline 00428 UDimensionObject::UDimensionObject() {} 00429 00430 inline 00431 UDimensionObject::UDimensionObject(const UDimension & dim) : UDimension(dim) {} 00432 00433 inline 00434 UDimensionObject::UDimensionObject(int w, int h) : UDimension(w, h) {} 00435 00436 inline unsigned int 00437 UDimensionObject::hashCode() const { 00438 // FIXME 00439 return (((w << 8) | h) ^ w); 00440 } 00441 00442 inline bool 00443 UDimensionObject::equals(const UObject * obj) { 00444 if (const UDimension * dim = dynamic_cast<const UDimension*>(obj)) { 00445 return (w == dim->w) && (h == dim->h); 00446 } 00447 return false; 00448 } 00449 00450 inline bool 00451 UDimensionObject::equals(const UDimension * obj) { 00452 if (obj) { 00453 return (w == obj->w) && (h == obj->h); 00454 } 00455 return false; 00456 } 00457 00458 inline UObject * 00459 UDimensionObject::clone() const { 00460 return new UDimensionObject(w, h); 00461 } 00462 00463 // 00464 // protected methods 00465 // 00466 00467 inline std::ostream & 00468 UDimensionObject::paramString(std::ostream & os) const { 00469 return os << w << "x" << h; 00470 } 00471 00472 } // namespace ufo 00473 00474 #endif // UDIMENSION_HPP

The libUFO Project - written by Johannes Schmidt