Position.h 4 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
36
class CSSComputedStyleDeclaration;
class Element;
class Range;
kocienda's avatar
kocienda committed
37

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

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

darin's avatar
darin committed
46
47
    void clear();

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

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

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

harrison's avatar
harrison committed
58
59
60
61
62
63
    // 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
64
65
    // 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
66
67
    Position leadingWhitespacePosition(WebCore::EAffinity affinity, bool considerNonCollapsibleWhitespace = false) const;
    Position trailingWhitespacePosition(WebCore::EAffinity affinity, bool considerNonCollapsibleWhitespace = false) const;
kocienda's avatar
kocienda committed
68

justing's avatar
justing committed
69
    // p.upstream() through p.downstream() is the range of positions that map to the same VisiblePosition as p.
mjs's avatar
mjs committed
70
71
    Position upstream() const;
    Position downstream() const;
kocienda's avatar
kocienda committed
72
    
kocienda's avatar
kocienda committed
73
    Position equivalentRangeCompliantPosition() const;
kocienda's avatar
kocienda committed
74
    bool inRenderedContent() const;
kocienda's avatar
kocienda committed
75
    bool isRenderedCharacter() const;
kocienda's avatar
kocienda committed
76
    bool rendersInDifferentPosition(const Position &pos) const;
kocienda's avatar
kocienda committed
77
    
darin's avatar
darin committed
78
    void debugPosition(const char* msg = "") const;
kocienda's avatar
kocienda committed
79
80

#ifndef NDEBUG
darin's avatar
darin committed
81
    void formatForDebugger(char* buffer, unsigned length) const;
harrison's avatar
harrison committed
82
    void showTree() const;
kocienda's avatar
kocienda committed
83
#endif
kocienda's avatar
kocienda committed
84
    
kocienda's avatar
kocienda committed
85
private:
adele's avatar
adele committed
86
    int renderedOffset() const;
darin's avatar
darin committed
87
88
89

    bool inRenderedText() const;

darin's avatar
darin committed
90
91
    Position previousCharacterPosition(WebCore::EAffinity affinity) const;
    Position nextCharacterPosition(WebCore::EAffinity affinity) const;
darin's avatar
darin committed
92
    
darin's avatar
darin committed
93
    RefPtr<Node> m_node;
adele's avatar
adele committed
94
    int m_offset;
kocienda's avatar
kocienda committed
95
96
};

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

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

darin's avatar
darin committed
107
108
Position startPosition(const Range*);
Position endPosition(const Range*);
darin's avatar
darin committed
109

harrison's avatar
harrison committed
110
#ifndef NDEBUG
darin's avatar
darin committed
111
112
void showTree(const Position& pos);
void showTree(const Position* pos);
harrison's avatar
harrison committed
113
114
#endif

darin's avatar
darin committed
115
} // namespace WebCore
kocienda's avatar
kocienda committed
116

117
#endif // Position_H