]> git.saurik.com Git - iphone-api.git/blob - WebCore/Selection.h
Adding the WebCore headers (for Cydget).
[iphone-api.git] / WebCore / Selection.h
1 /*
2 * Copyright (C) 2004 Apple Computer, 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 Selection_h
27 #define Selection_h
28
29 #include "TextGranularity.h"
30 #include "VisiblePosition.h"
31
32 namespace WebCore {
33
34 class Position;
35
36 const EAffinity SEL_DEFAULT_AFFINITY = DOWNSTREAM;
37
38 class Selection {
39 public:
40 enum EState { NONE, CARET, RANGE };
41 enum EDirection { FORWARD, BACKWARD, RIGHT, LEFT };
42
43 Selection();
44
45 Selection(const Position&, EAffinity);
46 Selection(const Position&, const Position&, EAffinity = SEL_DEFAULT_AFFINITY);
47
48 Selection(const Range*, EAffinity = SEL_DEFAULT_AFFINITY);
49
50 Selection(const VisiblePosition&);
51 Selection(const VisiblePosition&, const VisiblePosition&);
52
53 static Selection selectionFromContentsOfNode(Node*);
54
55 EState state() const { return m_state; }
56
57 void setAffinity(EAffinity affinity) { m_affinity = affinity; }
58 EAffinity affinity() const { return m_affinity; }
59
60 void setBase(const Position&);
61 void setBase(const VisiblePosition&);
62 void setExtent(const Position&);
63 void setExtent(const VisiblePosition&);
64
65 Position base() const { return m_base; }
66 Position extent() const { return m_extent; }
67 Position start() const { return m_start; }
68 Position end() const { return m_end; }
69
70 VisiblePosition visibleStart() const { return VisiblePosition(m_start, isRange() ? DOWNSTREAM : affinity()); }
71 VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); }
72
73 bool isNone() const { return state() == NONE; }
74 bool isCaret() const { return state() == CARET; }
75 bool isRange() const { return state() == RANGE; }
76 bool isCaretOrRange() const { return state() != NONE; }
77
78 bool isBaseFirst() const { return m_baseIsFirst; }
79
80 void appendTrailingWhitespace();
81
82 bool expandUsingGranularity(TextGranularity granularity);
83 TextGranularity granularity() const { return m_granularity; }
84
85 PassRefPtr<Range> toRange() const;
86
87 Element* rootEditableElement() const;
88 bool isContentEditable() const;
89 bool isContentRichlyEditable() const;
90 Node* shadowTreeRootNode() const;
91
92 void debugPosition() const;
93
94 #ifndef NDEBUG
95 void formatForDebugger(char* buffer, unsigned length) const;
96 void showTreeForThis() const;
97 #endif
98
99 void setWithoutValidation(const Position&, const Position&);
100
101 private:
102 void validate();
103 void adjustForEditableContent();
104
105 Position m_base; // base position for the selection
106 Position m_extent; // extent position for the selection
107 Position m_start; // start position for the selection
108 Position m_end; // end position for the selection
109
110 EAffinity m_affinity; // the upstream/downstream affinity of the caret
111 TextGranularity m_granularity; // granularity of start/end selection
112
113 // these are cached, can be recalculated by validate()
114 EState m_state; // the state of the selection
115 bool m_baseIsFirst; // true if base is before the extent
116 };
117
118 inline bool operator==(const Selection& a, const Selection& b)
119 {
120 return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity() && a.granularity() == b.granularity() && a.isBaseFirst() == b.isBaseFirst();
121 }
122
123 inline bool operator!=(const Selection& a, const Selection& b)
124 {
125 return !(a == b);
126 }
127
128 } // namespace WebCore
129
130 #ifndef NDEBUG
131 // Outside the WebCore namespace for ease of invocation from gdb.
132 void showTree(const WebCore::Selection&);
133 void showTree(const WebCore::Selection*);
134 #endif
135
136 #endif // Selection_h