Position.h 4.13 KB
Newer Older
kocienda's avatar
kocienda committed
1
/*
darin's avatar
darin committed
2
 * Copyright (C) 2004, 2006 Apple Computer, Inc.  All rights reserved.
kocienda's avatar
kocienda committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

26
27
#ifndef Position_h
#define Position_h
kocienda's avatar
kocienda committed
28

darin's avatar
darin committed
29
30
#include "Node.h"
#include "TextAffinity.h"
kocienda's avatar
kocienda committed
31

darin's avatar
darin committed
32
namespace WebCore {
kocienda's avatar
kocienda committed
33

darin's avatar
darin committed
34
35
class CSSComputedStyleDeclaration;
class Element;
weinig's avatar
weinig committed
36
class PositionIterator;
darin's avatar
darin committed
37
class Range;
kocienda's avatar
kocienda committed
38

harrison's avatar
harrison committed
39
enum EUsingComposedCharacters { NotUsingComposedCharacters = false, UsingComposedCharacters = true };
kocienda's avatar
kocienda committed
40

kocienda's avatar
kocienda committed
41
class Position
kocienda's avatar
kocienda committed
42
43
{
public:
darin's avatar
darin committed
44
    Position() : m_node(0), m_offset(0) { }
darin's avatar
darin committed
45
    Position(Node*, int offset);
weinig's avatar
weinig committed
46
    Position(const PositionIterator&);
darin's avatar
darin committed
47

darin's avatar
darin committed
48
49
    void clear();

darin's avatar
darin committed
50
51
    Node *node() const { return m_node.get(); }
    Element* documentElement() const;
adele's avatar
adele committed
52
    int offset() const { return m_offset; }
kocienda's avatar
kocienda committed
53

darin's avatar
darin committed
54
55
56
    bool isNull() const { return m_node == 0; }
    bool isNotNull() const { return m_node != 0; }

darin's avatar
darin committed
57
58
    Element* element() const;
    PassRefPtr<CSSComputedStyleDeclaration> computedStyle() const;
kocienda's avatar
kocienda committed
59

harrison's avatar
harrison committed
60
61
62
63
64
65
    // Move up or down the DOM by one position
    Position previous(EUsingComposedCharacters usingComposedCharacters=NotUsingComposedCharacters) const;
    Position next(EUsingComposedCharacters usingComposedCharacters=NotUsingComposedCharacters) const;
    bool atStart() const;
    bool atEnd() const;
    
darin's avatar
darin committed
66
67
    // FIXME: Make these non-member functions and put them somewhere in the editing directory.
    // These aren't really basic "position" operations. More high level editing helper functions.
darin's avatar
darin committed
68
69
    Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
    Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
kocienda's avatar
kocienda committed
70

justing's avatar
justing committed
71
    // p.upstream() through p.downstream() is the range of positions that map to the same VisiblePosition as p.
mjs's avatar
mjs committed
72
73
    Position upstream() const;
    Position downstream() const;
kocienda's avatar
kocienda committed
74
    
justing's avatar
justing committed
75
    bool isCandidate() const;
weinig's avatar
weinig committed
76
    bool inRenderedText() const;
kocienda's avatar
kocienda committed
77
    bool isRenderedCharacter() const;
kocienda's avatar
kocienda committed
78
    bool rendersInDifferentPosition(const Position &pos) const;
kocienda's avatar
kocienda committed
79
    
weinig's avatar
weinig committed
80
81
82
    static bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
    static bool nodeIsUserSelectNone(Node*);
    
darin's avatar
darin committed
83
    void debugPosition(const char* msg = "") const;
kocienda's avatar
kocienda committed
84
85

#ifndef NDEBUG
darin's avatar
darin committed
86
    void formatForDebugger(char* buffer, unsigned length) const;
darin's avatar
darin committed
87
    void showTreeForThis() const;
kocienda's avatar
kocienda committed
88
#endif
kocienda's avatar
kocienda committed
89
    
kocienda's avatar
kocienda committed
90
private:
adele's avatar
adele committed
91
    int renderedOffset() const;
darin's avatar
darin committed
92

darin's avatar
darin committed
93
94
    Position previousCharacterPosition(EAffinity) const;
    Position nextCharacterPosition(EAffinity) const;
darin's avatar
darin committed
95
    RefPtr<Node> m_node;
adele's avatar
adele committed
96
    int m_offset;
kocienda's avatar
kocienda committed
97
98
};

kocienda's avatar
kocienda committed
99
inline bool operator==(const Position &a, const Position &b)
kocienda's avatar
kocienda committed
100
101
102
103
{
    return a.node() == b.node() && a.offset() == b.offset();
}

kocienda's avatar
kocienda committed
104
inline bool operator!=(const Position &a, const Position &b)
kocienda's avatar
kocienda committed
105
106
107
108
{
    return !(a == b);
}

darin's avatar
darin committed
109
110
Position startPosition(const Range*);
Position endPosition(const Range*);
darin's avatar
darin committed
111

darin's avatar
darin committed
112
113
} // namespace WebCore

harrison's avatar
harrison committed
114
#ifndef NDEBUG
darin's avatar
darin committed
115
116
117
// Outside the WebCore namespace for ease of invocation from gdb.
void showTree(const WebCore::Position&);
void showTree(const WebCore::Position*);
harrison's avatar
harrison committed
118
119
#endif

120
#endif // Position_h