Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / geometry / size.h
blob7c0b8cf8ff8930d2c3e42cc4be5eab9fec3d3b56
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 #ifndef UI_GFX_GEOMETRY_SIZE_H_
6 #define UI_GFX_GEOMETRY_SIZE_H_
8 #include <iosfwd>
9 #include <string>
11 #include "base/compiler_specific.h"
12 #include "ui/gfx/geometry/size_f.h"
13 #include "ui/gfx/gfx_export.h"
15 #if defined(OS_WIN)
16 typedef struct tagSIZE SIZE;
17 #elif defined(OS_MACOSX)
18 typedef struct CGSize CGSize;
19 #endif
21 namespace gfx {
23 // A size has width and height values.
24 class GFX_EXPORT Size {
25 public:
26 Size() : width_(0), height_(0) {}
27 Size(int width, int height)
28 : width_(width < 0 ? 0 : width), height_(height < 0 ? 0 : height) {}
29 #if defined(OS_MACOSX)
30 explicit Size(const CGSize& s);
31 #endif
33 ~Size() {}
35 #if defined(OS_MACOSX)
36 Size& operator=(const CGSize& s);
37 #endif
39 #if defined(OS_WIN)
40 SIZE ToSIZE() const;
41 #elif defined(OS_MACOSX)
42 CGSize ToCGSize() const;
43 #endif
45 int width() const { return width_; }
46 int height() const { return height_; }
48 void set_width(int width) { width_ = width < 0 ? 0 : width; }
49 void set_height(int height) { height_ = height < 0 ? 0 : height; }
51 int GetArea() const;
53 void SetSize(int width, int height) {
54 set_width(width);
55 set_height(height);
58 void Enlarge(int grow_width, int grow_height);
60 void SetToMin(const Size& other);
61 void SetToMax(const Size& other);
63 bool IsEmpty() const { return !width() || !height(); }
65 operator SizeF() const {
66 return SizeF(static_cast<float>(width()), static_cast<float>(height()));
69 std::string ToString() const;
71 private:
72 int width_;
73 int height_;
76 inline bool operator==(const Size& lhs, const Size& rhs) {
77 return lhs.width() == rhs.width() && lhs.height() == rhs.height();
80 inline bool operator!=(const Size& lhs, const Size& rhs) {
81 return !(lhs == rhs);
84 // This is declared here for use in gtest-based unit tests but is defined in
85 // the gfx_test_support target. Depend on that to use this in your unit test.
86 // This should not be used in production code - call ToString() instead.
87 void PrintTo(const Size& size, ::std::ostream* os);
89 } // namespace gfx
91 #endif // UI_GFX_GEOMETRY_SIZE_H_