| 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.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief A wrapper around GdkRectangle. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "dlg_rect.h" |
| 27 |
|
| 28 |
// Cast GdkRectangle to DlgRect |
| 29 |
DlgRect::DlgRect (GdkRectangle &rect) |
| 30 |
{ |
| 31 |
top_left = DlgPoint (rect.x, rect.y); |
| 32 |
bottom_right = DlgPoint (x () + rect.width, y () + rect.height); |
| 33 |
} |
| 34 |
|
| 35 |
// Cast DlgRect to GdkRectangle |
| 36 |
DlgRect::operator GdkRectangle () |
| 37 |
{ |
| 38 |
GdkRectangle rect; |
| 39 |
|
| 40 |
rect.x = x (); |
| 41 |
rect.y = y (); |
| 42 |
rect.width = width (); |
| 43 |
rect.height = height (); |
| 44 |
|
| 45 |
return rect; |
| 46 |
} |
| 47 |
|
| 48 |
// Create a valid rectangle from any two points |
| 49 |
void DlgRect::init (DlgPoint &a, DlgPoint &b) |
| 50 |
{ |
| 51 |
top_left = DlgPoint (a.x () < b.x () ? a.x () : b.x (), |
| 52 |
a.y () < b.y () ? a.y () : b.y ()); |
| 53 |
bottom_right = DlgPoint (a.x () < b.x () ? b.x () : a.x (), |
| 54 |
a.y () < b.y () ? b.y () : a.y ()); |
| 55 |
} |
| 56 |
|
| 57 |
// create a Rectangle from a point and width and height |
| 58 |
DlgRect::DlgRect (DlgPoint &p, int width, int height) |
| 59 |
{ |
| 60 |
DlgPoint q (p.x () + width, p.y () + height); |
| 61 |
init (p, q); |
| 62 |
} |
| 63 |
|
| 64 |
// Test whether point lies within this rectangle's boundaries |
| 65 |
bool DlgRect::contains (const DlgPoint &point) |
| 66 |
{ |
| 67 |
if (point.x () < x () || point.x () > bottom_right.x () || |
| 68 |
point.y () < y () || point.y () > bottom_right.y ()) |
| 69 |
return false; |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
// Test whether the given rectangle overlaps with this one |
| 75 |
bool DlgRect::contains (const DlgRect &rect) |
| 76 |
{ |
| 77 |
if (x () <= rect.x ()) { |
| 78 |
if (bottomRight ().x () >= rect.x ()) { |
| 79 |
if (y () <= rect.y ()) { |
| 80 |
if (bottomRight ().y () >= rect.y ()) return true; |
| 81 |
} else { |
| 82 |
if (rect.bottomRight ().y () >= y ()) return true; |
| 83 |
} |
| 84 |
} |
| 85 |
} else { |
| 86 |
if (rect.bottomRight ().x () >= x ()) { |
| 87 |
if (y () <= rect.y ()) { |
| 88 |
if (bottomRight ().y () >= rect.y ()) return true; |
| 89 |
} else { |
| 90 |
if (rect.bottomRight ().y () >= y ()) return true; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
// Create a copy of this rectangle, inflated by the amount specified by p |
| 99 |
DlgRect DlgRect::inflate (int x, int y) |
| 100 |
{ |
| 101 |
DlgPoint tl (top_left.x () - x, top_left.y () - y); |
| 102 |
DlgPoint br (bottom_right.x () + x, bottom_right.y () + y); |
| 103 |
|
| 104 |
return DlgRect (tl, br); |
| 105 |
} |
| 106 |
|
| 107 |
// Grow this rectangle |
| 108 |
void DlgRect::grow (int x, int y) |
| 109 |
{ |
| 110 |
top_left = top_left.offset (-x, -y); |
| 111 |
bottom_right = bottom_right.offset (x, y); |
| 112 |
} |
| 113 |
|
| 114 |
// Move this rect to a new position |
| 115 |
void DlgRect::setPos (const DlgPoint &pos) |
| 116 |
{ |
| 117 |
int w = width (); |
| 118 |
int h = height (); |
| 119 |
|
| 120 |
top_left = pos; |
| 121 |
resize (w, h); |
| 122 |
} |
| 123 |
|
| 124 |
// Move this rect by the given amount |
| 125 |
void DlgRect::move (DlgPoint &p) |
| 126 |
{ |
| 127 |
top_left = DlgPoint (top_left.x () + p.x(), top_left.y() + p.y()); |
| 128 |
bottom_right = DlgPoint (bottom_right.x () + p.x(), bottom_right.y() + p.y()); |
| 129 |
} |
| 130 |
|
| 131 |
// Change the size of the rectangle. |
| 132 |
void DlgRect::resize (int width, int height) |
| 133 |
{ |
| 134 |
bottom_right = DlgPoint (top_left.x () + width, top_left.y () + height); |
| 135 |
} |
| 136 |
|
| 137 |
// compare position of two rects |
| 138 |
bool DlgRect::operator< (DlgRect &rect) |
| 139 |
{ |
| 140 |
return center () < rect.center (); |
| 141 |
} |