1/*
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/Flattenable.h>
21#include <utils/Log.h>
22#include <utils/TypeHelpers.h>
23#include <ui/Point.h>
24
25#include <android/rect.h>
26
27namespace android {
28
29class Rect : public ARect, public LightFlattenablePod<Rect>
30{
31public:
32    typedef ARect::value_type value_type;
33
34    static const Rect INVALID_RECT;
35
36    // we don't provide copy-ctor and operator= on purpose
37    // because we want the compiler generated versions
38
39    inline Rect() {
40      left = right = top = bottom = 0;
41    }
42
43    inline Rect(int32_t w, int32_t h) {
44        left = top = 0;
45        right = w;
46        bottom = h;
47    }
48
49    inline Rect(uint32_t w, uint32_t h) {
50        if (w > INT32_MAX) {
51            ALOG(LOG_WARN, "Rect",
52                    "Width %u too large for Rect class, clamping", w);
53            w = INT32_MAX;
54        }
55        if (h > INT32_MAX) {
56            ALOG(LOG_WARN, "Rect",
57                    "Height %u too large for Rect class, clamping", h);
58            h = INT32_MAX;
59        }
60        left = top = 0;
61        right = w;
62        bottom = h;
63    }
64
65    inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
66        left = l;
67        top = t;
68        right = r;
69        bottom = b;
70    }
71
72    inline Rect(const Point& lt, const Point& rb) {
73        left = lt.x;
74        top = lt.y;
75        right = rb.x;
76        bottom = rb.y;
77    }
78
79    void makeInvalid();
80
81    inline void clear() {
82        left = top = right = bottom = 0;
83    }
84
85    // a valid rectangle has a non negative width and height
86    inline bool isValid() const {
87        return (getWidth() >= 0) && (getHeight() >= 0);
88    }
89
90    // an empty rect has a zero width or height, or is invalid
91    inline bool isEmpty() const {
92        return (getWidth() <= 0) || (getHeight() <= 0);
93    }
94
95    // rectangle's width
96    inline int32_t getWidth() const {
97        return right - left;
98    }
99
100    // rectangle's height
101    inline int32_t getHeight() const {
102        return bottom - top;
103    }
104
105    inline Rect getBounds() const {
106        return Rect(right - left, bottom - top);
107    }
108
109    void setLeftTop(const Point& lt) {
110        left = lt.x;
111        top = lt.y;
112    }
113
114    void setRightBottom(const Point& rb) {
115        right = rb.x;
116        bottom = rb.y;
117    }
118
119    // the following 4 functions return the 4 corners of the rect as Point
120    Point leftTop() const {
121        return Point(left, top);
122    }
123    Point rightBottom() const {
124        return Point(right, bottom);
125    }
126    Point rightTop() const {
127        return Point(right, top);
128    }
129    Point leftBottom() const {
130        return Point(left, bottom);
131    }
132
133    // comparisons
134    inline bool operator == (const Rect& rhs) const {
135        return (left == rhs.left) && (top == rhs.top) &&
136               (right == rhs.right) && (bottom == rhs.bottom);
137    }
138
139    inline bool operator != (const Rect& rhs) const {
140        return !operator == (rhs);
141    }
142
143    // operator < defines an order which allows to use rectangles in sorted
144    // vectors.
145    bool operator < (const Rect& rhs) const;
146
147    const Rect operator + (const Point& rhs) const;
148    const Rect operator - (const Point& rhs) const;
149
150    Rect& operator += (const Point& rhs) {
151        return offsetBy(rhs.x, rhs.y);
152    }
153    Rect& operator -= (const Point& rhs) {
154        return offsetBy(-rhs.x, -rhs.y);
155    }
156
157    Rect& offsetToOrigin() {
158        right -= left;
159        bottom -= top;
160        left = top = 0;
161        return *this;
162    }
163    Rect& offsetTo(const Point& p) {
164        return offsetTo(p.x, p.y);
165    }
166    Rect& offsetBy(const Point& dp) {
167        return offsetBy(dp.x, dp.y);
168    }
169
170    Rect& offsetTo(int32_t x, int32_t y);
171    Rect& offsetBy(int32_t x, int32_t y);
172
173    bool intersect(const Rect& with, Rect* result) const;
174
175    // Create a new Rect by transforming this one using a graphics HAL
176    // transform.  This rectangle is defined in a coordinate space starting at
177    // the origin and extending to (width, height).  If the transform includes
178    // a ROT90 then the output rectangle is defined in a space extending to
179    // (height, width).  Otherwise the output rectangle is in the same space as
180    // the input.
181    Rect transform(uint32_t xform, int32_t width, int32_t height) const;
182
183    // this calculates (Region(*this) - exclude).bounds() efficiently
184    Rect reduce(const Rect& exclude) const;
185
186
187    // for backward compatibility
188    inline int32_t width() const { return getWidth(); }
189    inline int32_t height() const { return getHeight(); }
190    inline void set(const Rect& rhs) { operator = (rhs); }
191};
192
193ANDROID_BASIC_TYPES_TRAITS(Rect)
194
195}; // namespace android
196
197#endif // ANDROID_UI_RECT
198