1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef UI_GFX_GEOMETRY_INSETS_H_ 6*635a8641SAndroid Build Coastguard Worker #define UI_GFX_GEOMETRY_INSETS_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <string> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include "ui/gfx/geometry/insets_f.h" 11*635a8641SAndroid Build Coastguard Worker #include "ui/gfx/gfx_export.h" 12*635a8641SAndroid Build Coastguard Worker 13*635a8641SAndroid Build Coastguard Worker namespace gfx { 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Worker class Vector2d; 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker // Represents the widths of the four borders or margins of an unspecified 18*635a8641SAndroid Build Coastguard Worker // rectangle. An Insets stores the thickness of the top, left, bottom and right 19*635a8641SAndroid Build Coastguard Worker // edges, without storing the actual size and position of the rectangle itself. 20*635a8641SAndroid Build Coastguard Worker // 21*635a8641SAndroid Build Coastguard Worker // This can be used to represent a space within a rectangle, by "shrinking" the 22*635a8641SAndroid Build Coastguard Worker // rectangle by the inset amount on all four sides. Alternatively, it can 23*635a8641SAndroid Build Coastguard Worker // represent a border that has a different thickness on each side. 24*635a8641SAndroid Build Coastguard Worker class GFX_EXPORT Insets { 25*635a8641SAndroid Build Coastguard Worker public: Insets()26*635a8641SAndroid Build Coastguard Worker constexpr Insets() : top_(0), left_(0), bottom_(0), right_(0) {} Insets(int all)27*635a8641SAndroid Build Coastguard Worker constexpr explicit Insets(int all) 28*635a8641SAndroid Build Coastguard Worker : top_(all), left_(all), bottom_(all), right_(all) {} Insets(int vertical,int horizontal)29*635a8641SAndroid Build Coastguard Worker constexpr Insets(int vertical, int horizontal) 30*635a8641SAndroid Build Coastguard Worker : top_(vertical), 31*635a8641SAndroid Build Coastguard Worker left_(horizontal), 32*635a8641SAndroid Build Coastguard Worker bottom_(vertical), 33*635a8641SAndroid Build Coastguard Worker right_(horizontal) {} Insets(int top,int left,int bottom,int right)34*635a8641SAndroid Build Coastguard Worker constexpr Insets(int top, int left, int bottom, int right) 35*635a8641SAndroid Build Coastguard Worker : top_(top), left_(left), bottom_(bottom), right_(right) {} 36*635a8641SAndroid Build Coastguard Worker top()37*635a8641SAndroid Build Coastguard Worker constexpr int top() const { return top_; } left()38*635a8641SAndroid Build Coastguard Worker constexpr int left() const { return left_; } bottom()39*635a8641SAndroid Build Coastguard Worker constexpr int bottom() const { return bottom_; } right()40*635a8641SAndroid Build Coastguard Worker constexpr int right() const { return right_; } 41*635a8641SAndroid Build Coastguard Worker 42*635a8641SAndroid Build Coastguard Worker // Returns the total width taken up by the insets, which is the sum of the 43*635a8641SAndroid Build Coastguard Worker // left and right insets. width()44*635a8641SAndroid Build Coastguard Worker constexpr int width() const { return left_ + right_; } 45*635a8641SAndroid Build Coastguard Worker 46*635a8641SAndroid Build Coastguard Worker // Returns the total height taken up by the insets, which is the sum of the 47*635a8641SAndroid Build Coastguard Worker // top and bottom insets. height()48*635a8641SAndroid Build Coastguard Worker constexpr int height() const { return top_ + bottom_; } 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker // Returns true if the insets are empty. IsEmpty()51*635a8641SAndroid Build Coastguard Worker bool IsEmpty() const { return width() == 0 && height() == 0; } 52*635a8641SAndroid Build Coastguard Worker Set(int top,int left,int bottom,int right)53*635a8641SAndroid Build Coastguard Worker void Set(int top, int left, int bottom, int right) { 54*635a8641SAndroid Build Coastguard Worker top_ = top; 55*635a8641SAndroid Build Coastguard Worker left_ = left; 56*635a8641SAndroid Build Coastguard Worker bottom_ = bottom; 57*635a8641SAndroid Build Coastguard Worker right_ = right; 58*635a8641SAndroid Build Coastguard Worker } 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker bool operator==(const Insets& insets) const { 61*635a8641SAndroid Build Coastguard Worker return top_ == insets.top_ && left_ == insets.left_ && 62*635a8641SAndroid Build Coastguard Worker bottom_ == insets.bottom_ && right_ == insets.right_; 63*635a8641SAndroid Build Coastguard Worker } 64*635a8641SAndroid Build Coastguard Worker 65*635a8641SAndroid Build Coastguard Worker bool operator!=(const Insets& insets) const { 66*635a8641SAndroid Build Coastguard Worker return !(*this == insets); 67*635a8641SAndroid Build Coastguard Worker } 68*635a8641SAndroid Build Coastguard Worker 69*635a8641SAndroid Build Coastguard Worker void operator+=(const Insets& insets) { 70*635a8641SAndroid Build Coastguard Worker top_ += insets.top_; 71*635a8641SAndroid Build Coastguard Worker left_ += insets.left_; 72*635a8641SAndroid Build Coastguard Worker bottom_ += insets.bottom_; 73*635a8641SAndroid Build Coastguard Worker right_ += insets.right_; 74*635a8641SAndroid Build Coastguard Worker } 75*635a8641SAndroid Build Coastguard Worker 76*635a8641SAndroid Build Coastguard Worker void operator-=(const Insets& insets) { 77*635a8641SAndroid Build Coastguard Worker top_ -= insets.top_; 78*635a8641SAndroid Build Coastguard Worker left_ -= insets.left_; 79*635a8641SAndroid Build Coastguard Worker bottom_ -= insets.bottom_; 80*635a8641SAndroid Build Coastguard Worker right_ -= insets.right_; 81*635a8641SAndroid Build Coastguard Worker } 82*635a8641SAndroid Build Coastguard Worker 83*635a8641SAndroid Build Coastguard Worker Insets operator-() const { 84*635a8641SAndroid Build Coastguard Worker return Insets(-top_, -left_, -bottom_, -right_); 85*635a8641SAndroid Build Coastguard Worker } 86*635a8641SAndroid Build Coastguard Worker Scale(float scale)87*635a8641SAndroid Build Coastguard Worker Insets Scale(float scale) const { 88*635a8641SAndroid Build Coastguard Worker return Scale(scale, scale); 89*635a8641SAndroid Build Coastguard Worker } 90*635a8641SAndroid Build Coastguard Worker Scale(float x_scale,float y_scale)91*635a8641SAndroid Build Coastguard Worker Insets Scale(float x_scale, float y_scale) const { 92*635a8641SAndroid Build Coastguard Worker return Insets(static_cast<int>(top() * y_scale), 93*635a8641SAndroid Build Coastguard Worker static_cast<int>(left() * x_scale), 94*635a8641SAndroid Build Coastguard Worker static_cast<int>(bottom() * y_scale), 95*635a8641SAndroid Build Coastguard Worker static_cast<int>(right() * x_scale)); 96*635a8641SAndroid Build Coastguard Worker } 97*635a8641SAndroid Build Coastguard Worker 98*635a8641SAndroid Build Coastguard Worker // Adjusts the vertical and horizontal dimensions by the values described in 99*635a8641SAndroid Build Coastguard Worker // |vector|. Offsetting insets before applying to a rectangle would be 100*635a8641SAndroid Build Coastguard Worker // equivalent to offseting the rectangle then applying the insets. 101*635a8641SAndroid Build Coastguard Worker Insets Offset(const gfx::Vector2d& vector) const; 102*635a8641SAndroid Build Coastguard Worker InsetsF()103*635a8641SAndroid Build Coastguard Worker operator InsetsF() const { 104*635a8641SAndroid Build Coastguard Worker return InsetsF(static_cast<float>(top()), static_cast<float>(left()), 105*635a8641SAndroid Build Coastguard Worker static_cast<float>(bottom()), static_cast<float>(right())); 106*635a8641SAndroid Build Coastguard Worker } 107*635a8641SAndroid Build Coastguard Worker 108*635a8641SAndroid Build Coastguard Worker // Returns a string representation of the insets. 109*635a8641SAndroid Build Coastguard Worker std::string ToString() const; 110*635a8641SAndroid Build Coastguard Worker 111*635a8641SAndroid Build Coastguard Worker private: 112*635a8641SAndroid Build Coastguard Worker int top_; 113*635a8641SAndroid Build Coastguard Worker int left_; 114*635a8641SAndroid Build Coastguard Worker int bottom_; 115*635a8641SAndroid Build Coastguard Worker int right_; 116*635a8641SAndroid Build Coastguard Worker }; 117*635a8641SAndroid Build Coastguard Worker 118*635a8641SAndroid Build Coastguard Worker inline Insets operator+(Insets lhs, const Insets& rhs) { 119*635a8641SAndroid Build Coastguard Worker lhs += rhs; 120*635a8641SAndroid Build Coastguard Worker return lhs; 121*635a8641SAndroid Build Coastguard Worker } 122*635a8641SAndroid Build Coastguard Worker 123*635a8641SAndroid Build Coastguard Worker inline Insets operator-(Insets lhs, const Insets& rhs) { 124*635a8641SAndroid Build Coastguard Worker lhs -= rhs; 125*635a8641SAndroid Build Coastguard Worker return lhs; 126*635a8641SAndroid Build Coastguard Worker } 127*635a8641SAndroid Build Coastguard Worker 128*635a8641SAndroid Build Coastguard Worker } // namespace gfx 129*635a8641SAndroid Build Coastguard Worker 130*635a8641SAndroid Build Coastguard Worker #endif // UI_GFX_GEOMETRY_INSETS_H_ 131