]> git.saurik.com Git - iphone-api.git/blob - WebCore/Position.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / Position.h
1 /*
2 * Copyright (C) 2004, 2006, 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 Position_h
27 #define Position_h
28
29 #include "TextAffinity.h"
30 #include "TextDirection.h"
31 #include <wtf/PassRefPtr.h>
32 #include <wtf/RefPtr.h>
33
34 namespace WebCore {
35
36 class CSSComputedStyleDeclaration;
37 class Element;
38 class InlineBox;
39 class Node;
40 class Range;
41 class RenderObject;
42
43 enum EUsingComposedCharacters { NotUsingComposedCharacters = false, UsingComposedCharacters = true };
44
45 // FIXME: Reduce the number of operations we have on a Position.
46 // This should be more like a humble struct, without so many different
47 // member functions. We should find better homes for these functions.
48
49 class Position {
50 public:
51 RefPtr<Node> container;
52 int posOffset; // to be renamed to offset when we get rid of offset()
53
54 Position() : posOffset(0) { }
55 Position(PassRefPtr<Node> c, int o) : container(c), posOffset(o) { }
56
57 void clear() { container.clear(); posOffset = 0; }
58
59 Node* node() const { return container.get(); }
60 int offset() const { return posOffset; }
61 Element* documentElement() const;
62
63 bool isNull() const { return !container; }
64 bool isNotNull() const { return container; }
65
66 Element* element() const;
67 PassRefPtr<CSSComputedStyleDeclaration> computedStyle() const;
68
69 // Move up or down the DOM by one position.
70 // Offsets are computed using render text for nodes that have renderers - but note that even when
71 // using composed characters, the result may be inside a single user-visible character if a ligature is formed.
72 Position previous(EUsingComposedCharacters usingComposedCharacters=NotUsingComposedCharacters) const;
73 Position next(EUsingComposedCharacters usingComposedCharacters=NotUsingComposedCharacters) const;
74 static int uncheckedPreviousOffset(const Node*, int current);
75 static int uncheckedNextOffset(const Node*, int current);
76
77 bool atStart() const;
78 bool atEnd() const;
79
80 // FIXME: Make these non-member functions and put them somewhere in the editing directory.
81 // These aren't really basic "position" operations. More high level editing helper functions.
82 Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
83 Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
84
85 // These return useful visually equivalent positions.
86 Position upstream() const;
87 Position downstream() const;
88
89 bool isCandidate() const;
90 bool inRenderedText() const;
91 bool isRenderedCharacter() const;
92 bool rendersInDifferentPosition(const Position&) const;
93
94 void getInlineBoxAndOffset(EAffinity, InlineBox*&, int& caretOffset) const;
95 void getInlineBoxAndOffset(EAffinity, TextDirection primaryDirection, InlineBox*&, int& caretOffset) const;
96
97 static bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
98 static bool nodeIsUserSelectNone(Node*);
99
100 void debugPosition(const char* msg = "") const;
101
102 #ifndef NDEBUG
103 void formatForDebugger(char* buffer, unsigned length) const;
104 void showTreeForThis() const;
105 #endif
106
107 private:
108 int renderedOffset() const;
109
110 Position previousCharacterPosition(EAffinity) const;
111 Position nextCharacterPosition(EAffinity) const;
112 };
113
114 inline bool operator==(const Position& a, const Position& b)
115 {
116 return a.container == b.container && a.posOffset == b.posOffset;
117 }
118
119 inline bool operator!=(const Position& a, const Position& b)
120 {
121 return !(a == b);
122 }
123
124 Position startPosition(const Range*);
125 Position endPosition(const Range*);
126
127 } // namespace WebCore
128
129 #ifndef NDEBUG
130 // Outside the WebCore namespace for ease of invocation from gdb.
131 void showTree(const WebCore::Position&);
132 void showTree(const WebCore::Position*);
133 #endif
134
135 #endif // Position_h