|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef mozilla_TextRage_h_ |
|
7 #define mozilla_TextRage_h_ |
|
8 |
|
9 #include <stdint.h> |
|
10 |
|
11 #include "nsAutoPtr.h" |
|
12 #include "nsColor.h" |
|
13 #include "nsStyleConsts.h" |
|
14 #include "nsTArray.h" |
|
15 |
|
16 namespace mozilla { |
|
17 |
|
18 /****************************************************************************** |
|
19 * mozilla::TextRangeStyle |
|
20 ******************************************************************************/ |
|
21 |
|
22 struct TextRangeStyle |
|
23 { |
|
24 enum |
|
25 { |
|
26 LINESTYLE_NONE = NS_STYLE_TEXT_DECORATION_STYLE_NONE, |
|
27 LINESTYLE_SOLID = NS_STYLE_TEXT_DECORATION_STYLE_SOLID, |
|
28 LINESTYLE_DOTTED = NS_STYLE_TEXT_DECORATION_STYLE_DOTTED, |
|
29 LINESTYLE_DASHED = NS_STYLE_TEXT_DECORATION_STYLE_DASHED, |
|
30 LINESTYLE_DOUBLE = NS_STYLE_TEXT_DECORATION_STYLE_DOUBLE, |
|
31 LINESTYLE_WAVY = NS_STYLE_TEXT_DECORATION_STYLE_WAVY |
|
32 }; |
|
33 |
|
34 enum |
|
35 { |
|
36 DEFINED_NONE = 0x00, |
|
37 DEFINED_LINESTYLE = 0x01, |
|
38 DEFINED_FOREGROUND_COLOR = 0x02, |
|
39 DEFINED_BACKGROUND_COLOR = 0x04, |
|
40 DEFINED_UNDERLINE_COLOR = 0x08 |
|
41 }; |
|
42 |
|
43 // Initialize all members, because TextRange instances may be compared by |
|
44 // memcomp. |
|
45 TextRangeStyle() |
|
46 { |
|
47 Clear(); |
|
48 } |
|
49 |
|
50 void Clear() |
|
51 { |
|
52 mDefinedStyles = DEFINED_NONE; |
|
53 mLineStyle = LINESTYLE_NONE; |
|
54 mIsBoldLine = false; |
|
55 mForegroundColor = mBackgroundColor = mUnderlineColor = NS_RGBA(0, 0, 0, 0); |
|
56 } |
|
57 |
|
58 bool IsDefined() const { return mDefinedStyles != DEFINED_NONE; } |
|
59 |
|
60 bool IsLineStyleDefined() const |
|
61 { |
|
62 return (mDefinedStyles & DEFINED_LINESTYLE) != 0; |
|
63 } |
|
64 |
|
65 bool IsForegroundColorDefined() const |
|
66 { |
|
67 return (mDefinedStyles & DEFINED_FOREGROUND_COLOR) != 0; |
|
68 } |
|
69 |
|
70 bool IsBackgroundColorDefined() const |
|
71 { |
|
72 return (mDefinedStyles & DEFINED_BACKGROUND_COLOR) != 0; |
|
73 } |
|
74 |
|
75 bool IsUnderlineColorDefined() const |
|
76 { |
|
77 return (mDefinedStyles & DEFINED_UNDERLINE_COLOR) != 0; |
|
78 } |
|
79 |
|
80 bool IsNoChangeStyle() const |
|
81 { |
|
82 return !IsForegroundColorDefined() && !IsBackgroundColorDefined() && |
|
83 IsLineStyleDefined() && mLineStyle == LINESTYLE_NONE; |
|
84 } |
|
85 |
|
86 bool Equals(const TextRangeStyle& aOther) |
|
87 { |
|
88 if (mDefinedStyles != aOther.mDefinedStyles) |
|
89 return false; |
|
90 if (IsLineStyleDefined() && (mLineStyle != aOther.mLineStyle || |
|
91 !mIsBoldLine != !aOther.mIsBoldLine)) |
|
92 return false; |
|
93 if (IsForegroundColorDefined() && |
|
94 (mForegroundColor != aOther.mForegroundColor)) |
|
95 return false; |
|
96 if (IsBackgroundColorDefined() && |
|
97 (mBackgroundColor != aOther.mBackgroundColor)) |
|
98 return false; |
|
99 if (IsUnderlineColorDefined() && |
|
100 (mUnderlineColor != aOther.mUnderlineColor)) |
|
101 return false; |
|
102 return true; |
|
103 } |
|
104 |
|
105 bool operator !=(const TextRangeStyle &aOther) |
|
106 { |
|
107 return !Equals(aOther); |
|
108 } |
|
109 |
|
110 bool operator ==(const TextRangeStyle &aOther) |
|
111 { |
|
112 return Equals(aOther); |
|
113 } |
|
114 |
|
115 uint8_t mDefinedStyles; |
|
116 uint8_t mLineStyle; // DEFINED_LINESTYLE |
|
117 |
|
118 bool mIsBoldLine; // DEFINED_LINESTYLE |
|
119 |
|
120 nscolor mForegroundColor; // DEFINED_FOREGROUND_COLOR |
|
121 nscolor mBackgroundColor; // DEFINED_BACKGROUND_COLOR |
|
122 nscolor mUnderlineColor; // DEFINED_UNDERLINE_COLOR |
|
123 }; |
|
124 |
|
125 /****************************************************************************** |
|
126 * mozilla::TextRange |
|
127 ******************************************************************************/ |
|
128 |
|
129 #define NS_TEXTRANGE_CARETPOSITION 0x01 |
|
130 #define NS_TEXTRANGE_RAWINPUT 0x02 |
|
131 #define NS_TEXTRANGE_SELECTEDRAWTEXT 0x03 |
|
132 #define NS_TEXTRANGE_CONVERTEDTEXT 0x04 |
|
133 #define NS_TEXTRANGE_SELECTEDCONVERTEDTEXT 0x05 |
|
134 |
|
135 struct TextRange |
|
136 { |
|
137 TextRange() : |
|
138 mStartOffset(0), mEndOffset(0), mRangeType(0) |
|
139 { |
|
140 } |
|
141 |
|
142 uint32_t mStartOffset; |
|
143 // XXX Storing end offset makes the initializing code very complicated. |
|
144 // We should replace it with mLength. |
|
145 uint32_t mEndOffset; |
|
146 uint32_t mRangeType; |
|
147 |
|
148 TextRangeStyle mRangeStyle; |
|
149 |
|
150 uint32_t Length() const { return mEndOffset - mStartOffset; } |
|
151 |
|
152 bool IsClause() const |
|
153 { |
|
154 MOZ_ASSERT(mRangeType >= NS_TEXTRANGE_CARETPOSITION && |
|
155 mRangeType <= NS_TEXTRANGE_SELECTEDCONVERTEDTEXT, |
|
156 "Invalid range type"); |
|
157 return mRangeType != NS_TEXTRANGE_CARETPOSITION; |
|
158 } |
|
159 }; |
|
160 |
|
161 /****************************************************************************** |
|
162 * mozilla::TextRangeArray |
|
163 ******************************************************************************/ |
|
164 class TextRangeArray MOZ_FINAL : public nsAutoTArray<TextRange, 10> |
|
165 { |
|
166 NS_INLINE_DECL_REFCOUNTING(TextRangeArray) |
|
167 |
|
168 public: |
|
169 bool IsComposing() const |
|
170 { |
|
171 for (uint32_t i = 0; i < Length(); ++i) { |
|
172 if (ElementAt(i).IsClause()) { |
|
173 return true; |
|
174 } |
|
175 } |
|
176 return false; |
|
177 } |
|
178 |
|
179 // Returns target clase offset. If there are selected clauses, this returns |
|
180 // the first selected clause offset. Otherwise, 0. |
|
181 uint32_t TargetClauseOffset() const |
|
182 { |
|
183 for (uint32_t i = 0; i < Length(); ++i) { |
|
184 const TextRange& range = ElementAt(i); |
|
185 if (range.mRangeType == NS_TEXTRANGE_SELECTEDRAWTEXT || |
|
186 range.mRangeType == NS_TEXTRANGE_SELECTEDCONVERTEDTEXT) { |
|
187 return range.mStartOffset; |
|
188 } |
|
189 } |
|
190 return 0; |
|
191 } |
|
192 }; |
|
193 |
|
194 } // namespace mozilla |
|
195 |
|
196 #endif // mozilla_TextRage_h_ |