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_BASE_RANGE_RANGE_H_ 6 #define UI_BASE_RANGE_RANGE_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "ui/base/ui_export.h" 12 13 #if defined(OS_MACOSX) 14 #if __OBJC__ 15 #import <Foundation/Foundation.h> 16 #else 17 typedef struct _NSRange NSRange; 18 #endif 19 #endif // defined(OS_MACOSX) 20 21 #if defined(OS_WIN) 22 #include <windows.h> 23 #include <richedit.h> 24 #endif 25 26 namespace ui { 27 28 // A Range contains two integer values that represent a numeric range, like the 29 // range of characters in a text selection. A range is made of a start and end 30 // position; when they are the same, the Range is akin to a caret. Note that 31 // |start_| can be greater than |end_| to respect the directionality of the 32 // range. 33 class UI_EXPORT Range { 34 public: 35 // Creates an empty range {0,0}. 36 Range(); 37 38 // Initializes the range with a start and end. 39 Range(size_t start, size_t end); 40 41 // Initializes the range with the same start and end positions. 42 explicit Range(size_t position); 43 44 // Platform constructors. 45 #if defined(OS_MACOSX) 46 explicit Range(const NSRange& range); 47 #elif defined(OS_WIN) 48 // The |total_length| paramater should be used if the CHARRANGE is set to 49 // {0,-1} to indicate the whole range. 50 Range(const CHARRANGE& range, LONG total_length = -1); 51 #endif 52 53 // Returns a range that is invalid, which is {size_t_max,size_t_max}. 54 static const Range InvalidRange(); 55 56 // Checks if the range is valid through comparision to InvalidRange(). 57 bool IsValid() const; 58 59 // Getters and setters. 60 size_t start() const { return start_; } 61 void set_start(size_t start) { start_ = start; } 62 63 size_t end() const { return end_; } 64 void set_end(size_t end) { end_ = end; } 65 66 // Returns the absolute value of the length. 67 size_t length() const { 68 ptrdiff_t length = end() - start(); 69 return length >= 0 ? length : -length; 70 } 71 72 bool is_reversed() const { return start() > end(); } 73 bool is_empty() const { return start() == end(); } 74 75 // Returns the minimum and maximum values. 76 size_t GetMin() const; 77 size_t GetMax() const; 78 79 bool operator==(const Range& other) const; 80 bool operator!=(const Range& other) const; 81 bool EqualsIgnoringDirection(const Range& other) const; 82 83 // Returns true if this range intersects the specified |range|. 84 bool Intersects(const Range& range) const; 85 86 // Returns true if this range contains the specified |range|. 87 bool Contains(const Range& range) const; 88 89 // Computes the intersection of this range with the given |range|. 90 // If they don't intersect, it returns an InvalidRange(). 91 // The returned range is always empty or forward (never reversed). 92 Range Intersect(const Range& range) const; 93 94 #if defined(OS_MACOSX) 95 Range& operator=(const NSRange& range); 96 97 // NSRange does not store the directionality of a range, so if this 98 // is_reversed(), the range will get flipped when converted to an NSRange. 99 NSRange ToNSRange() const; 100 #elif defined(OS_WIN) 101 CHARRANGE ToCHARRANGE() const; 102 #endif 103 // GTK+ has no concept of a range. 104 105 std::string ToString() const; 106 107 private: 108 size_t start_; 109 size_t end_; 110 }; 111 112 } // namespace ui 113 114 #endif // UI_BASE_RANGE_RANGE_H_ 115