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/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 MarkerType {
     42         Spelling = 1 << 0,
     43         Grammar = 1 << 1,
     44         TextMatch = 1 << 2
     45     };
     46 
     47     class MarkerTypes {
     48     public:
     49         // The constructor is intentionally implicit to allow conversion from the bit-wise sum of above types
     50         MarkerTypes(unsigned mask) : m_mask(mask) { }
     51 
     52         bool contains(MarkerType type) const { return m_mask & type; }
     53         bool intersects(const MarkerTypes& types) const { return (m_mask & types.m_mask); }
     54         bool operator==(const MarkerTypes& other) const { return m_mask == other.m_mask; }
     55 
     56         void add(const MarkerTypes& types) { m_mask |= types.m_mask; }
     57         void remove(const MarkerTypes& types) { m_mask &= ~types.m_mask; }
     58 
     59     private:
     60         unsigned m_mask;
     61     };
     62 
     63     class AllMarkers : public MarkerTypes {
     64     public:
     65         AllMarkers()
     66             : MarkerTypes(Spelling | Grammar | TextMatch)
     67         {
     68         }
     69     };
     70 
     71     DocumentMarker();
     72     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset);
     73     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, const String& description);
     74     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, const String& description, uint32_t hash);
     75     DocumentMarker(unsigned startOffset, unsigned endOffset, bool activeMatch);
     76     DocumentMarker(MarkerType, unsigned startOffset, unsigned endOffset, PassRefPtr<DocumentMarkerDetails>);
     77 
     78     MarkerType type() const { return m_type; }
     79     unsigned startOffset() const { return m_startOffset; }
     80     unsigned endOffset() const { return m_endOffset; }
     81     uint32_t hash() const { return m_hash; }
     82 
     83     const String& description() const;
     84     bool activeMatch() const;
     85     DocumentMarkerDetails* details() const;
     86 
     87     void setActiveMatch(bool);
     88     void clearDetails() { m_details.clear(); }
     89 
     90     // Offset modifications are done by DocumentMarkerController.
     91     // Other classes should not call following setters.
     92     void setStartOffset(unsigned offset) { m_startOffset = offset; }
     93     void setEndOffset(unsigned offset) { m_endOffset = offset; }
     94     void shiftOffsets(int delta);
     95 
     96     bool operator==(const DocumentMarker& o) const
     97     {
     98         return type() == o.type() && startOffset() == o.startOffset() && endOffset() == o.endOffset();
     99     }
    100 
    101     bool operator!=(const DocumentMarker& o) const
    102     {
    103         return !(*this == o);
    104     }
    105 
    106 private:
    107     MarkerType m_type;
    108     unsigned m_startOffset;
    109     unsigned m_endOffset;
    110     RefPtr<DocumentMarkerDetails> m_details;
    111     uint32_t m_hash;
    112 };
    113 
    114 inline DocumentMarkerDetails* DocumentMarker::details() const
    115 {
    116     return m_details.get();
    117 }
    118 
    119 class DocumentMarkerDetails : public RefCounted<DocumentMarkerDetails>
    120 {
    121 public:
    122     DocumentMarkerDetails() { }
    123     virtual ~DocumentMarkerDetails();
    124     virtual bool isDescription() const { return false; }
    125     virtual bool isTextMatch() const { return false; }
    126 };
    127 
    128 } // namespace WebCore
    129 
    130 #endif // DocumentMarker_h
    131