Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_GFX_RANGE_RANGE_H_ | 5 #ifndef UI_GFX_RANGE_RANGE_H_ |
| 6 #define UI_GFX_RANGE_RANGE_H_ | 6 #define UI_GFX_RANGE_RANGE_H_ |
| 7 | 7 |
| 8 #include <ostream> | 8 #include <ostream> |
| 9 #include <sstream> | |
| 9 #include <string> | 10 #include <string> |
| 10 | 11 |
| 11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 12 #include "ui/gfx/gfx_export.h" | 13 #include "ui/gfx/gfx_export.h" |
| 13 | 14 |
| 14 #if defined(OS_MACOSX) | 15 #if defined(OS_MACOSX) |
| 15 #if __OBJC__ | 16 #if __OBJC__ |
| 16 #import <Foundation/Foundation.h> | 17 #import <Foundation/Foundation.h> |
| 17 #else | 18 #else |
| 18 typedef struct _NSRange NSRange; | 19 typedef struct _NSRange NSRange; |
| 19 #endif | 20 #endif |
| 20 #endif // defined(OS_MACOSX) | 21 #endif // defined(OS_MACOSX) |
| 21 | 22 |
| 22 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
| 23 #include <windows.h> | 24 #include <windows.h> |
| 24 #include <richedit.h> | 25 #include <richedit.h> |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 namespace gfx { | 28 namespace gfx { |
| 28 | 29 |
| 29 // A Range contains two integer values that represent a numeric range, like the | 30 template <typename T> |
|
Robert Sesek
2015/01/30 20:47:02
This comment was lost; please restore it.
| |
| 30 // range of characters in a text selection. A range is made of a start and end | 31 class TRangeBase; |
| 31 // position; when they are the same, the Range is akin to a caret. Note that | 32 |
| 32 // |start_| can be greater than |end_| to respect the directionality of the | 33 namespace internal { |
| 33 // range. | 34 |
| 34 class GFX_EXPORT Range { | 35 template <typename T> |
| 36 TRangeBase<T> InvalidRange(); | |
| 37 | |
| 38 template <> | |
| 39 GFX_EXPORT TRangeBase<size_t> InvalidRange<size_t>(); | |
|
Robert Sesek
2015/01/30 20:47:02
Why do these need to be GFX_EXPORT if they're inte
ckocagil
2015/01/31 02:44:39
Done.
| |
| 40 | |
| 41 template <> | |
| 42 GFX_EXPORT TRangeBase<float> InvalidRange<float>(); | |
| 43 | |
| 44 GFX_EXPORT bool IsValidRange(const TRangeBase<size_t>& range); | |
| 45 GFX_EXPORT bool IsValidRange(const TRangeBase<float>& range); | |
| 46 | |
| 47 GFX_EXPORT size_t RangeLength(const TRangeBase<size_t>& range); | |
| 48 GFX_EXPORT float RangeLength(const TRangeBase<float>& range); | |
| 49 | |
| 50 GFX_EXPORT bool RangeIsEmpty(const TRangeBase<size_t>& range); | |
| 51 GFX_EXPORT bool RangeIsEmpty(const TRangeBase<float>& range); | |
| 52 | |
| 53 } // namespace internal | |
| 54 | |
| 55 template <typename T> | |
| 56 class TRangeBase { | |
| 57 public: | |
| 58 // Initializes the range with a start and end. | |
| 59 TRangeBase(T start, T end) : start_(start), end_(end) {} | |
| 60 | |
| 61 TRangeBase(const TRangeBase<T>& other) | |
| 62 : start_(other.start()), end_(other.end()) {} | |
| 63 | |
| 64 // Returns a range that is invalid. | |
| 65 static const TRangeBase<T> InvalidRange() { | |
| 66 return internal::InvalidRange<T>(); | |
| 67 } | |
| 68 | |
| 69 // Checks if the range is valid. | |
| 70 bool IsValid() const { | |
| 71 return internal::IsValidRange(*this); | |
| 72 } | |
| 73 | |
| 74 // Getters and setters. | |
| 75 T start() const { return start_; } | |
| 76 void set_start(T start) { start_ = start; } | |
| 77 | |
| 78 T end() const { return end_; } | |
| 79 void set_end(T end) { end_ = end; } | |
| 80 | |
| 81 // Returns the absolute value of the length. | |
| 82 T length() const { | |
| 83 return internal::RangeLength(*this); | |
| 84 } | |
| 85 | |
| 86 bool is_reversed() const { return start_ > end_; } | |
| 87 bool is_empty() const { | |
| 88 return internal::RangeIsEmpty(*this); | |
| 89 } | |
| 90 | |
| 91 // Returns the minimum and maximum values. | |
| 92 T GetMin() const { return std::min(start_, end_); } | |
| 93 T GetMax() const { return std::max(start_, end_); } | |
| 94 | |
| 95 bool operator==(const TRangeBase& other) const { | |
| 96 return start_ == other.start() && end_ == other.end(); | |
| 97 } | |
| 98 bool operator!=(const TRangeBase& other) const { | |
| 99 return !(*this == other); | |
| 100 } | |
| 101 bool EqualsIgnoringDirection(const TRangeBase& other) const { | |
| 102 return GetMin() == other.GetMin() && GetMax() == other.GetMax(); | |
| 103 } | |
| 104 | |
| 105 // Returns true if this range intersects the specified |range|. | |
| 106 bool Intersects(const TRangeBase& range) const { | |
| 107 return IsValid() && range.IsValid() && | |
| 108 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax()); | |
| 109 } | |
| 110 | |
| 111 // Returns true if this range contains the specified |range|. | |
| 112 bool Contains(const TRangeBase& range) const { | |
| 113 return IsValid() && range.IsValid() && | |
| 114 GetMin() <= range.GetMin() && range.GetMax() <= GetMax(); | |
| 115 } | |
| 116 | |
| 117 // Computes the intersection of this range with the given |range|. | |
| 118 // If they don't intersect, it returns an InvalidRange(). | |
| 119 // The returned range is always empty or forward (never reversed). | |
| 120 TRangeBase Intersect(const TRangeBase& range) const { | |
| 121 T min = std::max(GetMin(), range.GetMin()); | |
| 122 T max = std::min(GetMax(), range.GetMax()); | |
| 123 | |
| 124 if (min >= max) // No intersection. | |
| 125 return InvalidRange(); | |
| 126 | |
| 127 return TRangeBase(min, max); | |
| 128 } | |
| 129 | |
| 130 std::string ToString() const { | |
| 131 std::ostringstream stream; | |
| 132 stream << "{" << start_ << "," << end_ << "}"; | |
| 133 return stream.str(); | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 T start_; | |
| 138 T end_; | |
| 139 }; | |
| 140 | |
| 141 // TODO(ckocagil): Move these constructors to TRangeBase and use delegated | |
| 142 // constructors (C++11) after the feature is allowed. | |
| 143 class Range : public TRangeBase<size_t> { | |
|
Robert Sesek
2015/01/30 20:47:02
This class should be GFX_EXPORT.
ckocagil
2015/01/31 02:44:39
Done. (ditto RangeF)
| |
| 35 public: | 144 public: |
| 36 // Creates an empty range {0,0}. | 145 // Creates an empty range {0,0}. |
| 37 Range(); | 146 Range() : TRangeBase(0, 0) {} |
| 38 | 147 |
| 39 // Initializes the range with a start and end. | 148 // Initializes the range with a start and end. |
| 40 Range(size_t start, size_t end); | 149 Range(size_t start, size_t end) : TRangeBase(start, end) {} |
| 150 | |
| 151 Range(const TRangeBase<size_t>& other) : TRangeBase(other) {} | |
| 41 | 152 |
| 42 // Initializes the range with the same start and end positions. | 153 // Initializes the range with the same start and end positions. |
| 43 explicit Range(size_t position); | 154 explicit Range(size_t position) : TRangeBase(position, position) {} |
| 44 | 155 |
| 45 // Platform constructors. | 156 // Platform constructors. |
| 46 #if defined(OS_MACOSX) | 157 #if defined(OS_MACOSX) |
| 47 explicit Range(const NSRange& range); | 158 explicit Range(const NSRange& range); |
| 48 #elif defined(OS_WIN) | 159 #elif defined(OS_WIN) |
| 49 // The |total_length| paramater should be used if the CHARRANGE is set to | 160 // The |total_length| paramater should be used if the CHARRANGE is set to |
| 50 // {0,-1} to indicate the whole range. | 161 // {0,-1} to indicate the whole range. |
| 51 Range(const CHARRANGE& range, LONG total_length = -1); | 162 GFX_EXPORT Range(const CHARRANGE& range, LONG total_length = -1); |
|
Robert Sesek
2015/01/30 20:47:02
Why is this ctor GFX_EXPORT but none of the others
ckocagil
2015/01/31 02:44:39
Removed, the class is itself GFX_EXPORT now. Same
| |
| 52 #endif | 163 #endif |
| 53 | 164 |
| 54 // Returns a range that is invalid, which is {size_t_max,size_t_max}. | |
| 55 static const Range InvalidRange(); | |
| 56 | |
| 57 // Checks if the range is valid through comparision to InvalidRange(). | |
| 58 bool IsValid() const; | |
| 59 | |
| 60 // Getters and setters. | |
| 61 size_t start() const { return start_; } | |
| 62 void set_start(size_t start) { start_ = start; } | |
| 63 | |
| 64 size_t end() const { return end_; } | |
| 65 void set_end(size_t end) { end_ = end; } | |
| 66 | |
| 67 // Returns the absolute value of the length. | |
| 68 size_t length() const { | |
| 69 ptrdiff_t length = end() - start(); | |
| 70 return length >= 0 ? length : -length; | |
| 71 } | |
| 72 | |
| 73 bool is_reversed() const { return start() > end(); } | |
| 74 bool is_empty() const { return start() == end(); } | |
| 75 | |
| 76 // Returns the minimum and maximum values. | |
| 77 size_t GetMin() const; | |
| 78 size_t GetMax() const; | |
| 79 | |
| 80 bool operator==(const Range& other) const; | |
| 81 bool operator!=(const Range& other) const; | |
| 82 bool EqualsIgnoringDirection(const Range& other) const; | |
| 83 | |
| 84 // Returns true if this range intersects the specified |range|. | |
| 85 bool Intersects(const Range& range) const; | |
| 86 | |
| 87 // Returns true if this range contains the specified |range|. | |
| 88 bool Contains(const Range& range) const; | |
| 89 | |
| 90 // Computes the intersection of this range with the given |range|. | |
| 91 // If they don't intersect, it returns an InvalidRange(). | |
| 92 // The returned range is always empty or forward (never reversed). | |
| 93 Range Intersect(const Range& range) const; | |
| 94 | |
| 95 #if defined(OS_MACOSX) | 165 #if defined(OS_MACOSX) |
| 96 Range& operator=(const NSRange& range); | 166 Range& operator=(const NSRange& range); |
| 97 | 167 |
| 98 // NSRange does not store the directionality of a range, so if this | 168 // NSRange does not store the directionality of a range, so if this |
| 99 // is_reversed(), the range will get flipped when converted to an NSRange. | 169 // is_reversed(), the range will get flipped when converted to an NSRange. |
| 100 NSRange ToNSRange() const; | 170 NSRange ToNSRange() const; |
| 101 #elif defined(OS_WIN) | 171 #elif defined(OS_WIN) |
| 102 CHARRANGE ToCHARRANGE() const; | 172 GFX_EXPORT CHARRANGE ToCHARRANGE() const; |
|
Robert Sesek
2015/01/30 20:47:02
Same, why is this GFX_EXPORT?
| |
| 103 #endif | 173 #endif |
| 104 // GTK+ has no concept of a range. | 174 // GTK+ has no concept of a range. |
| 175 }; | |
| 105 | 176 |
| 106 std::string ToString() const; | 177 class RangeF : public TRangeBase<float> { |
|
Robert Sesek
2015/01/30 20:47:02
Same, GFX_EXPORT.
| |
| 178 public: | |
| 179 // Creates an empty range {0,0}. | |
| 180 RangeF() : TRangeBase(0.0f, 0.0f) {} | |
| 107 | 181 |
| 108 private: | 182 // Initializes the range with a start and end. |
| 109 size_t start_; | 183 RangeF(float start, float end) : TRangeBase(start, end) {} |
| 110 size_t end_; | 184 |
| 185 RangeF(const TRangeBase<float>& other) : TRangeBase(other) {} | |
| 186 | |
| 187 // Initializes the range with the same start and end positions. | |
| 188 explicit RangeF(float position) : TRangeBase(position, position) {} | |
| 111 }; | 189 }; |
| 112 | 190 |
| 113 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range); | 191 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range); |
| 192 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const RangeF& range); | |
| 114 | 193 |
| 115 } // namespace gfx | 194 } // namespace gfx |
| 116 | 195 |
| 117 #endif // UI_GFX_RANGE_RANGE_H_ | 196 #endif // UI_GFX_RANGE_RANGE_H_ |
| OLD | NEW |