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