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