Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / gfx / rect_base.h
blob8ae2a64e5580d16c0fd389e20877b7ed4335a574
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A template for a simple rectangle class. The containment semantics
6 // are array-like; that is, the coordinate (x, y) is considered to be
7 // contained by the rectangle, but the coordinate (x + width, y) is not.
8 // The class will happily let you create malformed rectangles (that is,
9 // rectangles with negative width and/or height), but there will be assertions
10 // in the operations (such as Contains()) to complain in this case.
12 #ifndef UI_GFX_RECT_BASE_H_
13 #define UI_GFX_RECT_BASE_H_
15 #include <string>
17 #include "base/compiler_specific.h"
19 namespace gfx {
21 template<typename Class,
22 typename PointClass,
23 typename SizeClass,
24 typename InsetsClass,
25 typename VectorClass,
26 typename Type>
27 class UI_EXPORT RectBase {
28 public:
29 Type x() const { return origin_.x(); }
30 void set_x(Type x) { origin_.set_x(x); }
32 Type y() const { return origin_.y(); }
33 void set_y(Type y) { origin_.set_y(y); }
35 Type width() const { return size_.width(); }
36 void set_width(Type width) { size_.set_width(width); }
38 Type height() const { return size_.height(); }
39 void set_height(Type height) { size_.set_height(height); }
41 const PointClass& origin() const { return origin_; }
42 void set_origin(const PointClass& origin) { origin_ = origin; }
44 const SizeClass& size() const { return size_; }
45 void set_size(const SizeClass& size) { size_ = size; }
47 Type right() const { return x() + width(); }
48 Type bottom() const { return y() + height(); }
50 PointClass top_right() const { return PointClass(right(), y()); }
51 PointClass bottom_left() const { return PointClass(x(), bottom()); }
52 PointClass bottom_right() const { return PointClass(right(), bottom()); }
54 VectorClass OffsetFromOrigin() const {
55 return VectorClass(x(), y());
58 void SetRect(Type x, Type y, Type width, Type height);
60 // Shrink the rectangle by a horizontal and vertical distance on all sides.
61 void Inset(Type horizontal, Type vertical) {
62 Inset(horizontal, vertical, horizontal, vertical);
65 // Shrink the rectangle by the given insets.
66 void Inset(const InsetsClass& insets);
68 // Shrink the rectangle by the specified amount on each side.
69 void Inset(Type left, Type top, Type right, Type bottom);
71 // Move the rectangle by a horizontal and vertical distance.
72 void Offset(Type horizontal, Type vertical);
73 void Offset(const VectorClass& distance) {
74 Offset(distance.x(), distance.y());
76 void operator+=(const VectorClass& offset);
77 void operator-=(const VectorClass& offset);
79 InsetsClass InsetsFrom(const Class& inner) const {
80 return InsetsClass(inner.y() - y(),
81 inner.x() - x(),
82 bottom() - inner.bottom(),
83 right() - inner.right());
86 // Returns true if the area of the rectangle is zero.
87 bool IsEmpty() const { return size_.IsEmpty(); }
89 // A rect is less than another rect if its origin is less than
90 // the other rect's origin. If the origins are equal, then the
91 // shortest rect is less than the other. If the origin and the
92 // height are equal, then the narrowest rect is less than.
93 // This comparison is required to use Rects in sets, or sorted
94 // vectors.
95 bool operator<(const Class& other) const;
97 // Returns true if the point identified by point_x and point_y falls inside
98 // this rectangle. The point (x, y) is inside the rectangle, but the
99 // point (x + width, y + height) is not.
100 bool Contains(Type point_x, Type point_y) const;
102 // Returns true if the specified point is contained by this rectangle.
103 bool Contains(const PointClass& point) const {
104 return Contains(point.x(), point.y());
107 // Returns true if this rectangle contains the specified rectangle.
108 bool Contains(const Class& rect) const;
110 // Returns true if this rectangle intersects the specified rectangle.
111 bool Intersects(const Class& rect) const;
113 // Computes the intersection of this rectangle with the given rectangle.
114 void Intersect(const Class& rect);
116 // Computes the union of this rectangle with the given rectangle. The union
117 // is the smallest rectangle containing both rectangles.
118 void Union(const Class& rect);
120 // Computes the rectangle resulting from subtracting |rect| from |this|. If
121 // |rect| does not intersect completely in either the x- or y-direction, then
122 // |*this| does not change. If |rect| contains |this|, then an empty Rect is
123 // the result.
124 void Subtract(const Class& rect);
126 // Fits as much of the receiving rectangle into the supplied rectangle as
127 // possible, becoming the result. For example, if the receiver had
128 // a x-location of 2 and a width of 4, and the supplied rectangle had
129 // an x-location of 0 with a width of 5, the returned rectangle would have
130 // an x-location of 1 with a width of 4.
131 void AdjustToFit(const Class& rect);
133 // Returns the center of this rectangle.
134 PointClass CenterPoint() const;
136 // Becomes a rectangle that has the same center point but with a size capped
137 // at given |size|.
138 void ClampToCenteredSize(const SizeClass& size);
140 // Splits |this| in two halves, |left_half| and |right_half|.
141 void SplitVertically(Class* left_half, Class* right_half) const;
143 // Returns true if this rectangle shares an entire edge (i.e., same width or
144 // same height) with the given rectangle, and the rectangles do not overlap.
145 bool SharesEdgeWith(const Class& rect) const;
147 protected:
148 RectBase(const PointClass& origin, const SizeClass& size)
149 : origin_(origin), size_(size) {}
150 explicit RectBase(const SizeClass& size)
151 : size_(size) {}
152 explicit RectBase(const PointClass& origin)
153 : origin_(origin) {}
154 // Destructor is intentionally made non virtual and protected.
155 // Do not make this public.
156 ~RectBase() {}
158 private:
159 PointClass origin_;
160 SizeClass size_;
163 } // namespace gfx
165 #endif // UI_GFX_RECT_BASE_H_