1 // Scintilla source code edit control
3 ** Classes maintaining the selection.
5 // Copyright 2009 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
13 class SelectionPosition
{
14 Sci::Position position
;
15 Sci::Position virtualSpace
;
17 explicit SelectionPosition(Sci::Position position_
=INVALID_POSITION
, Sci::Position virtualSpace_
=0) noexcept
: position(position_
), virtualSpace(virtualSpace_
) {
18 PLATFORM_ASSERT(virtualSpace
< 800000);
26 void MoveForInsertDelete(bool insertion
, Sci::Position startChange
, Sci::Position length
);
27 bool operator ==(const SelectionPosition
&other
) const {
28 return position
== other
.position
&& virtualSpace
== other
.virtualSpace
;
30 bool operator <(const SelectionPosition
&other
) const;
31 bool operator >(const SelectionPosition
&other
) const;
32 bool operator <=(const SelectionPosition
&other
) const;
33 bool operator >=(const SelectionPosition
&other
) const;
34 Sci::Position
Position() const {
37 void SetPosition(Sci::Position position_
) {
41 Sci::Position
VirtualSpace() const {
44 void SetVirtualSpace(Sci::Position virtualSpace_
) {
45 PLATFORM_ASSERT(virtualSpace_
< 800000);
46 if (virtualSpace_
>= 0)
47 virtualSpace
= virtualSpace_
;
49 void Add(Sci::Position increment
) {
50 position
= position
+ increment
;
52 bool IsValid() const {
57 // Ordered range to make drawing simpler
58 struct SelectionSegment
{
59 SelectionPosition start
;
60 SelectionPosition end
;
61 SelectionSegment() : start(), end() {
63 SelectionSegment(SelectionPosition a
, SelectionPosition b
) {
75 void Extend(SelectionPosition p
) {
83 struct SelectionRange
{
84 SelectionPosition caret
;
85 SelectionPosition anchor
;
87 SelectionRange() noexcept
: caret(), anchor() {
89 explicit SelectionRange(SelectionPosition single
) noexcept
: caret(single
), anchor(single
) {
91 explicit SelectionRange(Sci::Position single
) noexcept
: caret(single
), anchor(single
) {
93 SelectionRange(SelectionPosition caret_
, SelectionPosition anchor_
) noexcept
: caret(caret_
), anchor(anchor_
) {
95 SelectionRange(Sci::Position caret_
, Sci::Position anchor_
) noexcept
: caret(caret_
), anchor(anchor_
) {
98 return anchor
== caret
;
100 Sci::Position
Length() const;
101 // Sci::Position Width() const; // Like Length but takes virtual space into account
102 bool operator ==(const SelectionRange
&other
) const {
103 return caret
== other
.caret
&& anchor
== other
.anchor
;
105 bool operator <(const SelectionRange
&other
) const {
106 return caret
< other
.caret
|| ((caret
== other
.caret
) && (anchor
< other
.anchor
));
112 void ClearVirtualSpace() {
113 anchor
.SetVirtualSpace(0);
114 caret
.SetVirtualSpace(0);
116 void MoveForInsertDelete(bool insertion
, Sci::Position startChange
, Sci::Position length
);
117 bool Contains(Sci::Position pos
) const;
118 bool Contains(SelectionPosition sp
) const;
119 bool ContainsCharacter(Sci::Position posCharacter
) const;
120 SelectionSegment
Intersect(SelectionSegment check
) const;
121 SelectionPosition
Start() const {
122 return (anchor
< caret
) ? anchor
: caret
;
124 SelectionPosition
End() const {
125 return (anchor
< caret
) ? caret
: anchor
;
128 bool Trim(SelectionRange range
);
129 // If range is all virtual collapse to start of virtual space
130 void MinimizeVirtualSpace();
134 std::vector
<SelectionRange
> ranges
;
135 std::vector
<SelectionRange
> rangesSaved
;
136 SelectionRange rangeRectangular
;
141 enum selTypes
{ noSel
, selStream
, selRectangle
, selLines
, selThin
};
144 Selection() noexcept
;
146 bool IsRectangular() const;
147 Sci::Position
MainCaret() const;
148 Sci::Position
MainAnchor() const;
149 SelectionRange
&Rectangular();
150 SelectionSegment
Limits() const;
151 // This is for when you want to move the caret in response to a
152 // user direction command - for rectangular selections, use the range
153 // that covers all selected text otherwise return the main selection.
154 SelectionSegment
LimitsForRectangularElseMain() const;
155 size_t Count() const;
157 void SetMain(size_t r
);
158 SelectionRange
&Range(size_t r
);
159 const SelectionRange
&Range(size_t r
) const;
160 SelectionRange
&RangeMain();
161 const SelectionRange
&RangeMain() const;
162 SelectionPosition
Start() const;
163 bool MoveExtends() const;
164 void SetMoveExtends(bool moveExtends_
);
166 SelectionPosition
Last() const;
167 Sci::Position
Length() const;
168 void MovePositions(bool insertion
, Sci::Position startChange
, Sci::Position length
);
169 void TrimSelection(SelectionRange range
);
170 void TrimOtherSelections(size_t r
, SelectionRange range
);
171 void SetSelection(SelectionRange range
);
172 void AddSelection(SelectionRange range
);
173 void AddSelectionWithoutTrim(SelectionRange range
);
174 void DropSelection(size_t r
);
175 void DropAdditionalRanges();
176 void TentativeSelection(SelectionRange range
);
177 void CommitTentative();
178 int CharacterInSelection(Sci::Position posCharacter
) const;
179 int InSelectionForEOL(Sci::Position pos
) const;
180 Sci::Position
VirtualSpaceFor(Sci::Position pos
) const;
182 void RemoveDuplicates();
184 bool Tentative() const { return tentativeMain
; }
185 std::vector
<SelectionRange
> RangesCopy() const {