1 /*
2 * This file is part of the DOM implementation for WebCore.
3 *
4 * Copyright (C) 2006 Apple Computer, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #ifndef DocumentMarker_h
24 #define DocumentMarker_h
25
26 #include "wtf/Forward.h"
27 #include "wtf/RefCounted.h"
28 #include "wtf/RefPtr.h"
29 #include "wtf/text/WTFString.h"
30
31 namespace WebCore {
32
33 class DocumentMarkerDetails;
34
35 // A range of a node within a document that is "marked", such as the range of a misspelled word.
36 // It optionally includes a description that could be displayed in the user interface.
37 // It also optionally includes a flag specifying whether the match is active, which is ignored
38 // for all types other than type TextMatch.
39 class DocumentMarker {
40 public:
41 enum MarkerTypeIndex {
42 SpellingMarkerIndex = 0,
43 GramarMarkerIndex,
44 TextMatchMarkerIndex,
45 InvisibleSpellcheckMarkerIndex,
46 MarkerTypeIndexesCount
47 };
48
49 enum MarkerType {
50 Spelling = 1 << SpellingMarkerIndex,
51 Grammar = 1 << GramarMarkerIndex,
52 TextMatch = 1 << TextMatchMarkerIndex,
53 InvisibleSpellcheck = 1 << InvisibleSpellcheckMarkerIndex
54 };
55
56 class MarkerTypes {
57 public:
58 // The constructor is intentionally implicit to allow conversion from the bit-wise sum of above types
MarkerTypes(unsigned mask)59 MarkerTypes(unsigned mask) : m_mask(mask) { }
60
contains(MarkerType type)61 bool contains(MarkerType type) const { return m_mask & type; }
intersects(const MarkerTypes & types)62 bool intersects(const MarkerTypes& types) const { return (m_mask & types.m_mask); }
63 bool operator==(const MarkerTypes& other) const { return m_mask == other.m_mask; }
64
add(const MarkerTypes & types)65 void add(const MarkerTypes& types) { m_mask |= types.m_mask; }
remove(const MarkerTypes & types)66 void remove(const MarkerTypes& types) { m_mask &= ~types.m_mask; }
67
68 private:
69 unsigned m_mask;
70 };
71
72 class AllMarkers : public MarkerTypes {
73 public:
AllMarkers()74 AllMarkers()
75 : MarkerTypes(Spelling | Grammar | TextMatch | InvisibleSpellcheck)
76 {
77 }
78 };
79
80 class MisspellingMarkers : public MarkerTypes {
81 public:
MisspellingMarkers()82 MisspellingMarkers()
83 : MarkerTypes(Spelling | Grammar)
84 {
85 }
86 };
87
88 class SpellCheckClientMarkers : public MarkerTypes {
89 public:
SpellCheckClientMarkers()90 SpellCheckClientMarkers()
91 : MarkerTypes(Spelling | Grammar | InvisibleSpellcheck)
92 {
93 }
94 };
95
96 DocumentMarker();
97 DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset);
98 DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, const String& description);
99 DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, const String& description, uint32_t hash);
100 DocumentMarker(unsigned startOffset, unsigned endOffset, bool activeMatch);
101 DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, PassRefPtr<DocumentMarkerDetails>);
102
type()103 MarkerType type() const { return m_type; }
startOffset()104 unsigned startOffset() const { return m_startOffset; }
endOffset()105 unsigned endOffset() const { return m_endOffset; }
hash()106 uint32_t hash() const { return m_hash; }
107
108 const String& description() const;
109 bool activeMatch() const;
110 DocumentMarkerDetails* details() const;
111
112 void setActiveMatch(bool);
clearDetails()113 void clearDetails() { m_details.clear(); }
114
115 // Offset modifications are done by DocumentMarkerController.
116 // Other classes should not call following setters.
setStartOffset(unsigned offset)117 void setStartOffset(unsigned offset) { m_startOffset = offset; }
setEndOffset(unsigned offset)118 void setEndOffset(unsigned offset) { m_endOffset = offset; }
119 void shiftOffsets(int delta);
120
121 bool operator==(const DocumentMarker& o) const
122 {
123 return type() == o.type() && startOffset() == o.startOffset() && endOffset() == o.endOffset();
124 }
125
126 bool operator!=(const DocumentMarker& o) const
127 {
128 return !(*this == o);
129 }
130
131 private:
132 MarkerType m_type;
133 unsigned m_startOffset;
134 unsigned m_endOffset;
135 RefPtr<DocumentMarkerDetails> m_details;
136 uint32_t m_hash;
137 };
138
details()139 inline DocumentMarkerDetails* DocumentMarker::details() const
140 {
141 return m_details.get();
142 }
143
144 class DocumentMarkerDetails : public RefCounted<DocumentMarkerDetails>
145 {
146 public:
DocumentMarkerDetails()147 DocumentMarkerDetails() { }
148 virtual ~DocumentMarkerDetails();
isDescription()149 virtual bool isDescription() const { return false; }
isTextMatch()150 virtual bool isTextMatch() const { return false; }
151 };
152
153 } // namespace WebCore
154
155 #endif // DocumentMarker_h
156