2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / editing / VisiblePosition.h
blob79f3a576a52f59fa3bec86d5ef020856e83d483d
1 /*
2 * Copyright (C) 2004, 2008 Apple Inc. All rights reserved.
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.
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.
26 #ifndef VisiblePosition_h
27 #define VisiblePosition_h
29 #include "Node.h"
30 #include "Position.h"
31 #include "TextDirection.h"
33 namespace WebCore {
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
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
48 class InlineBox;
50 class VisiblePosition {
51 public:
52 // NOTE: UPSTREAM affinity will be used only if pos is at end of a wrapped line,
53 // otherwise it will be converted to DOWNSTREAM
54 VisiblePosition() : m_affinity(VP_DEFAULT_AFFINITY) { }
55 VisiblePosition(Node*, int offset, EAffinity);
56 VisiblePosition(const Position&, EAffinity = VP_DEFAULT_AFFINITY);
58 void clear() { m_deepPosition.clear(); }
60 bool isNull() const { return m_deepPosition.isNull(); }
61 bool isNotNull() const { return m_deepPosition.isNotNull(); }
63 Position deepEquivalent() const { return m_deepPosition; }
64 EAffinity affinity() const { ASSERT(m_affinity == UPSTREAM || m_affinity == DOWNSTREAM); return m_affinity; }
65 void setAffinity(EAffinity affinity) { m_affinity = affinity; }
67 // next() and previous() will increment/decrement by a character cluster.
68 VisiblePosition next(bool stayInEditableContent = false) const;
69 VisiblePosition previous(bool stayInEditableContent = false) const;
70 VisiblePosition honorEditableBoundaryAtOrBefore(const VisiblePosition&) const;
71 VisiblePosition honorEditableBoundaryAtOrAfter(const VisiblePosition&) const;
73 VisiblePosition left(bool stayInEditableContent = false) const;
74 VisiblePosition right(bool stayInEditableContent = false) const;
76 UChar characterAfter() const;
77 UChar characterBefore() const { return previous().characterAfter(); }
79 void debugPosition(const char* msg = "") const;
81 Element* rootEditableElement() const { return m_deepPosition.isNotNull() ? m_deepPosition.node()->rootEditableElement() : 0; }
83 void getInlineBoxAndOffset(InlineBox*& inlineBox, int& caretOffset) const
85 m_deepPosition.getInlineBoxAndOffset(m_affinity, inlineBox, caretOffset);
88 void getInlineBoxAndOffset(TextDirection primaryDirection, InlineBox*& inlineBox, int& caretOffset) const
90 m_deepPosition.getInlineBoxAndOffset(m_affinity, primaryDirection, inlineBox, caretOffset);
93 IntRect caretRect() const;
95 #ifndef NDEBUG
96 void formatForDebugger(char* buffer, unsigned length) const;
97 void showTreeForThis() const;
98 #endif
100 private:
101 void init(const Position&, EAffinity);
102 Position canonicalPosition(const Position&);
104 Position leftVisuallyDistinctCandidate() const;
105 Position rightVisuallyDistinctCandidate() const;
107 Position m_deepPosition;
108 EAffinity m_affinity;
111 // FIXME: This shouldn't ignore affinity.
112 inline bool operator==(const VisiblePosition& a, const VisiblePosition& b)
114 return a.deepEquivalent() == b.deepEquivalent();
117 inline bool operator!=(const VisiblePosition& a, const VisiblePosition& b)
119 return !(a == b);
122 PassRefPtr<Range> makeRange(const VisiblePosition&, const VisiblePosition&);
123 bool setStart(Range*, const VisiblePosition&);
124 bool setEnd(Range*, const VisiblePosition&);
125 VisiblePosition startVisiblePosition(const Range*, EAffinity);
126 VisiblePosition endVisiblePosition(const Range*, EAffinity);
128 Node *enclosingBlockFlowElement(const VisiblePosition&);
130 bool isFirstVisiblePositionInNode(const VisiblePosition&, const Node*);
131 bool isLastVisiblePositionInNode(const VisiblePosition&, const Node*);
133 } // namespace WebCore
135 #ifndef NDEBUG
136 // Outside the WebCore namespace for ease of invocation from gdb.
137 void showTree(const WebCore::VisiblePosition*);
138 void showTree(const WebCore::VisiblePosition&);
139 #endif
141 #endif // VisiblePosition_h