OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 16 matching lines...) Expand all Loading... |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "core/editing/markers/DocumentMarker.h" | 31 #include "core/editing/markers/DocumentMarker.h" |
32 | 32 |
33 #include "wtf/StdLibExtras.h" | 33 #include "wtf/StdLibExtras.h" |
34 | 34 |
35 namespace blink { | 35 namespace blink { |
36 | 36 |
37 DocumentMarkerDetails::~DocumentMarkerDetails() {} | 37 DocumentMarker::DocumentMarker(const DocumentMarker& marker) |
| 38 : m_startOffset(marker.startOffset()), m_endOffset(marker.endOffset()) {} |
38 | 39 |
39 class DocumentMarkerDescription final : public DocumentMarkerDetails { | 40 DocumentMarker::~DocumentMarker() {} |
40 public: | |
41 static DocumentMarkerDescription* create(const String&); | |
42 | 41 |
43 const String& description() const { return m_description; } | 42 DocumentMarker::DocumentMarker(unsigned startOffset, unsigned endOffset) |
44 bool isDescription() const override { return true; } | 43 : m_startOffset(startOffset), m_endOffset(endOffset) {} |
45 | |
46 private: | |
47 explicit DocumentMarkerDescription(const String& description) | |
48 : m_description(description) {} | |
49 | |
50 String m_description; | |
51 }; | |
52 | |
53 DocumentMarkerDescription* DocumentMarkerDescription::create( | |
54 const String& description) { | |
55 return new DocumentMarkerDescription(description); | |
56 } | |
57 | |
58 inline DocumentMarkerDescription* toDocumentMarkerDescription( | |
59 DocumentMarkerDetails* details) { | |
60 if (details && details->isDescription()) | |
61 return static_cast<DocumentMarkerDescription*>(details); | |
62 return 0; | |
63 } | |
64 | |
65 class DocumentMarkerTextMatch final : public DocumentMarkerDetails { | |
66 public: | |
67 static DocumentMarkerTextMatch* create(bool); | |
68 | |
69 bool activeMatch() const { return m_match; } | |
70 bool isTextMatch() const override { return true; } | |
71 | |
72 private: | |
73 explicit DocumentMarkerTextMatch(bool match) : m_match(match) {} | |
74 | |
75 bool m_match; | |
76 }; | |
77 | |
78 DocumentMarkerTextMatch* DocumentMarkerTextMatch::create(bool match) { | |
79 DEFINE_STATIC_LOCAL(DocumentMarkerTextMatch, trueInstance, | |
80 (new DocumentMarkerTextMatch(true))); | |
81 DEFINE_STATIC_LOCAL(DocumentMarkerTextMatch, falseInstance, | |
82 (new DocumentMarkerTextMatch(false))); | |
83 return match ? &trueInstance : &falseInstance; | |
84 } | |
85 | |
86 inline DocumentMarkerTextMatch* toDocumentMarkerTextMatch( | |
87 DocumentMarkerDetails* details) { | |
88 if (details && details->isTextMatch()) | |
89 return static_cast<DocumentMarkerTextMatch*>(details); | |
90 return 0; | |
91 } | |
92 | |
93 class TextCompositionMarkerDetails final : public DocumentMarkerDetails { | |
94 public: | |
95 static TextCompositionMarkerDetails* create(Color underlineColor, | |
96 bool thick, | |
97 Color backgroundColor); | |
98 | |
99 bool isComposition() const override { return true; } | |
100 Color underlineColor() const { return m_underlineColor; } | |
101 bool thick() const { return m_thick; } | |
102 Color backgroundColor() const { return m_backgroundColor; } | |
103 | |
104 private: | |
105 TextCompositionMarkerDetails(Color underlineColor, | |
106 bool thick, | |
107 Color backgroundColor) | |
108 : m_underlineColor(underlineColor), | |
109 m_backgroundColor(backgroundColor), | |
110 m_thick(thick) {} | |
111 | |
112 Color m_underlineColor; | |
113 Color m_backgroundColor; | |
114 bool m_thick; | |
115 }; | |
116 | |
117 TextCompositionMarkerDetails* TextCompositionMarkerDetails::create( | |
118 Color underlineColor, | |
119 bool thick, | |
120 Color backgroundColor) { | |
121 return new TextCompositionMarkerDetails(underlineColor, thick, | |
122 backgroundColor); | |
123 } | |
124 | |
125 inline TextCompositionMarkerDetails* toTextCompositionMarkerDetails( | |
126 DocumentMarkerDetails* details) { | |
127 if (details && details->isComposition()) | |
128 return static_cast<TextCompositionMarkerDetails*>(details); | |
129 return nullptr; | |
130 } | |
131 | |
132 DocumentMarker* DocumentMarker::clone() const { | |
133 return new DocumentMarker(*this); | |
134 } | |
135 | |
136 DocumentMarker::DocumentMarker(MarkerType type, | |
137 unsigned startOffset, | |
138 unsigned endOffset, | |
139 const String& description) | |
140 : m_type(type), | |
141 m_startOffset(startOffset), | |
142 m_endOffset(endOffset), | |
143 m_details(description.isEmpty() | |
144 ? nullptr | |
145 : DocumentMarkerDescription::create(description)) { | |
146 DCHECK(type != DocumentMarker::TextMatch); | |
147 } | |
148 | |
149 DocumentMarker::DocumentMarker(unsigned startOffset, | |
150 unsigned endOffset, | |
151 bool activeMatch) | |
152 : m_type(DocumentMarker::TextMatch), | |
153 m_startOffset(startOffset), | |
154 m_endOffset(endOffset), | |
155 m_details(DocumentMarkerTextMatch::create(activeMatch)) {} | |
156 | |
157 DocumentMarker::DocumentMarker(unsigned startOffset, | |
158 unsigned endOffset, | |
159 Color underlineColor, | |
160 bool thick, | |
161 Color backgroundColor) | |
162 : m_type(DocumentMarker::Composition), | |
163 m_startOffset(startOffset), | |
164 m_endOffset(endOffset), | |
165 m_details(TextCompositionMarkerDetails::create(underlineColor, | |
166 thick, | |
167 backgroundColor)) {} | |
168 | |
169 DocumentMarker::DocumentMarker(const DocumentMarker& marker) | |
170 : m_type(marker.type()), | |
171 m_startOffset(marker.startOffset()), | |
172 m_endOffset(marker.endOffset()), | |
173 m_details(marker.details()) {} | |
174 | 44 |
175 DocumentMarker::ShiftMarkerResult DocumentMarker::getShiftedMarkerPosition( | 45 DocumentMarker::ShiftMarkerResult DocumentMarker::getShiftedMarkerPosition( |
176 unsigned offset, | 46 unsigned offset, |
177 unsigned oldLength, | 47 unsigned oldLength, |
178 unsigned newLength) const { | 48 unsigned newLength) const { |
179 ShiftMarkerResult result; | 49 ShiftMarkerResult result; |
180 result.newStartOffset = startOffset(); | 50 result.newStartOffset = startOffset(); |
181 result.newEndOffset = endOffset(); | 51 result.newEndOffset = endOffset(); |
182 result.shouldRemoveMarker = false; | 52 result.shouldRemoveMarker = false; |
183 | 53 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 result.shouldRemoveMarker = true; | 87 result.shouldRemoveMarker = true; |
218 | 88 |
219 return result; | 89 return result; |
220 } | 90 } |
221 | 91 |
222 void DocumentMarker::shiftOffsets(int delta) { | 92 void DocumentMarker::shiftOffsets(int delta) { |
223 m_startOffset += delta; | 93 m_startOffset += delta; |
224 m_endOffset += delta; | 94 m_endOffset += delta; |
225 } | 95 } |
226 | 96 |
227 void DocumentMarker::setActiveMatch(bool active) { | 97 bool DocumentMarker::operator==(const DocumentMarker& other) const { |
228 m_details = DocumentMarkerTextMatch::create(active); | 98 return type() == other.type() && m_startOffset == other.m_startOffset && |
| 99 m_endOffset == other.m_endOffset; |
229 } | 100 } |
230 | 101 |
231 const String& DocumentMarker::description() const { | 102 const String& DocumentMarker::description() const { |
232 if (DocumentMarkerDescription* details = | |
233 toDocumentMarkerDescription(m_details.get())) | |
234 return details->description(); | |
235 return emptyString; | 103 return emptyString; |
236 } | 104 } |
237 | 105 |
238 bool DocumentMarker::activeMatch() const { | 106 bool DocumentMarker::activeMatch() const { |
239 if (DocumentMarkerTextMatch* details = | |
240 toDocumentMarkerTextMatch(m_details.get())) | |
241 return details->activeMatch(); | |
242 return false; | 107 return false; |
243 } | 108 } |
244 | 109 |
245 Color DocumentMarker::underlineColor() const { | 110 Color DocumentMarker::underlineColor() const { |
246 if (TextCompositionMarkerDetails* details = | |
247 toTextCompositionMarkerDetails(m_details.get())) | |
248 return details->underlineColor(); | |
249 return Color::transparent; | 111 return Color::transparent; |
250 } | 112 } |
251 | 113 |
252 bool DocumentMarker::thick() const { | 114 bool DocumentMarker::thick() const { |
253 if (TextCompositionMarkerDetails* details = | |
254 toTextCompositionMarkerDetails(m_details.get())) | |
255 return details->thick(); | |
256 return false; | 115 return false; |
257 } | 116 } |
258 | 117 |
259 Color DocumentMarker::backgroundColor() const { | 118 Color DocumentMarker::backgroundColor() const { |
260 if (TextCompositionMarkerDetails* details = | |
261 toTextCompositionMarkerDetails(m_details.get())) | |
262 return details->backgroundColor(); | |
263 return Color::transparent; | 119 return Color::transparent; |
264 } | 120 } |
265 | 121 |
266 DEFINE_TRACE(DocumentMarker) { | |
267 visitor->trace(m_details); | |
268 } | |
269 | |
270 } // namespace blink | 122 } // namespace blink |
OLD | NEW |