| 1 |
/* |
| 2 |
Copyright (C) 2002 Kai Sterker <[email protected]> |
| 3 |
Part of the Adonthell Project <http://adonthell.nongnu.org> |
| 4 |
|
| 5 |
Dlgedit is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Dlgedit is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with Dlgedit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @file dlg_rect.h |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief A wrapper around GdkRectangle. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef DLG_RECT_H |
| 27 |
#define DLG_RECT_H |
| 28 |
|
| 29 |
#include "dlg_point.h" |
| 30 |
|
| 31 |
/** |
| 32 |
* The representation of a rectangle, defined by its upper left, and |
| 33 |
* bottom right corners. |
| 34 |
* |
| 35 |
* This class provides an abstraction from the GdkRectangle type |
| 36 |
* defined in GTK+, to remain as independent from the actual GUI |
| 37 |
* toolkit as possible. |
| 38 |
* |
| 39 |
* Moreover, it extends the functionality of GdkRectangle by providing |
| 40 |
* various query and comparison operations. |
| 41 |
*/ |
| 42 |
class DlgRect |
| 43 |
{ |
| 44 |
public: |
| 45 |
/** |
| 46 |
* Default-constructor. |
| 47 |
*/ |
| 48 |
DlgRect () { } |
| 49 |
/** |
| 50 |
* Cast a GdkRectangle to a %DlgRect. |
| 51 |
* @param rect The GdkRectangle to convert. |
| 52 |
*/ |
| 53 |
DlgRect (GdkRectangle &rect); |
| 54 |
/** |
| 55 |
* Create a rectangle from two points. No matter where point a is |
| 56 |
* located relative to point b, the resulting rectangle will always |
| 57 |
* be valid (i.e. it's top left corner will be left, and above from |
| 58 |
* the bottom right corner). |
| 59 |
* @param a The first point. |
| 60 |
* @param b The second point. |
| 61 |
*/ |
| 62 |
DlgRect (DlgPoint &a, DlgPoint &b) { init (a, b); } |
| 63 |
/** |
| 64 |
* Create a rectangle from a given origin and it's width and height. |
| 65 |
* @param p The origin (top, left corner) of the new rectangle |
| 66 |
* @param width the width |
| 67 |
* @param height the height |
| 68 |
*/ |
| 69 |
DlgRect (DlgPoint &p, int width, int height); |
| 70 |
|
| 71 |
/** |
| 72 |
* @name Queries for Attributes |
| 73 |
*/ |
| 74 |
//@{ |
| 75 |
/** |
| 76 |
* Retrieve the absolute position of the rectangle's top left corner. |
| 77 |
* @return a reference to the top left corner. |
| 78 |
*/ |
| 79 |
const DlgPoint &topLeft () const { return top_left; } |
| 80 |
/** |
| 81 |
* Retrieve the the absolute position of the rectangle's bottom right corner. |
| 82 |
* @return a reference to bottom right corner. |
| 83 |
*/ |
| 84 |
const DlgPoint &bottomRight () const { return bottom_right; } |
| 85 |
/** |
| 86 |
* Retrieve the center of the rectangle. |
| 87 |
* @return the absolute position of the rectangle's center. |
| 88 |
*/ |
| 89 |
DlgPoint center () const { return DlgPoint (x () + width ()/2, y () + height ()/2); } |
| 90 |
|
| 91 |
/** |
| 92 |
* Retrieve the rectangle's x coordinate, i.e. the absolute x value |
| 93 |
* of it's top left corner. |
| 94 |
* @return the x value of the top left corner. |
| 95 |
*/ |
| 96 |
int x () const { return top_left.x (); } |
| 97 |
/** |
| 98 |
* Retrieve the rectangle's y coordinate, i.e. the absolute y value |
| 99 |
* of it's top left corner. |
| 100 |
* @return the y value of the top left corner. |
| 101 |
*/ |
| 102 |
int y () const { return top_left.y (); } |
| 103 |
/** |
| 104 |
* Retrieve the rectangle's width, i.e. the distance between it's |
| 105 |
* left and right side. |
| 106 |
* @return the width of the rectangle. |
| 107 |
*/ |
| 108 |
int width () const { return bottom_right.x () - x (); } |
| 109 |
/** |
| 110 |
* Retrieve the rectangle's height, i.e. the distance between it's |
| 111 |
* top and bottom side. |
| 112 |
* @return the height of the rectangle. |
| 113 |
*/ |
| 114 |
int height () const { return bottom_right.y () - y (); } |
| 115 |
//@} |
| 116 |
|
| 117 |
/** |
| 118 |
* @name Various geometrical Tests |
| 119 |
*/ |
| 120 |
//@{ |
| 121 |
/** |
| 122 |
* Test whether a given point lies within the rectangle's boundaries. |
| 123 |
* @param point The point to test against. |
| 124 |
* @return <b>true</b> when the point lies inside the rectangle, |
| 125 |
* <b>false</b> otherwise. |
| 126 |
*/ |
| 127 |
bool contains (const DlgPoint &point); |
| 128 |
|
| 129 |
/** |
| 130 |
* Test whether a given rectangle lies completely within this rectangle's |
| 131 |
* boundaries, or overlaps it in some fashion. |
| 132 |
* @param rect The rectangle to test against. |
| 133 |
* @return <b>true</b> when rect lies (partly) within the rectangle's |
| 134 |
* boundaries, <b>false</b> otherwise. |
| 135 |
*/ |
| 136 |
bool contains (const DlgRect &rect); |
| 137 |
//@} |
| 138 |
|
| 139 |
/** |
| 140 |
* Make a copy of this rectangled, inflated by the specified amount. |
| 141 |
* @param x Value to inflate in x dimensions. |
| 142 |
* @param y Value to inflate the y dimensions. |
| 143 |
* @return A new, inflated rectangle with the same center as this one. |
| 144 |
*/ |
| 145 |
DlgRect inflate (int x, int y); |
| 146 |
|
| 147 |
/** |
| 148 |
* Grow all four sides of this rectangle. |
| 149 |
* @param x Value to grow in x dimensions. |
| 150 |
* @param y Value to grow in y dimensions. |
| 151 |
* @return A new, inflated rectangle with the same center as this one. |
| 152 |
*/ |
| 153 |
void grow (int x, int y); |
| 154 |
|
| 155 |
/** |
| 156 |
* Move the rectangle to a new position. |
| 157 |
* @param pos The new position of the rectangle |
| 158 |
*/ |
| 159 |
void setPos (const DlgPoint &pos); |
| 160 |
|
| 161 |
/** |
| 162 |
* Move this rectangle by the specified amount. |
| 163 |
* @param p Point representing the offset this rectangle shall be moved |
| 164 |
*/ |
| 165 |
void move (DlgPoint &p); |
| 166 |
|
| 167 |
/** |
| 168 |
* Change the size of the rectangle. |
| 169 |
* @param widht The new width. |
| 170 |
* @param height The new height. |
| 171 |
*/ |
| 172 |
void resize (int width, int height); |
| 173 |
|
| 174 |
/** |
| 175 |
* Convert a DlgRect into a GdkRectangle. |
| 176 |
*/ |
| 177 |
operator GdkRectangle(); |
| 178 |
|
| 179 |
/** |
| 180 |
* Compare the positions of two rectangles. If they have the same y |
| 181 |
* value, the x value determines the result, otherwise the y value. |
| 182 |
* @param rect The rectangle to compare with. |
| 183 |
* @return <b>true</b> if the given rectangle is smaller than this one, |
| 184 |
* <b>false</b> otherwise. |
| 185 |
*/ |
| 186 |
bool operator< (DlgRect &rect); |
| 187 |
|
| 188 |
protected: |
| 189 |
DlgPoint top_left; |
| 190 |
DlgPoint bottom_right; |
| 191 |
|
| 192 |
void init (DlgPoint &a, DlgPoint &b); |
| 193 |
}; |
| 194 |
|
| 195 |
#endif // DLG_RECT_H |