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
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.
29 #include "TextGranularity.h"
30 #include "VisiblePosition.h"
36 const EAffinity SEL_DEFAULT_AFFINITY
= DOWNSTREAM
;
40 enum EState
{ NONE
, CARET
, RANGE
};
41 enum EDirection
{ FORWARD
, BACKWARD
, RIGHT
, LEFT
};
45 Selection(const Position
&, EAffinity
);
46 Selection(const Position
&, const Position
&, EAffinity
= SEL_DEFAULT_AFFINITY
);
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 void appendTrailingWhitespace();
82 bool expandUsingGranularity(TextGranularity granularity
);
83 TextGranularity
granularity() const { return m_granularity
; }
85 PassRefPtr
<Range
> toRange() const;
87 Element
* rootEditableElement() const;
88 bool isContentEditable() const;
89 bool isContentRichlyEditable() const;
90 Node
* shadowTreeRootNode() const;
92 void debugPosition() const;
95 void formatForDebugger(char* buffer
, unsigned length
) const;
96 void showTreeForThis() const;
99 void setWithoutValidation(const Position
&, const Position
&);
103 void adjustForEditableContent();
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
110 EAffinity m_affinity
; // the upstream/downstream affinity of the caret
111 TextGranularity m_granularity
; // granularity of start/end selection
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
118 inline bool operator==(const Selection
& a
, const Selection
& b
)
120 return a
.start() == b
.start() && a
.end() == b
.end() && a
.affinity() == b
.affinity() && a
.granularity() == b
.granularity() && a
.isBaseFirst() == b
.isBaseFirst();
123 inline bool operator!=(const Selection
& a
, const Selection
& b
)
128 } // namespace WebCore
131 // Outside the WebCore namespace for ease of invocation from gdb.
132 void showTree(const WebCore::Selection
&);
133 void showTree(const WebCore::Selection
*);
136 #endif // Selection_h