Home | History | Annotate | Download | only in dom
      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/VectorTraits.h"
     27 #include "wtf/text/WTFString.h"
     28 
     29 namespace WebCore {
     30 
     31 class DocumentMarkerDetails;
     32 
     33 // A range of a node within a document that is "marked", such as the range of a misspelled word.
     34 // It optionally includes a description that could be displayed in the user interface.
     35 // It also optionally includes a flag specifying whether the match is active, which is ignored
     36 // for all types other than type TextMatch.
     37 class DocumentMarker {
     38 public:
     39     enum MarkerTypeIndex {
     40         SpellingMarkerIndex = 0,
     41         GramarMarkerIndex,
     42         TextMatchMarkerIndex,
     43         InvisibleSpellcheckMarkerIndex,
     44         MarkerTypeIndexesCount
     45     };
     46 
     47     enum MarkerType {
     48         Spelling = 1 << SpellingMarkerIndex,
     49         Grammar = 1 << GramarMarkerIndex,
     50         TextMatch = 1 << TextMatchMarkerIndex,
     51         InvisibleSpellcheck = 1 << InvisibleSpellcheckMarkerIndex
     52     };
     53 
     54     class MarkerTypes {
     55     public:
     56         // The constructor is intentionally implicit to allow conversion from the bit-wise sum of above types
     57         MarkerTypes(unsigned mask) : m_mask(mask) { }
     58 
     59         bool contains(MarkerType type) const { return m_mask & type; }
     60         bool intersects(const MarkerTypes& types) const { return (m_mask & types.m_mask); }
     61         bool operator==(const MarkerTypes& other) const { return m_mask == other.m_mask; }
     62 
     63         void add(const MarkerTypes& types) { m_mask |= types.m_mask; }
     64         void remove(const MarkerTypes& types) { m_mask &= ~types.m_mask; }
     65 
     66     private:
     67         unsigned m_mask;
     68     };
     69 
     70     class AllMarkers : public MarkerTypes {
     71     public:
     72         AllMarkers()
     73             : MarkerTypes(Spelling | Grammar | TextMatch | InvisibleSpellcheck)
     74         {
     75         }
     76     };
     77 
     78     class MisspellingMarkers : public MarkerTypes {
     79     public:
     80         MisspellingMarkers()
     81             : MarkerTypes(Spelling | Grammar)
     82         {
     83         }
     84     };
     85 
     86     class SpellCheckClientMarkers : public MarkerTypes {
     87     public:
     88         SpellCheckClientMarkers()
     89             : MarkerTypes(Spelling | Grammar | InvisibleSpellcheck)
     90         {
     91         }
     92     };
     93 
     94     DocumentMarker();
     95     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset);
     96     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, const String& description);
     97     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, const String& description, uint32_t hash);
     98     DocumentMarker(unsigned startOffset, unsigned endOffset, bool activeMatch);
     99     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, PassRefPtr<DocumentMarkerDetails>);
    100 
    101     MarkerType type() const { return m_type; }
    102     unsigned startOffset() const { return m_startOffset; }
    103     unsigned endOffset() const { return m_endOffset; }
    104     uint32_t hash() const { return m_hash; }
    105 
    106     const String& description() const;
    107     bool activeMatch() const;
    108     DocumentMarkerDetails* details() const;
    109 
    110     void setActiveMatch(bool);
    111     void clearDetails() { m_details.clear(); }
    112 
    113     // Offset modifications are done by DocumentMarkerController.
    114     // Other classes should not call following setters.
    115     void setStartOffset(unsigned offset) { m_startOffset = offset; }
    116     void setEndOffset(unsigned offset) { m_endOffset = offset; }
    117     void shiftOffsets(int delta);
    118 
    119     bool operator==(const DocumentMarker& o) const
    120     {
    121         return type() == o.type() && startOffset() == o.startOffset() && endOffset() == o.endOffset();
    122     }
    123 
    124     bool operator!=(const DocumentMarker& o) const
    125     {
    126         return !(*this == o);
    127     }
    128 
    129 private:
    130     MarkerType m_type;
    131     unsigned m_startOffset;
    132     unsigned m_endOffset;
    133     RefPtr<DocumentMarkerDetails> m_details;
    134     uint32_t m_hash;
    135 };
    136 
    137 inline DocumentMarkerDetails* DocumentMarker::details() const
    138 {
    139     return m_details.get();
    140 }
    141 
    142 class DocumentMarkerDetails : public RefCounted<DocumentMarkerDetails>
    143 {
    144 public:
    145     DocumentMarkerDetails() { }
    146     virtual ~DocumentMarkerDetails();
    147     virtual bool isDescription() const { return false; }
    148     virtual bool isTextMatch() const { return false; }
    149 };
    150 
    151 } // namespace WebCore
    152 
    153 namespace WTF {
    154 
    155 template<>
    156 struct VectorTraits<WebCore::DocumentMarker> : SimpleClassVectorTraits<WebCore::DocumentMarker> { };
    157 
    158 } // namespace WTF
    159 
    160 #endif // DocumentMarker_h
    161