Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2004, 2008 Apple 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 VisiblePosition_h
     27 #define VisiblePosition_h
     28 
     29 #include "core/editing/EditingBoundary.h"
     30 #include "core/editing/PositionWithAffinity.h"
     31 #include "platform/heap/Handle.h"
     32 #include "platform/text/TextDirection.h"
     33 
     34 namespace blink {
     35 
     36 // VisiblePosition default affinity is downstream because
     37 // the callers do not really care (they just want the
     38 // deep position without regard to line position), and this
     39 // is cheaper than UPSTREAM
     40 #define VP_DEFAULT_AFFINITY DOWNSTREAM
     41 
     42 // Callers who do not know where on the line the position is,
     43 // but would like UPSTREAM if at a line break or DOWNSTREAM
     44 // otherwise, need a clear way to specify that.  The
     45 // constructors auto-correct UPSTREAM to DOWNSTREAM if the
     46 // position is not at a line break.
     47 #define VP_UPSTREAM_IF_POSSIBLE UPSTREAM
     48 
     49 class InlineBox;
     50 class Range;
     51 
     52 class VisiblePosition FINAL {
     53     DISALLOW_ALLOCATION();
     54 public:
     55     // NOTE: UPSTREAM affinity will be used only if pos is at end of a wrapped line,
     56     // otherwise it will be converted to DOWNSTREAM
     57     VisiblePosition() : m_affinity(VP_DEFAULT_AFFINITY) { }
     58     explicit VisiblePosition(const Position&, EAffinity = VP_DEFAULT_AFFINITY);
     59     explicit VisiblePosition(const PositionWithAffinity&);
     60 
     61     void clear() { m_deepPosition.clear(); }
     62 
     63     bool isNull() const { return m_deepPosition.isNull(); }
     64     bool isNotNull() const { return m_deepPosition.isNotNull(); }
     65     bool isOrphan() const { return m_deepPosition.isOrphan(); }
     66 
     67     Position deepEquivalent() const { return m_deepPosition; }
     68     Position toParentAnchoredPosition() const { return deepEquivalent().parentAnchoredEquivalent(); }
     69     EAffinity affinity() const { ASSERT(m_affinity == UPSTREAM || m_affinity == DOWNSTREAM); return m_affinity; }
     70     void setAffinity(EAffinity affinity) { m_affinity = affinity; }
     71 
     72     // FIXME: Change the following functions' parameter from a boolean to StayInEditableContent.
     73 
     74     // next() and previous() will increment/decrement by a character cluster.
     75     VisiblePosition next(EditingBoundaryCrossingRule = CanCrossEditingBoundary) const;
     76     VisiblePosition previous(EditingBoundaryCrossingRule = CanCrossEditingBoundary) const;
     77     VisiblePosition honorEditingBoundaryAtOrBefore(const VisiblePosition&) const;
     78     VisiblePosition honorEditingBoundaryAtOrAfter(const VisiblePosition&) const;
     79     VisiblePosition skipToStartOfEditingBoundary(const VisiblePosition&) const;
     80     VisiblePosition skipToEndOfEditingBoundary(const VisiblePosition&) const;
     81 
     82     VisiblePosition left(bool stayInEditableContent = false) const;
     83     VisiblePosition right(bool stayInEditableContent = false) const;
     84 
     85     UChar32 characterAfter() const;
     86     UChar32 characterBefore() const { return previous().characterAfter(); }
     87 
     88     // FIXME: This does not handle [table, 0] correctly.
     89     Element* rootEditableElement() const { return m_deepPosition.isNotNull() ? m_deepPosition.deprecatedNode()->rootEditableElement() : 0; }
     90 
     91     void getInlineBoxAndOffset(InlineBox*& inlineBox, int& caretOffset) const
     92     {
     93         m_deepPosition.getInlineBoxAndOffset(m_affinity, inlineBox, caretOffset);
     94     }
     95 
     96     // Rect is local to the returned renderer
     97     LayoutRect localCaretRect(RenderObject*&) const;
     98     // Bounds of (possibly transformed) caret in absolute coords
     99     IntRect absoluteCaretBounds() const;
    100     // Abs x/y position of the caret ignoring transforms.
    101     // FIXME: navigation with transforms should be smarter.
    102     int lineDirectionPointForBlockDirectionNavigation() const;
    103 
    104     void trace(Visitor*);
    105 
    106 #ifndef NDEBUG
    107     void debugPosition(const char* msg = "") const;
    108     void formatForDebugger(char* buffer, unsigned length) const;
    109     void showTreeForThis() const;
    110 #endif
    111 
    112 private:
    113     void init(const Position&, EAffinity);
    114     Position canonicalPosition(const Position&);
    115 
    116     Position leftVisuallyDistinctCandidate() const;
    117     Position rightVisuallyDistinctCandidate() const;
    118 
    119     Position m_deepPosition;
    120     EAffinity m_affinity;
    121 };
    122 
    123 // FIXME: This shouldn't ignore affinity.
    124 inline bool operator==(const VisiblePosition& a, const VisiblePosition& b)
    125 {
    126     return a.deepEquivalent() == b.deepEquivalent();
    127 }
    128 
    129 inline bool operator!=(const VisiblePosition& a, const VisiblePosition& b)
    130 {
    131     return !(a == b);
    132 }
    133 
    134 PassRefPtrWillBeRawPtr<Range> makeRange(const VisiblePosition&, const VisiblePosition&);
    135 bool setStart(Range*, const VisiblePosition&);
    136 bool setEnd(Range*, const VisiblePosition&);
    137 VisiblePosition startVisiblePosition(const Range*, EAffinity);
    138 
    139 Element* enclosingBlockFlowElement(const VisiblePosition&);
    140 
    141 bool isFirstVisiblePositionInNode(const VisiblePosition&, const ContainerNode*);
    142 bool isLastVisiblePositionInNode(const VisiblePosition&, const ContainerNode*);
    143 
    144 } // namespace blink
    145 
    146 #ifndef NDEBUG
    147 // Outside the WebCore namespace for ease of invocation from gdb.
    148 void showTree(const blink::VisiblePosition*);
    149 void showTree(const blink::VisiblePosition&);
    150 #endif
    151 
    152 #endif // VisiblePosition_h
    153