rect.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_RECT_H
00022 #define WT_RECT_H
00023 
00024 #include "sdlrect.h"
00025 
00026 #include <wt/point.h>
00027 #include <wt/size.h>
00028 #include <wt/flags.h>
00029 
00030 namespace Wt {
00031 
00032 /// Rectangle class.  Has corners, fill, etc.
00033 class Rect : public SDLRect {
00034 public:
00035     /// Create an empty rectangle at (0, 0)
00036     Rect() : SDLRect() {
00037         assert(sizeof(Rect) == sizeof(SDLRect));
00038     }
00039     /// Create a rectangle
00040     Rect(int x_, int y_, int w_, int h_)
00041             : SDLRect(x_, y_, w_, h_) {
00042         assert(sizeof(Rect) == sizeof(SDLRect));
00043     }
00044     Rect(const Point& p, const Size& size)
00045             : SDLRect(p.x(), p.y(), size.width(), size.height()) {
00046         assert(sizeof(Rect) == sizeof(SDLRect));
00047     }
00048     /// Create a rectangle from a SDLRect
00049     Rect(const SDLRect& sdlrect)
00050             : SDLRect(sdlrect) {
00051         assert(sizeof(Rect) == sizeof(SDLRect));
00052     }
00053     /// set's a rect from a SDLRect
00054     Rect& operator=(const SDLRect &r) {
00055         *this = Rect(r);
00056         return *this;
00057     }
00058     ///
00059     ~Rect() {}
00060 
00061     /// An invalid rectangle
00062     static Rect invalid;
00063     /// An empty (w == h == 0) rectangle
00064     static Rect empty;
00065     /// Returns true if the rectangle is empty
00066     bool isEmpty() const {
00067         return width() == 0 && height() == 0;
00068     }
00069     /// Returns true if the rectangle is valid (dimensions non-negative)
00070     bool isValid() const {
00071         return (width() > 0 && height() > 0);
00072     }
00073 
00074     using SDLRect::contains;
00075     /// Returns true if the rectangle contains p
00076     bool contains(const Point &p) const {
00077         return contains(p.x(), p.y());
00078     }
00079 
00080     using SDLRect::moveTopLeft;
00081     ///move the rectangle, specifying new upperleft corner
00082     void moveTopLeft(const Point &p) {
00083         moveTopLeft(p.x(), p.y());
00084     }
00085 
00086     /// Return the smallest rectangle which contains both *this and r
00087     Rect unite(const Rect &r)    const;
00088     /// Return the largest rectangle which is contained by both *this and r
00089     Rect intersect(const Rect &r) const;
00090 
00091     /// Return the topleft corner of the rectangle
00092     Point topLeft() const {
00093         return Point(x(), y());
00094     }
00095 
00096     /// Return the width of the rectangle
00097     Size size() const {
00098         return Size(width(), height());
00099     }
00100     /// set's the size of the rect.
00101     void setSize(const Size& size) {
00102         resize(size.width(), size.height());
00103     }
00104     /// set's the rect's.
00105     void setCoords(int x1, int y1, int x2, int y2) {
00106         setRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
00107     }
00108 
00109     /// add a margin (smaller rectangle)
00110     Rect operator>>(int margin) const {
00111         return Rect(x() + margin, y() + margin,
00112                     width() - 2 * margin, height() - 2 * margin);
00113     }
00114 
00115     /// add a margin (smaller rectangle)
00116     Rect& operator>>(int margin) {
00117         setRect(x() + margin, y() + margin,
00118                 width() - 2 * margin, height() - 2 * margin);
00119         return *this;
00120     }
00121 
00122     /// subtract a margin (bigger rectangle)
00123     Rect operator<<(int margin) const {
00124         return Rect(x() - margin, y() - margin,
00125                     width() + 2 * margin, height() + 2 * margin);
00126     }
00127 
00128     /// subtract a margin (bigger rectangle)
00129     Rect& operator<<(int margin) {
00130         setRect(x() - margin, y() - margin,
00131                 width() + 2 * margin, height() + 2 * margin);
00132         return *this;
00133     }
00134 
00135     void setLeft(int x1) {
00136         setRect(x1, y(), x() - x1 + width(), height());
00137     }
00138     void setTop(int y1) {
00139         setRect(x(), y1, width(), y() - y1 + height());
00140     }
00141     void setRight(int x2) {
00142         setRect(x(), y(), x2 - x() + 1, height());
00143     }
00144     void setBottom(int y2) {
00145         setRect(x(), y(), width(), y2 - y() + 1);
00146     }
00147 
00148     bool clipLine(int& x1, int& y1, int& x2, int& y2) const;
00149     bool clipLine(Point& p1, Point& p2) const;
00150 
00151     Rect operator&(const Rect& other) const {
00152         return intersect(other);
00153     };
00154 
00155     Rect& operator&=(const Rect& other) {
00156         *this = intersect(other);
00157         return *this;
00158     };
00159 
00160     static Rect align(const Rect& container, const Size& size, int alignment);
00161 
00162 protected:
00163     typedef enum {
00164         Left = 1 << 0,
00165         Right = 1 << 1,
00166         Top = 1 << 2,
00167         Bottom = 1 << 3
00168     } BorderCross;
00169 
00170     int borderCross(int x, int y) const;
00171 };
00172 
00173 /// Print a rectangle to a stream, formatted as (x,y,w,h)
00174 std::ostream& operator<<(std::ostream& s, const Rect& p);
00175 
00176 typedef std::vector<Rect> RectArray;
00177 } // namespace Wt
00178 
00179 #endif // WT_RECT_H

Generated Fri Jul 28 19:23:00 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.