Position.h 3.95 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(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
    Position trailingWhitespacePosition(EAffinity, 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
    
justing's avatar
justing committed
73
    bool isCandidate() const;
kocienda's avatar
kocienda committed
74
    bool isRenderedCharacter() const;
kocienda's avatar
kocienda committed
75
    bool rendersInDifferentPosition(const Position &pos) const;
kocienda's avatar
kocienda committed
76
    
darin's avatar
darin committed
77
    void debugPosition(const char* msg = "") const;
kocienda's avatar
kocienda committed
78
79

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

    bool inRenderedText() const;

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

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

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

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

darin's avatar
darin committed
109
110
} // namespace WebCore

harrison's avatar
harrison committed
111
#ifndef NDEBUG
darin's avatar
darin committed
112
113
114
// 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
115
116
#endif

117
#endif // Position_h