Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / range / range.h
blob6007d754d42da03752da00a880136e59bdab7158
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_RANGE_RANGE_H_
6 #define UI_GFX_RANGE_RANGE_H_
8 #include <ostream>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "ui/gfx/gfx_export.h"
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)
22 #if defined(OS_WIN)
23 #include <windows.h>
24 #include <richedit.h>
25 #endif
27 namespace gfx {
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();
39 // Initializes the range with a start and end.
40 Range(size_t start, size_t end);
42 // Initializes the range with the same start and end positions.
43 explicit Range(size_t position);
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
54 // Returns a range that is invalid, which is {size_t_max,size_t_max}.
55 static const Range InvalidRange();
57 // Checks if the range is valid through comparison to InvalidRange().
58 bool IsValid() const;
60 // Getters and setters.
61 size_t start() const { return start_; }
62 void set_start(size_t start) { start_ = start; }
64 size_t end() const { return end_; }
65 void set_end(size_t end) { end_ = end; }
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;
73 bool is_reversed() const { return start() > end(); }
74 bool is_empty() const { return start() == end(); }
76 // Returns the minimum and maximum values.
77 size_t GetMin() const;
78 size_t GetMax() const;
80 bool operator==(const Range& other) const;
81 bool operator!=(const Range& other) const;
82 bool EqualsIgnoringDirection(const Range& other) const;
84 // Returns true if this range intersects the specified |range|.
85 bool Intersects(const Range& range) const;
87 // Returns true if this range contains the specified |range|.
88 bool Contains(const Range& range) const;
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;
95 #if defined(OS_MACOSX)
96 Range& operator=(const NSRange& range);
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.
106 std::string ToString() const;
108 private:
109 size_t start_;
110 size_t end_;
113 GFX_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range);
115 } // namespace gfx
117 #endif // UI_GFX_RANGE_RANGE_H_