blob: 902324d51198f1900d87508a40a5d58bf49fd68b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_UI_RECT
18#define ANDROID_UI_RECT
19
20#include <utils/TypeHelpers.h>
21#include <ui/Point.h>
22
23namespace android {
24
25class Rect
26{
27public:
28 int left;
29 int top;
30 int right;
31 int bottom;
32
Mathias Agopian20f68782009-05-11 00:03:41 -070033 typedef int value_type;
34
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035 // we don't provide copy-ctor and operator= on purpose
36 // because we want the compiler generated versions
37
38 inline Rect()
39 {
40 }
41
42 inline Rect(int w, int h)
43 : left(0), top(0), right(w), bottom(h)
44 {
45 }
46
47 inline Rect(int l, int t, int r, int b)
48 : left(l), top(t), right(r), bottom(b)
49 {
50 }
51
52 inline Rect(const Point& lt, const Point& rb)
53 : left(lt.x), top(lt.y), right(rb.x), bottom(rb.y)
54 {
55 }
56
57 void makeInvalid();
58
Mathias Agopian20f68782009-05-11 00:03:41 -070059 inline void clear() {
60 left = top = right = bottom = 0;
61 }
62
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 // a valid rectangle has a non negative width and height
64 inline bool isValid() const {
65 return (width()>=0) && (height()>=0);
66 }
67
68 // an empty rect has a zero width or height, or is invalid
69 inline bool isEmpty() const {
70 return (width()<=0) || (height()<=0);
71 }
72
73 inline void set(const Rect& rhs) {
74 operator = (rhs);
75 }
76
77 // rectangle's width
78 inline int width() const {
79 return right-left;
80 }
81
82 // rectangle's height
83 inline int height() const {
84 return bottom-top;
85 }
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian20f68782009-05-11 00:03:41 -070088 inline Point leftTop() const {
89 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 }
Mathias Agopian20f68782009-05-11 00:03:41 -070091 inline Point rightBottom() const {
92 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 }
Mathias Agopian20f68782009-05-11 00:03:41 -070094 inline Point rightTop() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 return Point(right, top);
96 }
Mathias Agopian20f68782009-05-11 00:03:41 -070097 inline Point leftBottom() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 return Point(left, bottom);
99 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700100
101 inline void setLeftTop(const Point& p) {
102 left = p.x;
103 top = p.y;
104 }
105
106 inline void setRightBottom(const Point& p) {
107 right = p.x;
108 bottom = p.y;
109 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
111 // comparisons
112 inline bool operator == (const Rect& rhs) const {
113 return (left == rhs.left) && (top == rhs.top) &&
114 (right == rhs.right) && (bottom == rhs.bottom);
115 }
116
117 inline bool operator != (const Rect& rhs) const {
118 return !operator == (rhs);
119 }
120
121 // operator < defines an order which allows to use rectangles in sorted
122 // vectors.
123 bool operator < (const Rect& rhs) const;
124
125 Rect& offsetToOrigin() {
126 right -= left;
127 bottom -= top;
128 left = top = 0;
129 return *this;
130 }
131 Rect& offsetTo(const Point& p) {
132 return offsetTo(p.x, p.y);
133 }
134 Rect& offsetBy(const Point& dp) {
135 return offsetBy(dp.x, dp.y);
136 }
137 Rect& operator += (const Point& rhs) {
138 return offsetBy(rhs.x, rhs.y);
139 }
140 Rect& operator -= (const Point& rhs) {
141 return offsetBy(-rhs.x, -rhs.y);
142 }
143 Rect operator + (const Point& rhs) const;
144 Rect operator - (const Point& rhs) const;
145
146 void translate(int dx, int dy) { // legacy, don't use.
147 offsetBy(dx, dy);
148 }
149
150 Rect& offsetTo(int x, int y);
151 Rect& offsetBy(int x, int y);
152 bool intersect(const Rect& with, Rect* result) const;
153};
154
155ANDROID_BASIC_TYPES_TRAITS(Rect)
156
157}; // namespace android
158
159#endif // ANDROID_UI_RECT