2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / editing / Selection.h
blobae5007360420fafb0e8eef48fb331798a56308b5
1 /*
2 * Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
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.
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.
26 #ifndef Selection_h
27 #define Selection_h
29 #include "TextGranularity.h"
30 #include "VisiblePosition.h"
32 namespace WebCore {
34 class Position;
36 const EAffinity SEL_DEFAULT_AFFINITY = DOWNSTREAM;
38 class Selection {
39 public:
40 enum EState { NONE, CARET, RANGE };
41 enum EDirection { FORWARD, BACKWARD, RIGHT, LEFT };
43 Selection();
45 Selection(const Position&, EAffinity);
46 Selection(const Position&, const Position&, EAffinity);
48 Selection(const Range*, EAffinity = SEL_DEFAULT_AFFINITY);
50 Selection(const VisiblePosition&);
51 Selection(const VisiblePosition&, const VisiblePosition&);
53 static Selection selectionFromContentsOfNode(Node*);
55 EState state() const { return m_state; }
57 void setAffinity(EAffinity affinity) { m_affinity = affinity; }
58 EAffinity affinity() const { return m_affinity; }
60 void setBase(const Position&);
61 void setBase(const VisiblePosition&);
62 void setExtent(const Position&);
63 void setExtent(const VisiblePosition&);
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; }
70 VisiblePosition visibleStart() const { return VisiblePosition(m_start, isRange() ? DOWNSTREAM : affinity()); }
71 VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); }
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; }
78 bool isBaseFirst() const { return m_baseIsFirst; }
80 bool expandUsingGranularity(TextGranularity granularity);
81 TextGranularity granularity() const { return m_granularity; }
83 PassRefPtr<Range> toRange() const;
85 Element* rootEditableElement() const;
86 bool isContentEditable() const;
87 bool isContentRichlyEditable() const;
88 Node* shadowTreeRootNode() const;
90 void debugPosition() const;
92 #ifndef NDEBUG
93 void formatForDebugger(char* buffer, unsigned length) const;
94 void showTreeForThis() const;
95 #endif
97 void setWithoutValidation(const Position&, const Position&);
99 private:
100 void validate();
101 void adjustForEditableContent();
103 Position m_base; // base position for the selection
104 Position m_extent; // extent position for the selection
105 Position m_start; // start position for the selection
106 Position m_end; // end position for the selection
108 EAffinity m_affinity; // the upstream/downstream affinity of the caret
109 TextGranularity m_granularity; // granularity of start/end selection
111 // these are cached, can be recalculated by validate()
112 EState m_state; // the state of the selection
113 bool m_baseIsFirst; // true if base is before the extent
116 inline bool operator==(const Selection& a, const Selection& b)
118 return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity() && a.granularity() == b.granularity() && a.isBaseFirst() == b.isBaseFirst();
121 inline bool operator!=(const Selection& a, const Selection& b)
123 return !(a == b);
126 } // namespace WebCore
128 #ifndef NDEBUG
129 // Outside the WebCore namespace for ease of invocation from gdb.
130 void showTree(const WebCore::Selection&);
131 void showTree(const WebCore::Selection*);
132 #endif
134 #endif // Selection_h