Position.h 4.96 KB
Newer Older
kocienda's avatar
kocienda committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2004, 2006, 2008 Apple 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
#include "TextAffinity.h"
mitz@apple.com's avatar
mitz@apple.com committed
30
#include "TextDirection.h"
darin@apple.com's avatar
darin@apple.com committed
31
32
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
kocienda's avatar
kocienda committed
33

darin's avatar
darin committed
34
namespace WebCore {
kocienda's avatar
kocienda committed
35

darin's avatar
darin committed
36
37
class CSSComputedStyleDeclaration;
class Element;
mitz@apple.com's avatar
mitz@apple.com committed
38
class InlineBox;
darin@apple.com's avatar
darin@apple.com committed
39
class Node;
darin's avatar
darin committed
40
class Range;
darin@apple.com's avatar
darin@apple.com committed
41
class RenderObject;
kocienda's avatar
kocienda committed
42

ap@webkit.org's avatar
ap@webkit.org committed
43
44
45
46
47
enum PositionMoveType {
    CodePoint,       // Move by a single code point.
    Character,       // Move to the next Unicode character break.
    BackwardDeletion // Subject to platform conventions.
};
kocienda's avatar
kocienda committed
48

darin@apple.com's avatar
darin@apple.com committed
49
50
51
52
53
// FIXME: Reduce the number of operations we have on a Position.
// This should be more like a humble struct, without so many different
// member functions. We should find better homes for these functions.

class Position {
kocienda's avatar
kocienda committed
54
public:
darin@apple.com's avatar
darin@apple.com committed
55
    RefPtr<Node> container;
56
    int m_offset;
darin@apple.com's avatar
darin@apple.com committed
57

58
59
    Position() : m_offset(0) { }
    Position(PassRefPtr<Node> c, int o) : container(c), m_offset(o) { }
darin's avatar
darin committed
60

61
    void clear() { container.clear(); m_offset = 0; }
darin's avatar
darin committed
62

darin@apple.com's avatar
darin@apple.com committed
63
    Node* node() const { return container.get(); }
darin's avatar
darin committed
64
    Element* documentElement() const;
kocienda's avatar
kocienda committed
65

darin@apple.com's avatar
darin@apple.com committed
66
67
    bool isNull() const { return !container; }
    bool isNotNull() const { return container; }
darin's avatar
darin committed
68

darin's avatar
darin committed
69
70
    Element* element() const;
    PassRefPtr<CSSComputedStyleDeclaration> computedStyle() const;
kocienda's avatar
kocienda committed
71

ap@webkit.org's avatar
ap@webkit.org committed
72
73
74
    // Move up or down the DOM by one position.
    // Offsets are computed using render text for nodes that have renderers - but note that even when
    // using composed characters, the result may be inside a single user-visible character if a ligature is formed.
ap@webkit.org's avatar
ap@webkit.org committed
75
76
    Position previous(PositionMoveType = CodePoint) const;
    Position next(PositionMoveType = CodePoint) const;
ap@webkit.org's avatar
ap@webkit.org committed
77
    static int uncheckedPreviousOffset(const Node*, int current);
ap@webkit.org's avatar
ap@webkit.org committed
78
    static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current);
ap@webkit.org's avatar
ap@webkit.org committed
79
80
    static int uncheckedNextOffset(const Node*, int current);

harrison's avatar
harrison committed
81
82
    bool atStart() const;
    bool atEnd() const;
ap@webkit.org's avatar
ap@webkit.org committed
83

darin's avatar
darin committed
84
85
    // 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
86
87
    Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
    Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
88
89
    
    // These return useful visually equivalent positions.
mjs's avatar
mjs committed
90
91
    Position upstream() const;
    Position downstream() const;
kocienda's avatar
kocienda committed
92
    
justing's avatar
justing committed
93
    bool isCandidate() const;
weinig's avatar
weinig committed
94
    bool inRenderedText() const;
kocienda's avatar
kocienda committed
95
    bool isRenderedCharacter() const;
darin@apple.com's avatar
darin@apple.com committed
96
    bool rendersInDifferentPosition(const Position&) const;
mitz@apple.com's avatar
mitz@apple.com committed
97
98
99
100

    void getInlineBoxAndOffset(EAffinity, InlineBox*&, int& caretOffset) const;
    void getInlineBoxAndOffset(EAffinity, TextDirection primaryDirection, InlineBox*&, int& caretOffset) const;

weinig's avatar
weinig committed
101
102
103
    static bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
    static bool nodeIsUserSelectNone(Node*);
    
darin's avatar
darin committed
104
    void debugPosition(const char* msg = "") const;
kocienda's avatar
kocienda committed
105
106

#ifndef NDEBUG
darin's avatar
darin committed
107
    void formatForDebugger(char* buffer, unsigned length) const;
darin's avatar
darin committed
108
    void showTreeForThis() const;
kocienda's avatar
kocienda committed
109
#endif
kocienda's avatar
kocienda committed
110
    
kocienda's avatar
kocienda committed
111
private:
adele's avatar
adele committed
112
    int renderedOffset() const;
darin's avatar
darin committed
113

darin's avatar
darin committed
114
115
    Position previousCharacterPosition(EAffinity) const;
    Position nextCharacterPosition(EAffinity) const;
kocienda's avatar
kocienda committed
116
117
};

darin@apple.com's avatar
darin@apple.com committed
118
inline bool operator==(const Position& a, const Position& b)
kocienda's avatar
kocienda committed
119
{
120
    return a.container == b.container && a.m_offset == b.m_offset;
kocienda's avatar
kocienda committed
121
122
}

darin@apple.com's avatar
darin@apple.com committed
123
inline bool operator!=(const Position& a, const Position& b)
kocienda's avatar
kocienda committed
124
125
126
127
{
    return !(a == b);
}

darin's avatar
darin committed
128
129
Position startPosition(const Range*);
Position endPosition(const Range*);
darin's avatar
darin committed
130

darin's avatar
darin committed
131
132
} // namespace WebCore

harrison's avatar
harrison committed
133
#ifndef NDEBUG
darin's avatar
darin committed
134
135
136
// 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
137
138
#endif

139
#endif // Position_h