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