size.h

Go to the documentation of this file.
00001 /*
00002   libwt - Vassilis Virvilis Toolkit - a widget library
00003   Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm>
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #ifndef WT_SIZE_H
00022 #define WT_SIZE_H
00023 
00024 #include <iosfwd>
00025 
00026 namespace Wt {
00027 
00028 class Size {
00029 public:
00030     Size(int width = 0, int height = 0)
00031             :width_(width), height_(height) {}
00032     int width() const {
00033         return width_;
00034     };
00035     int height() const {
00036         return height_;
00037     };
00038     bool isValid() const {
00039         return (width_ >=0) && (height_ >= 0);
00040     }
00041 
00042     void setWidth(int w) {
00043         width_ = w;
00044     }
00045 
00046     void setHeight(int h) {
00047         height_ = h;
00048     }
00049 
00050     void setSize(int w, int h) {
00051         setWidth(w);
00052         setHeight(h);
00053     }
00054 
00055     /// Compare two Sizes for equality
00056     bool operator==(const Size& other) const {
00057         return (width_ == other.width_ && height_ == other.height_);
00058     }
00059 
00060     /// Compare two Sizes
00061     bool operator!=(const Size &other) const {
00062         return !(*this == other);
00063     }
00064 
00065     bool contains(const Size& other) const {
00066         return (width_ >= other.width_ && height_ >= other.height_);
00067     }
00068 
00069     /// increment operator
00070     Size& operator+=(const Size& other) {
00071         width_ += other.width_;
00072         height_ += other.height_;
00073         return *this;
00074     }
00075 
00076     /// add a margin (smaller rectangle)
00077     Size& operator>>(int margin) {
00078         width_ -= 2 * margin;
00079         height_ -= 2 * margin;
00080         return *this;
00081     }
00082 
00083     /// subtract a margin (bigger rectangle)
00084     Size& operator<<(int margin) {
00085         width_ += 2 * margin;
00086         height_ += 2 * margin;
00087         return *this;
00088     }
00089 
00090     /// add a margin (smaller rectangle)
00091     Size operator>>(int margin) const {
00092         return Size(width_ - 2 * margin, height_ -2 * margin);
00093     }
00094 
00095     /// subtract a margin (bigger rectangle)
00096     Size operator<<(int margin) const {
00097         return Size(width_ + 2 * margin, height_ +2 * margin);
00098     }
00099 
00100     Size boundedTo(const Size& other) const {
00101         int w = std::min(other.width_, width_);
00102         int h = std::min(other.height_, height_);
00103         return Size(w, h);
00104     }
00105 
00106     Size& boundedTo(const Size& other) {
00107         width_ = std::min(other.width_, width_);
00108         height_ = std::min(other.height_, height_);
00109         return *this;
00110     }
00111 
00112     Size expandedTo(const Size& other) const {
00113         int w = std::max(other.width_, width_);
00114         int h = std::max(other.height_, height_);
00115         return Size(w, h);
00116     }
00117 
00118     Size& expandedTo(const Size& other) {
00119         width_ = std::max(other.width_, width_);
00120         height_ = std::max(other.height_, height_);
00121         return *this;
00122     }
00123 
00124 private:
00125     int width_;
00126     int height_;
00127 };
00128 
00129 std::ostream& operator<<(std::ostream& s, const Size& size);
00130 
00131 }  //namespace Wt
00132 
00133 #endif // WT_SIZE_H

Generated Fri Jul 28 19:23:01 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.