• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SIZE_BASE_H_
6 #define UI_GFX_SIZE_BASE_H_
7 
8 #include "ui/gfx/gfx_export.h"
9 
10 namespace gfx {
11 
12 // A size has width and height values.
13 template<typename Class, typename Type>
14 class GFX_EXPORT SizeBase {
15  public:
width()16   Type width() const { return width_; }
height()17   Type height() const { return height_; }
18 
GetArea()19   Type GetArea() const { return width_ * height_; }
20 
SetSize(Type width,Type height)21   void SetSize(Type width, Type height) {
22     set_width(width);
23     set_height(height);
24   }
25 
Enlarge(Type width,Type height)26   void Enlarge(Type width, Type height) {
27     set_width(width_ + width);
28     set_height(height_ + height);
29   }
30 
set_width(Type width)31   void set_width(Type width) {
32     width_ = width < 0 ? 0 : width;
33   }
set_height(Type height)34   void set_height(Type height) {
35     height_ = height < 0 ? 0 : height;
36   }
37 
SetToMin(const Class & other)38   void SetToMin(const Class& other) {
39     width_ = width_ <= other.width_ ? width_ : other.width_;
40     height_ = height_ <= other.height_ ? height_ : other.height_;
41   }
42 
SetToMax(const Class & other)43   void SetToMax(const Class& other) {
44     width_ = width_ >= other.width_ ? width_ : other.width_;
45     height_ = height_ >= other.height_ ? height_ : other.height_;
46   }
47 
IsEmpty()48   bool IsEmpty() const {
49     return (width_ == 0) || (height_ == 0);
50   }
51 
52  protected:
SizeBase(Type width,Type height)53   SizeBase(Type width, Type height)
54       : width_(width < 0 ? 0 : width),
55       height_(height < 0 ? 0 : height) {
56   }
57 
58   // Destructor is intentionally made non virtual and protected.
59   // Do not make this public.
~SizeBase()60   ~SizeBase() {}
61 
62  private:
63   Type width_;
64   Type height_;
65 };
66 
67 }  // namespace gfx
68 
69 #endif  // UI_GFX_SIZE_BASE_H_
70