blob: a213c0930967d729d45deda3f8453fcdea57480e [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 Agopian4b8160f2009-05-27 15:02:35 -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
Mathias Agopian35801ce2009-05-26 17:44:57 -070038 inline Rect() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040 inline Rect(int w, int h)
Mathias Agopian35801ce2009-05-26 17:44:57 -070041 : left(0), top(0), right(w), bottom(h) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043 inline Rect(int l, int t, int r, int b)
Mathias Agopian35801ce2009-05-26 17:44:57 -070044 : left(l), top(t), right(r), bottom(b) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046 inline Rect(const Point& lt, const Point& rb)
Mathias Agopian35801ce2009-05-26 17:44:57 -070047 : left(lt.x), top(lt.y), right(rb.x), bottom(rb.y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 }
49
50 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070051
52 inline void clear() {
53 left = top = right = bottom = 0;
54 }
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 // a valid rectangle has a non negative width and height
57 inline bool isValid() const {
58 return (width()>=0) && (height()>=0);
59 }
60
61 // an empty rect has a zero width or height, or is invalid
62 inline bool isEmpty() const {
63 return (width()<=0) || (height()<=0);
64 }
65
66 inline void set(const Rect& rhs) {
67 operator = (rhs);
68 }
69
70 // rectangle's width
71 inline int width() const {
72 return right-left;
73 }
74
75 // rectangle's height
76 inline int height() const {
77 return bottom-top;
78 }
79
Mathias Agopian35801ce2009-05-26 17:44:57 -070080 void setLeftTop(const Point& lt) {
81 left = lt.x;
82 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 }
Mathias Agopian35801ce2009-05-26 17:44:57 -070084
85 void setRightBottom(const Point& rb) {
86 right = rb.x;
87 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 }
89
90 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -070091 Point leftTop() const {
92 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 }
Mathias Agopian35801ce2009-05-26 17:44:57 -070094 Point rightBottom() const {
95 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 }
97 Point rightTop() const {
98 return Point(right, top);
99 }
100 Point leftBottom() const {
101 return Point(left, bottom);
102 }
103
104 // comparisons
105 inline bool operator == (const Rect& rhs) const {
106 return (left == rhs.left) && (top == rhs.top) &&
107 (right == rhs.right) && (bottom == rhs.bottom);
108 }
109
110 inline bool operator != (const Rect& rhs) const {
111 return !operator == (rhs);
112 }
113
114 // operator < defines an order which allows to use rectangles in sorted
115 // vectors.
116 bool operator < (const Rect& rhs) const;
117
118 Rect& offsetToOrigin() {
119 right -= left;
120 bottom -= top;
121 left = top = 0;
122 return *this;
123 }
124 Rect& offsetTo(const Point& p) {
125 return offsetTo(p.x, p.y);
126 }
127 Rect& offsetBy(const Point& dp) {
128 return offsetBy(dp.x, dp.y);
129 }
130 Rect& operator += (const Point& rhs) {
131 return offsetBy(rhs.x, rhs.y);
132 }
133 Rect& operator -= (const Point& rhs) {
134 return offsetBy(-rhs.x, -rhs.y);
135 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700136 const Rect operator + (const Point& rhs) const;
137 const Rect operator - (const Point& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138
139 void translate(int dx, int dy) { // legacy, don't use.
140 offsetBy(dx, dy);
141 }
142
143 Rect& offsetTo(int x, int y);
144 Rect& offsetBy(int x, int y);
145 bool intersect(const Rect& with, Rect* result) const;
146};
147
148ANDROID_BASIC_TYPES_TRAITS(Rect)
149
150}; // namespace android
151
152#endif // ANDROID_UI_RECT