Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2004 Apple Computer, Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef VisibleSelection_h
     27 #define VisibleSelection_h
     28 
     29 #include "TextGranularity.h"
     30 #include "VisiblePosition.h"
     31 
     32 namespace WebCore {
     33 
     34 class Position;
     35 
     36 const EAffinity SEL_DEFAULT_AFFINITY = DOWNSTREAM;
     37 
     38 class VisibleSelection {
     39 public:
     40     enum SelectionType { NoSelection, CaretSelection, RangeSelection };
     41 
     42     VisibleSelection();
     43 
     44     VisibleSelection(const Position&, EAffinity);
     45     VisibleSelection(const Position&, const Position&, EAffinity = SEL_DEFAULT_AFFINITY);
     46 
     47     VisibleSelection(const Range*, EAffinity = SEL_DEFAULT_AFFINITY);
     48 
     49     VisibleSelection(const VisiblePosition&);
     50     VisibleSelection(const VisiblePosition&, const VisiblePosition&);
     51 
     52     static VisibleSelection selectionFromContentsOfNode(Node*);
     53 
     54     SelectionType selectionType() const { return m_selectionType; }
     55 
     56     void setAffinity(EAffinity affinity) { m_affinity = affinity; }
     57     EAffinity affinity() const { return m_affinity; }
     58 
     59     void setBase(const Position&);
     60     void setBase(const VisiblePosition&);
     61     void setExtent(const Position&);
     62     void setExtent(const VisiblePosition&);
     63 
     64     Position base() const { return m_base; }
     65     Position extent() const { return m_extent; }
     66     Position start() const { return m_start; }
     67     Position end() const { return m_end; }
     68 
     69     VisiblePosition visibleStart() const { return VisiblePosition(m_start, isRange() ? DOWNSTREAM : affinity()); }
     70     VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); }
     71 
     72     bool isNone() const { return selectionType() == NoSelection; }
     73     bool isCaret() const { return selectionType() == CaretSelection; }
     74     bool isRange() const { return selectionType() == RangeSelection; }
     75     bool isCaretOrRange() const { return selectionType() != NoSelection; }
     76 
     77     bool isBaseFirst() const { return m_baseIsFirst; }
     78 
     79     bool isAll(StayInEditableContent) const;
     80 
     81     void appendTrailingWhitespace();
     82 
     83     bool expandUsingGranularity(TextGranularity granularity);
     84     TextGranularity granularity() const { return m_granularity; }
     85 
     86     // We don't yet support multi-range selections, so we only ever have one range to return.
     87     PassRefPtr<Range> firstRange() const;
     88 
     89     // FIXME: Most callers probably don't want this function, but are using it
     90     // for historical reasons.  toNormalizedRange contracts the range around
     91     // text, and moves the caret upstream before returning the range.
     92     PassRefPtr<Range> toNormalizedRange() const;
     93 
     94     Element* rootEditableElement() const;
     95     bool isContentEditable() const;
     96     bool isContentRichlyEditable() const;
     97     Node* shadowTreeRootNode() const;
     98 
     99     void debugPosition() const;
    100 
    101 #ifndef NDEBUG
    102     void formatForDebugger(char* buffer, unsigned length) const;
    103     void showTreeForThis() const;
    104 #endif
    105 
    106     void setWithoutValidation(const Position&, const Position&);
    107 
    108 private:
    109     void validate();
    110 
    111     // Support methods for validate()
    112     void setBaseAndExtentToDeepEquivalents();
    113     void setStartAndEndFromBaseAndExtentRespectingGranularity();
    114     void adjustSelectionToAvoidCrossingEditingBoundaries();
    115     void updateSelectionType();
    116 
    117     // We need to store these as Positions because VisibleSelection is
    118     // used to store values in editing commands for use when
    119     // undoing the command. We need to be able to create a selection that, while currently
    120     // invalid, will be valid once the changes are undone.
    121 
    122     Position m_base;   // Where the first click happened
    123     Position m_extent; // Where the end click happened
    124     Position m_start;  // Leftmost position when expanded to respect granularity
    125     Position m_end;    // Rightmost position when expanded to respect granularity
    126 
    127     EAffinity m_affinity;           // the upstream/downstream affinity of the caret
    128     TextGranularity m_granularity;  // granularity of start/end selection
    129 
    130     // these are cached, can be recalculated by validate()
    131     SelectionType m_selectionType;    // None, Caret, Range
    132     bool m_baseIsFirst;               // true if base is before the extent
    133 };
    134 
    135 inline bool operator==(const VisibleSelection& a, const VisibleSelection& b)
    136 {
    137     return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity() && a.granularity() == b.granularity() && a.isBaseFirst() == b.isBaseFirst();
    138 }
    139 
    140 inline bool operator!=(const VisibleSelection& a, const VisibleSelection& b)
    141 {
    142     return !(a == b);
    143 }
    144 
    145 } // namespace WebCore
    146 
    147 #ifndef NDEBUG
    148 // Outside the WebCore namespace for ease of invocation from gdb.
    149 void showTree(const WebCore::VisibleSelection&);
    150 void showTree(const WebCore::VisibleSelection*);
    151 #endif
    152 
    153 #endif // VisibleSelection_h
    154