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_RANGE_RANGE_H_ 6 #define UI_GFX_RANGE_RANGE_H_ 7 8 #include <ostream> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "ui/gfx/gfx_export.h" 13 14 #if defined(OS_MACOSX) 15 #if __OBJC__ 16 #import <Foundation/Foundation.h> 17 #else 18 typedef struct _NSRange NSRange; 19 #endif 20 #endif // defined(OS_MACOSX) 21 22 #if defined(OS_WIN) 23 #include <windows.h> 24 #include <richedit.h> 25 #endif 26 27 namespace gfx { 28 29 // A Range contains two integer values that represent a numeric range, like the 30 // range of characters in a text selection. A range is made of a start and end 31 // position; when they are the same, the Range is akin to a caret. Note that 32 // |start_| can be greater than |end_| to respect the directionality of the 33 // range. 34 class GFX_EXPORT Range { 35 public: 36 // Creates an empty range {0,0}. 37 Range(); 38 39 // Initializes the range with a start and end. 40 Range(size_t start, size_t end); 41 42 // Initializes the range with the same start and end positions. 43 explicit Range(size_t position); 44 45 // Platform constructors. 46 #if defined(OS_MACOSX) 47 explicit Range(const NSRange& range); 48 #elif defined(OS_WIN) 49 // The |total_length| paramater should be used if the CHARRANGE is set to 50 // {0,-1} to indicate the whole range. 51 Range(const CHARRANGE& range, LONG total_length = -1); 52 #endif 53 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. start()61 size_t start() const { return start_; } set_start(size_t start)62 void set_start(size_t start) { start_ = start; } 63 end()64 size_t end() const { return end_; } set_end(size_t end)65 void set_end(size_t end) { end_ = end; } 66 67 // Returns the absolute value of the length. length()68 size_t length() const { 69 ptrdiff_t length = end() - start(); 70 return length >= 0 ? length : -length; 71 } 72 is_reversed()73 bool is_reversed() const { return start() > end(); } is_empty()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) 96 Range& operator=(const NSRange& range); 97 98 // 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. 100 NSRange ToNSRange() const; 101 #elif defined(OS_WIN) 102 CHARRANGE ToCHARRANGE() const; 103 #endif 104 // GTK+ has no concept of a range. 105 106 std::string ToString() const; 107 108 private: 109 size_t start_; 110 size_t end_; 111 }; 112 113 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range); 114 115 } // namespace gfx 116 117 #endif // UI_GFX_RANGE_RANGE_H_ 118